NGINX, pronounced “engine-ex”, is one of the most popular web servers in the world; he is in charge of hosting some of the largest and most visited sites on the internet. It is more resource-friendly than Apache in most cases, and can be used as a web server or reverse proxy.
IT is a free, open-source, high-performance HTTP server and reverse proxy, as well as an IMAP/POP3 proxy server. NGINX is known for its high performance, stability, rich feature set, simple configuration, and low resource consumption.
The major performance differences between Apache and Nginx are in the way the cache is managed and the integration / interaction with PHP.
In this guide, you will learn how to install Nginx on a CentOS 8 server. As usual, we recommend that you do the installation of NGINX on a fresh installation of CentOS 8 with the latest version available.
Step 1 – Installation of the Nginx web server
To install Nginx, we will be using the dnf package manager, which is the new default package manager on CentOS 8.
Install the nginx package with :
sudo dnf install nginx
When prompted, enter y to confirm that you want to install nginx. Then dnf will install Nginx and all necessary dependencies on your server.
Once the installation is complete, run the following commands to activate and start the server:
sudo systemctl enable nginx
sudo systemctl start nginx
This will launch Nginx on system startup.
Step 2 – Adjusting the Firewall Rules
If you have enabled the firewalld as shown in our initial server setup guide for CentOS 8, you will need to adjust the firewall settings to allow external connections to your Nginx web server, which by default runs on port 80.
Run the following command to permanently enable HTTP connections on port 80:
sudo firewall-cmd --permanent --add-service=http
To verify that the http firewall service was added correctly, you can run:
sudo firewall-cmd --permanent --list-all
You will see output like this:
Outputpublic
target: default
icmp-block-inversion: no
interfaces:
sources:
services: cockpit dhcpv6-client http ssh
ports:
protocols:
masquerade: no
forward-ports:
source-ports:
icmp-blocks:
rich rules:
To apply the changes, you need to reload the firewall service:
sudo firewall-cmd --reload
Your Nginx server is now fully installed and ready to be viewed by external visitors.
Step 3 – Checking Your Web Server
You can now test if your Nginx web server is operational by accessing your server’s public or Local IP address.
Enter the NGINX web server IP address into your web browser and it will take you to the default Nginx home page:
If you can see this page, your web server is now correctly installed.
Step 4 – Managing the Nginx process
Now that the Nginx web server is operational, you will learn how to manage the Nginx service via systemctl.
Whenever you need to stop your web server, you can use:
sudo systemctl stop nginx
To start the web server when it is stopped, type:
sudo systemctl start nginx
To stop and then restart the service, you can use:
sudo systemctl restart nginx
Nginx can also reload configuration changes without breaking connections. To do this, type:
sudo systemctl reload nginx
By default, Nginx is configured to launch automatically when the server starts. If this is not what you want, you can turn off this behavior by typing:
sudo systemctl disable nginx
To reactivate the service and launch Nginx at startup, you can use:
sudo systemctl enable nginx
Step 5
Become familiar with important Nginx files and directories
Now that you know how to manage the Nginx service, you should take a few minutes to familiarize yourself with some important directories and files.
Content
/usr/share/nginx/html
: actual web content (which by default only consists of the default Nginx page you saw earlier), is served from the /usr/share/nginx/ html directory. This can be changed by changing the Nginx configuration files.
Server configuration
/etc/nginx
: Nginx configuration directory. All Nginx configuration files can be found here./etc/nginx/nginx.conf
: Nginx main configuration file. This can be modified to make changes to the overall configuration of Nginx./etc/nginx/conf.d/
: this directory contains the server block configuration files where you can define the websites that are hosted in Nginx. A typical approach is to put each website in a separate file that has the site’s domain name (for example, your_domain.conf). They are refered as Server Block in the official Nginx Wiki documentation.
Server logs
/var/log/nginx/access.log
: every request made to your web server is recorded in this log file, unless Nginx is configured to do otherwise./var/log/nginx/error.log
: any nginx error will be logged in this log.
Step 6 – Placing Server Blocks (Optional)
If you want to host multiple websites on the same Nginx server, you will need to set up server blocks. Nginx server blocks work similarly to Apache virtual hosts allowing a single server to respond to multiple domain names and serve different content for each of them. On CentOS 8, server blocks are defined in .conf files located in /etc/nginx/conf.d
We are going to set up a server block for a domain called your_domain.
By default, Nginx on CentOS 8 is configured to serve documents from a directory located in /usr/share/nginx/html. While this works well for a single site, it can get unmanageable if you host multiple sites. Instead of modifying /usr/share/nginx/html, you’ll create a directory structure in /var/www for the your_domain website, leaving /usr/share/nginx/ html in place as the default directory to serve if a customer request does not correspond to any other site.
Create the directory for your_domain as follows using the -p flag to create any necessary parent directory:
sudo mkdir -p /var/www/your_domain/html
Next, assign ownership of the directory with the environment variable $ USER, which should refer to your current system user:
sudo chown -R $USER:$USER /var/www/your_domain/html
Next, you’ll create an example index.html page to test the server block configuration. The default text editor supplied with CentOS 8 is vi. vi, an extremely powerful text editor, but it can be difficult to use for inexperienced users. You can install a more suitable editor, such as nano, to make it easier to edit configuration files on your CentOS 8 server:
sudo dnf install nano
You can now use nano to create the sample index.html file:
nano /var/www/your_domain/html/index.html
Inside this file, add the following HTML code: /var/www/your_domain/html/index.html
<html>
<head>
<title>Welcome to your_domain</title>
</head>
<body>
<h1>Success! Your Nginx server is successfully configured for <em>your_domain</em>. </h1>
<p>This is a sample page.</p>
</body>
</html>
Save and close the file when you are finished. If you used nano, you can do this by pressing CTRL + X, Y, then ENTER.
In order for Nginx to serve this content, you need to create a server block with the correct directives that point to our custom web root. You are going to create a new server block at the address /etc/nginx/conf.d/your_domain.conf:
sudo nano /etc/nginx/conf.d/your_domain.conf
Paste the following config block: /etc/nginx/conf.d/your_domain.conf
server {
listen 80;
listen [::]:80;
root /var/www/your_domain/html;
index index.html index.htm;
server_name your_domain www.your_domain;
location / {
try_files $uri $uri/ =404;
}
}
Save and close the file when you are done updating its contents.
To make sure there are no syntax errors in your Nginx files, run:
sudo nginx -t
If there is no problem, you will see the following output:
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
Once your configuration test is successful, restart Nginx to activate your changes:
sudo systemctl restart nginx
Tips:
There is another faster way to perform the previous two steps. The following command tells Nginx to check the config files for errors and load the new config if there are no errors in the configurations.
sudo nginx -s
Before you can test the changes from your browser, you will need to update your server’s SELinux security contexts so that Nginx is allowed to serve the contents of the /var/www/ your_domain directory.
The following command will allow the root of your custom document to be served as HTTP content:
chcon -vR system_u:object_r:httpd_sys_content_t:s0 /var/www/your_domain/
You can now test your custom domain setup by navigating to http: // your_domain, where you will see something like this:
This page renders the HTML code that you defined in the root of the custom document created for the server block. If you are able to see this page, it means that your Nginx server is correctly configured to serve your domain.
Conclusion
In this Hands-On, you have learn how to install and configure Nginx, which is both a powerful web server and a reverse proxy. You took a look at how to manage the Nginx service running on your server, and what are the main directories that Nginx uses to store configuration files, content, and logs.
Be the first to comment