Installing and configuring NGINX + PHP (php-fpm) on a Linux server


+1 vote
Hello!
I’m going to drop Apache and for the first time install Nginx on the server in conjunction with php-fpm. Please, post a detailed instruction.
And what is your opinion – is it true that nginx + php-fpm works much faster than Apache?
asked Jul 23, 2016 in System administration by Ivan Adams

1 Answer

0 votes

Good day to you!
Yes, to my mind, Nginx is several times faster than Apache server. But some people may object – the efficiency of Apache can be enhanced and it will become fast as well. This is true, but don’t forget that Nginx can also be boosted greatly. I will definitely write a separate detailed article on efficiency boosting.

A bit of theory about Nginx + php-fpm

In Apache web-server, php is a plugin module, such a conjunction works slowly and uses up lots of resources. Due to ineffectual architecture, in most cases Apache cannot process more than 200-300 requests per second, even using a very fat server. In Nginx another kind of architecture is used – Nginx web-server itself processes only the statics requests (pictures, css and others), and delegates php processing to another software server – php-fpm. Php-fpm (FastCGI Process Manager) – is completely independent software; it can be installed on the same server as Nginx (for small projects) or moved to a separate server. Large projects are usually handled, using several servers with Nginx, php-fpm and databases. 
 

By substituting Apache for Nginx + php-fpm, you can accelerate users’ requests processing and greatly save on “hardware”. A web project with traffic up to 10000 unique visitors per day can easily live on the cheapest virtual server by DigitalOcean for 5$ monthly. By the way, if you click this link - DigitalOcean, you will get 10$ for your registration, which means 2 months of free use of the virtual server. There are no additional conditions for that.
 

But let’s get down to business. I will describe the installation and setting up process through the example of CentOS 6.x, which is, however, not very different from other Linux installation packages.

Nginx installation:

1. Installation of Nginx repository: standard Linux distros on default include no Nginx distro, therefore add it to the system. 

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

Then, press i to switch to the edit mode of vi editing program and insert:

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

Then press ":wq" and Enter to save and exit. That’s it, now Nginx is available for automatic installation via yum package manager. If you use other Linux distro – proceed to this page to get the instruction, exactly suitable for your system. 
 

2. Nginx installation from repository: now just execute the installation command.

yum install nginx

Php-fpm installation:

PHP-fpm is available by default in any modern distros, therefore just execute its installation:

yum install php-fpm

At that, the server itself and all the optional packages will be installed. If something fails to install, you can assemble php-fpm from source codes yourself, you can find detailed instructions here.

Setting up and starting Nginx + php-fpm:

1. Let’s start from php-fpm: open the configuration file

vi /etc/php-fpm.d/www.conf

and spell the operation via socket in there (in this case, it will operate faster):

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

change the existing line "listen =" or add a new and delete the old one.


2. Now let’s move to Nginx: create the configuration file for your first web site (change site1 for the site’s name, however, it will work in any case)

vi /etc/nginx/conf.d/site1.conf

There is the entry-level configuration below, you can just copy it. Everything you need to change is highlighted in red. In this configuration, static files upload and access to a single php-file - index.php is set up. All the modern CMS can be started, using a single file, thus, this configuration will be suitable for the majority of tasks. If you need to start other php-files, spell them out separately. In this very configuration the site’s files should be located in the folder /home/mysite/public_html/

server {
        listen [server IP address]:80;
        server_name helpdesk.zone;
        resolver 8.8.8.8;
        error_log /var/log/nginx/helpdesk_zone_error.log warn;
        root /home/helpdesk_zone/public_html;
        access_log /var/log/nginx/helpdesk_zone-access.log;
        charset utf-8;
        index index.php;
        location ~ .*(gif|jpg|jpeg|png|ico|swf|txt|pdf|doc|docx|exe|xls|xlsx|strings|zip|rar|7z)$ {
            expires 1y;
        }
        
        location ~ .*(html|htm|js|css)$ {
            expires 1y;
        }

        location ~ ^/index.php {
                fastcgi_pass unix:/var/run/php5-fpm.sock;
                fastcgi_param SCRIPT_FILENAME /home/helpdesk_zone/public_html/index.php;
                include /etc/nginx/fastcgi_params;
                fastcgi_param SCRIPT_NAME /index.php;
        }    

}    

Now, place your first site’s files to the folder /home/site1/public_html/ and we are ready for the start.


3. Starting nginx + php-fpm:

service nginx start
service php-fpm start​

If you have done everything correct and made no mistakes, you will get the following result: 

First start of Nginx and php-fpm

If you have any questions left or need details – please, ask a question or leave a comment.
I’m always pleased to help you!
Good luck!

answered Jul 23, 2016 by Expert Brian (5,370 points)
Welcome to Help Desk, here you can ask any questions and receive answers from our experts and other members of the community.

If you have any questions, please do not hesitate to contact our help desk!

Make a question!