How to deploy nodejs app to AWS EC2 Ubuntu 22 Server with free SSL and Nginx reverse proxy
ssh -i <key.pem> ubuntu@<ip-address> -v
sudo apt update
sudo apt upgrade
sudo apt install -y git htop wget
To install or update nvm, you should run the [install script][2]. To do that, you may either download and run the script manually, or use the following cURL or Wget command:
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash
Or
wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash
Running either of the above commands downloads a script and runs it. The script clones the nvm repository to ~/.nvm
, and attempts to add the source lines from the snippet below to the correct profile file (~/.bash_profile
, ~/.zshrc
, ~/.profile
, or ~/.bashrc
).
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion" # This loads nvm bash_completion
nvm --version
nvm install --lts # Latest stable node js server version
node --version
npm -v
cd /home/ubuntu
git clone https://github.com/Shaharyar07/Dummy-Server-Node-Express.git
cd Dummy-Server-Node-Express
npm install
node index.js
npm install -g pm2 # may require sudo
pm2 start index.js --name=nodejs-ssl-server
pm2 save # saves the running processes
# if not saved, pm2 will forget
# the running apps on next boot
pm2 startup # starts pm2 on computer boot
sudo apt install nginx
sudo nano /etc/nginx/sites-available/default
server_name yourdomain.com www.yourdomain.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;
}
sudo nginx -t
sudo service nginx restart
You should now be able to visit your IP with no port (port 80) and see your app. Now let's add a domain
sudo snap install core; sudo snap refresh core
sudo apt remove certbot
sudo snap install --classic certbot
sudo ln -s /snap/bin/certbot /usr/bin/certbot
sudo nano /etc/nginx/sites-available/default
let edit this line:
...
server_name example.com www.example.com;
...
sudo nginx -t
sudo systemctl reload nginx
sudo certbot --nginx -d app.example.com
Output:
IMPORTANT NOTES:
Successfully received certificate.
Certificate is saved at: /etc/letsencrypt/live/your_domain/fullchain.pem
Key is saved at: /etc/letsencrypt/live/your_domain/privkey.pem
This certificate expires on 2022-06-01.
These files will be updated when the certificate renews.
Certbot has set up a scheduled task to automatically renew this certificate in the background.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
If you like Certbot, please consider supporting our work by:
* Donating to ISRG / Let's Encrypt: https://letsencrypt.org/donate
* Donating to EFF: https://eff.org/donate-le
sudo systemctl status snap.certbot.renew.service
Output:
○ snap.certbot.renew.service - Service for snap application certbot.renew
Loaded: loaded (/etc/systemd/system/snap.certbot.renew.service; static)
Active: inactive (dead)
TriggeredBy: ● snap.certbot.renew.timer
To test the renewal process, you can do a dry run with certbot:
sudo certbot renew --dry-run
Enjoy Your free Nodejs server with Free SSL :)
:)