|

How to install and config Nginx in CentOS 7

NGINX is a free, open-source and powerful HTTP web server and reverse proxy with an event-driven architecture.

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

It also works as a reverse proxy, standard mail and TCP/UDP proxy server, and can additionally be configured as a load balancer. It is powering many sites on the web; well known for its high-performance, stability and feature-rich set.

In this article, we will explain how to install, configure and manage Nginx HTTP web server on a CentOS 7 server using command line.

System Requirements:

  • CentOS 7.x server with minimal installation.
  • root credentials.

Install Nginx Web Server

Step 1: Let update our server and packages to latest version.

[root@newdelhihosting ~]# yum -y update

Updating the server may takes, more time. Once the update completes reboot the server

 

Step 2: Now, we go with install Nginx HTTP server from the EPEL repository using the YUM package manager

[root@newdelhihosting ~]# yum install epel-release
[root@newdelhihosting ~]# yum install nginx
=================================================================================
 Package                        Arch      Version             Repository  Size
=================================================================================
Installing:
 nginx                        x86_64    1:1.12.2-1.el7       epel        529 k
Installing for dependencies:
 fontconfig                   x86_64    2.10.95-11.el7       base        229 k
 fontpackages-filesystem      noarch    1.44-8.el7           base        9.9 k
 freetype                     x86_64    24.11-15.el7         base        392 k
 gd                           x86_64    2.0.35-26.el7        base        146 k
 gperftools-libs              x86_64    2.4-8.el7            base        272 k
 libX11                       x86_64    2.6.5-1.el7          base        606 k
 libX11-common                noarch    1.6.5-1.el7          base        164 k
 libXau                       x86_64    1.0.8-2.1.el7        base        29 k
 libXpm                       x86_64    3.5.12-1.el7         base        55 k
 libjpeg-turbo                x86_64    1.2.90-5.el7         base        134 k
 libpng                       x86_64    2:1.5.13-7.el7_2     base        213 k
 libunwind                    x86_64    2:1.2-2.el7          base        57 k
 libxcb                       x86_64    1.12-1.el7           base        211 k
 libxslt                      x86_64    1.1.28-5.el7         base        242 k
 lyx-fonts                    noarch    2.2.3-1.el7          epel        159 k
 nginx-all-modules            noarch    1:1.12.2-1.el7       epel        16 k
 nginx-filesystem             noarch    1:1.12.2-1.el7       epel        17 k
 nginx-mod-http-geoip         x86_64    1:1.12.2-1.el7       epel        23 k
 nginx-mod-http-image-filter  x86_64    1:1.12.2-1.el7       epel        26 k
 nginx-mod-http-perl          x86_64    1:1.12.2-1.el7       epel        35 k
 nginx-mod-http-xslt-filter   x86_64    1:1.12.2-1.el7       epel        25 k
 nginx-mod-mail               x86_64    1:1.12.2-1.el7       epel        53 k
 nginx-mod-stream             x86_64    1:1.12.2-1.el7       epel        76 k
Transaction Summary
============================================
Install  1 Package (+23 Dependent packages)

Total download size: 3.6 M
Installed size: 11 M
Is this ok [y/d/N]: y
Dependency Installed:
  fontconfig.x86_64 0:2.10.95-11.el7                  fontpackages-filesystem.noarch 0:1.44-8.el7
  freetype.x86_64 0:2.4.11-15.el7                     gd.x86_64 0:2.0.35-26.el7
  gperftools-libs.x86_64 0:2.4-8.el7                  libX11.x86_64 0:1.6.5-1.el7
  libX11-common.noarch 0:1.6.5-1.el7                  libXau.x86_64 0:1.0.8-2.1.el7
  libXpm.x86_64 0:3.5.12-1.el7                        libjpeg-turbo.x86_64 0:1.2.90-5.el7
  libpng.x86_64 2:1.5.13-7.el7_2                      libunwind.x86_64 2:1.2-2.el7
  libxcb.x86_64 0:1.12-1.el7                          libxslt.x86_64 0:1.1.28-5.el7
  lyx-fonts.noarch 0:2.2.3-1.el7                      nginx-all-modules.noarch 1:1.12.2-1.el7
  nginx-filesystem.noarch 1:1.12.2-1.el7              nginx-mod-http-geoip.x86_64 1:1.12.2-1.el7
  nginx-mod-http-image-filter.x86_64 1:1.12.2-1.el7   nginx-mod-http-perl.x86_64 1:1.12.2-1.el7
  nginx-mod-http-xslt-filter.x86_64 1:1.12.2-1.el7    nginx-mod-mail.x86_64 1:1.12.2-1.el7
  nginx-mod-stream.x86_64 1:1.12.2-1.el7
