How to install and configure Nginx server on CentOS 7

In this article, you are going to learn How to install and configure Nginx server on CentOS 7.

Nginx is the open-source and most popular HTTP web server. And It also works as a standard mail server, caching, media streaming, reverse proxy and load balancer for HTTP, TCP, and UDP servers. Nginx can also work as a proxy server for emails like IMAP, POP3, and SMTP.

In comparison with Apache, Nginx consumes fewer resources and improves server responsiveness.

Ngnix was first released publicly in 2004. This software was created by Igor Sysoev. In 2011, Ngnix started a paid version of Nginx with the name Nginx Plus. And the company name was Nginx.

But, in May 2019, F5 Inc. acquired Nginx.

Nginx is pronounced as “Engine-x”. You may like to read about more features and comparisons between Nginx and Nginx plus then you visit Features of Nginx.

It has a 32.3% market share in the year 2020. It provides many powerful features, some of them mentioned below,

Nginx Features

There are many features available in Nginx. But I have mentioned some of them below.

  • Webserver
  • Reverse proxy
  • Reverse proxy with caching
  • Load balancing
  • Web sockets
  • Media streaming
  • IPv6
  • Fast CGI support with caching
  • Handling of the static file, index file, and auto-indexing
  • TLS and SSL with SNI

and many more.

Alternative of Nginx

  • Apache
  • Apache Tomcat
  • Node.js
  • Lighttpd
  • Cherokee
  • Microsoft IIS
  • Appweb
  • Hiawatha

Preconditions for Nginx webserver

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

Step 1: Updating the CentOS 7 repository

In this step, we are going to update the system repository list. This is a good practice to keep the system up to date. We are using the below command for this.

sudo yum update

It will ask for your confirmation, press to accept the confirmation and press the enter key. This command will update your system with the latest version of the software packages.

Step 2: Installation of EPEL repository

Nginx is not available in the CentOS default repository. 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 yum install epel-release

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

Step 3: Install Nginx Server

As we have already installed the EPEL repository. Now, is the time to install Nginx. To install it, use the below command.

sudo yum install nginx

It will list all the Nginx and its dependencies. Press to confirm the installation and hit enter key to complete this. So that it will install it.

Step 4: Starting and enabling the Nginx 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

Note: If you have already installed the Apache webserver then stop the service of Apache first. You can use the below command for that

sudo systemctl stop httpd

Step 5: Checking the Nginx service status

It is good to check the service status. There is a simple command to check the service status of Nginx. And, I have mentioned the command below.

sudo systemctl status nginx
install and configure nginx server
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

So, to check if it is enabled on the startup

sudo systemctl is-enabled nginx

But, if you want to disable the Nginx service from startup then you can use the below command.

sudo systemctl disable nginx

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

In CentOS 7, firewalld is the default firewall to stop the incoming and outgoing traffic. So we have to allow service like http, https, or ports 80, and 443. For instance, let’s use the below commands.

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

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

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

Step 8: Checking the Nginx server

You can check your Nginx server using domain_name or using the IP address in the browser, like below,

http://domain_name_or_IP

For example, In my case the IP address for my server is http://192.168.43.10

You shall get the output like in the below picture

Nginx server welcome page.
Nginx server welcome page.

Step 9: Nginx configuration files and root

You must be familiar with Nginx configuration files and their locations.

Additional Server Blocks

When you want to run more than one website on one Nginx server, then this task will accomplish using additional server blocks.

And You can create additional server blocks by creating a new configuration file with a .conf file extension. But you have to put this file in the /etc/nginx/conf.d directory.

Default Nginx server root

The default directory of the Nginx server root is /usr/share/nginx. This information has been specified in the default server block configuration file, which is located at /etc/nginx/conf.d/default.conf.

The default server document root directory which contains web files is located /usr/share/nginx/html

Global configuration

Global configurations can be achieved by modifying the main Nginx configuration file and you can find this file in the directory /etc/nginx/nginx.conf.  This configuration file is broken down into different settings. So, you can find three settings in the below form.

  1. Events, this is the global settings that define how Nginx handles the connections in general.
  2. HTTP is the setting that defines how the server handles http and https connections.
  3. The server is defined within the HTTP setting. It specifies server ports, document root, etc.

If you want to add any other settings so you can add that.

Configuring a new directory

If you are hosting multiple websites so this is a good thing to follow the standard naming rules because it will help you to manage the websites and their settings easily.

So, you can do one thing that adds your domain name while creating a new public_html directory.

For example, I have a domain example.com so the directory will look like the below format.

sudo mkdir -p /var/www/example.com/public_html

So, You can change the red content as per your domain name.

In this step, we can create an html file to test this setting. For instance, if it is working or not. Indeed, you can replace the red highlighted name with your domain name.

sudo echo "This is the test of the newly created directory and it is an html page" > /var/www/example.com/public_html/index.html

Changing the permissions of the newly created directory

We have already created the directory. And, we have also created the index.html file. But, we did not change the permission. So, now let’s change the directory permissions.

sudo chmod 755 /var/www/example.com/public_html

Special note: If you are getting a problem, disabling the selinux fixes some problems. You can check the SELinux status using the command.

sestatus

So, you can find the setting of SELinux in the file /etc/sysconfig/selinux and you can edit this file using any editors like vi or vim.

vim  /etc/sysconfig/selinux

Another key point, changing the value from SELINUX=enforcing to SELINUX=disable. However, you have to save this file by pressing the Esc key and typing :wq and then hitting the enter key. Above all, reboot the computer, so selinux will be stopped. As a result, you can use this without any issues. But the SELInux will be disabled. Also, you can read about How to allow non-standard ports is SELinux?

Conclusion

Indeed, you learn about How to install and configure an Nginx server on CentOS 7. 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.

Also, You can visit the NGINX web page for information Nginx home page

install and configure nginx server | install and configure nginx server on CentOS | install and configure nginx server on CentOS 7 | install and configure nginx server | install and configure nginx server | install and configure nginx server | install and configure nginx server | install and configure nginx server

Leave a Reply

Your email address will not be published.