CentOS7 httpd MySQL PHP memcached SElinux

/omg

CentOS7 minimal

httpd
# yum install httpd
# systemctl start httpd.service
# systemctl enable httpd.service

firewalld
# firewall-cmd –zone=public –add-service=http –permanent
# firewall-cmd –zone=public –add-service=https –permanent
# firewall-cmd –reload

mariadb
# yum install mariadb-server mariadb
# systemctl start mariadb.service
# systemctl enable mariadb.service
# mysql_secure_installation

php
# yum install php php-mysql php-gd php-pear php-mbstring php-gd php-ldap php-odbc php-pear php-xml php-xmlrpc php-mbstring php-snmp php-soap curl curl-devel
# chcon -R -t httpd_sys_content_t /home/sourcode/domains/sourcode.net/public_html
# systemctl restart httpd.service

memcached
# yum install php-pecl-memcached memcached
# vi /etc/httpd/conf.d/php.conf

#
# Apache specific PHP configuration options
# those can be override in each configured vhost
#
# php_value session.save_handler "files"
# php_value session.save_path "/var/lib/php/session"
php_value session.save_handler "memcached"
php_value session.save_path "127.0.0.1:11211"

# setsebool -P httpd_can_network_memcache 1
# getsebool httpd_can_network_memcache

# vi /etc/httpd/conf.d/sourcode.conf

</VirtualHost *:80>
ServerAdmin [email protected]
DocumentRoot "/home/sourcode/domains/sourcode.net/public_html"
<Directory "/home/sourcode/domains/sourcode.net/public_html">
Require all granted
</Directory> 
ServerName sourcode.net
ServerAlias www.sourcode.net
ErrorLog “/var/log/httpd/sourcode.net/error_log”
CustomLog “/var/log/httpd/sourcode.net/access_log” common
<VirtualHost>

# end

memcache memcached

แค่มีตัว d เพิ่มเข้ามา ชีวิตก็เปลี่ยนครับ

ปกติแล้วการสร้าว session ใน php จะสร้างเป็น files เอาไว้ใน /tmp โดยจะมีชื่อเป็น sess
โดยจะมีค่าคอนฟิกอยู่ใน php.ini

แบบปกติ (default)

session.save_handler = files
;session.save_path = “/tmp”

แบบใช้ memcache

session.save_handler = memcache
session.save_path = “tcp://127.0.0.1:11211”

แบบใช้ memcached

session.save_handler = memcached
session.save_path = “127.0.0.1:21201”

ปล. port ของ memcache , memcached อาจไม่เหมือนตัวอย่าง โปรดแก้ไขให้ตรงกับคอนฟิกของตัวเอง
และอย่าลืม restart memcache หรือ restart memcahed ทุกครั้งหลังเปลี่ยนแปลงค่าใน php.ini

drupal 6 with memcached

ติดตั้ง memcached

# aptitude install memcachedb php5-memcached

โหลด module มาลงที่ sites/all/modules/

http://drupal.org/project/memcache

แก้ไข php.ini

memcache.hash_strategy="consistent"

แก้ไข settings.php

$conf = array(
    'cache_inc' => './sites/all/modules/memcache/memcache.db.inc',
        'memcache_servers' => array(
            'localhost:11211' => 'default',
        ),
        'memcache_bins' => array(
            'cache' => 'default',
        ),
);

จบ

yii with memcached

yii มีระบบ cache อยู่หลายส่วนพอสมควรครับ ซึ่งเราจะกล่าวกันในครั้งต่อไป

รอบนี้เราจะมาพูดถึงเรื่อง memcached ละกัน
คงไม่ต้องเกรื่นใช่ไหมครับ ว่า memcached คืออะไร ???

ลองดูตาม blog ของคุณ @FordAntiTrust ได้ครับ เขียนอ่านง่าย เข้าใจไว 😀

http://www.thaicyberpoint.com/ford/blog/id/909/

ตอนแรกสุดผมใช้ Alternative PHP Cache หรือชื่อเล่นว่า APC ครับ เนื่องจาก config ง่าย และทาง yii ก็แนะนำไว้ ตามหน้า นี้

http://www.yiiframework.com/performance/

มันก็ทำงานได้ดีครับ ตามที่มันควรจะเป็น

'components'=>array(
  'cache'=>array(
    'class'=>'CApcCache',
  ),
),

แต่เนื่องจากความต้องการของมนุษย์มีไม่สิ้นสุดครับ…
ผมถึงต้องเปลี่ยนเป็น memcached

เพราะ APC ไม่ได้ถูกออกแบบมาสำหรับรองรับ server หลายๆตัวครับ
เพราะ memcached ออกแบบมาให้เป็น centralized cache ครับ

memcached

มาเริ่มกันเลยครับ
เริ่มจากติดตั้งก่อนเลยครับ ซึ่งถ้าลอง search memcached ดูจะเจอความงงงวยครั้งแรกครับ ..

p memcached - A high-performance memory object caching system
p memcachedb - Persistent storage engine using the memcache protocol
p php5-memcached - memcached module for php5

memcache กับ memcached
ใช้อันไหน !?!?!?!?!?!?!!

ใน linux นะครับ ถ้าเห็นอะไรแบบนี้ให้เดาไว้ก่อนเลยครับว่า d ที่ต่อท้ายมา คือ daemon
ดังนั้นเราต้องติดตั้ง memcachedb กับ php5-memcached ครับ

หลังจากที่ติดตั้งเสร็จแล้ว ก็มาลองใช้กันครับ..
โดยการเปิด config ของ memcached มาดูเล่นก่อนครับ

/etc/memcachedb.conf

บรรทัดที่น่าสนใจคือ

# Default connection port is 11211
-p 21201

ครับ… มันเปลี่ยน port เฉยเลย ถ้าไม่เข้ามาดูก่อน จะรู้ไหมเนี่ย!!!
เนื่องจากถ้าเข้าไปดูวิธีการ config ตัว memcached ให้เข้ากับ yii ตามหน้านี้

http://www.yiiframework.com/doc/api/1.1/CMemCache

จะบอกว่า

'port'=>11211,

ซึ่งมันไม่ถูกครับ ต้องใช้เป็น 21201

        'cache' => array(
                'class'=>'CMemCache',
                'servers'=>array(
                        array(
                        'host'=>'127.0.0.1',
                        'port'=>21201,
                        'weight'=>100,
                        ),
                ),
        ),

ก็จะใช้งานได้ละครับ…. ซะที่ไหนหละ ถ้าทำแค่นี้ จะเจอ erorr 500 แน่นอนครับ
เพราะ ผมโดนมาแล้ว

เราต้องไปแก้อีกที่ครับ

framework/caching/CMemCache.php

เปลี่ยนจาก

public $useMemcached=false;

เป็น

public $useMemcached=true;

คราวนี้ใช้งานได้จริงๆละครับ 😉