7
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【メモ】Amazon Linux 2 でDjangoを動かす

Posted at

OSを最新の状態にする

$ sudo yum update -y

pythonを入れる

pythonに必要な物を入れる

$ sudo yum install git gcc zlib-devel libffi-devel bzip2-devel readline-devel openssl-devel sqlite-devel

pyenvを入れる

$ git clone https://github.com/yyuu/pyenv.git ~/.pyenv

パスを通す

$ 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

pyenvで入れれるpythonの最新バーションを調べる

$ pyenv install --list

最新のバージョンのpythonを入れる

$ pyenv install 3.8.1

インストールしたpythonをデフォルトで使うようにする

$ 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

Djangoを入れる

pyenvで入れたパッケージにpipもはいっているので、pip本体をアップグレードしてから、Djangoをインストール

$ pip install --upgrade pip
$ pip install Django Pillow

ディレクトリを作ってその中にDjangoプロジェクトを作成

$ cd /var/www
$ mkdir django
$ django-admin startproject config .

DB

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接続

Djangoはmysqlclientを推奨。PyMySQLはなるべく使わない。

$ sudo yum install mysql-devel
$ pip install mysqlclient

接続の確認

$ python manage.py dbshell
# 本番用の設定ファイルを使う
$ python manage.py dbshell --settings=config.settings.production

##gunicorn

$ pip install gunicorn

gunicornを使ってDjangoを起動してみる

$ gunicorn config.wsgi --bind=[プライベートIP]
# 本番用の設定ファイルを使う
$ gunicorn config.wsgi --bind=[プライベート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
# 自動起動
$ sudo systemctl enable nginx.service
$ systemctl is-enabled nginx.service
enabled
$ sudo vim /etc/nginx/conf.d/django.conf
server {
    listen  80;
    server_name [パブリックIP や elb やドメイン];

    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

確認できたらプロセスをkill

$ sudo lsof -i:8000
COMMAND    PID     USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
gunicorn 13502 ec2-user    5u  IPv4  68558      0t0  TCP [プライベートIP].ap-northeast-1.compute.internal:irdmi (LISTEN)
gunicorn 13504 ec2-user    5u  IPv4  68558      0t0  TCP ip-[プライベートIP].ap-northeast-1.compute.internal:irdmi (LISTEN)
$ sudo kill -9  13502 13504

systemctlコマンド登録

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

参考

7
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?