- Create an AWS EC2 Instance with Ubuntu AMI.
- SSH into the EC2 instance.
- Install fnm :
curl -fsSL https://fnm.vercel.app/install | bash - Select a Node version to install. For example :
fnm use 20(Will install Node v20) - Install pnpm:
npm i -g pnpmand setup :pnpm setup - Clone your backend into EC2.
- Install
pm2:pnpm i -g pm2. - Install project depedencies:
pnpm install --frozen-lockfile - Put Environment Variables in
.env. - If you have a domain for the API, go to manage DNS records and create an A record with value as the EC2 Public IP.
- Install Nginx to use as a Reverse Proxy:
sudo apt install nginx. Ensure it is running :sudo systemctl status nginx. - Now, cd into
etc/nginx/sites-available. Create a nginx configuration file which routes incoming HTTP traffic for server{YOUR_API_DOMAIN}to our express app running atlocalhost:{PORT}. Example Nginx Config:
server {
listen 80;
server_name {YOUR_API_DOMAIN};
location / {
proxy_pass http://localhost:{PORT};
}
}- You can name the config file as anything but it is good practice to name it same as your domain. For ex, the config file could be named
api.guidancegrid.gdscnits.in. Create a symlink for this file in/etc/nginx/sites-enabled. (Only configs in sites-enabled will be used by nginx).
sudo ln -s /etc/nginx/sites-available/{YOUR_CONFIG_FILE_NAME} /etc/nginx/sites-enabled/- Test if the nginx config is valid sudo nginx -t. If there are no errors, restart nginx :
sudo systemctl restart nginx. - Check if the endpoint works without specifying a PORT now: http://{YOUR_API_DOMAIN}/api/v1.
- To set up SSL certificates, we will use letsencrypt. Install
certbotandpython3-certbot-nginx.
sudo apt install certbot
sudo apt install python3-certbot-nginx-
Request an SSL certificate for your registered domain. The certbot-nginx plugin will automatically configure the certificate for your domain.
sudo certbot --nginx -d {YOUR_API_DOMAIN}It will ask for email. Enter it and accept all terms and conditions. -
If there were no errors, you should see a message that tells you that SSL certificate has been installed successfully on your domain.
-
Go to
https://{YOUR_API_DOMAIN}/api/v1to check if the endpoint is working.