Tuesday, March 25, 2014

Configure apache to use php-fpm



Apache httpd 2.4.x sử dụng mod_proxy_fcgi  php_fpm. Chi tiết bên dưới sẽ hướng dẫn bạn cấu hình php-fpm virtual host.
Code:
[root@linux24h httpd.d]# cd /usr/local/php/etc
[root@linux24h etc]# cp php-fpm.conf.default php-fpm.conf
[root@linux24h etc]# vim php-fpm.conf
Bỏ dấu “;” tại các dòng:
Code:
include=etc/fpm.d/*.conf
pid = run/php-fpm.pid
error_log = log/php-fpm.log
log_level = notice
listen.allowed_clients = 127.0.0.1
Sau đó nhấn ESC  :wq để thoát ra và lưu lại.

Tạo thư mục chứa các file cấu hình của virtual host:
Code:
[root@linux24h etc]# mkdir fpm.d
Tạo file cấu hình cho host linux24h.com
Code:
[root@linux24h etc]# vim fpm.d/linux24h.com.conf
Thêm vào các dòng như bên dưới:
Code:
[linux24h]
;listen = /var/run/php5-fpm/linux24h.com.sock
listen = 127.0.0.1:9002
listen.allowed_clients = 127.0.0.1
listen.owner = linux24h
listen.group = linux24h
user = linux24h
group = linux24h
pm = dynamic
pm.max_children = 50
pm.start_servers = 20
pm.min_spare_servers = 5
pm.max_spare_servers = 35
pm.max_requests = 500
chdir = /
Để php-fpm có thể tự khởi động khi reboot server:
  • Tạo file php-fpm:
Code:
[root@linux24h ~]#vim /etc/init.d/php-fpm
  • Nội dung như bên dưới và lưu tại /etc/init.d/
Code:
#! /bin/sh
### BEGIN INIT INFO
# Provides:          php-fpm
# Required-Start:    $all
# Required-Stop:    $all
# Default-Start:    2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: starts php-fpm
# Description:      starts the PHP FastCGI Process Manager daemon
### END INIT INFO
php_fpm_BIN=/usr/local/php/sbin/php-fpm
php_fpm_CONF=/usr/local/php/etc/php-fpm.conf
php_fpm_PID=/usr/local/php/var/run/php-fpm.pid
php_opts="--fpm-config $php_fpm_CONF"
wait_for_pid () {
        try=0
        while test $try -lt 35 ; do
                case "$1" in
                        'created')
                        if [ -f "$2" ] ; then
                                try=''
                                break
                        fi
                        ;;
                        'removed')
                        if [ ! -f "$2" ] ; then
                                try=''
                                break
                        fi
                        ;;
                esac
                echo -n .
                try=`expr $try + 1`
                sleep 1
        done
}
case "$1" in
        start)
                echo -n "Starting php-fpm "
                $php_fpm_BIN $php_opts
                if [ "$?" != 0 ] ; then
                        echo " failed"
                        exit 1
                fi
                wait_for_pid created $php_fpm_PID
                if [ -n "$try" ] ; then
                        echo " failed"
                        exit 1
                else
                        echo " done"
                fi
        ;;
        stop)
                echo -n "Gracefully shutting down php-fpm "
                if [ ! -r $php_fpm_PID ] ; then
                        echo "warning, no pid file found - php-fpm is not running ?"
                        exit 1
                fi
                kill -QUIT `cat $php_fpm_PID`
                wait_for_pid removed $php_fpm_PID
                if [ -n "$try" ] ; then
                        echo " failed. Use force-exit"
                        exit 1
                else
                        echo " done"
                fi
        ;;
        force-quit)
                echo -n "Terminating php-fpm "
                if [ ! -r $php_fpm_PID ] ; then
                        echo "warning, no pid file found - php-fpm is not running ?"
                        exit 1
                fi
                kill -TERM `cat $php_fpm_PID`
                wait_for_pid created $php_fpm_PID
                if [ -n "$try" ] ; then
                        echo " failed"
                        exit 1
                else
                        echo " done"
                fi
        ;;
        stop)
                echo -n "Gracefully shutting down php-fpm "
                if [ ! -r $php_fpm_PID ] ; then
                        echo "warning, no pid file found - php-fpm is not running ?"
                        exit 1
                fi
                kill -QUIT `cat $php_fpm_PID`
                wait_for_pid removed $php_fpm_PID
                if [ -n "$try" ] ; then
                        echo " failed. Use force-exit"
                        exit 1
                else
                        echo " done"
                fi
        ;;
        force-quit)
                echo -n "Terminating php-fpm "
                if [ ! -r $php_fpm_PID ] ; then
                        echo "warning, no pid file found - php-fpm is not running ?"
                        exit 1
                fi
                kill -TERM `cat $php_fpm_PID`
                wait_for_pid removed $php_fpm_PID
                if [ -n "$try" ] ; then
                        echo " failed"
                        exit 1
                else
                        echo " done"
                fi
        ;;
        restart)
                $0 stop
                $0 start
        ;;
        reload)
                echo -n "Reload service php-fpm "
                if [ ! -r $php_fpm_PID ] ; then
                        echo "warning, no pid file found - php-fpm is not running ?"
                        exit 1
                fi
                kill -USR2 `cat $php_fpm_PID`
                echo " done"
        ;;
        *)
                echo "Usage: $0 {start|stop|force-quit|restart|reload}"
                exit 1
        ;;
esac
Code:
[root@linux24h ~] chkconfig --level 235 php-fpm on

Compiling and installing php 5.4.15 and httpd 2.4.4 from source

High-performance PHP on apache httpd 2.4.x using mod_proxy_fcgi and php-fpm
Bài viết sau sẽ hướng dẫn các bạn build Linux Web server nâng cao bao gồm MySQL, php và apache(LAMP) từ source trên CentOS 6.4

1. Cài đặt MySQL
Xem chi tiết tại:
2. Build php 5.4.15 từ Source PHP 5.4.15 (tar.gz)
Tải gói cài đặt tại: http://fi2.php.net/get/php-5.4.15.tar.gz/from/a/mirror
Code:
[root@linux24h ~]# wget http://sg2.php.net/distributions/php-5.4.15.tar.gz
Giải nén và Build php
Code:
[root@linux24h ~]# tar xzvf php-5.4.15.tar.gz
[root@linux24h ~]# cd php-5.4.15
[root@linux24h php-5.4.15]# ./configure --prefix=/usr/local/php --with-libdir=/lib64 --enable-fpm --with-bz2 --with-config-file-path=/usr/local/php/ --with-config-file-scan-dir=/etc/php.d/ --with-curl=/opt/curlssl/ --with-gd --with-gettext --with-jpeg-dir=/usr/local/lib --with-freetype-dir=/usr/local/lib --with-mcrypt --with-mhash --with-mysql --with-mysql-sock=/tmp/mysql.sock --with-mysqli --with-pcre-regex --with-pear=/usr/local/lib64/php --with-png-dir=/usr/local/lib64 --with-sqlite=shared --with-tidy --with-xmlrpc --with-xsl --with-zlib --with-zlib-dir=/usr/local/lib64 --with-openssl --with-iconv --enable-bcmath --enable-calendar --enable-exif --enable-ftp --enable-gd-native-ttf --enable-libxml --enable-magic-quotes --enable-soap --enable-sockets --enable-mbstring --enable-zip --enable-wddx --with-mysql=mysqlnd -with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd
[root@linux24h php-5.4.15]# make && make install
[root@linux24h php-5.4.15]# cp php.ini-production /usr/local/php/php.ini
Lưu ý: Bạn sẽ nhận được thông báo lỗi:

Warning: No matches found for: mcrypt

No Matches found

Giải quyết:

Code:
[root@linux24h php-5.4.15]# rpm -Uvh http://ftp.jaist.ac.jp/pub/Linux/Fedora/epel/6/x86_64/epel-release-6-8.noarch.rpm
[root@linux24h php-5.4.15]# yum install libmcrypt-devel mcrypt -y
3. Build apache từ Source httpd-2.4.4.tar.gz
Code:
[root@linux24h ~]# wget http://mirrors.digipower.vn/apache//httpd/httpd-2.4.4.tar.gz
[root@linux24h ~]# tar xzvf httpd-2.4.4.tar.gz
[root@linux24h ~]# cd httpd-2.4.4
[root@linux24h httpd-2.4.4]# ./configure --prefix=/usr/local/apache --sysconfdir=/etc/httpd.d --with-mpm=prefork --disable-charset-lite --disable-include --disable-env --disable-status --disable-asis --disable-negotiation --disable-imap --disable-actions --disable-userdir --enable-so --enable-rewrite --enable-vhosts-alias --enable-ssl --enable-modules='unique-id' --with-included-apr [root@linux24h httpd-2.4.4]# make && make install
Lưu ý: các thông báo lỗi khi build apache(httpd)
Bạn sẽ nhận được thông báo lỗi:

configure: error: Bundled APR requested but not found at ./srclib/. Download and unpack the corresponding apr and apr-util packages to ./srclib/.

Giải quyết:
Code:
[root@linux24h httpd-2.4.4]# wget http://apache.osuosl.org/apr/apr-1.4.6.tar.gz
[root@linux24h httpd-2.4.4]# wget http://apache.osuosl.org/apr/apr-util-1.4.1.tar.gz
[root@linux24h httpd-2.4.4]# tar xzvf apr-1.4.6.tar.gz
[root@linux24h httpd-2.4.4]# tar xzvf apr-util-1.4.1.tar.gz
[root@linux24h httpd-2.4.4]# mv apr-1.4.6 srclib/apr
[root@linux24h httpd-2.4.4]# mv apr-util-1.4.1 srclib/apr-util
Bạn sẽ nhận được thông báo lỗi:

configure: error: pcre-config for libpcre not found. PCRE is required and available fromhttp://pcre.org/

Giải quyết:
Code:
[root@linux24h httpd-2.4.4]# yum install pcre-devel -y
Cấu hình cơ bản httpd:
Code:
[root@linux24h httpd-2.4.4]# cd /etc/httpd.d/
[root@linux24h httpd.d]# vim httpd.conf
Trong file httpd.conf, config các tham số cơ bản sau:

Code:
Listen 80
ServerAdmin admin@linux24h.com
UseCanonicalName Off
ServerSignature Off
HostnameLookups Off
ServerTokens Prod
Bỏ dấu # đằng trước các dòng:
Code:
LoadModule deflate_module modules/mod_deflate.so
LoadModule expires_module modules/mod_expires.so
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_connect_module modules/mod_proxy_connect.so
LoadModule proxy_ftp_module modules/mod_proxy_ftp.so
LoadModule proxy_http_module modules/mod_proxy_http.so
LoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so
LoadModule proxy_scgi_module modules/mod_proxy_scgi.so
LoadModule proxy_ajp_module modules/mod_proxy_ajp.so
LoadModule proxy_balancer_module modules/mod_proxy_balancer.so
LoadModule proxy_express_module modules/mod_proxy_express.soLoadModule rewrite_module modules/mod_rewrite.so
Include /etc/httpd.d/extra/httpd-vhosts.conf
Sau đó nhấn ESC  :wq để lưu lại và thoát

4. Config php-fpm
Xem thêm tại: 5. Config virtual host
Xem thêm tại: 6. Start apache httpd và kiểm tra kết quả
Khởi động dịch vụ apache server:
Code:
[root@linux24h ~]# cp /usr/local/apache/bin/apachectl /etc/init.d/httpd
[root@linux24h ~]# /etc/init.d/httpd start
Để apache httpd có thể tự khởi động khi reboot server:
Code:
[root@linux24h httpd.d]# chkconfig --level 235 httpd on
Bạn sẽ nhận được 1 thông báo lỗi:

service httpd does not support chkconfig

vào:
Code:
[root@linux24h httpd.d]# vim /etc/init.d/httpd
Thêm các dòng bên dưới vào trên cùng file sau dòng #!/bin/sh:
Code:
# Startup script for the Apache Web Server
#
# chkconfig: - 85 15
# description: Apache is a World Wide Web server. It is used to serve
# HTML files and CGI.
# processname: httpd
# pidfile: /usr/local/apache/logs/httpd.pid
# config: /usr/local/apache/conf/httpd.conf
Tạo file xem thông tin php để test:
Code:
<?php
echo phpinfo();
?>
Nhấn ESC  :wq để lưu và thoát ra.