12

I am very new to deploy Django and AWS. So now I would like to deploy my separate project (drf + react) to AWS.

So How can I deploy the separate project to AWS?

EC2 vs Elastic BeanStalk which one is better for that kind of environment?

I already search for one day but I found nothing useful information and found the similar thing but I don't understand like that=> Deploying Separate React Frontend and Django DRF API

2
  • You got so many options for Django. Elastic beans talk , ECS , deployment via cloudformation on ec2 etc. There are many articles about elasticbeanstalk Django Commented Jan 3, 2020 at 4:27
  • @ArunK, Yes, this so many options make me so confused. I just wondering Am I need to deploy the drf by elastic beanstalk and react by Amplify console or I can deploy this separate project to combine one and deploy to elastic beanstalk. which one is better and how to do that one. I see only a few articles for beginners and did not understand very well. If u know some explanation for deploy that case, I will really love it.Thanks. Commented Jan 3, 2020 at 4:32

2 Answers 2

15

You asked a difficult question. I think I can make it bit easy for you.

First let's look at the differences in the options.

Serving the front end app

Option1: Django to serve react app

In this option, the Django app will serve your app in a route for e.g /app/. Basically the route /app will load react app's index.html. it's that simple.

In order for this to work, you need to build your react app using npm run build and copy the files to Django's static folder. good so far? One of the benefits option 1 gives, option 2 doesn't is, you can use the same domain for backend and frontend for e.g www.example.com.

Option 2: deploy front end in S3

You still need to build the react app using npm run build, but you will not copy that to Django, in other words, you don't want to Django to serve your front end app, you want s3 to serve the static website. this method requires a separate subdomain or domain to host the react app.

Thats is the only difference between the options. Your frontend app will make api calls to the Django api, thats same for both options.My preferred option is option 2 to reduce complexity.

Deployment

I would pick elasticbeanstalk because it's easy to start with. You can do everything in the elasticbeanstalk console from load balancer to SSL, Healthcheck, Changing EC2 Keypair for EC2 Instance etc. Deployment is very easy. It supports multiple environments, You can use one AWS account for your test and production environments.

Sign up to request clarification or add additional context in comments.

8 Comments

I am trying as u said but how to set up for MySQL?
You can't deploy the app and the mysql server inside the same elasticbeanstalk application. how about using amazon RDS. you can also setup an EC2 instance for mysql
When I deploy Django to eb , it always shows favicon.ico 500 Internal Server Error and Do u already face that kind of issue? Here I asked=> stackoverflow.com/questions/59623908/…
So if you go for option 1, Django serving the build version of the react app, how do you create the build version of React in elastic beanstalk, since the build folder is not tracked by git? I know that with Heroku, you have the build pack for NodeJS and Python so they create the build version, but how can you do that with AWS?
When you create the deployment package for elasticbeanstalk it should contain the already built react app. If you are building using ci/cd tools, you can build the react app first, create a zip file of Django application and include the react app that you built. Hope this helps.
|
0

Create an amazon aws account

Search ec2- click ec2

Click instances

Click Launch Instance

Quick start - select ubuntu server

Select instance type - select any free tier eligible instance

Key pair login - create new key pair - give a key pai name. Then click create Click launch instance

Click view all instances

Open the .pem file containing folder in terminal. Change the permission of the file using chmod

chmod 400 key_name.pem // prevents accidental overwriting

Get to instance using pem file. Go to console ,then click new created instance. Check to see whether it is running (written). Click ‘connect’ button on top right

Opened a new page showing connect to instance. Click ssh client. Copy the ssh login command . under example Paste this in command prompt // save this ssh client code in docs or txt file

sudo apt-get update

sudo apt-get install nginx // or you can use apache

From the connect to instance , we also get ip address. Copy paste that into new browser tab window. It just showing loading because nginx is not started yet

To start nginx, type sudo systemctl start nginx

To know whether it is started or not , sudo systemctl status nginx // shows starting and started status

We have to add port also in aws console. Nginx runs on port 80. So go to instances detail page . scroll down and click security tab . under security groups , clicck on the link below it.click inbound rules tab. Edit inboundrules click button. Click add rule.

Type- custom tcp - port range -80,source - anywhere, click save rule button. Then port 80 is added

After any change restart nginx, sudo service nginx restart in command prompt Next copy ip then run. Copy auto-assigned ip address ,paste into browser tab, it will open nginx default page

Go to command prompt, activate venv

Clone our project from git repository, git clone https://github.com/***/*****.git

pip  install -r requirements.txt

Install wsgi server, pip install gunicorn

Go to route folder (folder containing manage.py) in cmd prompt, type gunicorn –bind 0.0.0.0:9090 projectname.wsgi //here 9090 is the port All requests coming to 9090 port of nginx should redirect to port 80 (previously added)

For that, we have to open configuration file of nginx. Configuration file is in etc/nginx/sites-enabled. So type like below in cmd prompt, Activate venv. cd $HOME

cd /etc/nginx/sites-enabled/

sudo nano default

Comment the line starting with try_files under location/

Under that type, proxy_pass http://0.0.0.0:9090; // type ctrl+O (O for Owl)

After every change restart nginx. sudo service nginx restart

If error shows, add this ip address in allowed host under settings.py

Go to settings.py containing folder in cmd prompt, sudo nano settings.py,

Allowed hosts =[‘ip address’ ,’0.0.0.0’ ] . restart nginx

gunicorn –bind 0.0.0.0:9090 projectname.wsgi

cd /etc/nginx/sites-enabled/

sudo nano default

Under location /, type

location /static/ {
autoindex on;
alias /home/path_to_static_dir/;
} 

//save and close

Restart nginx sudo service nginx restart

cd projectname/projectname/

Run server , gunicorn –bind 0.0.0.0:9090 projectname.wsgi

If forbidden error occurs, then type this in cmd,

ps aux | grep nginx

sudo usermod -a -G ubuntu www-data

sudo chown -R :www-data static

Restart nginx, sudo service nginx restart

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.