Published on July 26, 2023
Deploying an Angular Universal application on Ubuntu 22 LTS with an Nginx server involves multiple stages. Here's a comprehensive, step-by-step tutorial to assist you throughout the process.
Regularly updating and upgrading your Ubuntu system is essential to ensure it stays secure and benefits from the latest features and Enhancements. Within the command-line interface, there are two essential commands.
Please run the subsequent commands to update and upgrade Ubuntu:
apt update
apt dist-upgrade
sudo reboot now
this step is optional. but it is always recommended if you want other people working for you on the server without doing everything on the server.
Run below commands to create a new non-root user with administrative access. Upon granting administrative access, you will be prompted to provide certain information.
adduser #username#
usermod -aG sudo #username#
Nginx is a high-performance web server and a reverse proxy, Nginx is renowned for its exceptional efficiency in handling concurrent connections and ability to efficiently serve static content makes Nginx a favored option for hosting websites and applications. Upon executing this command, you establish Nginx on your system, fully prepared to serve web pages and manage incoming HTTP requests. This ensures a seamless and responsive web experience for your users.
Run below command to install Nginx.
apt install nginx
UFW (Uncomplicated Firewall) is a user-friendly interface for managing firewall rules on Ubuntu and UFW simplifies firewall configuration and control, making it accessible even for novice users to establish effective network protection. By defining rules with UFW, you can effortlessly control traffic flow, strengthening your system's resilience against potential attacks and unauthorized access attempts.
Execute this command to list down predefined application profiles available in Uncomplicated Firewall (UFW).
ufw app list
Create a rule to permit traffic for both 'OpenSSH' and 'Nginx Full' application profiles through the Uncomplicated Firewall (UFW).
Run below commands to add a rule.
ufw allow OpenSSH
ufw allow 'Nginx Full'
Execute the following command to enable the Uncomplicated Firewall (UFW) on your Ubuntu system.
ufw enable
To verify the status of the UFW on your Ubuntu system.
ufw status
The output of "ufw status" command:
Ubuntu 22.04 comes with support for snaps, so make sure your snapd core is up to date:
sudo snap install core
sudo snap refresh core
Execute the following command to remove the older version of Certbot installed.
sudo apt remove certbot
Install the Certbot package.
sudo snap install --classic certbot
Lastly, you have the option to create a symbolic link for the certbot command from the snap installation directory to your system's path. By adding the Certbot package to the system's path, you gain the convenience of running Certbot effortlessly by typing 'certbot' in the terminal.
sudo ln -s /snap/bin/certbot /usr/bin/certbot
Go to the directory where Nginx configurations are located.
cd /etc/nginx/sites-available
Create a new project-specific Nginx configuration file for your website.
sudo vi your_website_name
press "i" to edit file.
To save. Press "Esc" then type ":wq" and press "Enter".
Create a symbolic link with the sites-enabled config file.
sudo ln -s /etc/nginx/sites-available/your_website_name /etc/nginx/sites-enabled/
Test the Nginx configuration for any syntax errors.
sudo nginx -t
If there are no errors, reload Nginx to apply the changes.
sudo systemctl reload nginx
To support https and let in HTTPS traffic, allow the Nginx Full profile. Follow Step 3.
Replace mywebsite123.com with a valid domain name.
sudo certbot --nginx -d mywebsite123.com -d www.mywebsite123.com
You can query the status of the timer with systemctl:
sudo systemctl status snap.certbot.renew.service
To test the renewal process, you can do a dry run with certbot:
sudo certbot renew --dry-run
To install NodeJS latest LTS Version: 18.17.0 (includes npm 9.6.7) run below command.
curl -fsSL https://deb.nodesource.com/setup_18.x | sudo -E bash -
sudo apt-get install -y nodejs
To check NodeJs and npm versions run below commands.
node -v
npm -v
Install pm2 run below command
PM2 (Process Manager 2) allows you to manage, monitor, and keep your Node.js applications running continuously, even after server reboots or crashes.
sudo npm install -g pm2
Change the output path for browser folder in "src/server.ts" file.
const distFolder = join(process.cwd(), 'browser');
Run below command to create a production build.
npm run build:ssr
On an ubuntu machine, the folder directory will be "/var/www/"
Create a new folder with the project name. (Note: you can create folder with any name.)
Upload browser folder and main.js/server.js file inside this folder. The folder structure will look like this.
the file content should look like as shown below:
This Nginx configuration block is designed to handle HTTPS requests for the domain "mywebsite123.com" and "www.mywebsite123.com" and perform the following tasks:
Run below command from /var/www/workspace/ location.
pm2 start main.js
service nginx restart
systemctl reload nginx
service nginx status
This should be able to run the website on https;//www.mywebsite123.com.
Happy coding!