Amazon LinuxでDjangoプロジェクトの配置の優先パラメータを設定する

2373 ワード

ソースコードのインストール

1 $ mkdir projects
2 $ cd projects
3 $ git clone [email protected]:<github-user>/<github-repo>.git mynewproject

virtualenvの設定

1 $ sudo pip3 install virtualenv
2 $ mkdir PYENVS
3 $ cd PYENVS
4 $ virtualenv -p python3 mynewproject
5 $ source mynewproject/bin/activate
6 (mynewproject) $ cd ~/projects/mynewproject
7 (mynewproject) $ pip install -r requirments.txt

gunicorn設定

1 (mynewproject) $ pip install gunicorn
2 $ sudo vi /etc/systemd/system/mynewproject.service
3 $ sudo systemctl enable mynewproject.service
4 $ sudo systemctl start mynewproject
5 $ mkdir ~/logs

# service 파일 수정 시 설정 리로드
6 $ sudo systemctl daemon-reload

# 서버 시작
7 $ sudo systemctl start mynewproject

# 서버 재시작
8 $ sudo systemctl restart mynewproject

# 서버 상태 체크
9 $ sudo systemctl status mynewproject -l

gunicorn設定用systemdファイル

[Unit]
Description=My New Project
After=network.start

[Service]
Restart=always
RestartSec=5
WorkingDirectory=/home/ec2-user/projects/mynewproject
User=ec2-user
ExecStart=/home/ec2-user/PYENVS/mynewproject/bin/gunicorn \
  --workers=2 \  # (CPU core 수 + 1)
  --access-logfile=/home/ec2-user/logs/mynewproject-access.log \
  --error-logfile=/home/ec2-user/logs/mynewproject-error.log \
  --log-level=error \
  --bind=0.0.0.0:8000 \
  --timeout=600 \
  mynewproject.wsgi

[Install]
WantedBy=multi-user.target

Amazon Linux Catalogの更新

1 $ sudo amazon-linux-extras list

PostgreSQLのインストール

1 $ sudo amazon-linux-extras install 41
2 $ sudo yum install postgresql-server postgresql-devel -y
3 $ sudo service postgresql initdb
4 $ sudo systemctl start postgresql.service
5 $ sudo -u postgres psql postgres

postgres=# create database mynewproject;

NGINXのインストール

1 $ sudo amazon-linux-extras install 38