Install Nginx in CentOS 7

Nginx (or Engine X) is a free and powerful HTTP and proxy server open source and architecture based on event processing. It is written in the C programming language and can run on both Windows and Unix-like systems.

In addition to the functions of the web server, the program can work as a reverse proxy proxies for TCP, UDP, or mail, and also as load balancer. Nginx is used to support the work of a huge number of sites on the Internet, also known as the high-performance web server. In this article we will discuss how to install Nginx CentOS 7, and how to perform the initial configuration of the program.

Install Nginx in CentOS 7

The Nginx web server is in the official repositories of the distribution, the version is already outdated, and if you want to get a new version with modern features, you will have to use the EPEL repository. To add to the system, run:

yum install epel-release

You can then install Nginx:

yum install nginx

To start Nginx, run:

systemctl start nginx

Then you must add a program in startup:

systemctl enable nginx

Next you need to allow traffic for the web server on the firewall system:

firewall-cmd --zone=public --permanent --add-service=http

firewall-cmd --zone=public --permanent --add-service=https

And restart the firewall:

firewall-cmd --reload

If everything was done correctly, the opening address of the server on which you installed Nginx, you will see the default page:

Setting the location of the website files

Install Nginx CentOS 7 completed, now will be considered by the Nginx configuration. We need to tell Nginx where is the file of our website. In the configuration file /etc/nginx.conf is already configured with the same virtual host. We are going to use. Here are the default_server option, so it will be open for all requests to Nginx:

First you need to create the actual location of the files. Create a folder /var/www/html/default, which will store our files sites, and give it to the right user nginx:

mkdir /var/www/html/default

chown nginx:nginx /var/www/html/default

You can also create a file index.html in this folder to the test web server with this text:

vi /var/www/html/default/index.html

the <h2>It words!</h2>

This file must also be owned by the user Nginx. Later in the configuration file /etc/nginx/nginx.conf find the section server and replace the value of the root parameter in /var/www/html/default:

vi /etc/nginx/nginx.conf

Now Nginx will take the website files from this directory for all the requests. Restart Nginx:

systemctl restart nginx

Then open again the server address in the browser to see whether our location files. If everything was done correctly, you will see the message it works:

If an error occurs, make sure that Nginx has permission to access this file, and also that SELinux is disabled or configured correctly.

Configuring PHP-FPM, Nginx, CentOS

If you need Nginx, most likely, you will need to configure it to work with the PHP interpreter. Next will be parsed to configure php-fpm with Nginx CentOS 7. Read more about installing PHP in CentOS 7 in a separate article, and in this article we will work with the version available in the official repositories. For Nginx we need to install the package php-fpm:

yum install php-fpm

Then run service php-fpm with the following command:

systemctl start php-fpm

Open the configuration file of php-fpm is located at /etc/php-fpm.d/www.conf and see which port waits for connections service running. By default, this is 9000:

Next we need to link Nginx with a new service. To do this in the server section add this code:

vi /etc/nginx/nginx.conf

location ~ .php$ {
try_files $uri =404;
location 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
set by the fastcgi_buffer_size 128k;
fastcgi_buffers directives 256 16k;
fastcgi_busy_buffers_size 256k;
fastcgi_temp_file_write_size 256k;
include fastcgi_params;
}

It is very important the value of this parameter:

location 127.0.0.1:9000

It must be that which we have seen in the file /etc/php-fpm.d/www.conf. This is the address and port on which it listens for connections service php-fpm. All the rest you can leave as is and to modify if necessary. Then restart Nginx:

sudo systemctl reload nginx

The reload option allows you to re-read the configuration without restarting the web server. Left to create a test file with this content:

vi /var/www/html/default/phpinfo.php

<?php phpinfo ?>

Then open the address of the server on which you installed Nginx plus phpinfo.php in the browser:

Now you will see that php-fpm with nginx is fully configured and works correctly.

Please note that the location for php is a regular expression and if you have a more General regular expression, this section should be in front of them, because Nginx will not check all regular expressions and look for the most suitable, and choose the first one that matches.

Insights

In this article, we talked about how to install Nginx CentOS 7, and how to setup your first web site and to connect the processing script with the interpreter php-fpm. The following articles will deal with setting up virtual hosts for Nginx and SSL certificates.

Source: losst.ru

(Visited 42 times, 1 visits today)