Share, , Google Plus, Pinterest,

Print

Posted in:

Install Nginx, PHP 5.5 and FastCGI on CentOS 6

Nginx is a free and open source HTTP server, reverse proxy and IMAP/POP3 proxy server.

You should install Nginx because it is efficient, stable and high performance event based web server.

Install Nginx
Install Nginx

Apache vs. Nginx

Apache is process based web server while Nginx is event based web server. Apache as a process based web server (synchronous) requires a new thread for each simultaneous request (alot of overhead), while Nginx as an event based web server (asynchronous) handles all requests in one (or at least very few) threads. Under heavier loads Apache can use far too much RAM which can reduce it’s performance, while Nginx will use significantly less RAM in the same situation and perform much better and faster. Try to install Nginx and test it out! 🙂

FastCGI (PHP-FPM)

With FastCGI, PHP workers are running separately from Web Server workers – different process – and with Apache mod_php each Apache Web Server worker has entire PHP interpreter loaded into it. By using PHP in FastCGI mode you can tune the number of PHP workers separately from the number of incoming connections and run PHP workers on a different server. Restarting PHP workers does not require Web Server restart.

Let’s Install Nginx, PHP 5.5 and FastCGI on CentOS 6!

1. Install EPEL and Remi CentOS 6 Repository

[root@foo1 ~]# rpm -ivh http://ftp.uninett.no/linux/epel/6/i386/epel-release-6-8.noarch.rpm
[root@foo1 ~]# rpm -ivh http://rpms.famillecollet.com/enterprise/remi-release-6.rpm

2. Next step to install Nginx is to create Nginx Repository file

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

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

3. Install Nginx, PHP 5.5 and PHP-FPM packages

[root@foo1 ~]# yum --enablerepo=remi,remi-php55 install nginx php-common php-fpm php-mysqlnd-y

You can install additional PHP modules if you require!

4. Nginx is installed, start Nginx Web Server and PHP-FPM

[root@foo1 ~]# /etc/init.d/nginx start
[root@foo1 ~]# /etc/init.d/php-fpm start

5. Enable Nginx and PHP-FPM autostart on boot

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

6. Configure Nginx and FastCGI

Create Website HTML and Log file directories and make apache user owner:

[root@foo1 ~]# mkdir -p /srv/www/mysite/public_html
[root@foo1 ~]# mkdir -p /srv/www/mysite/logs
[root@foo1 ~]# chown -R apache:apache /srv/www/mysite

Create Nginx virtualhost directories:

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

Add the following include line to your /etc/nginx/nginx.conf file after “include /etc/nginx/conf.d/*.conf;” line:

include /etc/nginx/sites-enabled/*;

 

Create Website virtualhost file called “mysite” in /etc/nginx/sites-available directory with the following content (/etc/nginx/sites-available/mysite):

server {
    server_name mysite.com;
    access_log /srv/www/mysite/logs/access.log;
    error_log /srv/www/mysite/logs/error.log;
    root /srv/www/mysite/public_html;

    location / {    
        index index.html index.htm index.php;
    }

    location ~ .php$ {
        try_files $uri =404;
        include /etc/nginx/fastcgi_params;
        fastcgi_pass  127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME /srv/www/mysite/public_html$fastcgi_script_name;
    }
}

 

Please note the “try_files $uri =404;” line! This is added due to a possible Nginx misconfiguration – misconfigured nginx server can allow non-PHP files to be executed as PHP. Read more HERE!

Link virtualhost file to /etc/nginx/sites-enabled/mysite:

[root@foo1 ~]# ln -s /etc/nginx/sites-available/mysite /etc/nginx/sites-enabled/mysite

Restart Nginx:

/etc/init.d/nginx restart

7. Test your Nginx and FastCGI configuration

Create PHP info file (index.php) in your virtualhost root directory with the following content (/srv/www/mysite/public_html/index.php):

<?php
 phpinfo();
 ?>

Visit your website from Web Browser, you should see PHP version and additional info: