Thursday, 23 March 2023

Deployment Node.js Nuxt App with Nginx, SSL with Lets Encrypt

1. Access your Vps

ssh user@ip

2.Install nodejs, npm, yarn

sudo apt update

# select node version
curl -sL https://deb.nodesource.com/setup_14.x | sudo -E bash -

# install nodejs
sudo apt install nodejs

# check nodejs
node --version
npm --version

# install yarn
npm install -g yarn

3. Clone, build and run Nuxt app

git clone [your-url-project-on-github]
cd your-project
yarn install
yarn build
yarn start

4. Run Nuxt app using pm2 (process manager for Node.js)

- create file ecosystem.config.js in root Nuxt app

# create file
touch ecosystem.config.js
# with content
module.exports = {
apps: [
{
name: 'NuxtAppName',
exec_mode: 'cluster',
instances: 'max', // Or a number of instances
script: './node_modules/nuxt/bin/nuxt.js',
args: 'start'
}
]
}

- Build and run Nuxt app using pm2

# intall pm2
npm install pm2 -g

# run nuxt app
pm2 start
 
# check pm2
pm2 ls
pm2 logs (Show log stream)
pm2 flush (Clear logs)  
 
# stop nuxt app
pm2 stop [app_name_or_id]
 
# delete all process managed
pm2 delete all
 

- To make sure app starts when reboot

pm2 startup ubuntu
# test reboot server
reboot

5. Setup ufw firewall

# setup ufw
sudo ufw enable
sudo ufw allow http
sudo ufw allow https
sudo ufw allow 'Nginx Full'
sudo ufw delete allow 'Nginx HTTP'
# check ufw status
sudo ufw status

# output:
sudo ufw allow ssh (Port 22)
sudo ufw allow http (Port 80)
sudo ufw allow https (Port 443)

6. Install NGINX and configure

- Install nginx

# install nginx
sudo apt update
sudo apt install nginx

# check nginx working
sudo systemctl status nginx
sudo systemctl enable nginx
sudo systemctl stop nginx
sudo systemctl restart nginx

- Errors (if happen): unknow host Ubuntu-20.04

# open /ect/host
sudo nano /etc/hosts

# update line become
127.0.0.1 Ubuntu-20.04 localhost

- Set Up Nginx Server Blocks (Virtual Hosts) for example.com

sudo cp /etc/nginx/sites-available/default /etc/nginx/sites-available/example.com
sudo nano /etc/nginx/sites-available/example.com

- Config for /etc/nginx/sites-available/example.com

server {
listen 80 default_server;
listen [::]:80
default_server;
 
# Add the following to the location part of the server block
server_name example.com www.example.com;

location / {
proxy_pass http://localhost:3000; #whatever port your app runs on
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;
}

* Now that we have our server block file, we need to enable it. We can do this by creating symbolic link from this file to the sites-enabled directory, which Nginx reads from during startup.

sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/  
# to delete symbolic link
cd /etc/nginx/sites-enabled/
rm site_want_delete

- Check nginx config status

# Check NGINX config
sudo nginx -t

# Restart NGINX
sudo service nginx restart

7. Add record for domain

Host Type Value TTL
  @
 A
 your-ip-vps  3600 
 www 
 CNAME 
 example.com 
 3600

8. Add SSL with LetsEncrypt

sudo apt install certbot python3-certbot-nginx
sudo certbot --nginx -d example.com -d www.example.com
 
# check certbot auto renew  
sudo systemctl status certbot.timer  
 
# or manually renew ssl (90 day will expired)
sudo certbot renew --dry-run 

Thank you.


No comments:

Post a Comment

Golang Advanced Interview Q&A