- OS Ubuntu (t2.micro, or any that is eligible for free tier)
- Create a new key for SSH (.pem)
- Set up the memory space for the volume
- Create instance
- Head to Elastic IPs under the Network & Security Group
- Associate an Elastic IP with the newly created EC2 instance
- Run the following commands:
chmod 400 <path-to-pem-file>/<file-name>.pem
ssh -i <path-to-pem-file>/<file-name>.pem ubuntu@<elastic-ip>
- Trust the fingerprint and add the EC2 Instance into the list of known hosts
- Run the following commands:
sudo apt update
sudo apt upgrade
sudo apt install -y git htop
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"
- Verify that nvm has been installed:
nvm --version
nvm install --lts
- Verify that node has been installed:
node --version
- Verify that npm has been installed:
npm -v
- Create a public key (within EC2 Instance)
ssh-keygen #Press enter thrice
- Retrieve public key (within EC2 Instance)
cat ~/.ssh/id_rsa.pub # Please becareful to not share this with anyone
- Navigate to GitHub
- Click on your profile photo -> Settings -> SSH and GPG keys (Under Access)
- Click on "NEW SSH key"
- Choose a title as the identifier for the connection between Git and the EC2 Instance
- Paste the key from 4 under "Key"
- Click on "Add SSH Key" (Complete MFA if needed)
- You should be able to clone the repository!
git clone [email protected]:syongkheng/expressjs-ec2-guide.git
- Build the typescript files into javscript files
npm run build
- The web application can be deployed with
npm run dev
- The application should now be accessible at http://:3000
- This simulates a "dev" envrionment and the terminal running the process has to be kept alive.
- Persisting the application with pm2:
npm install pm2
pm2 start dist/src/index.js --name=expressjs-server
pm2 save
pm2 startup # Execute the command as prompted
- The web application should still be accessible even after exiting the terminal
- Optional: You can run npm run migration which will create a dev account username:
dev
, password:secret
sudo apt install nginx
sudo nano /etc/nginx/sites-available/default
- Add the following to the location {} and server_name
server_name <subdomain>.<domain>.<tld>
location / {
proxy_pass http://localhost:3000;
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;
}
- Ensure that you change the default path of /var/www/html to /home/ubuntu/expressjs-ec2-guide
- Check the config
sudo nginx -t
- Restart nginx
sudo service nginx restart
- Add a "A" record that points to the EC2 Instance Public IPV4, the same IP that you use to ssh.
sudo snap install core; sudo snap refresh core
sudo apt remove certbot
sudo snap install --classic certbot
- Adding certbot to path:
sudo ln -s /snap/bin/certbot /usr/bin/certbot
sudo certbot --nginx -d <subdomain>.<domain>.<tld>
sudo systemctl status snap.certbot.renew.service
sudo certbot renew --dry-run
- The server should be accessible with HTTPS now!