Installing nginx web server with php fpm and mysql on CentOS 6

In this article you will find a way to install NGINX web server and add php supporting.
PHP from version 5.3 got php-fpm included so now it’s relatively easy to get it work with nginx without additional scripting.

There are two methods to install nginx into your centos box. There is official repository from where you can install the nginx package with yum. The other method is to build it from source.
In this article we will install the nginx web server from the official repository. If you need to customize nginx for your needs you need to download source code and install from it.
First you need to add the repository to your yum configuration:

cd /etc/yum.repos.d/
vi nginx.repo

Fill nginx.repo :

name=nginx repo

yum install nginx
service nginx start

Now we need to open firewall for it:

iptables -I INPUT 2 -p tcp -m tcp –dport 80 -j ACCEPT

service iptables save

You can check now if the web server is up and running:


We will install php from source. First we will need tools to compile the source code:

yum groupinstall ‘Development Tools’
mkdir /usr/src/php
cd /usr/src/php
curl -o php-5.4.9.tar.gz
tar xvzf php-5.4.9.tar.gz
cd php-5.4.9
./configure –enable-fpm –with-mysql –with-gd –enable-mbstring –with-pdo-mysql

This will drop error. Try to solve the errors by installing the missing libraries:

configure: error: xml2-config not found. Please check your libxml2 installation.

yum search libxml2
yum install libxml2-devel
yum install gd-devel mysql-devel mysql-server

You can get the configurable parameters with –help parameter.

./configure –help

After configuration you can build the source and make the test:

make test

Now it’s time to install the php from source code:

make install

You can check now the location of the php installation:

Installing PHP CLI binary:        /usr/local/bin/
Installing PHP CLI man page:      /usr/local/php/man/man1/
Installing PHP FPM binary:        /usr/local/sbin/
Installing PHP FPM config:        /usr/local/etc/
Installing PHP FPM man page:      /usr/local/php/man/man8/
Installing PHP FPM status page:      /usr/local/php/fpm/
Installing PHP CGI binary:        /usr/local/bin/
Installing build environment:     /usr/local/lib/php/build/
Installing header files:          /usr/local/include/php/
Installing helper programs:       /usr/local/bin/
program: phpize
program: php-config
Installing man pages:             /usr/local/php/man/man1/
page: phpize.1
page: php-config.1
Installing PEAR environment:      /usr/local/lib/php/
[PEAR] Archive_Tar    – installed: 1.3.7
[PEAR] Console_Getopt – installed: 1.3.0
[PEAR] Structures_Graph- installed: 1.0.4
[PEAR] XML_Util       – installed: 1.2.1
[PEAR] PEAR           – installed: 1.9.4
Wrote PEAR system config file at: /usr/local/etc/pear.conf
You may want to add: /usr/local/lib/php to your php.ini include_path
/usr/src/php/php-5.4.9/build/shtool install -c ext/phar/phar.phar /usr/local/bin
ln -s -f /usr/local/bin/phar.phar /usr/local/bin/phar
Installing PDO headers:          /usr/local/include/php/ext/pdo/

Now it’s time to get the init.d script and add it to startup:

cp sapi/fpm/init.d.php-fpm /etc/init.d
chmod +x /etc/init.d/php-fpm
chkconfig php-fpm on

Now if you want to start php-fpm daemon you will get error:
ERROR: failed to open configuration file ‘/usr/local/etc/php-fpm.conf’: No such file or directory (2)
So it’s time to rename the default config file, and edit it for our needs:

cp /usr/local/etc/php-fpm.conf.default /usr/local/etc/php-fpm.conf
vi /usr/local/etc/php-fpm.conf

You can place later multiple pool configuration to /usr/local/etc/fpm.d of course for this you need remove the ‘;’ character from the include line create the directory and place the configuration file for the pool. I suggest you to use unix socket instead of tcp socket if the web server and php-fpm will be on one server:
listen = /var/run/fpm-www.socket

now it’s time to start the daemon:

service php-fpm start

You can check if it’s started:

ps awux|grep php
root     27751  0.0  0.2 137152  2924 ?        Ss   12:07   0:00 php-fpm: master process (/usr/local/etc/php-fpm.conf)
nobody   27752  0.0  0.2 137152  2588 ?        S    12:07   0:00 php-fpm: pool www
nobody   27753  0.0  0.2 137152  2588 ?        S    12:07   0:00 php-fpm: pool www
root     27762  0.3  0.2 137124  2920 ?        Ss   12:11   0:00 php-fpm: master process (/usr/local/etc/php-fpm.conf)
nobody   27763  0.0  0.2 137124  2584 ?        S    12:11   0:00 php-fpm: pool www
nobody   27764  0.0  0.2 137124  2584 ?        S    12:11   0:00 php-fpm: pool www
root     27766  0.0  0.0 103236   868 pts/2    S+   12:11   0:00 grep php

And you can check if the socket exists:

ls -la /var/run/fpm-www.socket

As seems everything is fine, it’s time to configure our nginx server to run php programs. Now we will use only for the default site, but of course you will need to configure this on all virtualhosts.
You can check the configuration for nginx at

vi /etc/nginx/conf.d/default

server {
listen       80;
server_name  localhost;

root   /usr/share/nginx/html;
index  index.html index.htm index.php;

#charset koi8-r;
#access_log  /var/log/nginx/log/host.access.log  main;

#    location / {
#        root   /usr/share/nginx/html;
#        index  index.html index.htm;
#    }

#error_page  404              /404.html;

# redirect server error pages to the static page /50x.html
error_page   500 502 503 504  /50x.html;
location = /50x.html {
root   /usr/share/nginx/html;

# proxy the PHP scripts to Apache listening on
#location ~ \.php$ {
#    proxy_pass;

# pass the PHP scripts to FastCGI server listening on
location ~ \.php$ {
fastcgi_pass   unix:/var/run/fpm-www.socket;
fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
include        fastcgi_params;

# deny access to .htaccess files, if Apache’s document root
# concurs with nginx’s one
location ~ /\.ht {
deny  all;

Save,exit. Now it’s time to restart nginx:

service nginx restart

Now we have to create a test file for testing if php is working:

vi /usr/share/nginx/html/test.php


Go and test it from your browser.
Now everything is prepared. You can add other virtualhosts for your nginx installation, and you can create other php pools for different users and virtualhosts. Or you can just play by installing a drupal to this default virtualhost.
Keep in mind that nginx is currently running as nginx user and the php processes are currently running as nobody. Of course you can change the users for nginx at /etc/nginx/nginx.conf at the user line and for php-fpm at each pool definition at the line user. Currently we are using one pool.
At [www] pool definition user line you can change the user of this pool.
And now here is the installation of drupal:

yum install mysql-server
service mysqld start

create a table and user for drupal.

mysql -u root -p

create database drupal;
create user drupal@localhost identified by ‘drupal’;
grant all privileges on drupal.* to drupal@localhost;
flush privileges;

Download and place the drupal into a directory what can be reached by nginx and php process:

mkdir /usr/share/nginx/html/drupal/
curl -o  /usr/src/drupal.tar.gz
tar xvzf /usr/src/drupal.tar.gz -C /usr/share/nginx/html/
chown nobody:nobody -R /usr/share/nginx/html/drupal

now open the drupal directory on your web server and start installation.


If everything went fine now you have a working drupal site on your nginx/php/mysql/centos powered server.


1 thought on “Installing nginx web server with php fpm and mysql on CentOS 6”

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s