Deploying a Django-Vue application on AWS EC2 using NGINX

A simple approach to hosting two Servers on a single EC2 Instance

  • Port 80 - Vue.js frontend serve

  • Port 8000 - Django backend serve

1. Create an EC2 instance

  • Choose your desired Amazon Machine Image (AMI)

    • I chose Ubuntu 16.04

      • I love Ubuntu

2. Create a PEM key

  • If you don't have a previously generated PEM key, you will be asked whether to create a PEM key after instance creation, and you can create one at that time

    • The PEM Key must be stored in a safe place!

    • Since anyone who possesses the private key can connect to the instance, the private key must be stored in a secure location!

3. Connect to EC2

3-1. Navigate to the path where the PEM key is stored

3-2. Change access permissions

  • chmod is a command to change file access permissions

    • 400 grants read permission to the file owner

3-3. Connect to EC2 using the ssh command

  1. In the AWS console's EC2 Instance list, select the instance you want to connect to and click Connect, and a modal window with connection instructions will appear

  2. Copy the command that starts with ssh -i

  3. Execute that command from where the PEM Key is stored

    • After doing this, you will be connected to the Ubuntu Bash!

4. Install the files needed for deployment on EC2

4-1. Install nvm

  • Node will actually be installed all at once later after running apt update and modifying & applying .bashrc

4-2. Install python 3.7

4-3. Set aliases in .bashrc

  • Enter the following aliases

  • Save and exit with esc + :wq + enter

  • Apply the modified values immediately with the source command

4-4. Install node & npm

4-5. Install nginx

  • Will serve the built files using nginx

5. Configure Security Group

: Configure the security group for the EC2 instance in the AWS console

5-2. Create Security Group

  • Since this security group will allow ports 80 and 8000

    • The security group name is set to 80-8000,

    • and the description says "Allow 80-8000"

5-2. Set Inbound Rules

  • Add Inbound rules for ports 80 and 8000

6. Check nginx status

  • Verify that Active: active (running) appears

  • Copy the address that appears when you click on the instance in the AWS Console's Instance list and run it in the Browser; the following screen should appear

7. Clone the project to deploy

7-1. clone

7-2 Install required packages for Frontend & build

8. Prepare for serving

8-1. Modify nginx.conf

  • Change enabled to available in include /etc/nginx/sites-enabled/*.conf;

    • After modification: include /etc/nginx/sites-available/*.conf;

  • Save and exit

8-2. Modify sites-available

  • Remove everything except comments, then add the following content

  • Save and exit with :wq

8-3. Restart nginx

  • You can verify that the Frontend is being served by accessing the site URL

9. MySQL Setup

9-1. Install

9-2. Set up root account

  • Set the password after entering the command

9-3. Try connecting with root account

  • Enter the configured password after entering the command

9-4. Create database

10 . Configure .env file

  • Enter the required content and exit

11. Set up backend virtual environment

11-1. Install pipenv

11-2. Activate the virtual environment

11-3. Install required packages

12. Configure gunicorn

12-1. Install gunicorn

12-2. gunicorn

  • web server gateway interface

    • Adding --daemon runs it in the Background

12-3. Verify it is running properly

12-4. Restart nginx

Deployment complete~

+

Redeployment

With the current approach, redeployment is really, really inefficient! Planning to automate it!!!! I can't just leave it like this!!!!!!!!

When backend is modified

When frontend is modified

Last updated