Search Knowledge Base by Keyword

Installing And Configuring NGINX On CentOS 7

What is NGINX?

NGINX is a high performance open source web server software, designed to also facilitate reverse proxying, load balancing, caching and streaming. NGINX is much more flexible and stable than Apache web server Server.

In this article, we will be guiding you on how to install and configure NGINX on your CentOS 7 Server.


1. CentOS 7 Server

2. Root privileges

First of all we need log in to your CentOS 7 console or command line with root privilege (sudo su – ), then make sure that your yum feature is working and the system is up to date and has completed any previous installs.

Here are some of the commands we will be using to update the yum repositories.

yum -y update
yum clean all

Adding NGINX Repository

Now we can start to install the NGINX Repository by using the below command:

yum -y install epel-release

Installing NGINX

Then we can now install the NGINX since we have the NGINX Repository:

yum -y install nginx

Once this is executed, it will install all the NGINX components on the system.

Starting NGINX Service

Next we will be starting the NGINX, as it will not run automatically. Just type in:

systemctl start nginx

If you are running a firewall on your server, you need to allow HTTP and HTTPS traffic.

These commands will do just that:

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

Once done, you may check NGINX right after the firewall has restarted to verify if it accessible on your browsers.

You may use your server’s domain name or IP address.

e.g: “” OR “yourIPaddress”.

You will then see the NGINX default page loaded. This screen is only used for testing purposes and should look like this.

"welcome to nginx" screen

Congratulations! You now have your NGINX installed and working!

Now, we need to enable the NGINX so it will start every time the server is booted up. For you to do that, you need to execute the command below:

systemctl enable nginx

To check if the service is enabled you may also check that using the command below:

systemctl status nginx

This should show the results below.

Getting your server’s public IP address

To get your server’s public IP address, you need to find the network interfaces on your machine. You may use the below command:

ip addr

You will be seeing a number of interfaces depending on the hardware on your server, but what you need is as shown below. In this case, it is eno2 that is connected to the network and has internet access.

Note: the mac address on this screenshot has been removed for security purposes.

To show the IP address of the network interface that has the public IP address, you will need to use the command below:

ip addr show eno2 | grep inet | awk '{ print $2; }' | sed 's/\/.*$//'

Your Server Root and Configuration

Now, to server your own pages and application through NGINX, you will need to know where your NGINX configurations and the default server root directories are.

Default Server Root

This should be located in “/usr/share/nginx/html” where the default files below are stored. The files that are stored within this directory will be served on your web server.

  • 404.html
  • 50x.html
  • index.html

This location is also specified in the default server block configuration file that is included in NGINX and is located in “/etc/nginx/conf.d/”.

Server Block Configuration

For the server blocks, all the added server blocks, also known as Virtual Hosts in Apache Web Server, can be created in “/etc/nginx/conf.d/”. These server blocks are loaded automatically when NGINX is started.

Global Configuration

This main NGINX configuration file is stored in “/etc/nginx/nginx.conf”. It is the .conf file that you can update for the settings, like the user that runs nginx processes and number of worker processes that will spawn when nginx is running.

NGINX Server Blocks

Server blocks allow you to host multiple domains on a single server with NGINX. This feature is the same as Virtual Hosts on Apache Web Server. Server blocks can be added by creating new configuration files with a .conf file extensions. These configuration files are stored in the “/etc/nginx/conf.d/ and are loaded everytime NGINX starts.

If you are going to host multiple websites on a single server, it is recommended to follow the standard naming conventions. In this article, we are using the cPanel standards and creating a new directory.

mkdir -p /var/www/

Once you have created a directory for your new website, the next thing to do is to create an index page that we will use to test the configuration. You may use any text editor like “nano” or “vi” for this.

nano /var/www/

Just add a single text line for testing purposes.

After you have inserted this line, you may save and close the .html file.

Finally, you need to set the folder permissions for the data to be accessible online.

chmod 755 /var/www/

You can now test this on your browser.

Configuring NGINX Server Blocks

First step is to create the directories where the server blocks will be stored.

mkdir /etc/nginx/sites-available
mkdir /etc/nginx/sites-enabled

Setting up this directory tree allows you to easily configure the system when more websites are added.

Then we configure NGINX to look at the directories you have made for the server blocks.

Open the nginx.conf and add the lines at the end of http {} block, making sure to save once done.

nano /etc/nginx//nginx.conf
include /etc/nginx/sites-enabled/*.conf;
server_names_hash_bucket_size 64;

Now, create a new file specifically for the server block, for the “” website:

nano /etc/nginx/sites-available/

You are now going to paste the new NGINX Server Block on this file:

server {
listen 80;

location / {
root /var/www/;
index index.html index.htm;
try_files $uri $uri/ =404;

error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;

Then save and close the file once done. After this, you will need to create a symbolic link between the sites-available and sites-enabled as shown below:

ln -s /etc/nginx/sites-available/ /etc/nginx/sites-enabled/

Once completed, you may now restart your NGINX server:

systemctl restart nginx

Congratulations! You may now access your new website!

Was this article helpful?
0 out Of 5 Stars
5 Stars 0%
4 Stars 0%
3 Stars 0%
2 Stars 0%
1 Stars 0%
How can we improve this article?
Please submit the reason for your vote so that we can improve the article.