Django を CentOS7で動かす

Django を CentOS7 で動かす最低限メモ

これまで、CentOS6 やら、AWSやらでDjangoの設定を行ってきた が、CentOS7上に、なるべく簡単、最低限で(手抜き)で環境を作りたい。

1.EPEL のインストール

以下のサイトで、最新のバージョンを確認し、rpm でインストール

http://www.liquidweb.com/kb/how-to-install-pip-on-centos-7/

http://dl.fedoraproject.org/pub/epel/7/x86_64/e/ でバージョンを確認

  1. rpm -iUvh http://dl.fedoraproject.org/pub/epel/7/x86_64/e/epel-release-7-6.noarch.rpm

yum を更新しておく

  1. yum -y update

2.pip のインストール

これで、pip がyumからインストールできるようになる。pipが導入できれば、最近のPythonだと何とかなる感。

  1. yum install python-pip

3.Djangoのインストール

ローカルでの動作確認バージョンに合わせてインストール。

CentOS6 のときは、デフォルトのPythonが、2.6 だったので、2.7のインストールとか面倒だったが、CentOS7 では、2.7 なのでそのまま利用する。

VirtualEnv 使ったほうが良いかもだが、今回は簡易。

  1. pip install Django==1.9.6

4.mod_wsgi

apache と django をつなぐための mod_wsgi をインストール

http://www.unixmen.com/configure-django-with-apache-centos-7/

  1. yum install mod_wsgi

django の設定ファイルを編集する

  1. vi /etc/httpd/conf.d/django.conf

WSGIScriptAlias に、リクエストされたパス"/" を転送するパス "…wsgi.py" を指定。

WSGIPythonPath に、アプリケーションのルートを指定

https://docs.djangoproject.com/ja/1.9/howto/deployment/wsgi/modwsgi/

  1. WSGIScriptAlias / /path/to/mysite.com/mysite/wsgi.py

  2. WSGIPythonPath /path/to/mysite.com

    <Directory /path/to/mysite.com/mysite>

  3. <Files wsgi.py>

  4. Require all granted

  5. </Files>

  6. </Directory>

5.ユーザーディレクトリに配置する場合、有効化

ここまでで、実行しても、ずーーーーーーと permission エラーになっていた。

結局、自分は、ユーザーディレクトリに、git から pull したDjangoアプリを置いていたのだが、

以下の設定を編集し、apache の UserDir を有効にする必要があった。

/etc/httpd/conf.d/userdir.conf

  1. <IfModule mod_userdir.c>
  2. #
  3. # UserDir is disabled by default since it can confirm the presence
  4. # of a username on the system (depending on home directory
  5. # permissions).
  6. #
  7. UserDir enable
  8. #
  9. # To enable requests to /~user/ to serve the user's public_html
  10. # directory, remove the "UserDir disabled" line above, and uncomment
  11. # the following line instead:
  12. #
  13. #UserDir public_html
  14. </IfModule>

6.Webサーバーの再起動

  1. systemctl restart httpd.service

以上。

動作確認とれた。

数時間、上記5.ではまったが、前に比べれば簡単になった。

Follow me!

コメントを残す

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です