[PYTHON] Launched a web application on AWS with django and changed jobs

Hello mogken.

It's been over 3 months since I wrote the article about changing jobs by making a scraping app with Python + Django + AWS last time. As the title suggests, fortunately I changed jobs successfully and changed jobs to a company that is developing a cloud platform from January for service planning * \ (^ o ^) / *

So, this time, I will briefly record the procedure when building the Python web application created at that time on AWS. I'm sorry, but it's been a long time ago, so I can't guarantee that the content is correct because my memory is a little vague ... Think of it as an amateur's memorandum.

What to do this time

Install python3, Django, nginx, and gunicorn on the AWS VM (Centos7) and deploy the created web application. I was thinking of doing a load balancer and auto scaling, but since I was able to change jobs, I was exhausted with the simplest configuration ...

Configuration diagram (I don't need it because it's too small, but for the time being) スクリーンショット 2020-02-12 17.54.50.png

AWS settings

The items that should be set in AWS in this configuration are roughly described as follows.

  1. Creating a network
  2. Create a VM
  3. DNS settings

The detailed procedure is not described here. Because the procedure of remembering only amateurs is so scary that I can't refer to it.

Reference URL: https://qiita.com/okoppe8/items/dc1de147a36797442e4c

1. Creating a network

On AWS, you first need to create a VPC to launch a VM.

The flow is as follows

  1. Create a VPC
  2. Create subnet
  3. Create an internet gateway
  4. Creating a route table
  5. Create a security group

2. Creating a VM

Create a VM to build your application. But all you have to do is create EC2 by clicking the button.

3. DNS settings

Set the domain to publish using Route53, which is the DNS service of AWS.

I went by referring to this site.

https://avinton.com/academy/route53-dns-vhost/

Linux server settings

After creating the VM on AWS, the next step is to set up the launched VM.

From here, I will explain in a little more detail. (Because I failed many times and repeated it, my memory is clear ...)

After that, the settings from here are basically

https://narito.ninja/blog/detail/21/#_3

I refer to this page. This blog has a lot of very easy-to-understand articles around Django, so I highly recommend it.

The procedure is as follows

  1. Log in to the launched VM with SSH or Web console
  2. Install python3
  3. Install and configure django
  4. Nginx installation and configuration
  5. Install gunicorn
  6. Service launch

1. Log in to the launched VM with SSH or Web console

An authentication key for accessing with ssh is also created at the same time when EC2 is created, so download and use it. If you don't understand what you are saying, you can access the VM on the Web from the AWS console screen, so you may want to try it there.

If you try to use the downloaded authentication key as it is, you will get angry if the authentication key authority is too open, so you need to set the file authority with the following command.

chmod 400 "downloaded authentication key file name"

After that, you can access ssh with the following command.

ssh -i "***. Pem" ec2-user @ "EC2 public DNS name or public IP"

2. Install python3

Reference URL: https://qiita.com/s_runoa/items/156f3fa67c82e9cd9f42

From here, install the necessary packages. First of all, python. I'm using pyenv but I still don't know exactly what pyenv is ...

#yum package update yum update -y

#Install required packages sudo yum install git gcc zlib-devel libffi-devel bzip2-devel readline-devel openssl-devel sqlite-devel

Install #pyenv git clone https://github.com/yyuu/pyenv.git ~/.pyenv

Added to # .bash_profile export PYENV_ROOT="HOME/.pyenv" export PATH="PYENV_ROOT/bin:PATH" eval "(pyenv init -)"

** Now restart the server **

Install #Pyhton pyenv install --list CFLAGS="-fPIC" pyenv install 3.7.2

#python settings pyenv versions pyenv global 3.7.2 pyenv rehash

#Installation confirmation python --version

3. Install django

Once you have python installed, install django. By the way, my first framework is django, so I have a very special feeling for django.

Install #django pip install Django

This completes the installation of django, but I think it's a good idea to take measures here as you will get angry later if the version of sqllite is old.

Sometimes you can't get angry, so if you're annoyed, you can skip it and do it after you're really angry.

** Sqllite upgrade **

Reference URL: https://qiita.com/rururu_kenken/items/8202b30b50e3bfa75821

Get the #tar file wget https://www.sqlite.org/2019/sqlite-autoconf-3280000.tar.gz

Unpack #tar tar xvfz sqlite-autoconf-3280000.tar.gz

#Build and install cd sqlite-autoconf-3280000 $ ./configure --prefix=/usr/local $ make $ sudo make install $ sudo find /usr/ -name sqlite3

$ sudo mv /usr/bin/sqlite3 /usr/bin/sqlite3_old
$ sudo ln -s /usr/local/bin/sqlite3 /usr/bin/sqlite3

Pass the path to the shared library

$ export LD_LIBRARY_PATH="/usr/local/lib"

** Edit setting file **

Make the minimum settings here to get Django up and running properly.

Create a #django project django-admin startproject "project name"

#django app creation python manage.py startapp "app name"

#Edit configuration file The django configuration file is created in a folder with the same name as the project name. vi / "project name" / "project name" /setting.py

#Edit items in setting.py below Added to the end of #installed_app "App name" .apps. "App name (capitalize the first letter)" Config Example)'myapp.apps.MyappConfig'

#Hide debug and specify host DEBUG = False ALLOWED_HOSTS = ["set domain name"]

