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.
Now we will install Nginx, as well as the nano text editor for convenience.
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:
Find the next line and comment on it, that is, put a # sign at its beginning:
Result:
Let's save the file and check the config for errors:
Output:
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.
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=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.
Now let's create the page itself in a new folder:
And add content to it, which can be anything. For example:
Let's set the owner of the directory and the file in it to the nginx user:
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:
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.
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:
Now let's run it and add it to startup:
Add to the nginx configuration at the end of the same server section as before, settings for working with php-fpm:
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:
Let's create a file for testing php-fpm functionality:
Let's open http://server-IP/info.php in the address bar of the browser and see information about PHP: