How to deploy nodejs app to AWS EC2 Ubuntu 22 Server with free SSL and Nginx reverse proxy
- In EC2 instance connect: you can choice your username ubuntu or as administrator
(root)
- In SSH instance connect: open terminal and write this command
ssh -i <key.pem> ubuntu@<ip-address> -v
sudo apt update && sudo apt upgrade
git --version
- if git version under
2.42.0
then you can update it by running
apt install git
curl -sL https://deb.nodesource.com/setup_18.x | bash -
apt-get install -y nodejs
node --version
npm --version
- for upload your project you can user clone the repository from github
git clone https://github.com/yourUsername/yourProject.git
- install package dependencies for your project
npm install
- test run
node index.js
- then you can take ip address and run it in your browser
npm install -g pm2
pm2 start index.js
pm2 save
pm2 startup
sudo apt install nginx
Delete the default config
rm /etc/nginx/sites-available/default
rm /etc/nginx/sites-enabled/default
sudo nano /etc/nginx/sites-available/project_name
Add the following to the location part of the server block
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/html;
index index.html index.htm index.nginx-debian.html;
client_max_body_size 100M;
server_name supdomain.domian.com;
add_header X-XSS-Protection "1; mode=block";
add_header X-Content-Type-Options "nosniff";
location /socket.io/ {
proxy_pass http://localhost:9000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
proxy_set_header Host $host;
}
location /api {
proxy_pass http://localhost:9000$request_uri;
proxy_http_version 1.1;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header Referer $http_referer;
proxy_set_header Connection "";
proxy_set_header Content-Type $content_type;
proxy_set_header Accept-Encoding "";
}
# Correctly map the PDF file location
location /v0/public/Invoice/ {
alias /root/production/public/Invoice/;
autoindex on;
allow all;
add_header Content-Disposition "attachment";
add_header X-Content-Type-Options nosniff;
# Adjust other headers as needed
add_header X-Frame-Options "SAMEORIGIN";
add_header X-XSS-Protection "1; mode=block";
add_header X-Content-Type-Options "nosniff";
add_header Referrer-Policy "strict-origin-when-cross-origin";
}
location /v2/socket.io/ {
proxy_pass http://localhost:8000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
proxy_set_header Host $host;
}
location /v2/api {
proxy_pass http://localhost:8000$request_uri;
proxy_http_version 1.1;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header Referer $http_referer;
proxy_set_header Connection "";
proxy_set_header Content-Type $content_type;
proxy_set_header Accept-Encoding "";
}
}
create site-available and site-enabled to let any change make in both
ln -s /etc/nginx/sites-available/project_name /etc/nginx/sites-enabled/project_name
Check NGINX config
sudo nginx -t
systemctl start nginx
Restart NGINX
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