Skip to content

Instantly share code, notes, and snippets.

@walosha
Last active October 2, 2023 22:15
Show Gist options
  • Save walosha/31cd6ab71f2b639421ac2b1291d162d7 to your computer and use it in GitHub Desktop.
Save walosha/31cd6ab71f2b639421ac2b1291d162d7 to your computer and use it in GitHub Desktop.
Hosting backend on EC2 with Github actions

Create AWS EC2 Instance

  • Go to the AWS Management Console. Launch a new EC2 instance.

  • Set security rules:

    • Click on "Security" in the EC2 dashboard.
    • Click "Security Groups."
    • Select your security group and click "Edit Inbound Rules."
    • Click "Add Rules."
    • Set the type to "Custom TCP."
    • Set the port range to '80.'
    • Set the source to '0.0.0.0.'
    • Save the rules.
    • Check the EC2 instance and click "Connect."
  • Click "SSH Client" and run the SSH command with sudo to connect to the EC2 instance.

  • Set up GitHub Actions Runner

  • On GitHub:

    • Go to your repository settings.
    • Click on "Actions" and then "Runner."
  • Follow these instructions to install GitHub Actions on your EC2 instance:

SSH into your EC2 instance. Run the following commands on the EC2 instance:

sudo apt update
sudo apt install nodejs npm nginx
sudo npm install -g pm2
sudo ./svc.sh install
sudo ./svc.sh start
cd _work  # Move to that directory
sudo pm2 start dist/main.js --name=<app-name>
sudo pm2 restart <app-name>
cd etc/nginx/sites-available
sudo nano default
  • Add the following code to the Nginx configuration: nginx
location /api/ {
    proxy_pass  http://localhost:3000/;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}

Restart Nginx:

  sudo systemctl restart nginx

Voila, your GitHub Actions runner should now be set up on your EC2 instance!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment