$ sudo yum update -y
Put what you need in python
$ sudo yum install git gcc zlib-devel libffi-devel bzip2-devel readline-devel openssl-devel sqlite-devel
Put pyenv
$ git clone https://github.com/yyuu/pyenv.git ~/.pyenv
Pass through
$ echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bash_profile
$ echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bash_profile
$ echo -e 'if command -v pyenv 1>/dev/null 2>&1; then\n eval "$(pyenv init -)"\nfi' >> ~/.bash_profile
$ exec "$SHELL" -l
Find out the latest version of python that you can put in pyenv
$ pyenv install --list
Put the latest version of python
$ pyenv install 3.8.1
Use the installed python by default
$ python -V
Python 2.7.16
$ pyenv versions
* system (set by /home/ec2-user/.pyenv/version)
3.8.1
$ pyenv global 3.8.1
$ pyenv versions
system
* 3.8.1 (set by /home/ec2-user/.pyenv/version)
$ pyenv rehash
$ python -V
Python 3.8.1
Since pip is also included in the package put in pyenv, upgrade the pip itself and then install Django
$ pip install --upgrade pip
$ pip install Django Pillow
Create a directory and create a Django project in it
$ cd /var/www
$ mkdir django
$ django-admin startproject config .
DB
$ sudo rpm -ivh http://dev.mysql.com/get/mysql57-community-release-el7-8.noarch.rpm
$ yum install mysql-community-server
$ mysqld --version
mysqld Ver 5.7.29 for Linux on x86_64 (MySQL Community Server (GPL))
Django recommends mysqlclient (https://docs.djangoproject.com/en/3.0/ref/databases/#mysql-db-api-drivers). I don't use PyMySQL as much as possible.
$ sudo yum install mysql-devel
$ pip install mysqlclient
Check connection
$ python manage.py dbshell
#Use production configuration files
$ python manage.py dbshell --settings=config.settings.production
gunicorn
$ pip install gunicorn
Try launching Django with gunicorn
$ gunicorn config.wsgi --bind=[Private IP]
#Use production configuration files
$ gunicorn config.wsgi --bind=[Private IP] --env DJANGO_SETTINGS_MODULE=config.settings.production
nginx
$ sudo amazon-linux-extras install nginx1.12 -y
$ sudo cp -a /etc/nginx/nginx.conf /etc/nginx/nginx.conf.back
$ sudo systemctl start nginx.service
#Automatic start
$ sudo systemctl enable nginx.service
$ systemctl is-enabled nginx.service
enabled
$ sudo vim /etc/nginx/conf.d/django.conf
server {
listen 80;
server_name [Public IP, elb and domain];
location /static {
alias /var/www/manage/static;
}
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;
}
}
$ gunicorn config.wsgi --daemon --bind 127.0.0.1:8000
$ gunicorn config.wsgi --daemon --bind 127.0.0.1:8000 --env DJANGO_SETTINGS_MODULE=config.settings.production
If you can confirm, kill the process
$ sudo lsof -i:8000
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
gunicorn 13502 ec2-user 5u IPv4 68558 0t0 TCP [Private IP].ap-northeast-1.compute.internal:irdmi (LISTEN)
gunicorn 13504 ec2-user 5u IPv4 68558 0t0 TCP ip-[Private IP].ap-northeast-1.compute.internal:irdmi (LISTEN)
$ sudo kill -9 13502 13504
sudo vim /etc/systemd/system/project.service
[Unit]
Description=gunicorn
After=network.target
[Service]
WorkingDirectory=/var/www/django
ExecStart=/home/ec2-user/.pyenv/shims/gunicorn --bind 127.0.0.1:8000 config.wsgi:application --env DJANGO_SETTINGS_MODULE=config.settings.production
[Install]
WantedBy=multi-user.target
https://narito.ninja/blog/detail/21/
Recommended Posts