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> -vsudo apt updatesudo apt upgradesudo apt install -y git htop wgetTo 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 | bashOr
wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bashRunning 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_completionnvm --versionnvm install --lts # Latest stable node js server versionnode --versionnpm -vcd /home/ubuntugit clone https://github.com/saasscaleup/nodejs-ssl-server.gitcd nodejs-ssl-servernpm installnode app.jsnpm install -g pm2 # may require sudopm2 start app.js --name=nodejs-ssl-serverpm2 save # saves the running processes
# if not saved, pm2 will forget
# the running apps on next bootpm2 startup # starts pm2 on computer bootsudo apt install nginxsudo nano /etc/nginx/sites-available/default server_name yourdomain.com www.yourdomain.com;
location / {
proxy_pass http://localhost:5000; #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 -tsudo service nginx restartYou should now be able to visit your IP with no port (port 80) and see your app. Now let's add a domain
If you have domain, you can add A record to your EC2 instance IP with a new subdomain as I'm going to show you next
sudo snap install core; sudo snap refresh coresudo apt remove certbotsudo snap install --classic certbotsudo ln -s /snap/bin/certbot /usr/bin/certbotsudo nano /etc/nginx/sites-available/defaultlet edit this line:
...
server_name example.com www.example.com;
...sudo nginx -tsudo systemctl reload nginxsudo 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.serviceOutput:
○ 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-runEnjoy Your free Nodejs server with Free SSL :)
If you Like the tutorial and you want to support my channel so I will keep releasing amzing content that will turn you to a desirable Developer with Amazing Cloud skills... I will realy appricite if you:
- Subscribe to My youtube channel and leave a comment: http://www.youtube.com/@ScaleUpSaaS?sub_confirmation=1
- Buy me A coffee ❤️ : https://www.buymeacoffee.com/scaleupsaas
Thanks for your support :)