Installing nginx & PHP-FPM under CentOS 7.0

Nginx is a free, high-performance HTTP-server, open source. Nginx is known for its stability, wide range of functions, simple configuration, and low resource consumption. In this article we will discuss how to install Nginx on a Fedora / CentOS with PHP version 5 (through PHP-FPM) and MySQL.

Article originally written for distributions Fedora version 14 and CentOS 6.5, and then adapted for CentOS 7.0. However, setting it to the version RHEL 7.0 / CentOS 7.0 is devoted to a separate article.
1. Preliminary remarks

In this article, we will, we will use as the IP-address of the experimental machine. These settings in your case may be different, so replace them with your own, where you need it.
2. Connecting additional storage

Unfortunately, php-fpm is not available in the repositories (official storage systems) CentOS, but is available from RemiRPM, which itself depends on EPEL. You can enable both storage systems as follows (true for CentOS 6.5):


rpm –import
rpm -ivh

Run yum with the following parameters:

Then do a similar modification to the section [remi] /etc/yum.repos.d/remi.repo and change enabled 1:

3. Installing MySQL 5

First, install MySQL 5 (MariaDB), by running yum with the following parameters:


Then create links to startup for MySQL (MySQL will run automatically at startup) and run the server MySQL:

For the system under CentOS earlier had to do the following:

Now check whether the work is permitted on the network. Proceed as follows:

If you see a message like this:

[root]# netstat -tap | grep mysql
tcp 0 0 *:mysql *:* LISTEN 1279/mysqld

Proceed as follows:

To set the administrator password (otherwise someone may want to access your databases MySQL!):

For further installation, you will need to enter your username and password, the other questions click Enter.

On that note, MySQL 5 installation is completed.


4. Installing Nginx

Nginx on CentOS as available from EPEL, and the official website of the developer to install yum run with the following parameters:


Then create a link to nginx startup and then run the program itself:

For the system under CentOS earlier had to do the following:

You might see an error like: port 80 is already in use, the error will be as follows:

The error appears in the event that is already running another service that uses port 80, for example, apache. Stop and start the apache service NGINX:

We introduce the IP-address of our server or host name in the browser (eg and move on to the start page nginx.

5. Installing PHP5

PHP5 can work with nginx through PHP-FPM (FastCGI Process Manager), which is an alternative PHP FastCGI, with additional features useful for sites of all sizes, especially for sites visited. Will install php-fpm with php-cli and some PHP5 modules, such as php-mysql, which is required to use MySQL in your PHP-scripts to run yum install with the following parameters:


APC is a free and open opcode kesher for PHP, which caches and optimizes the intermediate code PHP. APC is similar to other opcode-kesherami for PHP, such as: eAccelerator and Xcache. It is recommended to install any of these modules to speed up dynamic pages in PHP.

To install APC run yum with the following parameters:


Then open /etc/php.ini and set cgi.fix_pathinfo=0:

; cgi.fix_pathinfo provides *real* PATH_INFO/PATH_TRANSLATED support for CGI. PHP's
; previousbehaviour 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

If required, perform a detailed configuration of PHP.

In order to avoid errors in the time zone file /var/log/php-fpm/www-error.log, when triggered PHP script in your browser, we need to set the time zone (date.timezone) in /etc/php.ini:


Determine the timezone to our system can be as follows:

Then create a link on startup for php-fpm and run:

PHP-FPM is a process (with a script init / etc / init.d / php-fpm), which runs the FastCGI server on port 9000.
6. Configure nginx

Open the configuration file nginx /etc/nginx/nginx.conf:

The first thing (recommended for heavy high-end servers) can increase the number of running processes and set the maximum time maintaining the keepalive-connection:


And you can trust the developers nginx and specify values for auto.

Virtual hosts are defined on the server in a directory /etc/nginx/conf.d. Edit the default vhost (it /etc/nginx/conf.d/default.conf) as follows:


server_name _; creates a standard repository vhost (if you define the host name).

In the section / part, add index.php to the line indkeksa root / usr / share / nginx / html; means that the document root directory is in / usr / share / nginx / html.

PHP is an important part of the location, a stanza: ~ \ .php $ {}. Remove ~ \ .php $ {} to turn on location. Change the root line in the document root of our site (for example / usr / share / nginx / html;). Also add strokutry_files $ uri = 404, to prevent vulnerabilities. Make sure you change the string on fastcgi_param fastcgi_param SCRIPT_FILENAME $ document_root $ fastcgi_script_name, otherwise PHP interpreting software will not find a PHP-script that is used in the browser. At $ document_root given by / usr / share / nginx / html, as this is our root directory.

PHP-FPM standard runs on port 9000 at the address, so ask nginx connect to with a string fastcgi_pass ;. You can also use PHP-FPM socket

Now save the file and restart nginx:


Let’s create a PHP file in the site root / usr / share / nginx / html.

Then open the file created in the browser (eg

PHP5 is working through the FPM / FastCGI, as shown in line Server API. Bottom of the page provides information about the connected modules, including MySQL.
7. Using Unix-socket in PHP-FPM

By default, PHP-FPM uses port 9000 to the address You can also use Unix socket PHP-FPM, thus avoiding the costs of TCP. Open the file and edit /etc/php-fpm.d/www.conf follows:


Restart  PHP-FPM:

Now nginx configuration and replace string fastcgi_pass; on fastcgi_passunix: /tmp/php5-fpm.sock; it will look as follows:

In the end then restart nginx: