EC2+django+Apache+mod_wsgi 環境の最速構築


参考URL
https://water2litter.net/gin/?p=1751
https://qiita.com/dekosuke-menti/items/e416f198980c0fd6e75b
https://www.sejuku.net/blog/27637#mod_wsgi

$ sudo yum install gcc zlib-devel bzip2 bzip2-devel readline readline-devel sqlite sqlite-devel openssl openssl-devel git
$ sudo yum install libffi-devel
$ sudo yum install -y python3-devel mysql-devel gcc httpd httpd-devel
$ git clone https://github.com/pyenv/pyenv.git ~/.pyenv
$ echo 'export PATH="$HOME/.pyenv/bin:$PATH"' >> ~/.bash_profile
$ echo 'eval "$(pyenv init -)"' >> ~/.bash_profile
$ source ~/.bash_profile
$ env CONFIGURE_OPTS="--enable-shared" pyenv install 3.7.4
$ pyenv global 3.7.4
$ pyenv rehash
$ pip install django mysqlclient mod_wsgi
$ django-admin startproject myproject
$ chmod 755 /home/ec2-user
https://qiita.com/rururu_kenken/items/8202b30b50e3bfa75821
$ vim /home/ec2-user/.pyenv/versions/3.7.4/lib/python3.7/site-packages/django/db/backends/sqlite3/base.py

check_sqlite_version()
→#check_sqlite_version()
/etc/httpd/conf.d/django.conf
LoadModule wsgi_module /home/ec2-user/.pyenv/versions/3.7.4/lib/python3.7/site-packages/mod_wsgi/server/mod_wsgi-py37.cpython-37m-x86_64-linux-gnu.so

WSGIScriptAlias / /home/ec2-user/(projectname)/(projectname)/wsgi.py

<Directory /home/ec2-user/(projectname)/(projectname)>
<Files wsgi.py>

</Files>
</Directory>
~/myproject/myproject/wsgi.py
import os
import sys

from django.core.wsgi import get_wsgi_application

FILE_PATH = os.path.dirname(__file__)
PROJECT_NAME = os.path.basename(FILE_PATH)

sys.path.append(os.path.dirname(FILE_PATH))
sys.path.append(FILE_PATH)

os.environ.setdefault("DJANGO_SETTINGS_MODULE", PROJECT_NAME + ".settings")

application = get_wsgi_application()

データベースのアクセス権限に注意する。