Apache MPM Worker and PHP-FPM on CentOS

Introduction

A LAMP server is one of the most used configurations of the GNU/Linux operating system. And being Wordpress the most used CMS to power Internet sites, it is not surprise that Apache + PHP + MySQL is mostly used to power Wordpress installations.
Apache can work in different modes, being prefork the most used one, but worker is more efficient. Read more about differences between prefork and worker MPM
I have shown in the past how to install Apache MPM Worker and PHP-FPM on Ubuntu, because Ubuntu and Debian is what I use the most. But a friend asked me how to do it for CentOS, so, here we go.

Installing Apache MPM Worker with PHP-FPM and MySQL on CentOS

This have been tested on CentOS 6, and more specifically on a 64 bits installation on a Digital Ocean VPS.
First we need to add one repository.
rpm --import http://dag.wieers.com/rpm/packages/RPM-GPG-KEY.dag.txt
cd /tmp/
wget http://pkgs.repoforge.org/rpmforge-release/rpmforge-release-0.5.3-1.el6.rf.x86_64.rpm
rpm -ivh rpmforge-release-0.5.3-1.el6.rf.x86_64.rpm
Now let’s install the needed packages.
yum install vim php-fpm mod_fastcgi httpd mysql mysql-server php-mysql
And make all daemons start automatically when we reboot the server
chkconfig --levels 235 httpd on
chkconfig --levels 235 mysqld on
chkconfig --levels 235 php-fpm on
Add a name to the Apache server, this is to avoid the warning message of, Could not reliably determine the server’s fully qualified domain name, using 127.0.1.1 for ServerName.
echo "ServerName localhost" >> /etc/httpd/conf.d/servername.conf
Now, configure the fastcgi Apache module. We need to make to changes.
vim /etc/httpd/conf.d/fastcgi.conf
Change this
FastCgiWrapper On
for this
FastCgiWrapper Off
and add this at the end
<IfModule mod_fastcgi.c>
    AddHandler php5-fcgi .php
    Action php5-fcgi /php5-fcgi
    Alias /php5-fcgi /usr/lib/cgi-bin/php5-fcgi
    FastCgiExternalServer /usr/lib/cgi-bin/php5-fcgi -socket /var/run/php5-fpm.sock -pass-header Authorization
</IfModule>
That will make it possible for Apache to pass PHP code back to PHP-FPM and get the responce once the code have been executed.
Because we are using the folder /usr/lib/cgi-bin/ we need to create it:
mkdir -p /usr/lib/cgi-bin
On Debian, PHP-FRM listens to a socket by default, but on CentOS it listens to a TPC port, in order to reserver TCP ports for Apache, we will change PHP-FPM to be like Debian.
vim /etc/php-fpm.d/www.conf 
change this line
listen = 127.0.0.1:9000
for this one
listen = /var/run/php5-fpm.sock
You will need to uncomment the /etc/sysconfig/httpd
Line:
# HTTPD=/usr/sbin/httpd.worker

If mod_php is installed and enabled, we need to disable it. Open /etc/httpd/conf.d/php.conf
... and comment out the AddHandler and AddType lines:

#
# PHP is an HTML-embedded scripting language which attempts to make it
# easy for developers to write dynamically generated webpages.
#
<IfModule prefork.c>
  LoadModule php5_module modules/libphp5.so
</IfModule>
<IfModule worker.c>
  LoadModule php5_module modules/libphp5-zts.so
</IfModule>
#
# Cause the PHP interpreter to handle files with a .php extension.
#
#AddHandler php5-script .php
#AddType text/html .php
#
# Add index.php to the list of files that will be served as directory
# indexes.
#
DirectoryIndex index.php
#
# Uncomment the following line to allow PHP to pretty-print .phps
# files as PHP source code:
#
#AddType application/x-httpd-php-source .phps
Finall restart all servers.
service httpd restart
service php-fpm restart
service mysqld restart
You can now install your PHP application, and have a more efficient Apache server.

Nhận xét

Bài đăng phổ biến từ blog này

CLEANING UP THE ZABBIX DATABASE

Configuring DHCP Relay service on the FortiGate unit

WAN link load balancing