How to Install Nginx and PHP-FPM on CentOS 6.4 Server with MySQL Support

How to Install Nginx and PHP-FPM on CentOS 6.4 Server with MySQL Support
By Shay Anderson on October 2013
Follow these instructions to install Nginx Web server and PHP-FPM (FastCGI Process Manager) on CentOS 6.4 with MySQL database support.

1. Install Nginx
First, we install the Nginx Web server. We need to add package repository for install nginx, so add with command line: # rpm -Uvh http://download.fedoraproject.org/pub/epel/6/i386/epel-release-6-8.noarch.rpm
Or, you can use a later nginx version (nginx/1.4.3) with:
# wget http://nginx.org/packages/centos/6/noarch/RPMS/nginx-release-centos-6-0.el6.ngx.noarch.rpm
# rpm -ivh nginx-release-centos-6-0.el6.ngx.noarch.rpm
Then, install the nginx Web server: # yum install nginxAnd start the nginx Web server: # service nginx start

2. Install PHP-FPM
To install PHP-FPM we first need to add package repository: # rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-6.rpmInstall PHP-FPM: # yum –enablerepo=remi install php-fpm php-mysql php-common php-cli php-pdo php-curl php-gd php-mbstring php-mcryptNow PHP should be installed, check version with: # php -v
PHP 5.4.21 (cli) (built: Oct 27 2013 12:07:11)
Copyright (c) 1997-2013 The PHP Group
Zend Engine v2.4.0, Copyright (c) 1998-2013 Zend Technologies

3. Configure PHP
Edit the /etc/php.ini file and make sure cgi.fix_pathinfo is set to 0: cgi.fix_pathinfo=0This setting will help secure PHP by only allowed PHP to process exact file paths.

4. Configure Nginx
Edit the /etc/nginx/nginx.conf file and raise the number of worker processes to 4: worker_processes 4;
Create directory for Web server root (you can make this wherever you want, I prefer /var/www/html): # mkdir -p /var/www/html
# cp -v /usr/share/nginx/html/* /var/www/html

Next, edit the # nano /etc/nginx/conf.d/default.conf file for virtual hosts. Add server name: server_name example.com;Set root directory and add index.php as index file: location / {
root /var/www/html;
index index.php index.html index.htm;
}Also, change the root directory for 404 and 50x pages: error_page 404 /404.html;
location = /404.html {
root /var/www/html;
}

error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /var/www/html;
}Finally, uncomment the PHP / FastCGI server block so it works (also set the root to /var/www/html, and set the fastcgi_param setting to help the PHP interpreter locate the PHP scripts): # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
root /var/www/html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
# must have newer nginx version (1.2 works) to use HTTPS for PHP $_SERVER['HTTPS']:
fastcgi_param HTTPS $https;
# original: fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
include fastcgi_params;
}
If you are using iptables set an exception for the nginx port, for example, if you are using port 8080 use: # iptables -A INPUT -p tcp -m tcp –dport 8080 -j ACCEPT
# service iptables save

5. Configure PHP-FPM
Edit the file /etc/php-fpm.d/www.conf and change the user/group to nginx: ; Unix user/group of processes
; Note: The user is mandatory. If the group is not set, the default user’s group
; will be used.
; RPM: apache Choosed to be able to access some dir as httpd
user = nginx
; RPM: Keep a group allowed to write in log dir.
group = nginxRestart PHP-FPM and nginx: # service php-fpm restart
# service nginx restart
You should now be able to view a Web page on your nginx Web server by going to http://[your server IP]/ or http://[your server hostname]/

You can setup Nginx and PHP-FPM to auto start on server boot: # chkconfig nginx on
# chkconfig php-fpm on

Note: when starting nginx if you see an error like: nginx: [emerg] could not build the server_names_hash, you should increase server_names_hash_bucket_size: 32Add this line to the /etc/nginx/nginx.conf file: http {
server_names_hash_bucket_size 64;