Installing and configuring Nginx on CentOS.

Installing and configuring Nginx on CentOS.

In the web server world, you could find multiple options. Some of the most popular web servers are:

  • Apache (leader)
  • IIS
  • Boa
  • Monkey
  • Nginx

... and a few more.

Nginx has gain market shared in this space in the last couple years, and part of this success have to do with some important factors like reliability, concurrency, low resource usage and scalability.

In this point, we are going to show how to install and configure Nginx on a CentOS 7 server.

Installing Nginx###

There are many ways to install Nginx on CentOS. the easiest and most efficient way is to do it using the EPEL repository. In order to do this:

Install the EPEL repository:

[root@webserver ~]# yum install epel-release -y

Verify that Nginx is available on the system by using yum search :

[root@webserver ~]# yum search nginx 
Loaded plugins: fastestmirror
epel/x86_64/metalink                               | 13 kB  00:00:00
epel                                               | 4.4 kB  00:00:00
(1/2): epel/x86_64/group_gz                        |257 kB  00:00:00
(2/2): epel/x86_64/primary_db                      | 4.1 MB  00:00:00
(1/2): epel/x86_64/updateinfo                      | 337 kB  00:00:00
(2/2): epel/x86_64/pkgtags                         | 1.4 MB  00:00:00
Loading mirror speeds from cached hostfile
* base: mirrors.tripadvisor.com
* epel: mirror.symnds.com
* extras: mirror.thelinuxfix.com
* updates: mirrors.tripadvisor.com
=============================================================================== N/S matched: nginx ================================================================================
collectd-nginx.x86_64 : Nginx plugin for collectd
munin-nginx.noarch : Network-wide graphing framework (cgi files for nginx)
nginx-filesystem.noarch : The basic directory layout for the Nginx server
owncloud-nginx.noarch : Nginx integration for ownCloud
nginx.x86_64 : A high performance web server and reverse proxy server

After verifying that the repository is enabled and the software is available we are going to install it:

[root@webserver ~]# yum install nginx -y

Now, we are going to start and make the service persistent across reboots.

To make the service persistent:

[root@webserver ~]# systemctl enable nginx.service

To start the service:

[root@webserver ~]#systemctl start nginx.service

After this, you need to make sure that you could serve traffic from your server. In order to do that verify that ports 80 and 443 are open on your system firewall.

Configuring Virtual Hosts on Nginx###

Virtual Host provides the ability to serve more than one domain from a single server. Nginx has this feature and there are two (2) ways to achieve this on the software.

  • Creating the virtual host configuration directly on the Nginx configuration file at /etc/nginx/nginx.conf.
  • Creating a configuration file per virtual host on /etc/nginx/conf.d.

In this post, I am going to show how to create virtual hosts using the latest method. This is the easiest way to do it and also will not impact the main configuration file for the software.

I am going to create two (2) virtual host, one for www.mysite.com and another one for blog.mysite.com. The image below ilustrates this scenario.

In order to do this, we need to follow the next steps.

Create the directories where the sites will reside:

[root@webserver ~]# mkdir -p /var/www/virtual_hosts/www.mysite.com/{html,logs}

[root@webserver ~]# mkdir -p /var/www/virtual_hosts/blog.mysite.com/{html,logs}

Now we are going to create the configuration files for the virtual hosts. To do that use any text editor.

For the virtual host for www.mysite.com:

[root@webserver ~]# vim /etc/nginx/conf.d/www.mysite.com.conf

The file content will look like this:

server {
listen  80;
server_name www.mysite.com mysite.com;
access_log /var/www/virtual_hosts/www.mysite.com/logs/access.log;
error_log /var/www/virtual_hosts/www.mysite.com/logs/error.log;
 
location / {
    root  /var/www/virtual_hosts/www.mysite.com/logs/html;
    index index.html index.htm index.php;
    }
}

For the second virtual host, we repeat the same process, and name the file blog.mysite.com:

server {
listen  80;
server_name blog.mysite.com www.blog.mysite.com;
access_log /var/www/virtual_hosts/blog.mysite.com/logs/access.log;
error_log /var/www/virtual_hosts/blog.mysite.com/logs/error.log;
 
location / {
    root  /var/www/virtual_hosts/blog.mysite.com/logs/html;
    index index.html index.htm index.php;
    }
}

After this we will restart the service:

[root@webserver ~]# systemctl restart nginx.service

Now just add your web content to each of the virtual host directories and verify the functionality. It is really important that you have DNS resolution for the virtual host domains in order for it to work.