#Language and timezone settings LANGUAGE_CODE = 'ja' TIME_ZONE = 'Asia/Tokyo'

#Static file storage settings STATIC_URL ='/ static /'# This is from the beginning. STATIC_ROOT = '/usr/share/nginx/html/static' MEDIA_URL = '/media/' MEDIA_ROOT = '/usr/share/nginx/html/media'

#Save and finish editing setting.py

#Finally hit this command to finish sudo python manage.py collectstatic

4. Nginx installation and configuration

Use Nginx for the web server. When I first encountered this, I didn't know how to read it.

nginx installation

Install #nginx sudo amazon-linux-extras install nginx1.12

nginx settings

#Edit configuration file sudo vim /etc/nginx/conf.d/project.conf

#The following setting items server { listen 80; server_name "public IP address of the server";

    location /static {
        alias /usr/share/nginx/html/static;
    }

    location /media {
        alias /usr/share/nginx/html/media;
    }

    location / {
        proxy_pass http://127.0.0.1:8000;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_redirect off;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
}

#Save and exit

#Check configuration file sudo nginx -t

Start #nginx sudo systemctl reload nginx

#Setting to start nginx at the same time when the server starts sudo systemctl enable nginx

5. Install gunicorn

Finally, install gunicorn as wsgi. I'm not sure about wsgi and gunicorn by name ...

sudo pip3.7 install gunicorn

That's it. How easy.

Service launch

Finally launch the service

Move to the project folder where the # manage.py file is located cd /project

Start #gunicorn sudo gunicorn --bind 127.0.0.1:8000 project.wsgi:application

At this point, you'll be able to access your django project with your domain name.

Finally, put the settings when you want to stop gunicorn

Check the process number of #gunicorn lsof -i:8000

#Kill the displayed process kill -9 "displayed process number"

Thank you for your support.

I was very happy when the page was finally launched after fighting various errors. I felt like I was a little grown up.

Recommended Posts

Launched a web application on AWS with django and changed jobs
Build a web application with Django
I made a WEB application with Django
Build a Flask / Bottle-like web application on AWS Lambda with Chalice
Make a scraping app with Python + Django + AWS and change jobs
Web application creation with Django
Looking back on creating a web service with Django 2
Try creating a web application with Vue.js and Django (Mac)-(1) Environment construction, application creation
Deploy a Django application on EC2 with Nginx + Gunicorn + Supervisor
Deploy a Django application with Docker
A memo with Python2.7 and Python3 on CentOS
[Python] A quick web application with Bottle!
A note on enabling PostgreSQL with Django
Run a Python web application with Docker
Until you publish (deploy) a web application made with bottle on Heroku
# 1 Until you deploy Django's web application (instance construction with EC2 on AWS)
Publish a web application for viewing data created with Streamlit on heroku
Build a WardPress environment on AWS with pulumi
Try Tensorflow with a GPU instance on AWS
Launch a web server with Python and Flask
[ES Lab] I tried to develop a WEB application with Python and Flask ②
How to run a Django application on a Docker container (development and production environment)
Deploy a Python 3.6 / Django / Postgres web app on Azure
Develop a web API that returns data stored in DB with Django and SQLite
A story stuck with Memory Error and No space left on device on AWS EC2
(Python) Try to develop a web application using Django
How to deploy a Django application on Alibaba Cloud
Deploy a Django application on Google App Engine (Python3)
Launch a Python web application with Nginx + Gunicorn with Docker
Try running a Django application on an nginx unit
Web App Development Practice: Create a Shift Creation Page with Django! (Experiment on admin page)
Web application made with Python3.4 + Django (Part.1 Environment construction)
I made a web application that maps IT event information with Vue and Flask
Single sign-on to your Django application with AWS SSO
A memo about building a Django (Python) application with Docker
Deploy a Django app made with PTVS on Azure
Launch Django on a Docker container with docker-compose up
Create a web application that recognizes numbers with a neural network
Create a web surveillance camera with Raspberry Pi and OpenCV
Set up a web server with CentOS7 + Anaconda + Django + Apache
(Failure) Deploy a web app made with Flask on heroku
[Introduction to AWS] A memorandum of building a web server on AWS
(For beginners) Try creating a simple web API with Django
Web application development with Flask
Create a homepage with django
Web application with Python + Flask ② ③
Real-time web with Django Channels
Web application with Python + Flask ④
"2/2" I am making a simple web application for robot operation. "Raspberry Pi 3B + and Django Channels"
"1/2" I am making a simple web application for robot operation. "Raspberry Pi 3B + and Django Channels"
Create a Python3.4 + Nginx + uWSGI + Flask Web application execution environment with haste using pyenv on Ubuntu 12.04
Let's make a WEB application for phone book with flask Part 1
Build a 64-bit Python 2.7 environment with TDM-GCC and MinGW-w64 on Windows 7
Create a temporary file with django as a zip file and return it
Build a Python environment on your Mac with Anaconda and PyCharm
# 3 Build a Python (Django) environment on AWS EC2 instance (ubuntu18.04) part2
Web App Development Practice: Create a Shift Creation Page with Django! (Shift creation page)
Let's make a WEB application for phone book with flask Part 2
Get data from MySQL on a VPS with Python 3 and SQLAlchemy
Let's make a WEB application for phone book with flask Part 3
How to deploy a web application on Alibaba Cloud as a freelancer