How To Install Nginx on Ubuntu 18

How To Install Nginx on Ubuntu 18

 

Installing Nginx

Since Nginx is available in Ubuntu’s default repositories, it is possible to install it from these repositories using the apt packaging system.

sudo apt install nginx

After accepting the procedure, apt will install Nginx and any required dependencies to your server.

 

Adjusting the Firewall

Before testing Nginx, the firewall software needs to be adjusted to allow access to the service. Nginx registers itself as a service with ufw upon installation, making it straightforward to allow Nginx access.

List the application configurations that ufw knows how to work with by typing the following:

sudo ufw app list

Your output should be a list of the application profiles:

Available applications:

Nginx Full

Nginx HTTP

Nginx HTTPS

OpenSSH

This list displays three profiles available for Nginx:

  • Nginx Full: This profile opens both port 80 (normal, unencrypted web traffic) and port 443 (TLS/SSL encrypted traffic)
  • Nginx HTTP: This profile opens only port 80 (normal, unencrypted web traffic)
  • Nginx HTTPS: This profile opens only port 443 (TLS/SSL encrypted traffic)

It is recommended that you enable the most restrictive profile that will still allow the traffic you’ve configured. Since you haven’t configured SSL for your server yet in this guide, you’ll only need to allow traffic on port 80.

You can enable this by typing the following:

ufw allow ‘Nginx HTTP’

Then, verify the change:

ufw status

You should receive a list of HTTP traffic allowed in the output:

Status: active

To Action From

— —— —-

OpenSSH ALLOW Anywhere

Nginx HTTP ALLOW Anywhere

OpenSSH (v6) ALLOW Anywhere (v6)

Nginx HTTP (v6) ALLOW Anywhere (v6)

Now that you’ve added the appropriate firewall rule, you can check that your web server is running and able to serve content correctly.

 

Checking your Web Server

At the end of the installation process, Ubuntu 18 starts Nginx. The web server should already be up and running.

Check with the systemd init system to make sure the service is running:

systemctl status nginx

Output● 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 Wed 2021-11-17 11:29:44 PST; 10s ago

Docs: man:nginx(8)

Process: 11363 ExecStop=/sbin/start-stop-daemon –quiet –stop –retry QUIT/5 –pidfile

Process: 11423 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited,

Process: 11422 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code

Main PID: 11424 (nginx)

Tasks: 5 (limit: 4915)

CGroup: /system.slice/nginx.service

├─11424 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;

├─11425 nginx: worker process

├─11426 nginx: worker process

├─11427 nginx: worker process

└─11428 nginx: worker process

This output shows that the service has started successfully. However, the best way to test this is to actually request a page from Nginx.

You can access the default Nginx landing page to confirm that the software is running properly by navigating to your server’s IP address.

When you have your server’s IP address, enter it into your browser’s address bar:

http://your_server_ip

You should receive the default Nginx landing page:

This page is included with Nginx to verify that the server is running correctly.

 

Managing the Nginx Process

Now that you have your web server up and running, let’s review some basic management commands.

To stop your web server, type the following:

sudo systemctl stop nginx

To start the web server when it is stopped, type the following:

sudo systemctl start nginx

To stop and then start the service again, type the following:

sudo systemctl restart nginx

If you are simply making configuration changes, you can often reload Nginx without dropping connections instead of restarting it. To do this, type the following:

sudo systemctl reload nginx

By default, Nginx is configured to start automatically when the server boots. If this is not what you want, you can disable this behavior by typing the following:

sudo systemctl disable nginx

To re-enable the service to start up at boot, you can type the following:

sudo systemctl enable nginx

Nginx should now start automatically when the server boots again.

 

Errors are stored in the nginx log file and you can see them in real time using the command.

tail -f /var/log/nginx/access.log

79.113.228.1.. – – [17/Nov/2021:12:12:17 -0800] “GET /o_pagina-care-nu-exista HTTP/1.1” 404 209 “-” “Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.45 Safari/537.36”

82.79.3.1.. – – [17/Nov/2021:12:12:38 -0800] “GET /asda HTTP/1.1” 404 209 “-” “Mozilla/5.0(Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.45 Safari/537.36”

82.79.3.1.. – – [17/Nov/2021:12:12:48 -0800] “GET /asda HTTP/1.1” 404 209 “-” “Mozilla/5.0(Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.45 Safari/537.36”

82.79.3.1.. – – [17/Nov/2021:12:12:51 -0800] “GET /asda465 HTTP/1.1” 404 209 “-” “Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664. 45 Safari/537.36”

82.79.3.1.. – – [17/Nov/2021:12:14:47 -0800] “GET / HTTP/1.1” 200 110 “-” “Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.45 Safari/537.36”

79.113.228.1.. – – [17/Nov/2021:12:14:51 -0800] “GET / HTTP/1.1” 200 110 “-” “Mozilla/5.0(Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.45 Safari/537.36”

82.79.3.1.. – – [17/Nov/2021:12:16:51 -0800] “GET / HTTP/1.1” 304 0 “-” “Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.45 Safari/537.36”

82.79.3.1.. – – [17/Nov/2021:12:17:52 -0800] “GET / HTTP/1.1” 304 0 “-” “Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.45 Safari/537.36”

82.79.3.1.. – – [17/Nov/2021:12:17:55 -0800] “GET / HTTP/1.1” 304 0 “-” “Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.45 Safari/537.36”

82.79.3.1.. – – [17/Nov/2021:12:18:00 -0800] “GET /index.html HTTP/1.1” 200 110 “-” “Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.45 Safari/537.36”