Install Nginx and PHP-FastCGI or PHP-FPM on CentOS 5.xx or 6.xx and configure with website


First update your linux machine using
You need EPEL Repo as by default for Centos repos dont have nginx
for CentOS 5.xx
for CentOS 6.xx
1
yum update

Install Nginx
1
2
3
yum install nginx php-cli php make automake gcc gcc-c++ spawn-fcgi wget
chkconfig --add nginx
chkconfig --level 35 nginx on
Start Nginx
1
service nginx start
Configure your Websites
You can place your folders even in /var/www/html/ you can change the path in the below commands
1
2
3
mkdir -p /srv/www/www.example.com/public_html
mkdir /srv/www/www.example.com/logs
chown -R nginx:nginx /srv/www/www.example.com
Issue the following commands to create virtual hosting directories:
1
2
mkdir /etc/nginx/sites-available
mkdir /etc/nginx/sites-enabled
Configure Nginx config file to add your sites
1
nano /etc/nginx/nginx.conf
or use vi to edit the file
Add the following lines to your /etc/nginx/nginx.conf file, immediately after the line for “include /etc/nginx/conf.d/*.conf”:
1
2
# Load virtual host configuration files.
include /etc/nginx/sites-enabled/*;
Define your site’s virtual host file:
Make a your virtual host files
1
nano /etc/nginx/sites-available/www.example.com
Add the following code in the file above
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
server {
    server_name www.example.com example.com;
    access_log /srv/www/www.example.com/logs/access.log;
    error_log /srv/www/www.example.com/logs/error.log;
    root /srv/www/www.example.com/public_html;
 
    location / {
        index index.html index.htm index.php;
    }
 
    location ~ \.php$ {
        include /etc/nginx/fastcgi_params;
        fastcgi_pass  127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME /srv/www/www.example.com/public_html$fastcgi_script_name;
    }
}
Issue the following commands to enable the site:
goto the enabled sites
1
cd /etc/nginx/sites-enabled/
create a symlink
1
ln -s /etc/nginx/sites-available/www.example.com
now restart nginx
1
service nginx restart
You may wish to create a test HTML page under /srv/www/www.example.com/public_html/ and view it in your browser to verify that nginx is properly serving your site (PHP will not work yet).
Configure spawn-fcgi
Issue the following command sequence to download scripts to control spawn-fcgi and php-fastcgi, set privileges, make the init script run at startup, and launch it for the first ti
1
2
3
4
5
6
7
8
9
10
cd /opt
mv php-fastcgi-rpm.sh /usr/bin/php-fastcgi
chmod +x /usr/bin/php-fastcgi
mv php-fastcgi-init-rpm.sh /etc/rc.d/init.d/php-fastcgi
chmod +x /etc/rc.d/init.d/php-fastcgi
chkconfig --add php-fastcgi
chkconfig php-fastcgi on
/etc/init.d/php-fastcgi start
Test PHP with FastCGI
Make a php file
1
nano /srv/www/www.example.com/public_html/test.php
add the following code
1
<?php echo phpinfo(); ?>
Install php-fpm
if you dont want to use fast cgi you can install php-fpm. You need to use remi repo to install php-fpm
now you can install it through yum
1
yum --enablerepo=remi install php-fpm
you can now start php-fpm
1
service php-fpm start
when linux start put this on auto
1
chkconfig --add php-fpm