Content
How to setup a production server, specifically to solve the Assignment 3.
Introduction
In the following text and demo videos we will go through how to work with our own private cloud. All student will get an own Linux virtual server to use as a production server for your web application. The severs will be taken down after the examination.
The recommendation is to follow along in this exercise before you start working on the examination assignments.
NOTE. The computer you have access to is publicly reachable over the Internet. You are only allowed to use this machine for the purposes described in this course. You are not allowed to use the machine for private projects.
NOTE. The SSH-key you have received (through GitLab) should be considered a secret, and it is important that you do not send it to a third party or show the key in any online streams.
What to do
This is a brief list of things that this exercise will process. You can, if you like, try to do it by your self by reading the documentation, or by follow along in the recordings.
-
Connect to your cloud server
- Download the SSH-key
- Connect to the server using SSH
-
Update the server and install:
- npm and node
-
Configure NGINX to serve as a proxy for your applications
-
Add HTTPS support using Lets Encrypt
- Enable HTTP2 (optional)
Step 1 - Connect to your server
First of all you need to download the SSH-key from Gitlab. You find the file in a project called "Secrets" in your course.
Useful commands
# Change the mod of the ssh key file
chmod 600 ~/.ssh/the-ssh-key
# Login using a ssh key file
ssh ubuntu@{HOST} -i ~/.ssh/the-ssh-key
# Add the ssh key file and login with easier access
ssh-add ~/.ssh/the-ssh-key
ssh ubuntu@{HOST}
Step 2 - Update, Upgrade and install Node and NPM
Useful links
Useful commands
# Update and upgrade your system
sudo apt-get update
sudo apt-get dist-upgrade
# Install version (14) of Node (get the latest stable)
curl -sL https://deb.nodesource.com/setup_14.x | sudo -E bash -
sudo apt-get install -y nodejs
# Check versions (you might have other versions)
$ git --version
git version 2.25.1
$ node --version
v14.15.1
$ npm --version
6.14.8
Step 3 - Configure NGINX
Useful links
Useful commands
# Install nginx
sudo apt-get install nginx
# Check status and reload after changes in configuration
sudo systemctl status nginx
sudo systemctl reload nginx
sudo systemctl restart nginx
# Location of nginx configuration files
/etc/nginx/nginx.conf # Location of global config file
/etc/nginx/sites-enabled/default # Location of default server
# block config file.
Tip Useful terminal texteditors to view and edit configuration files are vim
and nano
.
Step 4 - Add TLS (https)
NOTE. Since Lets Encrypt has a rate limit of 50 certificates per week and domain (lnu.se) we might reach the limits from time to time. I that happens please notify in Slack. The only thing to do then is to wait for the limit to be reset.
Useful links
Useful commands
Tip Optional learn to use a terminal multiplexor lite tmux
or screen
. You will then be able to save your terminal session at the server and connect to it on next login.
Step 5 - Enable HTTP v2 (optional)
This is an optional step, but it is also a small and easy step. We will probably get around just fine using good old http 1.1.
Step 6 - Process Manager, PM2
In this step we will make sure our applications keeps on running on our server using a process manager.
Useful commands
sudo npm i pm2 -g
PORT=5001 pm2 start npm --name hello-world:5001 -- start // Run "npm start" with port set to 5001
pm2 logs
pm2 monit
Step 7 - Getting the code to the server
Now, how do we get the code to the production server? There are several ways:
...using secure copy, FileZilla or VSCode
Useful links
Resources
Review these resources.
About Linux.
- If you feel the need of learning more about Linux (commands, tools, bash scripting and so on) there is a Linux learning object available.
About a production server.
- In the book "Web development with Node and Express" chapter 21, 22.
- Hands-on tips about how to configure Node.js and Express.js for production
- Additional tips on how to setup Node/Express server.
Optional.
- Focusing on the bigger picture of taking code to production.