How to Setup Nginx,PHP5.4, PHP-FPM, MySQL 5.5 On CentOS 6.5 VPS

This post will show you the procedure to setup Nginx, PHP5.4, PHP-FPM and MySQL 5.5 On CentOS 6.5 Virtual private server(VPS). You need to setup the required repo such as EPEL, Remi and also the NGINX repo.

What is NGINX ?

NGINX is an alternate web server for Apache. Nginx is an open source web server and a reverse proxy server for HTTP, SMTP, POP3 and IMAP protocols. Many websites and the web developer have moved to NGINX because it’s scalable, low resources, can handle many users concurrency and good website performance. Now it still third most popular web server in the world and it serve just over 14% of all hostnames.

What is PHP(PHP-FPM) ?

PHP (recursive acronym for PHP: Hypertext Preprocessor) is a widely-used open source general-purpose scripting language. PHP-FPM (FastCGI Process Manager) is an alternative PHP FastCGI implementation with some additional features useful for sites of any size, especially busier sites.

What is MySQL ?

MySQL Database server is one of the most popular used database in the internet especially for content management and blogging site.

Steps to setup Nginx,PHP5.4, PHP-FPM, MySQL 5.5 On CentOS 6.5 VPS

1. Setup EPEL and Remi repository.
How to prepared EPEL and how to configure Remi repository in to CentOS.

2. Install php 5.4, php-fpm and MySQL 5.5 Server :

[root@vps-08 ~]# yum --enablerepo=remi install php php-mysql php-fpm mysql mysql-server -y

3. Setup repository for nginx :

[root@vps-08 ~]# vi /etc/yum.repos.d/nginx.repo

Add the following and save :