Complete!
[root@newdelhihosting ~]#

Now, Nginx is installed on the server.

Manage Nginx HTTP Server on CentOS 7

Step 3: Once Nginx web server installed, we can start it first time and enable it to start automatically at system boot.

[root@newdelhihosting ~]# systemctl start nginx
[root@newdelhihosting ~]# systemctl enable nginx
[root@newdelhihosting ~]# systemctl status nginx

 

[root@newdelhihosting ~]# systemctl status nginx
● 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 2018-03-16 08:28:09 EET; 10s ago
Main PID: 29730 (nginx)
CGroup: /system.slice/nginx.service
├─29730 nginx: master process /usr/sbin/nginx
├─29731 nginx: worker process
└─29732 nginx: worker process

Mar 17 08:28:09 newdelhihosting.co.in systemd[1]: Starting The nginx HTTP and reverse proxy server...
Mar 17 08:28:09 newdelhihosting.co.in nginx[29724]: nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
Mar 17 08:28:09 newdelhihosting.co.in nginx[29724]: nginx: configuration file /etc/nginx/nginx.conf test is successful
Mar 17 08:28:09 newdelhihosting.co.in systemd[1]: Started The nginx HTTP and reverse proxy server.
[root@newdelhihosting ~]#

 

Configure firewalld to allow Nginx and itsTraffic

Step 4: Enable firewalld and start the service.

[root@newdelhihosting ~]# systemctl enable firewalld
[root@newdelhihosting ~]# systemctl status firewalld

By default, CentOS 7 built-in firewall is set to block Nginx traffic. To allow web traffic on Nginx, update the system firewall rules to permit inbound packets on HTTP and HTTPS using the commands below.

[root@newdelhihosting ~]# firewall-cmd --zone=public --permanent --add-service=http
[root@newdelhihosting ~]# firewall-cmd --zone=public --permanent --add-service=https
[root@newdelhihosting ~]# firewall-cmd --reload

 

Test Nginx Server on CentOS 7

Step 5: Now, verify Nginx server by going to the following URL, a default nginx page will be shown.

http://domain_name.tld or IP_address

Creating Virtual Hosts in Nginx

A virtual host is a website that is served by Nginx in a single VPS or physical server. However, in the Nginx docs you will find the term “server blocks” instead, but they are basically the same thing called by different names.

The first step to set up virtual hosts is to create one or more server blocks in the main configuration file (/etc/nginx/nginx.conf) or inside /etc/nginx/sites-available.

Although the name of the configuration files in this directory(sites-available) can be set to whatever you want, it is an idea to use the name of the domains, and in addition we chose to add the .conf extension to indicate that these are configuration files.

These server blocks can be relatively complex, but in their basic form they consist of the following content:

In /etc/nginx/sites-available/newdelhosting.com.conf:

  1. First, we need to set up our directories where the server blocks will live.
[root@newdelhihosting ~]# mkdir /etc/nginx/sites-available
[root@newdelhihosting ~]# mkdir /etc/nginx/sites-enabled

 

Step 2: Now we need to tell NGINX to use look at those directories for the server blocks. Open the global NGINX configuration file in the text editor of your choice. We will use nano

[root@newdelhihosting ~]# nano /etc/nginx/nginx.conf

Now, Add the below lines at end of http section and save the file

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

As below

# Load modular configuration files from the /etc/nginx/conf.d directory.
# See http://nginx.org/en/docs/ngx_core_module.html#include
# for more information.
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*.conf;
server_names_hash_bucket_size 64;

Fine. Now, NGINX can recognize the server block.

Configuring the actual NGINX server blocks

Step 3: Create a new file specifically for the server block for your site. The line below will do this

[root@newdelhihosting ~]# /etc/nginx/sites-available/newdelhihosting.com.conf

and, add the below lines with your actual domain name

server {
listen       80;
server_name  newdelhihosting.com www.newdelhihosting.com;
location / {
root   /var/www/newdelhihosting.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;
}
}

Step 4:  Now, we need to create a symbolic link between sites-available and sites-enabled using the below command.

# ln -s /etc/nginx/sites-available/newdelhihosting.com.conf /etc/nginx/sites-enabled/newdelhihosting.com.conf

Next, restart Nginx

[root@newdelhihosting ~]# nginx -t && systemctl start nginx
[root@newdelhihosting ~]# nginx -t && systemctl start nginx
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@newdelhihosting ~]#

You’re done! Provided your DNS and/or hosts file is pointed for your domain, you should now be able to go to the domain in a web browser and see the test HTML page we created earlier.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *