💡

Linux環境でDjangoのアプリを公開するまで

2021/06/01に公開

前提

ローカル環境で動くDjangoのアプリがある

補足として、このガイダンスはEC2のインスタンスを元に作成しました(ec2-user)

初期セットアップとapacheの導入

sudo yum update -y

# Failed to set locale, defaulting to Cのエラーが出ている場合
export LC_ALL=C
printenv | grep LC

sudo yum install -y httpd httpd-devel
httpd -v
sudo service httpd start
sudo service httpd status 

#タイムゾーンの設定
sudo systemctl enable --now httpd
sudo timedatectl set-timezone Asia/Tokyo
ls -ls /etc/localtime
sudo localectl set-locale LANG=js_JP.UTF-8

Pythonのインストール・MySQLの導入

sudo yum install -y httpd-devel python3-devel gcc gcc-c++ python3
sudo pip3 install django  mod_wsgi
pip3 freeze | grep wsgi
sudo yum install -y https://dev.mysql.com/get/mysql80-community-release-el7-1.noarch.rpm
sudo yum install -y mysql-devel
sudo pip3 install mysqlclient
sudo systemctl start mysqld.service

gitのインストール・ソースの配置

sudo yum install git
#ec2-userのディレクトリ内でソースをclone
git clone xxxxxx

必要なモジュールのインストール(例)

sudo pip3 install djangorestframework django-cors-headers  django_filter  django-debug-toolbar
sudo pip3 djangorestframework django-cors-headers  django_filter  django-debug-toolbar
sudo pip3 install selenium pandas reportlab werkzeug beautifulsoup4

apache(conf)の設定

sudo vi /etc/httpd/conf.d/project_name.conf
#構文チェック
httpd -t
sudo service httpd restart
sudo chmod 755 /home/ec2-user/
project_name.conf
LoadModule wsgi_module /usr/local/lib64/python3.7/site-packages/mod_wsgi/server/mod_wsgi-py37.cpython-37m-x86_64-linux-gnu.so

ServerName 18.181.1.45
Alias /static /home/ec2-user/project_name/static
<Directory /home/ec2-user/project_name/static>
    Require all granted
</Directory>
WSGIScriptAlias / /home/ec2-user/project_name/config/wsgi.py
WSGIPythonPath /home/ec2-user/project_name/:/usr/bin/python3
ErrorLog "logs/project_name-error_log"
CustomLog "logs/project_name-access_log" combined

<Directory /home/ec2-user/project_name/config>
<Files wsgi.py>
Require all granted
</Files>
</Directory>

以上。

番外編-chromedriverの設定方法

sudo vi /etc/yum.repos.d/google.chrome.repo

google.chrome.repo
[google-chrome]
name=google-chrome
baseurl=http://dl.google.com/linux/chrome/rpm/stable/$basearch
enabled=1
gpgcheck=1
gpgkey=https://dl-ssl.google.com/linux/linux_signing_key.pub
sudo yum update
sudo yum -y install google-chrome-stable
google-chrome --version
sudo yum -y install ipa-gothic-fonts ipa-mincho-fonts ipa-pgothic-fonts ipa-pmincho-fonts
google-chrome --headless --no-sandbox --dump-dom https://www.google.com/
sudo pip3 install chromedriver-binary

cd /usr/local/bin
sudo wget https://chromedriver.storage.googleapis.com/89.0.4389.23/chromedriver_linux64.zip
sudo unzip chromedriver_linux64.zip
sudo chmod 755 chromedriver
sudo rm chromedriver_linux64.zip
sudo yum -y install GConf2

参考:
https://qiita.com/mindwood/items/245adeb6da18999bbfc4
http://chromedriver.chromium.org/downloads

Discussion

ログインするとコメントできます