[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=0
enabled=1

4. Install and setup NGINX :
Run the following command to install NGINX.

[root@vps-08 ~]# yum install nginx -y

a. Setup NGINX config file :

[root@vps-08 ~]# vi /etc/nginx/nginx.conf

Add the following and save:

user  nginx;
worker_processes  2;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    gzip  on;
    gzip_types text/css text/x-component application/x-javascript application/javascript text/javascript text/x-js text/richtext image/svg+xml text/plain text/xsd text/xsl text/xml image/x-icon;

    include /etc/nginx/sites-available/*.conf;

}

b. Create sites-available directory and create nginx virtual host for example.com domain :

[root@vps-08 ~]# mkdir /etc/nginx/sites-available
[root@vps-08 ~]# vi /etc/nginx/sites-available/example.com.conf

Add the following and save :

server {
listen       80;
    server_name example.com;
    rewrite ^/(.*)$ http://www.example.com/$1 permanent;
}

server {
        server_name www.example.com;
        root /var/www/html/example;
        access_log /var/log/nginx/example.com.access.log;
        error_log /var/log/nginx/example.com.error.log;
        include conf.d/common.conf;
        include conf.d/wordpress.conf;
        include conf.d/w3tc.conf;
}

c. Create these three configuration files. It was optimized for WordPress site.

/etc/nginx/conf.d/common.conf
/etc/nginx/conf.d/wordpress.conf
/etc/nginx/conf.d/w3tc.conf

Create common.conf :

[root@vps-08 ~]# vi /etc/nginx/conf.d/common.conf

Add the following and save.

# Global configuration file.
# ESSENTIAL : Configure Nginx Listening Port
listen 80;
# ESSENTIAL : Default file to serve. If the first file isn't found,
index index.php index.html index.htm;
# ESSENTIAL : no favicon logs
location = /favicon.ico {
    log_not_found off;
    access_log off;
}
# ESSENTIAL : robots.txt
location = /robots.txt {
    allow all;
    log_not_found off;
    access_log off;
}
# ESSENTIAL : Configure 404 Pages
error_page 404 /404.html;
# ESSENTIAL : Configure 50x Pages
error_page 500 502 503 504 /50x.html;
    location = /50x.html {
        root /usr/share/nginx/html;
    }
# SECURITY : Deny all attempts to access hidden files .abcde
location ~ /\. {
    deny all;
}
# PERFORMANCE : Set expires headers for static files and turn off logging.
location ~* ^.+\.(js|css|swf|xml|txt|ogg|ogv|svg|svgz|eot|otf|woff|mp4|ttf|rss|atom|jpg|jpeg|gif|png|ico|zip|tgz|gz|rar|bz2|doc|xls|exe|ppt|tar|mid|midi|wav|bmp|rtf)$ {
    access_log off; log_not_found off; expires 30d;
}

Configure wordpress.conf :

[root@vps-08 ~]# vi /etc/nginx/conf.d/wordpress.conf

Add the following and save :

# WORDPRESS : Rewrite rules, sends everything through index.php and keeps the appended query string intact
location / {
    try_files $uri $uri/ /index.php?q=$uri&$args;
}

# SECURITY : Deny all attempts to access PHP Files in the uploads directory
location ~* /(?:uploads|files)/.*\.php$ {
    deny all;
}
# REQUIREMENTS : Enable PHP Support
location ~ \.php$ {
    # SECURITY : Zero day Exploit Protection
    try_files $uri =404;
    # ENABLE : Enable PHP, listen fpm sock
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    fastcgi_pass unix:/var/run/php-fpm.sock;
    fastcgi_index index.php;
    include fastcgi_params;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
# PLUGINS : Enable Rewrite Rules for SiteMap
rewrite ^/sitemap(-+([a-zA-Z0-9_-]+))?\.xml$ "/index.php?xml_sitemap=params=$2" last;
rewrite ^/sitemap(-+([a-zA-Z0-9_-]+))?\.xml\.gz$ "/index.php?xml_sitemap=params=$2;zip=true" last;
rewrite ^/sitemap(-+([a-zA-Z0-9_-]+))?\.html$ "/index.php?xml_sitemap=params=$2;html=true" last;
rewrite ^/sitemap(-+([a-zA-Z0-9_-]+))?\.html.gz$ "/index.php?xml_sitemap=params=$2;html=true;zip=true" last;

Create w3tc.conf file :

[root@vps ~]# vi /etc/nginx/conf.d/w3tc.conf

Add the following and save :

# BEGIN W3TC Page Cache core
set $w3tc_rewrite 1;
if ($request_method = POST) {
    set $w3tc_rewrite 0;
}
if ($query_string != "") {
    set $w3tc_rewrite 0;
}
if ($http_cookie ~* "(comment_author|wp\-postpass|w3tc_logged_out|wordpress_logged_in|wptouch_switch_toggle)") {
    set $w3tc_rewrite 0;
}
if ($http_cookie ~* "(w3tc_preview)") {
    set $w3tc_rewrite _preview;
}
set $w3tc_enc "";
if ($http_accept_encoding ~ gzip) {
    set $w3tc_enc _gzip;
}
set $w3tc_ext "";
if (-f "$document_root/wp-content/cache/page_enhanced/$http_host/$request_uri/_index$w3tc_rewrite.html$w3tc_enc") {
    set $w3tc_ext .html;
}
if (-f "$document_root/wp-content/cache/page_enhanced/$http_host/$request_uri/_index$w3tc_rewrite.xml$w3tc_enc") {
    set $w3tc_ext .xml;
}
if ($w3tc_ext = "") {
  set $w3tc_rewrite 0;
}
if ($w3tc_rewrite = 1) {
    rewrite .* "/wp-content/cache/page_enhanced/$http_host/$request_uri/_index$w3tc_rewrite$w3tc_ext$w3tc_enc" last;
}
# END W3TC Page Cache core

d. Modify default.conf file :

[root@vps-08 ~]# vi /etc/nginx/conf.d/default.conf
server {
    listen       80;
    server_name  localhost;

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

# 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;
    }
}

5. Secure Mysql :

[root@vps-08 ~]# /usr/bin/mysql_secure_installation

Sample :

[root@vps-08 ~]# /usr/bin/mysql_secure_installation

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MySQL
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!


In order to log into MySQL to secure it, we'll need the current
password for the root user.  If you've just installed MySQL, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.

Enter current password for root (enter for none):
OK, successfully used password, moving on...

Setting the root password ensures that nobody can log into the MySQL
root user without the proper authorisation.

Set root password? [Y/n] y
New password:
Re-enter new password:
Password updated successfully!
Reloading privilege tables..
 ... Success!


By default, a MySQL installation has an anonymous user, allowing anyone
to log into MySQL without having to have a user account created for
them.  This is intended only for testing, and to make the installation
go a bit smoother.  You should remove them before moving into a
production environment.

Remove anonymous users? [Y/n] y
 ... Success!

Normally, root should only be allowed to connect from 'localhost'.  This
ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n] n
 ... skipping.

By default, MySQL comes with a database named 'test' that anyone can
access.  This is also intended only for testing, and should be removed
before moving into a production environment.

Remove test database and access to it? [Y/n] y
 - Dropping test database...
 ... Success!
 - Removing privileges on test database...
 ... Success!

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

Reload privilege tables now? [Y/n] y
 ... Success!

Cleaning up...



All done!  If you've completed all of the above steps, your MySQL
installation should now be secure.

Thanks for using MySQL!


6. Configure php-fpm :

[root@vps-08 ~]# vi /etc/php-fpm.d/www.conf

Update and uncomment the following :

listen = /var/run/php-fpm.sock
..
listen.mode = 0666
..
user = nginx
group = nginx
..
pm = dynamic
..
pm.max_children = 50
pm.start_servers = 10
pm.min_spare_servers = 10
pm.max_spare_servers = 10
pm.max_requests = 200
..
slowlog = /var/log/php-fpm/www-slow.log
..
php_admin_value[error_log] = /var/log/php-fpm/www-error.log
php_admin_flag[log_errors] = on
..
php_value[session.save_handler] = files
php_value[session.save_path] = /var/lib/php/session

7. Restart the NGINX and php-fpm :
Restart the NGINX and php-fpm service to apply the changes on the configuration files.

[root@vps-08 ~]# service nginx restart; service php-fpm restart
Stopping nginx:                                            [  OK  ]
Starting nginx:                                            [  OK  ]
Stopping php-fpm:                                          [  OK  ]
Starting php-fpm:                                          [  OK  ]

Check the service listened on your VPS :

[root@vps-08 ~]# netstat -plunt | grep LISTEN
tcp        0      0 0.0.0.0:111                 0.0.0.0:*                   LISTEN      1097/rpcbind
tcp        0      0 0.0.0.0:80                  0.0.0.0:*                   LISTEN      18070/nginx
tcp        0      0 0.0.0.0:22                  0.0.0.0:*                   LISTEN      1130/sshd
tcp        0      0 0.0.0.0:3306                0.0.0.0:*                   LISTEN      18358/mysqld
tcp        0      0 :::111                      :::*                        LISTEN      1097/rpcbind
tcp        0      0 :::22                       :::*                        LISTEN      1130/sshd

Now you can start to setup WordPress blog into your VPS server.

setup nginx

How to Install Nginx With PHP5 (and PHP-FPM) and MySQL (LEMP) On Ubuntu 14.04

NGINX, pronounced “Engine X” is an alternate web server for Apache. NGINX is an open source web server and a reverse proxy server for HTTP, SMTP, POP3 and IMAP protocols. If you plan to run a very busy and high concurrency websites but with low memory usage, i would suggest you to setup NGINX as a web server on your Virtual Private Server (VPS) or dedicated server.

1. Install nginx, PHP and MySQL :

ehowstuff@ubuntu14:~$ sudo apt-get install nginx php5 php5-fpm php5-mysql php5-curl php5-gd php5-intl php-pear php5-imagick php5-imap php5-mcrypt php5-memcache php5-ming php5-ps php5-pspell php5-recode php5-snmp php5-sqlite php5-tidy php5-xmlrpc php5-xsl mysql-server-5.6 -y

During this installation, you will require to set MySQL’s root password :
1

2

2. Open /etc/php5/fpm/php.ini and set cgi.fix_pathinfo=0:

ehowstuff@ubuntu14:~$ sudo vi /etc/php5/fpm/php.ini
; http://php.net/cgi.fix-pathinfo
cgi.fix_pathinfo=0

; FastCGI under IIS (on WINNT based OS) supports the ability to impersonate
; securi

3. Set date.timezone in /etc/php5/fpm/php.ini :

ehowstuff@ubuntu14:~$ sudo vi /etc/php5/fpm/php.ini
[Date]
; Defines the default timezone used by the date functions
; http://php.net/date.timezone
date.timezone = "Asia/Kuala_Lumpur"

; http://php.net/date.default-latitude
;date.default_latitude = 31.7667

4. By default PHP-FPM is listening on the socket /var/run/php5-fpm.sock. We have an option to make PHP-FPM use a TCP connection. Open and add “listen = 127.0.0.1:9000” and comment out “listen = /var/run/php5-fpm.sock”

ehowstuff@ubuntu14:~$ sudo vim /etc/php5/fpm/pool.d/www.conf

Change as below :

..
..
;   '/path/to/unix/socket' - to listen on a unix socket.
; Note: This value is mandatory.
;listen = /var/run/php5-fpm.sock
listen = 127.0.0.1:9000
..
..

5. Restart PHP-FPM :

ehowstuff@ubuntu14:~$ sudo service php5-fpm restart
stop: Unknown instance:
php5-fpm start/running, process 7314

6. Backup NGINX config file :

ehowstuff@ubuntu14:~$ sudo cp -p /etc/nginx/nginx.conf /etc/nginx/nginx.conf.bak

7. Check Number CPU on your VPS server. This will will require to adjust NGINX Worker Processes & Connections. NGINX workers is equal to the number of processors :

ehowstuff@ubuntu14:~$ lscpu | grep '^CPU(s)'
CPU(s):                1

8. Configure worker_processes on nginx.conf as below :

..
..
worker_processes 1;
..
..

Full nginx configuration should like below :

user www-data;
worker_processes 1;
pid /run/nginx.pid;

events {
        worker_connections 768;
        # multi_accept on;
}

http {

        ##
        # Basic Settings
        ##

        sendfile on;
        tcp_nopush on;
        tcp_nodelay on;
        keepalive_timeout 65;
        types_hash_max_size 2048;
        # server_tokens off;

        # server_names_hash_bucket_size 64;
        # server_name_in_redirect off;

        include /etc/nginx/mime.types;
        default_type application/octet-stream;

        ##
        # Logging Settings
        ##

        access_log /var/log/nginx/access.log;
        error_log /var/log/nginx/error.log;

        ##
        # Gzip Settings
        ##

        gzip on;
        gzip_disable "msie6";

        # gzip_vary on;
        # gzip_proxied any;
        # gzip_comp_level 6;
        # gzip_buffers 16 8k;
        # gzip_http_version 1.1;
        # gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;

        ##
        # nginx-naxsi config
        ##
        # Uncomment it if you installed nginx-naxsi
        ##

        #include /etc/nginx/naxsi_core.rules;

        ##
        # nginx-passenger config
        ##
        # Uncomment it if you installed nginx-passenger
        ##

        #passenger_root /usr;
        #passenger_ruby /usr/bin/ruby;

        ##
        # Virtual Host Configs
        ##

        include /etc/nginx/conf.d/*.conf;
        include /etc/nginx/sites-enabled/*;
}


#mail {
#       # See sample authentication script at:
#       # http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript
#
#       # auth_http localhost/auth.php;
#       # pop3_capabilities "TOP" "USER";
#       # imap_capabilities "IMAP4rev1" "UIDPLUS";
#
#       server {
#               listen     localhost:110;
#               protocol   pop3;
#               proxy      on;
#       }
#
#       server {
#               listen     localhost:143;
#               protocol   imap;
#               proxy      on;
#       }
#}

9. Under your home directory, create public_html and change owner to www-data. This will be your document root directory :

ehowstuff@ubuntu14:~$ sudo mkdir public_html/
ehowstuff@ubuntu14:~$ sudo chown -R www-data:www-data public_html/

10. Create index.php under document root directory :

ehowstuff@ubuntu14:~$ sudo vim public_html/index.php

Insert the following :

<?php
phpinfo();
?>

11. Now it’s turn to setup your NGINX. Please modify the default site.

ehowstuff@ubuntu14:~$ sudo vim /etc/nginx/sites-available/default

Kindly update the root directory and comment out the required lines as below :

# You may add here your
# server {
#	...
# }
# statements for each of your virtual hosts to this file

##
# You should look at the following URL's in order to grasp a solid understanding
# of Nginx configuration files in order to fully unleash the power of Nginx.
# http://wiki.nginx.org/Pitfalls
# http://wiki.nginx.org/QuickStart
# http://wiki.nginx.org/Configuration
#
# Generally, you will want to move this file somewhere, and start with a clean
# file but keep this around for reference. Or just disable in sites-enabled.
#
# Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples.
##

server {
	listen 80 default_server;
	listen [::]:80 default_server ipv6only=on;

	root /home/ehowstuff/public_html;
	index index.html index.htm index.php;

	# Make site accessible from http://localhost/
	server_name localhost;

	location / {
		# First attempt to serve request as file, then
		# as directory, then fall back to displaying a 404.
		try_files $uri $uri/ =404;
		# Uncomment to enable naxsi on this location
		# include /etc/nginx/naxsi.rules
	}

	# Only for nginx-naxsi used with nginx-naxsi-ui : process denied requests
	#location /RequestDenied {
	#	proxy_pass http://127.0.0.1:8080;    
	#}

        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;
	}

	# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
	#
	location ~ \.php$ {
		fastcgi_split_path_info ^(.+\.php)(/.+)$;
	#	# NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
	#
	#	# With php5-cgi alone:
		fastcgi_pass 127.0.0.1:9000;
	#	# With php5-fpm:
	#	fastcgi_pass unix:/var/run/php5-fpm.sock;
		fastcgi_index index.php;
		include fastcgi_params;
	}

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


# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
#	listen 8000;
#	listen somename:8080;
#	server_name somename alias another.alias;
#	root html;
#	index index.html index.htm;
#
#	location / {
#		try_files $uri $uri/ =404;
#	}
#}


# HTTPS server
#
#server {
#	listen 443;
#	server_name localhost;
#
#	root html;
#	index index.html index.htm;
#
#	ssl on;
#	ssl_certificate cert.pem;
#	ssl_certificate_key cert.key;
#
#	ssl_session_timeout 5m;
#
#	ssl_protocols SSLv3 TLSv1 TLSv1.1 TLSv1.2;
#	ssl_ciphers "HIGH:!aNULL:!MD5 or HIGH:!aNULL:!MD5:!3DES";
#	ssl_prefer_server_ciphers on;
#
#	location / {
#		try_files $uri $uri/ =404;
#	}
#}

12. Restart your nginx and php5-fpm service to take effect all changes :

ehowstuff@ubuntu14:~$ sudo service nginx restart
ehowstuff@ubuntu14:~$ sudo service php5-fpm restart

13. Test your NGINX site by visit http://IP-Adress :
ubuntu-lemp

Done!!!

How to Setup WordPress on Nginx, PHP-FPM and MySQL

WordPress website can be served using Apache or NGINX. Apache is the most popular Web server and most widely used for WordPress blogging platform. Apache is a great option and has served many of the world’s largest Web sites. Alternate web server for Apache is Nginx, pronounced “Engine X”. Nginx is an open source web server and a reverse proxy server for HTTP, SMTP, POP3 and IMAP protocols. Many websites and the web developer have moved to NGINX because it’s scalable, low resources, can handle many users concurrency and good website performance. For largest and busiest website, i would suggest you to host you websites and blog on Virtual Private Server (VPS) or dedicated server and run NGINX as a web server. Assumed that MySQL has been prepared and configured for WordPress and was tested on CentOS 6.5.

1. Prepared Nginx Repository :

[root@vps ~]# vi /etc/yum.repos.d/nginx.repo
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=0
enabled=1

2. Install NGINX, PHP, php-fpm amd MySQL server :

[root@vps ~]# yum install nginx php php-cli php-mysql php-gd php-xml php-fpm mysql mysql-server -y

3. Open /etc/php.ini and set cgi.fix_pathinfo=0:

[root@vps ~]# vi /etc/php.ini
..
..
; cgi.fix_pathinfo provides *real* PATH_INFO/PATH_TRANSLATED support for CGI.  PHP's
; previous behaviour was to set PATH_TRANSLATED to SCRIPT_FILENAME, and to not grok
; what PATH_INFO is.  For more information on PATH_INFO, see the cgi specs.  Setting
; this to 1 will cause PHP CGI to fix its paths to conform to the spec.  A setting
; of zero causes PHP to behave as before.  Default is 1.  You should fix your scripts
; to use SCRIPT_FILENAME rather than PATH_TRANSLATED.
; http://www.php.net/manual/en/ini.core.php#ini.cgi.fix-pathinfo
cgi.fix_pathinfo=0
..
..

4. Set date.timezone in /etc/php.ini :

[Date]
; Defines the default timezone used by the date functions
; http://www.php.net/manual/en/datetime.configuration.php#ini.date.timezone
date.timezone = "Asia/Kuala_Lumpur"

5. This server configuration was setup for PHP-FPM use a UNIX Socket.

[root@vps ~]# vim /etc/php-fpm.d/www.conf

Specify .sock path :

..
listen = /var/run/php-fpm.sock
..

Change user to run php-fpm :

..
user = nginx
..
..
group = nginx
..

6. Backup NGINX config file :

[root@vps ~]# cp -p /etc/nginx/nginx.conf /etc/nginx/nginx.conf.bak

7. Create sites-available folder under /etc/nginx :

[root@vps ~]# mkdir /etc/nginx/sites-available

8. Adjust NGINX Worker Processes & Connections. NGINX workers equal the number of processors :

Check Number CPU on your VPS server :

[root@vps ~]# lscpu | grep '^CPU(s)'
CPU(s):                1

or

[root@vps ~]# cat /proc/cpuinfo | grep processor
processor       : 0
..
worker_processes  1;
..
..
worker_connections  1024;
..

Configure nginx.conf as below :

user  nginx;
worker_processes  1;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    gzip  on;
    gzip_types text/css text/x-component application/x-javascript application/javascript  text/javascript text/x-js text/richtext image/svg+xml text/plain text/xsd text/xsl text/xml image/x-icon;

    
    include /etc/nginx/sites-available/*.conf;
}

9. Create common.conf and wordpress.conf under /etc/nginx/conf.d/ folder :

[root@vps ~]# vim /etc/nginx/conf.d/common.conf
[root@vps ~]# vim /etc/nginx/conf.d/wordpress.conf
[root@vps ~]# vim /etc/nginx/conf.d/common.conf

Add the following :

# Global configuration file.
# ESSENTIAL : Configure Nginx Listening Port
listen 80;
# ESSENTIAL : Default file to serve. If the first file isn't found,
index index.php index.html index.htm;
# ESSENTIAL : no favicon logs
location = /favicon.ico {
    log_not_found off;
    access_log off;
}
# ESSENTIAL : robots.txt
location = /robots.txt {
    allow all;
    log_not_found off;
    access_log off;
}
# ESSENTIAL : Configure 404 Pages
error_page 404 /404.html;
# ESSENTIAL : Configure 50x Pages
error_page 500 502 503 504 /50x.html;
    location = /50x.html {
        root /usr/share/nginx/html;
    }
# SECURITY : Deny all attempts to access hidden files .abcde
location ~ /\. {
    deny all;
}
# PERFORMANCE : Set expires headers for static files and turn off logging.
location ~* ^.+\.(js|css|swf|xml|txt|ogg|ogv|svg|svgz|eot|otf|woff|mp4|ttf|rss|atom|jpg|jpeg|gif|png|ico|zip|tgz|gz|rar|bz2|doc|xls|exe|ppt|tar|mid|midi|wav|bmp|rtf)$ {
    access_log off; log_not_found off; expires 30d;

Explanation :

listen 80; 

-Specifies the listening port of the server.

index index.php index.html index.htm;

-Specifies the default file to serve (WordPress index.php). For HTML sites, please include index.html & index.htm;.

location = /robots.txt {allow all;} 

-Allows the access to robots.txt

Create wordpress.conf :

[root@vps ~]# vim /etc/nginx/conf.d/wordpress.conf

Add the following :

# WORDPRESS : Rewrite rules, sends everything through index.php and keeps the appended query string intact
location / {
    try_files $uri $uri/ /index.php?q=$uri&$args;
}

# SECURITY : Deny all attempts to access PHP Files in the uploads directory
location ~* /(?:uploads|files)/.*\.php$ {
    deny all;
}
# REQUIREMENTS : Enable PHP Support
location ~ \.php$ {
    # SECURITY : Zero day Exploit Protection
    try_files $uri =404;
    # ENABLE : Enable PHP, listen fpm sock
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    fastcgi_pass unix:/var/run/php-fpm.sock;
    fastcgi_index index.php;
    include fastcgi_params;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
# PLUGINS : Enable Rewrite Rules for Yoast SEO SiteMap
rewrite ^/sitemap_index\.xml$ /index.php?sitemap=1 last;
rewrite ^/([^/]+?)-sitemap([0-9]+)?\.xml$ /index.php?sitemap=$1&sitemap_n=$2 last;

Explanation :

try_files $uri $uri/ /index.php?q=$uri&$args 

-rewrite rule required to allow you to choose your custom permalink structure on WordPress.

location ~* /(?:uploads|files)/.*\.php$ {deny all;} 

-this will prevent malicious code from being uploaded and executed from the WordPress media directory.

location ~ \.php$ {...}

-since WordPress is a php site, we need to tell NGINX how to a pass our php scripts to PHP5.

try_files $uri =404; 

-this is a security rule, you only want to either serve a determined php file or go to a 404 error.

10. Create a virtual server under /etc/nginx/sites-available/ directory :

[root@vps ~]# vi /etc/nginx/sites-available/ehowstuff.local.conf

Assumed that you want to configure a WordPress site with www.ehowstuff.local domain, please setup as below :

server {
    server_name ehowstuff.local;
    rewrite ^/(.*)$ http://www.ehowstuff.local/$1 permanent;
}

server {
        server_name www.ehowstuff.local;
        root /var/www/html/wordpress;
        access_log /var/log/nginx/www.ehowstuff.local.access.log;
        error_log /var/log/nginx/www.ehowstuff.local.error.log;
        include conf.d/common.conf;
        include conf.d/wordpress.conf;
}

Please change the following for virtual server ehowstuff.local.conf :

server_name: Define the server block for the URL.
root: Where you keep your website file
access log & error log: Paths for your logs

11. Start php-fpm and nginx :

[root@vps ~]# /etc/init.d/php-fpm start
Starting php-fpm:                                          [  OK  ]
[root@vps ~]# /etc/init.d/nginx start
Starting nginx:                                            [  OK  ]

12. Make php-fpm and nginx start at boot :

[root@vps ~]# chkconfig php-fpm on
[root@vps ~]# chkconfig nginx on

13. Verify that the required port already present.

[root@vps ~]# netstat -plunt
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address               Foreign Address             State       PID/Program name
tcp        0      0 0.0.0.0:111                 0.0.0.0:*                   LISTEN      1042/rpcbind
tcp        0      0 0.0.0.0:80                  0.0.0.0:*                   LISTEN      3174/nginx
tcp        0      0 0.0.0.0:22                  0.0.0.0:*                   LISTEN      1096/sshd
tcp        0      0 0.0.0.0:3306                0.0.0.0:*                   LISTEN      3394/mysqld
tcp        0      0 :::111                      :::*                        LISTEN      1042/rpcbind
tcp        0      0 :::22                       :::*                        LISTEN      1096/sshd
udp        0      0 0.0.0.0:111                 0.0.0.0:*                               1042/rpcbind
udp        0      0 0.0.0.0:793                 0.0.0.0:*                               1042/rpcbind
udp        0      0 :::111                      :::*                                    1042/rpcbind
udp        0      0 :::793                      :::*                                    1042/rpcbind

How to Install PHP-FPM on CentOS 6.2

PHP-FPM (FastCGI Process Manager) is a PHP FastCGI implementation and is an alternative PHP FastCGI implementation with some additional features useful for sites of any size, especially busier sites. PHP-FPM is a set of patches for PHP to work with the FastCGI process manager, turning PHP-FPM into a service rather than loading all of its libraries everytime a PHP request it’s made. In this post, i will show on how to install PHP-FPM on linux CentOS 6.2 server. Assume that you have configured EPEL and remi repositories on you linux CentOS 6.2 server.
How to Install and Configure EPEL Repository on CentOS 6.2
How to Install Remi yum Repository on CentOS 6.2

1. How to install PHP and other php extensions :

[root@centos62 ~]# yum --enablerepo=remi install php php-fpm php-common php-pecl-apc php-cli php-pear php-pdo php-mysql php-pgsql php-pecl-mongo php-sqlite php-pecl-memcache php-pecl-memcached php-gd php-mbstring php-mcrypt php-xml

Output :


  Installing : libmemcached-1.0.4-1.el6.remi.i686                                              8/23
  Installing : php-pecl-memcached-2.0.1-2.el6.remi.i686                                        9/23
  Installing : php-mysql-5.4.0-1.el6.remi.i686                                                10/23
  Installing : php-gd-5.4.0-1.el6.remi.i686                                                   11/23
  Installing : php-pecl-mongo-1.2.10-2.el6.remi.i686                                          12/23
  Installing : php-pecl-apc-3.1.10-2.el6.remi.i686                                            13/23
  Installing : php-pecl-memcache-3.0.6-3.el6.remi.i686                                        14/23
  Installing : php-pecl-sqlite-2.0.0-0.1.svn313074.el6.remi.i686                              15/23
  Installing : php-5.4.0-1.el6.remi.i686                                                      16/23
  Installing : php-pgsql-5.4.0-1.el6.remi.i686                                                17/23
  Installing : php-mcrypt-5.4.0-1.el6.remi.i686                                               18/23
  Installing : php-xml-5.4.0-1.el6.remi.i686                                                  19/23
  Installing : php-fpm-5.4.0-1.el6.remi.i686                                                  20/23
  Installing : php-mbstring-5.4.0-1.el6.remi.i686                                             21/23
  Installing : compat-mysql51-5.1.54-1.el6.remi.i686                                          22/23
  Cleanup    : mysql-libs-5.1.61-1.el6_2.1.i686                                               23/23

Installed:
  php.i686 0:5.4.0-1.el6.remi                           php-cli.i686 0:5.4.0-1.el6.remi
  php-common.i686 0:5.4.0-1.el6.remi                    php-fpm.i686 0:5.4.0-1.el6.remi
  php-gd.i686 0:5.4.0-1.el6.remi                        php-mbstring.i686 0:5.4.0-1.el6.remi
  php-mcrypt.i686 0:5.4.0-1.el6.remi                    php-mysql.i686 0:5.4.0-1.el6.remi
  php-pdo.i686 0:5.4.0-1.el6.remi                       php-pear.noarch 1:1.9.4-7.el6.remi
  php-pecl-apc.i686 0:3.1.10-2.el6.remi                 php-pecl-memcache.i686 0:3.0.6-3.el6.remi
  php-pecl-memcached.i686 0:2.0.1-2.el6.remi            php-pecl-mongo.i686 0:1.2.10-2.el6.remi
  php-pecl-sqlite.i686 0:2.0.0-0.1.svn313074.el6.remi   php-pgsql.i686 0:5.4.0-1.el6.remi
  php-xml.i686 0:5.4.0-1.el6.remi

Dependency Installed:
  compat-mysql51.i686 0:5.1.54-1.el6.remi                   libmemcached.i686 0:1.0.4-1.el6.remi
  php-pecl-igbinary.i686 0:1.1.2-0.1.git3b8ab7e.el6.remi    t1lib.i686 0:5.1.2-6.el6_2.1

Dependency Updated:
  mysql-libs.i686 0:5.5.22-1.el6.remi

Complete!

2. How to check PHP Version :

[root@centos62 ~]# php -v
PHP 5.4.0 (cli) (built: Mar  2 2012 17:49:53)
Copyright (c) 1997-2012 The PHP Group
Zend Engine v2.4.0, Copyright (c) 1998-2012 Zend Technologies

3. Proceed to configure PHP-FPM to meet your requirement :

[root@centos62 ~]# vi /etc/php-fpm.d/www.conf

4. Start PHP-FPM :

[root@centos62 ~]# service php-fpm start
Starting php-fpm:                                          [  OK  ]