Guide - How to set up a nodejs application on ubuntu 16?
Introduction
This guide is a recollection of knowledge shared by others, this is intended to make the guide easier for myself:
The following frameworks, technologies, and services were used:
- node 14.16.1
- digital ocean
- ubuntu 16
You will need to have the following prepared:
- A domain name pointed at your server’s public IP
Install nodejs
How To Install Node.js on Ubuntu 16.04
- Refresh our local package index first
sudo apt-get update
- Install from the repositories
sudo apt-get install nodejs
- Install npm
sudo apt-get install npm
- Check node is installed
nodejs -v
Optional: you could also install it using nvm for better nodejs version management
Install Nginx
How to install nginx on ubuntu 16.04
- Refresh our local package index first
sudo apt-get update
- Install from the repositories
sudo apt-get install nginx
- Adjust the firewall
sudo ufw app list
- Check status
sudo ufw status
- Allow https traffic
sudo ufw allow 'Nginx Full'
- Check your web server
systemctl status nginx
Some useful commands
- To stop your web server
sudo systemctl stop nginx
- To start your web server
sudo systemctl start nginx
- To stop and start your web server
sudo systemctl restart nginx
- If you are simply making configuration changes, Nginx can often reload without dropping connections
sudo systemctl reload nginx
Install certbot
How To Secure Nginx with Let's Encrypt on Ubuntu 16.04
- Add repository
sudo add-apt-repository ppa:certbot/certbot
- Update package information
sudo apt-get update
- Install certbot nginx package
sudo apt-get install python-certbot-nginx
- Setup nginx
Certbot can automatically configure SSL for Nginx, but it needs to be able to find the correct server block in your config. It does this by looking for a server_name directive that matches the domain you’re requesting a certificate for.
sudo nano /etc/nginx/sites-available/default
- Check for mistakes on nginx config
sudo nginx -t
- Reload nginx config
sudo systemctl reload nginx
- Obtaining a ssl certificate. 'example.com' and 'www.example.com' should be substituted for
your website
sudo certbot --nginx -d example.com -d www.example.com
Output
Please choose whether or not to redirect HTTP traffic to HTTPS, removing HTTP access.
-------------------------------------------------------------------------------
1: No redirect - Make no further changes to the webserver configuration.
2: Redirect - Make all requests redirect to secure HTTPS access. Choose this for
new sites, or if you're confident your site works on HTTPS. You can undo this
change by editing your web server's configuration.
-------------------------------------------------------------------------------
Select the appropriate number [1-2] then [enter] (press 'c' to cancel):
`
*redirect makes changes to the 'default' file in nginx config*
- Verifying Certbot Auto-Renewal
sudo certbot renew --dry-run
If you see no errors, you’re all set. When necessary, Certbot will renew your certificates and reload Nginx to pick up the changes. If the automated renewal process ever fails, Let’s Encrypt will send a message to the email you specified, warning you when your certificate is about to expire.
Install pm2
- Create a pm2 process. Substitute "{app_name}" with your preferred process name
pm2 start npm --name "{app_name}" -- run {script_name}
- Stop process. Substitute "{app_name}" with your preferred process name
pm2 stop "{app_name}"
- Start process. Substitute "{app_name}" with your preferred process name
pm2 start "{app_name}"
Set up nginx as a reverse proxy server
- Access /etc/nginx/sites-available/default
sudo nano /etc/nginx/sites-available/default
- Configure file to make it a proxy server
}
location / {
proxy_pass http://localhost:8080;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
}
This configures the server to respond to requests at its root. Assuming our server is available at example.com, accessing https://example.com/ via a web browser would send the request to hello.js, listening on port 8080 at localhost.
- Check configuration changes
sudo nginx -t
- Restart nginx
sudo systemctl restart nginx
OPTIONAL
Mysql config
How do I set the time zone of MySQL? (stack overflow)
Because of how mysql and typeorm change to a date native object. Dates strings get converted into a timezone date string and data may be incorrect. Some other precautions may be needed like changing how typeorm store and retrieve DATE_TIME values. (see nestjs snippets)
In this case, we are setting the timezone to the one Mexico uses.
[mysqld]
!includedir /etc/mysql/conf.d/
!includedir /etc/mysql/mysql.conf.d/
default-time-zone = "-05:00"
Restart mysql service
Services
sudo service mysql restart
[Unit]
Description=Grupo inopack server application
After=network.target
[Service]
Restart=always
AmbientCapabilities=CAP_SYS_RAWIO
User=root
Type=simple
WorkingDirectory=/root/nestjs-inopack
Environment=NODE_PORT=3000
ExecStart=/usr/bin/npm run start:prod --prefix /root/nestjs-inopack
[Install]
WantedBy=multi-user.target
Alias=inopack.service