[Note] Run Django on Amazon Linux 2

Keep your OS up to date

$ sudo yum update -y

Put python

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

Put in Django

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

Install MySQL

$ 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))

DB connection

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

systemctl command registration

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

reference

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

Recommended Posts

[Note] Run Django on Amazon Linux 2
[Note] Install Imagick on Amazon Linux2
Run docker-compose on Amazon Linux2 on ARM64
Run cron on Amazon Linux (set on Linux)
Run Keycloak on Amazon Linux 2 without Docker
Run Django on PythonAnywhere
Install Python3 and Django on Amazon Linux (EC2) and run your web server
Put jenv on Amazon Linux
Install tomcat 5.5 on Amazon Linux.
Install Homebrew on Amazon Linux 2
Install strongSwan 5.9.1 on Amazon Linux 2
Run Amazon Linux 2 in VirtualBox
Run FreeBSD on Linux + qemu
Publish your Django app on Amazon Linux + Apache + mod_wsgi
Django note 4
Install Python Pillow on Amazon Linux
Install oracle java8 on amazon linux2
Run a Linux server on GCP
Install pyenv on EC2 (Amazon Linux)
Django Note 5
Django Note 1
Introduce Python 3.5.2 environment on Amazon Linux
Django note 3
Django note 2
Deploy Django api on heroku (personal note)
Use Numpy, Scipy, scikit-learn on Amazon Linux
React → Ajax → Django on Linux implementation memo
Build an LNPP environment on Amazon Linux 2
Learn sshd_config and authorized_keys (on Amazon Linux 2)
Upgraded mysql on Cloud9 (Amazon Linux) (5.5 to 5,7)
Run bootgen on Debian GNU / Linux, Ubuntu
A note on enabling PostgreSQL with Django
Install Python 3.8, Pip 3.8 on EC2 (Amazon Linux 2)
Run Linux on ARM architecture with QEMU
Run a batch of Python 2.7 with nohup on Amazon Linux AMI on EC2
Run django applications on Windows + Apache + mod_wsgi + services.
[Note] Install wxPython 3.x on Linux Mint (Ubuntu)
Install PHP 7 series on Amazon Linux 2 with Amazon Linux Extras
Daemonizing processes on Linux
Run Kali Linux on Windows with GUI (without VirtualBox)
jblas on Arch Linux
Linux (WSL) on Windows
NAT router on Linux
Celery notes on Django
Run mysqlclient on Lambda
Develop .NET on Linux
Wake on lan on Linux
OpenVPN Summary + Amazon Linux2
Hello World on Django
Compile and install MySQL-python for python2.7 on amazon linux
Monitor traffic on Linux
Install Docker on Arch Linux and run it remotely
Update vscode on linux
[AWS EC2] How to install Maven on Amazon Linux 2
Try NeosVR on Linux
Django Girls Tutorial Note
How to run Django on IIS on a Windows server
Check capacity on Linux
How to build a Python environment on amazon linux 2
Announcing the availability of Java 11 LTS on Amazon Linux 2
LiveUSB creation on Linux