What is NGINX?

NGINX (pronounced ‘engine X’) is an alternative to Apache and is considered to be one of the most popular HTTP web servers in the world. As opposed to Apache, NGINX tends to consume fewer resources and is ‘faster.’ Its event-driven design is what makes it resource-friendly.

Above all, it allows admins to set up advanced configurations and can deal with a high load of concurrent connections.

Besides being a good fit for an HTTP web server, NGINX also works as a reverse proxy, load balancer, and standard mail server.

In this article, you will learn how to install and configure NGINX on CentOS 7.

how to install nginx on centos7

Apache or NGINX?

Consider using NGINX if you are running a single website with high traffic. Most benchmarks point that NGINX is faster than Apache. Skillful admins can set up very complex configurations while maintaining high-performance thanks to NGINX’s lightweight design.

Also, it is a good fit for working with newer web development platforms.

However, admins need to be aware that NGINX demands advanced technical skills and working with complex configurations. It is highly customizable, but there is less documentation on NGINX, and it isn’t an out-of-the-box solution. Additionally, there are fewer modules and add-ons available for Nginx.

Both options are considered as one of the best in the business. We have provided the general pointers, but your decision should entirely rely on your requirements and how you plan on using a web server.

Apache comes pre-installed with most Linux distributions. It requires less technical knowledge to run a website on Apache, as long as the configuration is simple. If you are running multiple sites with different configurations, Apache is most definitely your best choice. Furthermore, Apache is a necessity if you need access to numerous modules and add-ons.

WordPress, Drupal, and other traditional MySQL/PHP applications work great on an Apache server.


  • A CentOS self-managed server or VM
  • A Domain. You will need to connect a domain to your NGINX web server
  • A user with root privileges [root@user ~]
  • SELinux set up properly

How to Install Nginx on CentOS 7

Generally, CentOS is the preferred distribution package in the hosting industry. Currently, CentOS 7 is the latest version, while CentOS 6 is supported until the year 2020.

The configuration procedure below details the steps necessary to set up NGINX on CentOS 7.

Update System Repositories to the Latest Centos Version

To do so, run the following command:

yum -y update

-y is an optional parameter. It serves to avoid confirmation questions. We will be using it a lot, as you will notice. If you do not use this parameter, you will be prompted to confirm your commands.

Install Extra Packages for Enterprise Linux (EPEL)

NGINX is not available in the standard repositories that come with the CentOS package, so you will need to install the EPEL repository on your server. This will ensure that you always have the latest version of NGINX. EPEL is free to use and provides numerous open-source packages to install with Yum.

To install EPEL, run the following command using the YUM package manager:

yum install – y epel-release

screenshot of installing NGINX on centOS 7

Install NGINX

Step number two has added the NGINX repository to your server. Now, you can install NGINX by running the following yum command:

# yum –y install nginx

Disable Apache and Start NGINX

Your instance of NGINX is installed, but it won’t start automatically. If you already have an Apache server running, you will need to disable it before starting NGINX.

service httpd stop

Bear in mind that disabling Apache will bring down any currently hosted website.

You disabled Apache, but it still might start automatically during server reboot. Disable automatic starts by running the following command:

systemctl disable httpd

To start NGINX, run:

systemctl start nginx

After you hit enter, you will not get a return value. However, NGIX should have started. To check its status, run:

systemctl status nginx

There should be a green piece of text that reads ‘active (running).’ If you can’t find it, your instance of NGINX might not have started successfully.

NGINX – Automatic Start Upon Boot

Most admins will want to make sure that NGINX starts up automatically whenever the server restarts. To set this up, run:

systemctl enable nginx

Set up Your Firewall to Allow Traffic

CentOS 7 enables firewalls by default and blocks access to ports 80 and 443. It will block any inbound HTTPS and HTTP packets from NGINX.

To allow HTTP and HTTPS traffic, run the following commands:

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

Verify the Install

The easiest way to check whether NGINX is behaving properly is by visiting your server’s public IP address. Just open your web browser and visit http://server_IP_or_domain_name/

First, find your server’s public IP address.

dig +short your domain @

Copy the IP address and paste it into your browser. This should load the Welcome to NGINX page.

welcome to nginx page

Test Failed Error Message

If you receive a ‘test failed’ error message for nginx.conf file, you might be facing an IP address issue. The NGINX service listens IPv4 and IPv6, by default. Your test will fail if your server doesn’t support IPv6. However, this can be fixed by modifying the main configuration file.

Open the main configuration file, /etc/nginx/nginx.conf. Find and comment out the following line:

listen [::]:80 default_server;

Just add ‘#’ at the beginning of the line. It should look like this:

# listen [::]:80 default_server;

Save the changes you have made and reload the NGINX service.

systemctl reload nginx

Browse to your server’s IP address in your browser. You should see the default NGINX test page.

NGINX Configuration Files and Root

You will most definitely need to know the location of NGINX configuration files and the default NGINX server root directory.

Additional Server Blocks

On Apache, admins use virtual hosts to run multiple websites. With NGINX, running several websites on a single server is accomplished with server blocks. Additional server blocks are added by creating new configuration files with a .conf file extension. Place these files in /etc/nginx/conf.d and they will be loaded each time NGINX starts.

Default NGINX Server Root

The default NGINX server root directory is /usr/share/nginx. This is specified in the default server block configuration file, located at /etc/nginx/conf.d/default.conf.

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

Global Configuration

Global configurations can be tweaked by modifying the main NGINX configuration file located at /etc/nginx/nginx.conf. This configuration file is broken down into contexts. By default, you can identify three (3) contexts.

  1. Events are global settings that define how NGINX handles connections in general.
  2. HTTP defines how the server handles HTTP and HTTPS connections.
  3. Server is defined within the HTTP context. It specifies server ports, document root, etc.

You can always add additional contexts.

Managing NGINX – Basic Tasks


To stop an NGINX server, run:

systemctl stop nginx

Restart NGINX

To restart the NGINX service, run:

systemctl restart nginx

Reload NGINX after Modifying its Configuration

So, you made some modifications to the NGINX main configuration changes. You need to reload the server to propagate the changes.

systemctl reload nginx

Disable Automatic Start upon Boot

You don’t want NGINX to automatically start at boot, run:

systemctl disable nginx

Configuring a New Directory

If you are hosting multiple websites, a good rule of thumb is to follow standard naming conventions. Let’s use cPanel’s standard naming and create a directory.

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

Next thing you need is an index page. This will help you test the configuration.

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

For testing purposes, input a single line of text in index.html. Save and close the file.

Set folder permissions, so that data can be accessed online.

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

Try opening the index.html page. It should be available online.


After successfully installing NGINX on CentOS 7, many opt to fully install LEMP stack, an alternative to the well-known LAMP stack. LEMP is an acronym; each letter stands for a single open-source component.

  • L – Linux OS
  • E – NGINX (pronounced as you would pronounce ‘engine x’)
  • M – MySQL or MariaDB relational database management system
  • P – PHP for hosting dynamic websites

It’s called stack because each component ‘sits on top of the other.’ Your Linux OS serves as the base layer.

On top of that sits NGINX, your web daemon. The relational database management system stores all the data processed and served by your web daemon. Finally, PHP allows users to interact with that data.