How To Setup Nginx Server In Linux?

How To Setup Nginx Server In Linux?

   

This post was last updated on March 1st, 2020 at 03:15 am

Nginx, which is pronounced as ‘engine x’ is a free, opensource and high-performance HTTP server. It is among the most popular web servers that host some largest and most trafficked websites on the internet. Nginx is used as a reverse proxy, load balancing, HTTP cache, and mail proxy. It was developed by Igor Sysoev and publicly released in 2004. Ahead in this article, we will see how to install and use the Nginx server.

Installing Nginx Server

Based on different package management system and Linux distro you can choose one of the following commands:

How to Install Nginx on ubuntu / Linumx Mint / Debian?

To install the Nginx server in Debian based distributions follow the below steps-
First, update your OS repository by using-

sudo apt-get update

Use the following command to install it –

sudo apt-get install nginx -y

and verify your installation by using –

sudo nginx -v

How to Install Nginx on CentOS / Fedora?

To install the Nginx server in RPM-based distributions follow the given steps-
First, install the EPEL repository by using –

sudo yum install epel-release

Update OS repository by using –

sudo yum update

and install it by using –

sudo yum install nginx

Verify your installation

sudo nginx -v

How to configure Firewall to allow Nginx?

Enable your firewall to block non-essential ports in your system. You can use ufw or firewalld firewall application on Linux.

Configure ufw for Nginx

Check the availability of ufw application profiles by using the following command –

sudo ufw app list

Allow Nginx HTTP with the following command –

sudo ufw allow 'Nginx HTTP'

And verify the changes with –

sudo ufw status

Configure firewalld for Nginx

If not installed, install the firewalld using the below command –

sudo apt-get install firewalld -y
or
sudo yum install firewalld -y

By default the firewalld would not be running. You can check the status –

sudo firewall-cmd --state

Start the firewalld service and make an entry to restart the daemon on every reboot –

sudo systemctl start firewalld
sudo systemctl enable firewalld

Once installed, add http and https services to the public zone (if you are planning to use the server in a public domain)

sudo firewall-cmd --zone=public --add-service=http
and/or
sudo firewall-cmd --zone=public --add-service=https

Check the status of the webserver

Check the status of Nginx webserver in your system by using the following command-

systemctl status nginx

Now open your browser and enter your server IP or URL to test the working of the Nginx server.

http://your_server_Ip

It will display the default landing page of Nginx.

Creating Multiple Virtual Host on A Server?

After installing the server probably you want to host a website. Nginx makes it easy to create virtual hosts that allow you to host multiple websites on a single server. The default root directory for the web is /var/www/html. You will have to place all your website files inside this directory. Now, If you want to run multiple websites on a single server then you can create unique directories inside the root directory. For example – Suppose I have two websites www.example.com and www.example2.com to host.

Please use your own domains in place of using example.com and example2.com

And follow the steps given below to host and configure them –

  1. Go to the web’s root directory –
    cd /var/www/html
  2. Create two unique directories –
    sudo mkdir example.com example2.com
  3. Create an index file in both the directories –
    cd example.com
    sudo nano index.html

    Enter some text here-

     

    And then press ctrl+s to save and ctrl+x to exit from the text editor. now move into the second directory and create an index file for it.

    cd ..
    cd example2.com
    sudo nano index.html

    Add some text in this file

    press ctrl+s to save and ctrl+x to exit from text editor.

  4. Change the ownership –
    chown -R $user:$user /var/www/html/example.com
    chown -R $user:$user /var/www/html/example2.com
  5. Create and edit web’s root directory for each –
    sudo cp /etc/nginx/sites-available/default  /etc/nginx/sites-available/example.conf

    Edit the example2.conf configuration file with a text editor –

    sudo nano /etc/nginx/sites-available/example.conf

    Change the root directory to /var/www/html/example2.com and insert the server name as given below.

     

    sudo cp /etc/nginx/sites-available/default  /etc/nginx/sites-available/example2.conf

    now edit the example2.conf configuration file with a text editor –

    sudo nano /etc/nginx/sites-available/example2.conf

    Change the root directory to /var/www/html/example2.com and insert the server name as given below.

     

  6. Remove default server block file
    sudo rm /etc/nginx/sites-enabled/default
  7. Enable the newly created files
    ln -s /etc/nginx/sites-available/example.conf /etc/nginx/sites-enabled/
    ln -s /etc/nginx/sites-available/example2.conf /etc/nginx/sites-enabled/

    Now you can check the status of Nginx configuration by –

    sudo nginx -t
  8. Restart the Nginx services
    systemctl restart nginx
  9. Edit your host file
    sudo nano /etc/hosts

    and add your domains with IP of the server

Test Websites In The Browser

You can test your websites in a web browser by entering URL like given below –

http://www.example.com
http://www.example2.com

This will display the message typed in the index file of both domains if the server configuration is done appropriately.

Apache Vs Nginx: What are the key differences?

APACHENGINX
apache is also known as httpdIt is pronounced as ‘engine x’
Developed and maintained by an open community of developer under Apache software foundationIt is also community supported but it also offers commercial product Nginx plus
Implies a process-driven approachNginx implies an event-driven approach
Create a new thread for each requestIt handles multiple requests within one thread
Apache handles static content using its conventional file-based methodNginx is faster than the apache in serving static content
It processes dynamic content within the serverIt can’t process dynamic content within the server as apache does
Apache fully supports all Unix like operating systems as MS windowsSupports all Unix like operating system and partially Microsoft windows
Apache supports customization of web server through the dynamic modulesIt is not flexible enough to support dynamic modules and landing

I hope this article is useful to you. If you have a query regarding the topic you can write to us in the comments below.

Previous How To Run Windows Applications In Linux Using Wine
Next WordPress Breadcrumbs: How to Use This Function?

About author

You might also like

Administration 0 Comments

How To Create Virtual Machine In Linux With KVM?

Practically a virtual machine is used to make some resource(s) to be used by multiple users. Use of Virtual Machine in Linux is more common as the latter is a

Administration 0 Comments

How To Setup Nginx As Reverse Proxy?

As we already discussed to setup the Ngnix server and creating virtual hosts on it. In this article, we will see about proxy servers, reverse proxy server and what they

Administration 0 Comments

How To Set/Change Root Password In Ubuntu?

A root user is a most privileged user account in a Unix based operating system. It has access to all the commands and files of the system. A root user

0 Comments

No Comments Yet!

You can be first to comment this post!

Leave a Reply