How To Install Nginx on Ubuntu 20.04 | DigitalOcean

Install nginx

sudo apt update
sudo apt install nginx

sudo ufw app list

Available applications:
  Nginx Full
  Nginx HTTP
  Nginx HTTPS
sudo ufw allow 'Nginx HTTP'
Rules updated
Rules updated (v6)

sudo ufw status
Status: inactive

If you get a Status: inactive message when running the ufw status command, it means that the firewall is not yet enabled on the system. You will need to run a command to enable it.

By default, when UFW is enabled, it blocks external access to all ports on the server.

sudo ufw enable
Command may disrupt existing ssh connections. Proceed with operation (y|n)? y
Firewall is active and enabled on system startup
sudo ufw allow 'Nginx HTTP'
Skipping adding existing rule
Skipping adding existing rule (v6)

sudo ufw status
Status: active

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

Check it

systemctl status nginx

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 2023-06-24 12:49:46 UTC; 6min ago
       Docs: man:nginx(8)
   Main PID: 2971 (nginx)

Checking the webserver, add and NSG inbound allow 80 in Azure.


Managing nginx

sudo systemctl stop nginx
sudo systemctl start nginx
sudo systemctl restart nginx

# If you are only making configuration changes, Nginx can often reload without dropping connections.
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:
sudo systemctl disable nginx

#To re-enable the service to start up at boot, you can type:
sudo systemctl enable nginx

Edit .html and reload test


/var/www/html$ sudo systemctl reload nginx


Setting Up Server Blocks (Recommended)
When using the Nginx web server, server blocks (similar to virtual hosts in Apache) can be used to encapsulate configuration details and host more than one domain from a single server. We will set up a domain called your_domain, but you should replace this with your own domain name.


Getting Familiar with Important Nginx Files and Directories


/var/www/html: The actual web content, which by default only consists of the default Nginx page you saw earlier, is served out of the /var/www/html directory. This can be changed by altering Nginx configuration files.

 cd /var/www/html/


Server configuration

# /etc/nginx: The Nginx configuration directory. All of the Nginx configuration files reside here.

cd /etc/nginx/
/etc/nginx$ ls
conf.d        fastcgi_params  koi-win     modules-available  nginx.conf    scgi_params      sites-enabled  uwsgi_params
fastcgi.conf  koi-utf         mime.types  modules-enabled    proxy_params  sites-available  snippets       win-utf

# /etc/nginx/sites-available/: The directory where per-site server blocks can be stored. Nginx will not use the configuration files found in this directory unless they are linked to the sites-enabled directory. Typically, all server block configuration is done in this directory, and then enabled by linking to the other directory.

cd /etc/nginx/sites-available/
/etc/nginx/sites-available$ ls

# /etc/nginx/sites-enabled/: The directory where enabled per-site server blocks are stored. Typically, these are created by linking to configuration files found in the sites-available directory.

cd /etc/nginx/
/etc/nginx$ cd sites-enabled/
/etc/nginx/sites-enabled$ ls

# /etc/nginx/snippets: This directory contains configuration fragments that can be included elsewhere in the Nginx configuration. Potentially repeatable configuration segments are good candidates for refactoring into snippets.

cd /etc/nginx/snippets/
/etc/nginx/snippets$ ls
fastcgi-php.conf  snakeoil.conf

Server logs

# /var/log/nginx/access.log: Every request to your web server is recorded in this log file unless Nginx is configured to do otherwise.

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

/var/log/nginx$ sudo cat access.log 

IPADDRESS - - [24/Jun/2023:13:00:09 +0000] "GET / HTTP/1.1" 200 396 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/114.0"
IPADDRESS - - [24/Jun/2023:13:00:10 +0000] "GET /favicon.ico HTTP/1.1" 404 134 "" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/114.0"

/var/log/nginx/error.log: Any Nginx errors will be recorded in this log.