Skip to content

Intro

Nginx is one of the most popular web servers in the world and is responsible for some of the largest and most trafficked sites on the Internet. It is a lightweight choice that can be used as either a web server or a reverse proxy.

Step 0 – Installing NGINX to new VM

Make a new computer for NGINX. See video for information

Step 1 – Installing NGINX

Since Nginx is available in Ubuntu's default package directory (default repositories), you can install it using the apt package manager.

First, let's update our local package directory to get access to the latest package lists. After that, nginx can be installed:

Text Only
sudo apt update && sudo apt upgrade
sudo apt install nginx
Press Y when prompted to confirm installation. If prompted to restart the service, accept the defaults and press ENTER to continue. apt installs Nginx and all necessary dependencies on your server.

Step 2 – UFW-firewall configuration if needed. (In the Google Cloud main firewall will be the clouds.)

NOTE : You don't need to do this in google cloud

Before testing Nginx, the firewall software must be configured to allow access to the service. Nginx registers itself as a service with ufw upon installation, which makes allowing Nginx access simple.

Bash
sudo ufw status
sudo ufw app list
Bash
user@ngix2:~$ sudo ufw app list
Available applications:
  Nginx Full
  Nginx HTTP
  Nginx HTTPS
  OpenSSH
user@ngix2:~$ sudo ufw status
Status: inactive

List of application profiles:

Bash
Output
Available applications:
  Nginx Full
  Nginx HTTP
  Nginx HTTPS
  OpenSSH

As the output shows, there are 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)

To enable traffic through http, type:

Bash
sudo ufw allow 'Nginx HTTP'
Check the status of the UFW firewall:
Bash
sudo ufw status
HTTP traffic (80) is allowed. Note : We are using Google Cloud so UFW is not really active.

Bash
Output
Status: active

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

Step 3 – Checking the status of the web server

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

Bash
sudo systemctl status nginx

Bash
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 Fri 2022-03-01 16:08:19 UTC; 3 days ago
     Docs: man:nginx(8)
 Main PID: 2369 (nginx)
    Tasks: 2 (limit: 1153)
   Memory: 3.5M
   CGroup: /system.slice/nginx.service
           ├─2369 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
           └─2380 nginx: worker process

To test that Nginx is working correctly, open a web browser and enter the IP address of your server in the address bar. The browser should display the home page of the NGINX www server: NGINX frontpage

Step 4 – Managing NGINX processes

Now that you have your web server up and running, let's look at some basic administration commands.

Stop the web server by typing:

Bash
sudo systemctl stop nginx
To start the web server when it is stopped, type:
Bash
sudo systemctl start nginx
To stop and then start the service again, type:
Bash
sudo systemctl restart nginx
If you are only making configuration changes, Nginx can often reload without dropping connections. To do this, type:
Bash
sudo systemctl reload nginx

NGINX to apache2 and vice versa

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:

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

Same commands can be used to enable/disable apache2 service.

Bash
sudo systemctl disable apache2
sudo systemctl enable apache2

Step 5 – Configuring NGINX

Let's take a look at a few more important NGINX server directories and files.

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

Basic settings

/etc/nginx Nginx configuration directory. All Nginx configuration files are located here.

/etc/nginx/nginx.conf Nginx main configuration file. This can be edited to change the general configuration of Nginx.

/etc/nginx/sites-available/ Directory where Site-specific server blocks can be saved. Configuration files found in this directory are not used by Nginx unless they are linked to the sites-enabled directory. Typically, all server block configuration is done in this directory and then deployed by linking to another directory.

/etc/nginx/sites-enabled/ The directory where the Site-specific server blocks in use are stored. Typically, these are created by linking to configuration files found in the sites-available directory.

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

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/error.log All Nginx errors are recorded in this log.

You have now learned the basic administration commands and should be ready to configure your site to host more than one domain.

Step 6 – Quality of life improvements Vscode and ssh keys

Configuring user account to www-data group

Bash
sudo usermod -aG www-data $USER

SSH keys login to server

Plan A

You have to create a key pair and transfer the public key to Google Cloud keymanager. Then you can use that key to login to all servers.

Plan B (Add Teros and Your Github key to webserver)

0) Make sure that you have github key in https://github.com/settings/keys 1) Download Tero Keso's public key from moodle and save it to your computer. 2) If needed generate your own key pair. 3) Open WSL Ubuntu --> Navigate to .ssh folder --> Locate needed public key(s) 4) Transfer the public key(s) to the server

Bash
ssh-copy-id -i ~/.ssh/id_rsa.pub user@ip
#OR
ssh-import-id-gh USERNAME

How to make a SSH config to VScode

Bash
Host cluster01-dev
  HostName 172.18.138.62
  User student
  IdentityFile C:\Users\tkeso\.ssh\SSHKEY

Host rancher01-dev
  HostName 172.18.137.4
  User student
  IdentityFile C:\Users\tkeso\.ssh\SSHKEY

Dev tunnels (typically a bad idea, but useful for testing)

Basic information: https://code.visualstudio.com/docs/remote/tunnels

Bash
curl -Lk 'https://code.visualstudio.com/sha/download?build=stable&os=cli-alpine-x64' --output vscode_cli.tar.gz

tar -xf vscode_cli.tar.gz

./code tunnel --name rancher01-USERNAME --accept-server-license-terms &