Hosting Backend Server on AWS EC2 - SimPPL/arbiter-documentation GitHub Wiki

Repository Link - https://github.com/SimPPL/arbiter-backend

Launching an EC2 Instance on AWS

  1. Log into AWS
  2. Search for EC2 from your AWS dashboard
  3. Click on Launch Instance
  4. Select the latest Ubuntu Image
  5. Set the instance type to t2.micro (You can use more powerful instance if required)
  6. Select an existing key pair or create a new one (You will need this to ssh into the instance later)
  7. Click on Launch Instance

The instance is successfully created.


Assigning IP Address

  1. Head to your AWS Console
  2. Search for Elastic IPs
  3. Click on Allocate Elastic IP address on the top right & follow the prompts
  4. On the Elastic IPs console select the IP address that was allocated to you in the above step
  5. Click on the Actions button on the left of Allocate Elastic IP address button
  6. Select the Associate Elastic IP address option
  7. Follow the prompts and assign the IP address to the EC2 instance created in the above steps

The allocated IP address will help in making the backend server accessible via the internet.


Connecting to the EC2 Instance

  1. Go to your EC2 dashboard
  2. Click on instances (running)
  3. Select the instance you want to connect to
  4. Press connect
  5. Select ssh client option
  6. Copy the ssh cmd to your terminal which will look something like this ssh -i key_pair_file user@host
  7. Replace the key_pair_file with the path to the actual key_pair_file on your computer
  8. Press enter

Now you will be connected to the EC2 instance.


Setting up the EC2 Instance

  1. Connect to the EC2 instance using your terminal
  2. Run the cmd
    sudo apt update && sudo apt -y upgrade
    
  3. Press enter if any prompts pop up
  4. This cmd updated the entire system
  5. Install nginx using the cmd (Press enter if any prompts pop up)
    sudo apt install -y nginx
    
  6. Install supervisor via the cmd (Press enter if any prompts pop up)
    sudo apt install -y supervisor
    
  7. Run the cmd (Press enter if any prompts pop up)
    sudo apt install -y python3-venv
    
  8. Create a virtual environment using the cmd
    python3 -m venv env
    
  9. Activate the virtual environment with the cmd
    source env/bin/activate
    
  10. Install gunicorn by running the cmd
    pip install gunicorn
    

Our EC2 instance is all setup and ready to host our backend server.


Setting up the SSH Key

  1. Connect to the EC2 instance using your terminal

  2. We need to first create a public private key pair for enabling ssh so as to pull new changes from GitHub as our repository is private. To do this:

    1. On EC2 run the ssh-keygen cmd to generate new key (Replace [email protected] with your own email used on GitHub)
      ssh-keygen -t rsa -b 4096 -C "[email protected]"
      
    2. The above cmd will prompt you to enter a name for the key, press enter and let it be the default name. Then you will get a prompt to keep a passphrase, keep it empty
      ubuntu@ip1-2-3-4:~$ ssh-keygen -t rsa -b 4096 -C "[email protected]"
      Generating public/private rsa key pair.
      Enter file in which to save the key (/home/ubuntu/.ssh/id_rsa): 
      Enter passphrase (empty for no passphrase): 
      Enter same passphrase again: 
      
    3. Start the ssh agent via the cmd
      eval "$(ssh-agent -s)"
      
    4. Add the ssh key to the agent (Replace key_name with the actual name of the key created in the above steps)
      ssh-add ~/.ssh/key_name
      

    You can refer to the GitHub Documentation for further reference

  3. We need to now add the public part of the key generated to GitHub:

    1. On EC2 head to the .ssh folder
      cd ~/.ssh
      
    2. To view the public key run the cmd (Replace key_name with the actual name of the key created in the above steps)
      cat key_name.pub
      
    3. Copy the output of the above cmd
    4. Head to the repository
    5. Go to settings
    6. Look for Deploy Keys in the sidebar on the left
    7. Add the copied key here with an appropriate name

    You can read the GitHub Documentation for more information on deploy keys

