In this lab, we'll be setting up a web server running on a virtual machine, obtaining a DNS name that points to its IP address, and then installing a web application on it. We'll then obtain a TLS certificate for the site via the Let's Encrypt service so that a web client can bring the site up over HTTPS.

Option #1: Web UI

Visit GCP's Compute Engine and start configuring an instance with a recent LTS version of Ubuntu with HTTP and HTTPS traffic enabled. Create the instance. When it comes up, note its External IP address. You will be using this address in subsequent steps.

Option #2: Cloud Shell

The instance may also be launched from Cloud Shell via the gcloud SDK. To do so, begin by creating firewall rules that enable both HTTP and HTTPS traffic. Note that if these rules already exist, they will return errors. Continue if so.

gcloud compute firewall-rules create default-allow-http \
  --allow=tcp:80 --target-tags=http-server

gcloud compute firewall-rules create default-allow-https \
  --allow=tcp:443 --target-tags=https-server

Launch the instance using the command, which will output the External IP address upon completion

gcloud compute instances create nginx-gb \
  --image-family=ubuntu-2204-lts \
  --image-project=ubuntu-os-cloud \
  --machine-type=e2-medium \
  --tags=http-server,https-server

Our guestbook web site requires a name so we don't have to remember the external IP address to access it. To do so, visit a free DNS registration service and register a name on the VM's external IP address.

Example services include

Note that if you get a rate-limit error on Let's Encrypt via one service, you will need to use the alternate ones. This is a result of Let's Encrypt being abused by malicious actors to create web sites with valid certificates that look legitimate. (This, of course, won't be an issue with our janky web application)

duckDNS

freeDNS

We will now set up our guestbook web site on the Compute Engine VM and use the name from the prior step to obtain a TLS certificate.

If you are using a Compute Engine VM, ssh into it from the Web UI

or from Cloud Shell via

gcloud compute ssh nginx-gb

Once connected to the VM, change the permissions for your home directory to add execute permissions for others to it. This will allow the web server, which runs with group www-data, to access resources we set up for it.

chmod o+X .

Then, clone the repository and change into the source directory

git clone https://github.com/bfritscher/guestbook-src.git
cd guestbook-src/03_nginx_gunicorn_certbot

We will now examine the code and configuration of our application.

etc/systemd.template

The repository contains template files for configuring the server and nginx to run the Guestbook code. These templates will generate corresponding files in /etc, the directory where Linux configuration files are stored. The first template is etc/systemd.template. systemd is the default service manager for Ubuntu. The file configures the startup of the gunicorn daemon and its environment. In addition, the working directory is set to PROJECT_DIR, which will be replaced on installation with your directory. The PATH environment variable is set to the location where the Python environment's binaries will eventually be installed via a subsequent venv. The last command configures the entry point for the Python application, sets the number of processes to use for it, and specifies the domain socket that will be used by the nginx web server to communicate with the web application.

The other template is etc/nginx.template which configures nginx with the server's DNS name (setup from the prior step) and the path to the root directory for the server. It also specifies that requests to the top-level path (/) should go through the gunicorn socket and be handled by the Python application while requests to the /static path should be served directly by nginx from the specified directory.


install.sh

The script used to setup the site is install.sh

The script takes two parameters: the DNS name you set-up earlier and your email (e.g. ./install.sh cs430-wuchang.ipq.co name@school.com)

The script begins by pulling out the first part of the DNS name (e.g. cs430-wuchang) and eliminating the first period and all subsequent characters. The name is used to label the systemd service for the site.

Then, the script installs python3, venv, nginx, and certbot. It then creates the venv environment for the web application and installs its packages into it from requirements.txt.

Then, the script sets up a systemd startup file for the web site from its template using sed to replace the PROJECT_DIR with the current working directory ($PWD) and PROJECT_USER with the current user ($SUDO_USER). It will then name the systemd service using the $SITE label above.

The script then sets up the nginx configuration file from its template using sed. Similar substitutions are made as well as a substitution for PROJECT_HOST using the fully-qualified domain name (FQDN) of the site (registered earlier). It also sets up nginx to add the site (via adding config file to /etc/nginx/sites-available and a link in /etc/nginx/sites-enabled to it)

It then starts systemd service for the site, enables it by default (on startup), and restarts nginx for changes to take hold.

Run the script. Fill in Your_FQ_DNS_Name_Here with the name you registered (example: abc.duckdns.org)

sudo ./install.sh Your_FQ_DNS_Name_Here

Bring up a web browser and navigate to the application running on the instance over HTTP to ensure that you're able to access the Guestbook application that is running on it.

http://<Your_FQ_DNS_Name_Here>

Then, fill in the command below with your OdinID to run certbot in order to obtain a TLS certificate for the web site from Let's Encrypt.

sudo certbot --nginx -d Your_FQ_DNS_Name_Here -n -m YourEmailAdresse --agree-tos --redirect

Bring the site up in a browser and note its valid certificate. Add an entry to the guestbook.

Task Progress Check

Go to the web console of Compute Engine, find the VM in "VM instances" and select Delete. When prompted, confirm that you would like to delete the VM.

Option #1: Web UI

Option #2: Cloud Shell

This can also be done via Cloud Shell via:

gcloud compute instances delete nginx-gb