Install Nginx on CentOS 7 / RHEL 7

0

NGINX

Nginx is a popular lightweight server for those who do not need the bulk and extra services that Apache may offer. This article will look at installing Nginx on a CentOS using the ‘yum’ package manager.

Install Nginx:

To install Nginx using yum we need to include the Nginx repository, install the Nginx repository.

rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm

Install the Nginx by issuing the following command.

yum install nginx

Starting Nginx:

Once installed, start the Nginix by using the following command.

systemctl start nginx.service

Once started. Navigate the web browser to Nginx server(IP Address or Host Name). The following image shows the default Welcome Screen of Nginx.

CentOS 7 - Nginx Default Page
CentOS 7 – Nginx Default Page

Extras:

Init scripts will help you to start and stop the service. The init scripts for the Nginx is installed while installing Nginx. However, we need to set up Nginx to start automatically if the server is rebooted:

systemctl enable nginx.

Configuration File:

The name of the default configuration file is nginx.conf, located in /etc/nginx directory. Configuration file is based on directives and parameters, each directive ends with a semi colon. The following shows the directives for worker connection and in which user name Nginx process should run.

user  nginx;
worker_processes  1;
events {
worker_connections  1024;
}

Creating a Virtual Server:

We must setup at least one virtual server for Nginx, in order to process the HTTP request by Nginx. When Nginx process the request,  it looks for the server directive which is placed in http context. You can add multiple server directives, to define multiple virtual servers.

Default virtual server config can be found under /etc/nginx/conf.d directory, if you open and see that; first line itself a virtual server for localhost and listening on port 80.

server {
listen       80;
server_name  localhost;
}

You will find the location directive, which will tell the server to look for the static file when the requests comes for the localhost.

location / {
root   /usr/share/nginx/html;
index  index.html index.htm;
}

Additionally you can mention the error pages.

error_page   500 502 503 504  /50x.html;
location = /50x.html {
root   /usr/share/nginx/html;
}

The above is the minimum configuration for a virtual server, you can find the full configuration here.

Example:

The following virtual server will accept the request for server.itzgeek.com, create a configuration file  in /etc/nginx/conf.d or copy the default configuration file.

cp /etc/nginx/conf.d/default.conf /etc/nginx/conf.d/itzgeek.conf

Edit in such a way that, it receives the request. Minimal configuration will look like below.

vi /etc/nginx/conf.d/itzgeek.conf
server {
listen       80;
server_name  server.itzgeek.com;
location / {
root   /usr/share/nginx/html/itzgeek;
index  index.html index.htm;
}
}

Create root directory.

mkdir /usr/share/nginx/html/itzgeek

Create Index.html page.

echo “This is ITzGeek Home” > /usr/share/nginx/html/itzgeek/index.html

Restart the Nginx service.

 systemctl restart nginx.service

Test with browser, url will be http://server.itzgeek.com

CentOS 7 - Nginx Virtual Server
CentOS 7 – Nginx Virtual Server

That’s all.

You might also like