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
- Log into AWS
- Search for EC2 from your AWS dashboard
- Click on
Launch Instance - Select the latest Ubuntu Image
- Set the instance type to
t2.micro(You can use more powerful instance if required) - Select an existing key pair or create a new one (You will need this to ssh into the instance later)
- Click on
Launch Instance
The instance is successfully created.
Assigning IP Address
- Head to your AWS Console
- Search for
Elastic IPs - Click on
Allocate Elastic IP addresson the top right & follow the prompts - On the Elastic IPs console select the IP address that was allocated to you in the above step
- Click on the
Actionsbutton on the left ofAllocate Elastic IP addressbutton - Select the
Associate Elastic IP addressoption - 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
- Go to your EC2 dashboard
- Click on
instances (running) - Select the instance you want to connect to
- Press
connect - Select
ssh clientoption - Copy the ssh cmd to your terminal which will look something like this
ssh -i key_pair_file user@host - Replace the
key_pair_filewith the path to the actualkey_pair_fileon your computer - Press enter
Now you will be connected to the EC2 instance.
Setting up the EC2 Instance
- Connect to the EC2 instance using your terminal
- Run the cmd
sudo apt update && sudo apt -y upgrade - Press enter if any prompts pop up
- This cmd updated the entire system
- Install nginx using the cmd (Press enter if any prompts pop up)
sudo apt install -y nginx - Install supervisor via the cmd (Press enter if any prompts pop up)
sudo apt install -y supervisor - Run the cmd (Press enter if any prompts pop up)
sudo apt install -y python3-venv - Create a virtual environment using the cmd
python3 -m venv env - Activate the virtual environment with the cmd
source env/bin/activate - 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
-
Connect to the EC2 instance using your terminal
-
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:
- 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]" - 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: - Start the ssh agent via the cmd
eval "$(ssh-agent -s)" - Add the ssh key to the agent (Replace
key_namewith 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
- On EC2 run the ssh-keygen cmd to generate new key (Replace
-
We need to now add the public part of the key generated to GitHub:
- On EC2 head to the .ssh folder
cd ~/.ssh - To view the public key run the cmd (Replace
key_namewith the actual name of the key created in the above steps)cat key_name.pub - Copy the output of the above cmd
- Head to the repository
- Go to settings
- Look for
Deploy Keysin the sidebar on the left - Add the copied key here with an appropriate name
You can read the GitHub Documentation for more information on deploy keys
- On EC2 head to the .ssh folder
With this the ssh keys have been setup for us to easily clone & pull new changes from our private repository.
Cloning the Repository
- Connect to the EC2 instance using your terminal
- Activate the virtual environment
source env/bin/activate - Clone the GitHub Repository
git clone [email protected]:SimPPL/arbiter-backend.git - Install dependencies
cd arbiter-backend pip install -r requirements.txt - Head to the report directory
cd arbiter/report - Create a .env file as per example
Supervisor & Gunicorn Configuration
- Connect to the EC2 instance using your terminal
- Navigate to the to the directory where Supervisor configuration files are stored
cd /etc/supervisor/conf.d/ - Create a new configuration file named
gunicorn.confsudo touch gunicorn.conf - Open the
gunicorn.conffile for editing using the Nano text editor
Copy paste the following code in the config file, then presssudo nano gunicorn.confctrl + oto save the file, pressenterwhen asked for the file name & lastly pressctrl + xto 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 - Create a new directory named
gunicornunder/var/logfor logging related to the backend serversudo mkdir /var/log/gunicorn - Instruct Supervisor to re-read its configuration files, refreshing the list of available programs and updating its internal state accordingly
sudo supervisorctl reread - Update Supervisor's configuration based on any changes made, ensuring that new programs are started and stopped as necessary
sudo supervisorctl update - Display the status of all Supervisor-managed programs, showing whether they are running, stopped, or in an error state
sudo supervisorctl status
Nginx Configuration
- Connect to the EC2 instance using your terminal
- Navigate to the to the directory where Nginx configuration files are stored
cd /etc/nginx - Open the
nginx.conffile for editing using the Nano text editorsudo nano nginx.conf - Replace the user on the first line from
www-datatorootas shown below, then pressctrl + oto save the file, pressenterwhen asked for the file name & lastly pressctrl + xto exit the editoruser root; -
cd sites-available - Create a new config file
sudo touch django.conf - Edit the newly created config file
sudo nano django.conf - Copy paste the following code in the config file. Replace the
ip_Addresson the third line with the IP address obtained above, then pressctrl + oto save the file, pressenterwhen asked for the file name & lastly pressctrl + xto exit the editorserver{ 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; } } - Check if the config file was written correctly with the cmd
sudo nginx -t - To make the server live on nginx run
sudo ln django.conf /etc/nginx/sites-enabled - 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.