how to deploy a MERN environment that is composed of Ubuntu 22.04 servers providing users access to the application through an Nginx reverse proxy to the application server in aws ec2.
- Log in to the AWS console
- Go to instances
- Click Launch Instances
- Add your instance name
- Select ubuntu server
- Select instance type t2.micro
- In network settings add following security group rules
Type | Port range | Source type | Source |
---|---|---|---|
SSH | 22 | Anywhere | 0.0.0.0/0 |
HTTP | 80 | Anywhere | 0.0.0.0/0 and ::0 |
HTTPS | 443 | Anywhere | 0.0.0.0/0 |
Custom TCP | 5000 (Node server port) | Anywhere | 0.0.0.0/0 |
- Create new keypair with .pem and save it on your computer
- Launch your instance
-
Go to the .pem file location and open terminal
-
Replace your Public IPv4 DNS
ssh -i visAct.pem ubuntu@Public_IPv4_DNS
-
Run system update
sudo apt-get update
-
install curl
sudo apt install curl -y
-
Check available Node.js Version
sudo apt policy nodejs
-
Add Nodejs latest repo on Ec2 Ubuntu
curl -fsSL https://deb.nodesource.com/setup_lts.x | sudo -E bash -
-
Install Nodejs & NPM
sudo apt-get install nodejs
-
Check the installed version of node
node -v
-
Check the installed version of npm
npm -v
-
Install nginx
sudo apt-get install nginx
-
Start nginx
sudo service nginx start
-
Check nginx status
sudo service nginx status
-
Exit nginx status
q
-
Ensure every time you restart your system Nginx starts up automatically.
sudo systemctl enable nginx
-
Run system update
sudo apt-get update
-
Install the editor
sudo apt-get install nano
-
Run system update
sudo apt-get update
-
install pm2
sudo npm install pm2 -g
-
Configure PM2 to start Express application at startup.
sudo env PATH=$PATH:/usr/local/bin pm2 startup -u ubuntu
-
Run system update
sudo apt-get update
-
Clone from git hub
git clone <url>
-
Input your github account name
-
Input your password
-
Navigate to client folder
cd client_folder_path
-
Install depencies
sudo npm install
-
Build the app
sudo npm run build
-
Navigate to server folder
cd server_folder_path
-
Install depencies
sudo npm install
-
Create .env file ,add credentials and save it
sudo nano .env
-
Run the app with pm2
pm2 start app.js
-
Freeze a process list on reboot
pm2 save
-
Display all processes logs in streaming
pm2 logs
-
Remove default file to sites-available path
sudo rm /etc/nginx/sites-available/default
-
Add default file to sites-available path
sudo nano /etc/nginx/sites-available/default
-
Add the code for server block
listen 80 default_server; server_name yourdomain.com www.yourdomain.com; location / { proxy_pass http://private_ip_of_server:5000; 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; }
-
Test configuration
sudo nginx -t
-
Reload Nginx
sudo /etc/init.d/nginx reload