aws EC2 ubuntu server
nodejs-ssl-server
Section titled “nodejs-ssl-server”How to deploy nodejs app to AWS EC2 Ubuntu 22 Server with free SSL and Nginx reverse proxy
Installation instructions
Section titled “Installation instructions”1. Launch amazon ubuntu server in aws + Attach Elastic IP to the new instance
Section titled “1. Launch amazon ubuntu server in aws + Attach Elastic IP to the new instance”2. ssh to ubuntu to install packages
Section titled “2. ssh to ubuntu to install packages”ssh -i <key.pem> ubuntu@<ip-address> -v3. Update and Upgrade linux machine and install node and nvm
Section titled “3. Update and Upgrade linux machine and install node and nvm”sudo apt updatesudo apt upgradesudo apt install -y git htop wget3.1 install node
Section titled “3.1 install node”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 | 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).
3.2 Copy & Past (each line separately)
Section titled “3.2 Copy & Past (each line separately)”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_completion3.3 Verify that nvm has been installed
Section titled “3.3 Verify that nvm has been installed”nvm --version3.4 Install node
Section titled “3.4 Install node”nvm install --lts # Latest stable node js server version3.5 Check nodejs installed
Section titled “3.5 Check nodejs installed”node --version3.6 Check npm installed
Section titled “3.6 Check npm installed”npm -v4. Clone nodejs-ssl-server repository
Section titled “4. Clone nodejs-ssl-server repository”cd /home/ubuntugit clone https://github.com/saasscaleup/nodejs-ssl-server.git5. Run node app.js (Make sure everything working)
Section titled “5. Run node app.js (Make sure everything working)”cd nodejs-ssl-servernpm installnode app.js6. Install pm2
Section titled “6. Install pm2”npm install -g pm2 # may require sudo7. Starting the app with pm2 (Run nodejs in background and when server restart)
Section titled “7. Starting the app with pm2 (Run nodejs in background and when server restart)”pm2 start app.js --name=nodejs-ssl-serverpm2 save # saves the running processes # if not saved, pm2 will forget # the running apps on next boot7.1 IMPORTANT: If you want pm2 to start on system boot
Section titled “7.1 IMPORTANT: If you want pm2 to start on system boot”pm2 startup # starts pm2 on computer boot8. FREE SSL - Install Nginx web server
Section titled “8. FREE SSL - Install Nginx web server”sudo apt install nginxsudo nano /etc/nginx/sites-available/defaultAdd the following to the location part of the server block
Section titled “Add the following to the location part of the server block”server { server_name op1.evolo.fr;
location / { root /var/www/html; index index.html index.htm index.nginx-debian.html; }
location /op1/ { proxy_pass http://localhost:3000/; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; }
listen [::]:443 ssl ipv6only=on; # managed by Certbot listen 443 ssl; # managed by Certbot ssl_certificate /etc/letsencrypt/live/op1.evolo.fr/fullchain.pem; # managed by Certbot ssl_certificate_key /etc/letsencrypt/live/op1.evolo.fr/privkey.pem; # managed by Certbot include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}Check NGINX config
Section titled “Check NGINX config”sudo nginx -tRestart NGINX
Section titled “Restart NGINX”sudo 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
Section titled “You should now be able to visit your IP with no port (port 80) and see your app. Now let’s add a domain”9 Add domain in goDaddy.com
Section titled “9 Add domain in goDaddy.com”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
9.1 Check that Port 80 redirect to Nodejs server
Section titled “9.1 Check that Port 80 redirect to Nodejs server”10 Installing Free SSL
Section titled “10 Installing Free SSL”10.1 Installing Certbot
Section titled “10.1 Installing Certbot”sudo snap install core; sudo snap refresh coresudo apt remove certbotsudo snap install --classic certbotsudo ln -s /snap/bin/certbot /usr/bin/certbot10.2 Confirming Nginx’s Configuration
Section titled “10.2 Confirming Nginx’s Configuration”sudo nano /etc/nginx/sites-available/defaultlet edit this line:
...server_name example.com www.example.com;...sudo nginx -tsudo systemctl reload nginx10.3 Obtaining an FREE SSL Certificate
Section titled “10.3 Obtaining an FREE SSL Certificate”sudo certbot --nginx -d app.example.comOutput:
IMPORTANT NOTES:Successfully received certificate.Certificate is saved at: /etc/letsencrypt/live/your_domain/fullchain.pemKey is saved at: /etc/letsencrypt/live/your_domain/privkey.pemThis 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-le10.4 Verifying Certbot Auto-Renewal
Section titled “10.4 Verifying Certbot Auto-Renewal”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.timerTo test the renewal process, you can do a dry run with certbot:
sudo certbot renew --dry-run11. install Webmin
Section titled “11. install Webmin”webmin username = root
How do I change my Webmin password if I can’t login? If you installed Webmin using package manager (i.e. rpm or deb) use the following command to change Webmin user password:
webmin passwd username
12. install Rsync
Section titled “12. install Rsync”rsync -avz --exclude 'node_modules' --exclude '.git' --exclude '.env' \-e "ssh -i ~/.ssh/your-key.pem" \. ubuntu@ip-address:~/appSupport 🙏😃
Section titled “Support 🙏😃”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 :)