How to Install Nginx on Ubuntu 16.04

Nginx is one among the foremost fashionable open supply internet server software system. it’s wide used for load equalisation, reverse proxying, media streaming and far a lot of. Nginx is that the solely all-in-one superior protocol server, designed to serve a high volume of simultaneous connections, as another to Apache internet server. nowadays Nginx is put in on quite 450 million servers worldwide and is employed by one among the best traffic websites on the online like Dropbox, Zynga games, Netflix. during this tutorial, we are going to show you the way to put in Nginx on Ubuntu 16.04.

install nginx ubuntu 16.04 There is not any question that Nginx is that the best-in-class internet server and application delivery answer. putting in Nginx on Ubuntu 16.04 is a simple task, simply rigorously follow the steps below and you ought to have Nginx put in on your Ubuntu server in but ten min. Let’s start.

Step 1: Install Nginx on Your Ubuntu Server

Nginx is available in the Ubuntu package repositories simple. First, update the apt cache with the following command:

sudo apt update

and install Nginx by issuing:

sudo apt install nginx

Once the installation is completed Nginx will be automatically started.
You can make sure that Nginx service is running with the following command:

sudo systemctl status nginx

The output should look like below:

● nginx.service - A high performance web server and a reverse proxy server
   Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
   Active: active (running) since Sat 2018-03-31 01:50:44 CDT; 8s ago
 Main PID: 716 (nginx)
   CGroup: /system.slice/nginx.service
           ├─716 nginx: master process /usr/sbin/nginx -g daemon on; master_process on
           ├─717 nginx: worker process
           ├─718 nginx: worker process
           ├─719 nginx: worker process
           └─720 nginx: worker process

Step 2: Open Firewall Ports

If you are using ufw you need to open HTTP port 80 and/or HTTPS port 433. Ufw comes with profiles based on the default ports of most common daemons and programs.

To open both Nginx ports run the following command:

sudo ufw allow 'Nginx Full'

To verify the change run:

sudo ufw status

The output should look like below:

Status: active

To                         Action      From
--                         ------      ----
Nginx Full                 ALLOW       Anywhere
Nginx Full (v6)            ALLOW       Anywhere (v6)

You can now open your browser, enter your server IP address into your browser address bar and you should see the default Nginx page.

Step 3: Managing Nginx Service

You can manage the Nginx service same as any other systemd unit.

Start the nginx service with the following command:

sudo systemctl start nginx

Stop the service with:

sudo systemctl stop nginx

Restart the service with:

sudo systemctl restart nginx

Check the status of the service with:

sudo systemctl status nginx

Enable the service on system boot with:

sudo systemctl enable nginx

Disable the service on system boot with:

sudo systemctl disable nginx

Step 4: Create a New Server Block

The default Nginx installation will have one server block enabled with a document root set to /var/www/html.
In this guide, we will create a new server block for the domain example.com and set the document root to /var/www/example.com.

First, create the domain document root with the following command:

sudo mkdir -p /var/www/example.com

and then create an index.html file with the following content:

sudo vim /var/www/example.com/index.html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>example.com</title>
</head>
<body>
    <h1>example.com server block</h1>
</body>
</html>

Next, create a new server block with the following content:

sudo vim /etc/nginx/sites-available/example.com.conf
server {
       listen 80;
       listen [::]:80;

       server_name example.com www.example.com;

       root /var/www/example.com;
       index index.html;

       location / {
               try_files $uri $uri/ =404;
       }
}

Activate the server block by creating a symbolic link :

sudo ln -s /etc/nginx/sites-available/example.com.conf /etc/nginx/sites-enabled/example.com.conf

Step 5: Restart Nginx

Test the Nginx configuration and restart nginx:

sudo nginx -t
sudo systemctl restart nginx

Now if you enter example.com into your browser address bar you should see example.com server block.


الكاتب ع ـامر

ع ـامر

مواضيع متعلقة

اترك رد