11.12.2024

How to Install and Configure Nginx on CentOS 7

Nginx is a very popular web server due to its performance and ease of use. In this tutorial, we will walk you through the installation and basic configuration of Nginx on CentOS 7.

In the Serverspace you can create a server with already installed app "Nginx".

Installing Nginx

If you use a non-root user, then you must have sudo rights and to add to all the commands sudo in front of them.

To install Nginx, add the EPEL repository.

yum install epel-release -y

Now we will install Nginx, as well as the nano text editor for convenience.

yum install nginx nano -y

By default, Nginx is configured to work with both IPv4 and IPv6 addresses. If the last one is not configured on the server, as in my case, then the web server simply will not start. Therefore, we immediately go to the config:

ano /etc/nginx/nginx.conf

Find the next line and comment on it, that is, put a # sign at its beginning:

listen [::]:80 default_server;

Result:

# listen [::]:80 default_server;

Let's save the file and check the config for errors:

nginx -t

Output:

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

If your output looks different, then it says what errors need to be corrected in the config.

Now let's start Nginx and add it to autorun.

systemctl enable --now nginx

If you are using Firewall, you need to add an allowing rule for the web server. Below is an example for Firewalld, enter the commands in sequence:

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

Configuring the location of site files

By default, nginx displays an information page on the site. Let's set up a different location for the site files and add our own page. First, let's create the required directories.

mkdir -p /var/www/default

Now let's create the page itself in a new folder:

nano /var/www/default/index.html

And add content to it, which can be anything. For example:

<h1>My first web page</h1>

Let's set the owner of the directory and the file in it to the nginx user:

chown -R nginx:nginx /var/www/default/

We will use the virtual host already in the nginx configuration and replace the configuration for the location of the site files. To do this, open the nginx configuration:

nano /etc/nginx/nginx.conf

You need to find the server section, which starts with listen 80 default_server;, and in it the root parameter, which sets the path to the site files. Let's replace it with the path /var/www/default/ we just created:

    server {
        listen       80 default_server;
#        listen       [::]:80 default_server;
        server_name  _;
        root         /var/www/default/;
...

Save the file and restart nginx.

systemctl restart nginx

Now, when accessing the server address from the browser window, you will see the contents of the newly created page.

PHP processing setup

To work with PHP, install php-fpm:

yum install php-fpm

Now let's run it and add it to startup:

systemctl enable --now php-fpm

Add to the nginx configuration at the end of the same server section as before, settings for working with php-fpm:

nano /etc/nginx/nginx.conf
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass 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;
fastcgi_buffer_size 128k;
fastcgi_buffers 256 16k;
fastcgi_busy_buffers_size 256k;
fastcgi_temp_file_write_size 256k;
include fastcgi_params;
}

Restart nginx:

systemctl restart nginx

Let's create a file for testing php-fpm functionality:

echo "" > /var/www/default/info.php

Let's open http://server-IP/info.php in the address bar of the browser and see information about PHP:

Screenshot 1. Checking PHP operation.

This output confirms that nginx + php-fpm is working. After testing the server operation, be sure to delete this page, because it poses a threat to server security.