Install Nginx on CentOS 7 Server - Step by Step Process ?

Nginx is one of the most popular web servers in the world and is responsible for hosting some of the largest and most popular sites on the internet. It is more resource-friendly than Apache in most cases and can be used as a web server or reverse proxy.

Here at LinuxAPT, as part of our Server Management Services, we regularly help our Customers to perform related Nginx queries.

In this context, we shall look into how to install nginx on CentOS 7.


How to Install Nginx on CentOS ?

Before proceeding with this Installation procedure, ensure that the following requirements are met:

  • Ensure that you use a a non-root sudo enabled user account.
  • Make sure Apache or any other services are not running on port 80 or 443.


Start by updating system software packages to the latest version by type:

$ sudo yum -y update

Nginx packages are available in the EPEL repositories so we will install EPEL repository by below command:

$ sudo yum install epel-release

Now, Install Nginx by typing the following yum command:

$ sudo yum install nginx

If you are installing a package first time from the EPEL repository, it may prompt you to import the EPEL GPG key:

Retrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7
Importing GPG key 0x352C64E5:
 Userid     : "Fedora EPEL (7) <epel@fedoraproject.org>"
 Fingerprint: 91e9 7d7c 4a5e 96f1 7f3e 888f 6a2f aea2 352c 64e5
 Package    : epel-release-7-11.noarch (@extras)
 From       : /etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7
Is this ok [y/N]:

Press Y and hit Enter key to go ahead.

After that, we will start and enable nginx service by following commands:

$ sudo systemctl enable nginx
$ sudo systemctl start nginx

You can check status of nginx service by typing:

$ sudo systemctl status nginx

It will show output as given below:

● nginx.service - The nginx HTTP and reverse proxy server
    Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
    Active: active (running) since Sat 2019-04-13 20:28:32 IST; 47s ago
   Process: 17772 ExecStart=/usr/sbin/nginx (code=exited, status=0/SUCCESS)
   Process: 17766 ExecStartPre=/usr/sbin/nginx -t (code=exited, status=0/SUCCESS)
   Process: 17761 ExecStartPre=/usr/bin/rm -f /run/nginx.pid (code=exited, status=0/SUCCESS)
  Main PID: 17774 (nginx)
     Tasks: 2
    CGroup: /system.slice/nginx.service
            ├─17774 nginx: master process /usr/sbin/nginx
            └─17775 nginx: worker process


How to configure Firewall for Nginx ?

If your server is protected by firewall then you need to open both HTTP(80) and HTTPS(443) ports. 

Execute the following commands to open both HTTP(80) and HTTPS(443) ports:

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

You can verify your Nginx installation by visiting below URL in your browser:

http://YOUR_SERVER_IP_ADDRESS

It will show you default Nginx welcome page.


How to Manage Nginx Service ?

Finally, you installed Nginx web server on your machine. We will see few basic commands to manage Nginx service.

To stop Nginx service execute below command:

$ sudo systemctl stop nginx

You can start it again by typing:

$ sudo systemctl start nginx

Restart (stop and start) Nginx service the Apache service by:

$ sudo systemctl restart nginx

If you made changes to configuration file and want to reload nginx service then you can do it by typing:

$ sudo systemctl reload nginx

For disable Nginx to auto start after boot execute below command:

$ sudo systemctl disable nginx

Again to do re-enable type:

$ sudo systemctl enable nginx


Nginx Configuration Files Structure

  • All configuration files are located in the /etc/nginx/ directory.
  • Nginx main configuration file is at /etc/nginx/nginx.conf.
  • It's best practice to create a separate configuration file of each domain for better maintainability.
  • New server blocks (configuration file) of each domain should be stored in /etc/nginx/conf.d.
  • The default server web root directory is /usr/share/nginx/html.
  • It's best practice to to follow standard naming convention. Nginx server block files name should as domain name and must end with .conf extension. For example, your domain name is example.com then server block file name should example.com.conf.
  • Nginx log files (access.log and error.log) are located in the /var/log/nginx/ directory. It's also recommended to have a different access and error log files for each server block.


[Need urgent assistance in fixing Nginx configuration errors? We can help you. ]

This article covers how to Install and configure Nginx on your CentOS 7 server. Now you can deploy your applications and use Nginx as a web or proxy server. 

Nginx is a high performance web server software. It is a much more flexible and lightweight program than Apache HTTP Server.


To Install Nginx Web Server on CentOS 8:

1. Install the nginx package with:

$ sudo dnf install nginx

When prompted, enter y to confirm that you want to install nginx. After that, dnf will install Nginx and any required dependencies to your server.

2. After the installation is finished, run the following commands to enable and start the server:

$ sudo systemctl enable nginx
$ sudo systemctl start nginx

This will make Nginx start at system boot.


To configure Firewall Rules on CentOS for Nginx:

1. Run the following command to permanently enable HTTP connections on port 80:

$ sudo firewall-cmd --permanent --add-service=http

2. To verify that the http firewall service was added correctly, you can run:

$ sudo firewall-cmd --permanent --list-all

3. To apply the changes, you'll need to reload the firewall service:

$ sudo firewall-cmd --reload

Related Posts