How to install nginx webserver on CentOS 8

In this article, you will learn how to install Nginx on CentOS 8.

Nginx is an open-source software package. It is the most popular web servers now in the world. And, there are many important and popular websites on the internet that are hosted using Nginx. It works as a reverse proxy, load balancer, and many other features.

You can read the article How to install and configure Nginx server on CentOS 7 to get more details about Nginx here Installing Nginx open-source.

Preconditions for Nginx webserver

  • A VPS or a local server on which CentOS 8 is running.
  • You must log-in with a root user or a user with sudo privilege to make the changes.
  • Internet connection

Step 1: Installation of EPEL repository

The latest version of Nginx is not available in the CentOS default repository. In this case, you have to install the EPEL repository to get the latest version of Nginx. But, if you want to install the default repository version then you can skip this step and follow step 3.

So, you have to install the EPEL (Extra Package for Enterprise Linux) repository on your server. It is a free repository and allows you to install many other open-source software packages. Use the below command to install EPEL.

sudo dnf install epel-release

Press y to accept the installation confirmation and press the enter key.

After completing the EPEL installation, run the below command to view the available Nginx versions.

 dnf module list nginx

For instance, You can see in the below picture, which is listing the module.

install nginx
Checking the Nginx versions

Step 2: Enabling the latest Nginx web server module

After that, you have to enable the latest Nginx module. In other words, you have to tell the dnf command to install the latest version. So, use the below command for this purpose.

For instance, The available version of Nginx right now is in EPEL repository 1.8.

sudo dnf module enable nginx:1.18

For instance, You can see in the below picture,

Enabling the module for latest nginx version
Enabling the module for the latest Nginx version

Troubleshooting for module enable error,

In case, you are getting an error while enabling the module like the below then you can use the command to reset the module.

The operation would result in the switching of module ‘nginx’ stream ‘1.14’ to stream ‘1.18’

Error: It is not possible to switch enabled streams of a module.

It is recommended to remove all installed content from the module and reset the module using the dnf module reset <module_name> command. After you reset the module, you can install the other stream.

Therefore, you can use the below command to reset the module for Nginx

sudo dnf module reset nginx

After that, run the enable command again.

Step 3: Install Nginx webserver

So, we have already installed the EPEL repository. As a result, we can install it directly using the dnf command. Use the below command to install the latest version of Nginx.

sudo dnf install nginx

It will list all the Nginx software packages and their dependencies. Press y to confirm the installation and hit enter key to complete this.

Step 4: Starting and enabling the Nginx service daemon

We have completed the installation process. So, it is time to start the Nginx service and enable it to automatically start on system boot.

Therefore, use the below command which will start the service immediately, and also it will enable the service on boot.

sudo systemctl enable --now nginx

Step 5: Checking the Nginx service status

Just to make sure the service is running or not, you want to check the service status. So, use the below command for that.

sudo systemctl status nginx
nginx service status
Checking the nginx service status.

Step 6: Managing the Nginx services

There are some commands which are useful to manage the services. You can find them below.

To reload the service

sudo systemctl reload nginx

Also, to restart the service

sudo systemctl restart nginx

Similarly, if you want to start the service

sudo systemctl start nginx

To check if it is enabled on the startup

sudo systemctl is-enabled nginx

After that, if you want to disable the Nginx service from the startup

sudo systemctl disable nginx

Step 7: Configuration of the firewall to allow the Nginx traffic

Firewalld is the service daemon name that helps to protect from incoming and outgoing internet traffic in CentOS 8. But, we have to allow external connections. So, the Nginx server can talk externally.

You can run the below command to add permanently http and https service to the firewall.

sudo firewall-cmd --permanent --add-service http
sudo firewall-cmd --permanent --add-service https

After allowing the service, we need to reload the firewall service daemon.

sudo firewall-cmd --reload
nginx service allow in firewall
Services allowed for Nginx in firewall

Step 8: Checking the Nginx webserver

Now, you can test your Nginx web server that if it is up and running or not by accessing the public IP of your server or domain name from your web browser.