With this the ssh keys have been setup for us to easily clone & pull new changes from our private repository.


Cloning the Repository

  1. Connect to the EC2 instance using your terminal
  2. Activate the virtual environment
    source env/bin/activate
    
  3. Clone the GitHub Repository
    git clone [email protected]:SimPPL/arbiter-backend.git
    
  4. Install dependencies
    cd arbiter-backend
    pip install -r requirements.txt
    
  5. Head to the report directory
    cd arbiter/report
    
  6. Create a .env file as per example

Supervisor & Gunicorn Configuration

  1. Connect to the EC2 instance using your terminal
  2. Navigate to the to the directory where Supervisor configuration files are stored
    cd /etc/supervisor/conf.d/
    
  3. Create a new configuration file named gunicorn.conf
    sudo touch gunicorn.conf
    
  4. Open the gunicorn.conf file for editing using the Nano text editor
    sudo nano gunicorn.conf
    
    Copy paste the following code in the config file, then press ctrl + o to save the file, press enter when asked for the file name & lastly press ctrl + x to exit the editor
    [program:gunicorn]
    directory=/home/ubuntu/arbiter-backend/arbiter
    command=/home/ubuntu/env/bin/gunicorn --workers 3 --bind unix:/home/ubuntu/arbiter-backend/arbiter/app.sock arbiter.wsgi:application  
    autostart=true
    autorestart=true
    stderr_logfile=/var/log/gunicorn/gunicorn.err.log
    stdout_logfile=/var/log/gunicorn/gunicorn.out.log
    
    [group:guni]
    programs:gunicorn
    
  5. Create a new directory named gunicorn under /var/log for logging related to the backend server
    sudo mkdir /var/log/gunicorn
    
  6. Instruct Supervisor to re-read its configuration files, refreshing the list of available programs and updating its internal state accordingly
    sudo supervisorctl reread
    
  7. Update Supervisor's configuration based on any changes made, ensuring that new programs are started and stopped as necessary
    sudo supervisorctl update
    
  8. Display the status of all Supervisor-managed programs, showing whether they are running, stopped, or in an error state
    sudo supervisorctl status
    

Nginx Configuration

  1. Connect to the EC2 instance using your terminal
  2. Navigate to the to the directory where Nginx configuration files are stored
    cd /etc/nginx
    
  3. Open the nginx.conf file for editing using the Nano text editor
    sudo nano nginx.conf
    
  4. Replace the user on the first line from www-data to root as shown below, then press ctrl + o to save the file, press enter when asked for the file name & lastly press ctrl + x to exit the editor
    user root;
    
  5. cd sites-available
    
  6. Create a new config file
    sudo touch django.conf
    
  7. Edit the newly created config file
    sudo nano django.conf
    
  8. Copy paste the following code in the config file. Replace the ip_Address on the third line with the IP address obtained above, then press ctrl + o to save the file, press enter when asked for the file name & lastly press ctrl + x to exit the editor
    server{
        listen 80;
        server_name ip_address;
    
        location / {
     	   include proxy_params;
     	   proxy_pass http://unix:/home/ubuntu/arbiter-backend/arbiter/app.sock;
    
     	   proxy_connect_timeout 10s;
                    proxy_read_timeout 120s;
                    proxy_send_timeout 120s;
        }
    }
    
  9. Check if the config file was written correctly with the cmd
    sudo nginx -t
    
  10. To make the server live on nginx run
    sudo ln django.conf /etc/nginx/sites-enabled
    
  11. Finally restart nginx with the cmd
    sudo service nginx restart
    

The backend server should now be accessible using the IP address obtained above.


⚠️ NOTE: The backend server hosted on AWS EC2 using this documentation operates over HTTP. For enhanced security and encryption of data transmission, implementing HTTPS requires the installation of SSL/TLS certificates on the server, which is not covered in this guide.