21 Sep 2024
Setup EC2 with Debian OS
Update and Upgrade Packages
sudo apt update
sudo apt upgrade
Install Node.js
Install PM2 Globally
sudo npm install pm2 -g
Install MongoDB
Allow MongoDB Compass Access
sudo nano /etc/mongod.conf
Save the MongoDB Configuration
Ctrl + X
, Y
, Enter
Restart MongoDB
sudo systemctl restart mongod
Check MongoDB status:
sudo systemctl status mongod
Check MongoDB Compass Login
mongodb://ec2-ip:27017
Install Nginx
sudo apt install nginx
Change Ownership for /var/www Directory
sudo chown admin /var/www
to allow file permissions.Setup Nginx Reverse Proxy
sudo nano /etc/nginx/sites-available/default
Nginx Configuration Example
server { listen 80; server_name _; location / { proxy_pass http://localhost:8080; 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; } }
Save the Nginx Configuration
Ctrl + X
, Y
, Enter
Restart Nginx
sudo systemctl restart nginx
Check Nginx status:
sudo systemctl status nginx
Get SSL on Nginx
Install Certbot and Nginx plugin:
sudo apt install certbot python3-certbot-nginx
Obtain SSL certificates:
sudo certbot --nginx -d demo.com -d www.demo.com
Test SSL certificate renewal:
sudo certbot renew --dry-run
Handle SSL Errors (if any)
sudo nano /etc/nginx/sites-available/default
Nginx SSL Configuration Example
server { listen 80; server_name demo.com www.demo.com; location / { proxy_pass http://localhost:8080; 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; } # Redirect www to non-www if ($host = 'www.demo.com') { return 301 http://demo.com$request_uri; } # Other necessary configurations }
Restart Nginx and Check Status
sudo systemctl restart nginx
sudo systemctl status nginx