For Instance,  the IP address of the server is So, the link will look like

You shall get the output like in the below picture

Nginx server welcome page.
Nginx server welcome page.

Step 9: Important Nginx files and directories

Also, you must be familiar with Nginx important files and directories.


  • This is the default location to store the Nginx welcome page. This is located at in directory /usr/share/nginx/html. You can change it after modifying the Nginx configuration file.

Server Configuration

  • The Nginx configuration directory is located at /etc/nginx. All of the Nginx configuration files reside here.
  • The main Nginx configuration file is located at /etc/nginx/nginx.conf. You can modify it to make changes to the Nginx global configuration.
  • This is the directory /etc/nginx/conf.d/ which contains server block configuration files, here you can define the websites that are hosted within Nginx. This is the good convention to have the configuration file in a separate file which can be based on your domain name. For instance, your_domain.conf.

Server Logs

  • All the requests come to your server are listed in /var/log/nginx/access.log. But, you can change this location in the configuration file.
  • After that, all the errors are recorded in /var/log/nginx/error.log.

Step 10: Setting Up Server Blocks

This is an optional step, if you want to host multiple websites on this Nginx webserver, then you can follow these steps. You have to set up server blocks. It works similarly to the Apache virtual host. You can set the server to respond to multiple domain names and provide different content for each of them.

Server blocks are defined by the .conf file which is located in /etc/nginx/conf.d.

For instance, we are going to set up a server block for the domain

By default, the document files located at /usr/share/nginx/html in CentOS 8. It is good for one website. But, if you are hosting multiple websites then it is not easy to manage it. So, we are going to create a new directory structure in the directory /var/www for the domain website. However, we will leave the default directory /usr/share/nginx/html.

Creating a directory for

Firstly, we are going to create a directory for the domain, and we will use the -p argument to create any necessary parent directories.

sudo mkdir -p /var/www/

Secondly, assign ownership of the directory with the $USER environment variable. So, this will assign ownership to the current user.

sudo chown -R $USER:$USER /var/www/

Thirdly, we are going to create a sample html page. So, we can test the server block configuration. I’m using the vim editors. But, you can use any other editors as per your choice.

sudo vim /var/www/

It will open a new file, press to go into insert mode. After that, add the below lines.

<title>Welcome to</title>
<h1>Success! your have configured you Nginx server for  <em></em> </h1>
<p>This is a test page.</p>

Note for saving this file while using vim editors: Press the Esc key and type:wq to save this file.

Creating a new server block

Now we have to create a server block that will help Nginx to serve this content. So, it will work. We are going to create a new server block at /etc/nginx/conf.d/

sudo vim /etc/nginx/conf.d/

Add the below lines and change the with your domain name.

server {
listen 80;
listen [::]:80;

root /var/www/;
index index.html index.htm index.nginx-debian.html;


location / {
try_files $uri $uri/ =404;

Note for saving this file while using vim editors: Press the Esc key and type:wq to save this file.

Above all, to make sure there is no syntax error, use the below command.

sudo nginx -t

If you did not get any error, restart the nginx service

sudo systemctl restart nginx

Changing the SELinux Context

Further, we have to update the SELinux security context. So, Nginx will be allowed to serve the content from the /var/www/ directory.

chcon -vR system_u:object_r:httpd_sys_content_t:s0 /var/www/

Now you can test your custom domain setup by navigating to As a result, you can get the below output. If you are getting the below output, therefore, it is working fine. In other words, all the configuration is good.

nginx server block test


Indeed, you learn about How to install and configure an Nginx server on CentOS 8. In conclusion, you can install an Nginx server now. Therefore, Keep practicing. So, I hope, you understand but if you have any questions, you can ask in the comment section.

You can read about How to setup NFS server on CentOS/RHEL 7

How to install Nginx? | How to install Nginx | How to install Nginx on CentOS | How to install Nginx on CentOS 8 | How to install Nginx On RHEL | How to install Nginx RHEL 8

Leave a Reply

Your email address will not be published.