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.
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)
The items that should be set in AWS in this configuration are roughly described as follows.
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
On AWS, you first need to create a VPC to launch a VM.
The flow is as follows
Create a VM to build your application. But all you have to do is create EC2 by clicking the button.
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/
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
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"
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="
** 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
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
$ 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
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
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.
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"
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