Knowledgebase: Support
Installing And Configuring NGINX On CentOS 7
Posted by Daniel Naval on 11 May 2019 06:26 PM

What is NGINX?

It is a high performance open source web server software. It was designed as a web server, reverse proxying, load balancing, caching and streaming. NGINX is much more flexible and stable than Apache HTTP Server. For more informations on NGINX, you may follow this LINK.

 

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

 

Requirements:
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 – ) and 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.

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 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. You may run the commands below:

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


 

Once done, you may check the 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.

http://your_server_domain_name_OR_ip_address/

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

Congratulations! You now have your NGINX installed and working! Now, we need to enable the NGINX so it will start everytime 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 you 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 directory.

 

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 serverd on your web server.
404.html
50x.html
index.html
nginx-logo.png
poweredby.png

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/” an ends with .conf. 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 allows you to host multiple domains on a single server with NGINX. This feature is the same with 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. On this article, we are using the cPanel’s standards and create a new directory.

mkdir -p /var/www/yourdomain.com/public_html

 

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 “nano” or “vi” for this one.

nano /var/www/yourdomain.com/public_html/index.html

Just add single text line for testing purposes.

After you have inserted this lines, you may save and close the .html file.
You need to set the folder permissions for the data to be accessible online.

chmod 755 /var/www/yourdomain.com/public_html

 

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 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 the lines at the end of http {} block. You can use “nano” or “vi” for this and 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 “yourdomain.com” website:

nano /etc/nginx/sites-available/yourdomain.com.conf

 

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

server {
listen 80;
server_name yourdomain.com www.yourdomain.com;

location / {
root /var/www/yourdomain.com/public_html;
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/yourdomain.com.conf /etc/nginx/sites-enabled/yourdomain.com.conf

 

Once completed, you may now restart your NGINX server:

systemctl restart nginx

 

Congratulations! You may now be able to access your new website!