Easy guide to install NGINX server & configuring Virtual hosts (Server Blocks)

Nginx is a great alternative to apache & has been gaining popularity in recent times. Though Nginx has been known to being a web server with less modules/add-ons & more complex settings than apache but if you are hosting a single website that you want to work at fast speed, than Nginx is right fit for you.

Nginx is an open source has been known to address some of performance related issues that apache has. It uses an event driven design, as opposed to process driven design used by apache which causes apache to slow down under heavy loads as it needs to create new processes which causes increased memory consumption. Worker processes can be configured for Nginx (each worker process is equal to one cpu) & each worker process can handle thousand concurrent connections.

(Recommended – Read our tutorials on installing Apache, Securing apache with SSL cert & hardening tips for apache)

 

In this tutorial, we will learn to install Nginx server, but before we move onto install Nginx server, we need to perform the following steps.

1- Remove Apache

If you are using an old installation on apache server, then remove it as both nginx & apache uses same ports i.e. 80 & 443. To remove apache

$ yum remove httpd

2- Entry for our virtual host in /etc/hosts

We need to create an entry for out virtual host (newhost.com) in /etc/hosts,

$ vi /etc/hosts
192.168.0.100                     newhost.com

3- Epel or Nginx repositories enabled

Since Nginx is available with default RHEL/CentOS repositories, we either need to enable official Nginx repository or Epel repository

Nginx Repository

To enable Nginx repository, create a file named ‘nginx.repo’ in ‘/etc/yum.repos.d’ & add the following repo information

$ vi /etc/yum.repos.d/nginx.repo


[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/OS/OSRELEASE/$basearch/
gpgcheck=0
enabled=1

Replace “OS” with “rhel” or “centos”, depending on the distribution used, and “OSRELEASE” with “5”, “6”, or “7”, for 5.x, 6.x, or 7.x versions, respectively.

Epel Repository

RHEL/CentOS 7

$ rpm -Uvh https://dl.fedoraproject.org/pub/epel/7/x86_64/Packages/e/epel-release-7-11.noarch.rpm

RHEL/CentOS 6 (64 Bit)

$ rpm -Uvh http://download.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm

RHEL/CentOS 6 (32 Bit)

$ rpm -Uvh http://dl.fedoraproject.org/pub/epel/6/i386/epel-release-6-8.noarch.rpm

Install Nginx server

Once the Nginx/Epel repository has been enabled, installation can be easily done with yum. To install nginx server, run

$ yum install nginx

Now start the nginx service & also enable it after reboot,

$ systemctl start nginx
$ systemctl enable nginx

We can also test the installation by opening web browser & entering IP address/FQDN of our server

http://IPAddress or FQDN/

& a test page will open.

 

Configuring Virtual hosts (aka Server Blocks)

Like apache, document root directory for nginx is also ‘/var/www/html’ & it works fine if hosting a single wesbite (virtual host) but when deploying multiple websites, it is advisable to create our custom document directory. So, we will first create a document directory for our website ‘newhost.com’

$ mkdir -p /var/www/newhost/html

& provide necessary permissions i.e. 755 to all directories created in /var/www

$ chmod 755 /var/www

Next, create an index.html file to greet website users,

$ cd /var/www/newhost/html
$ echo “Welcome to newhost.com” > index.html

Last thing now to configure newhost.com is to create an entry for the website/virtual host in configuration file i.e. ‘/etc/nginx/conf.d/virtual.conf’ . Open the file & create an entry for our host,

$  vi /etc/nginx/conf.d/virtual.conf

server {
listen       80;
#    listen       *:80;
server_name  newhost.com;

location / {
root   /var/www/newhost.com/html/;
index  index.html index.htm;
}
}

Save file & exit it. Lastly restart nginx services to implement the changes made to configuration file.

$ systemctl restart nginx

Now open web browser & enter ‘newhost.com’ in the address bar. This will open our website newhost.com.

Note – If we need to create more than one virtual host, repeat the same process. Create a document root directory, place website files in it, create an entry for it in virtual.conf & restart services.

This concludes our tutorial on how to install nginx server. Please feel free to mention your queries/questions in the comment box below.

If you think we have helped you or just want to support us, please consider these :-

Connect to us: Facebook | Twitter | Google Plus

Donate us some of your hard earned money:

Linux TechLab is thankful for your continued support.

Shusain

Passionate about Linux & open source. Loves to learn, read & write about Linux as well as new technologies.

You may also like...

Leave a Reply

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