【Python】Django2.0 + Python3 + ApacheでWebアプリケーション構築

【Python】Django2.0 + Python3 + ApacheでWebアプリケーション構築

2018-05-195 min read

目次

  1. 概要
  2. 環境
  3. インストール
  4. djangoプロジェクト作成
  5. apacheの設定
  6. 参考

概要

Django + Python3 + Apache + wsgi でWebサーバを構築した。 VirtualHost & デーモンモードの条件下で 公式通りに作っていたらいくつかつまづくポイントがあったので、メモ。

環境

Django2.0 Python3.4 Apache2.4

インストール

pipでDjangoとmod_wsgiを入れる

pip3 install mod-wsgi
pip3 install django

Djangoプロジェクト作成

そして公式の手順通りに作成し、 以下のような構成を作った。

/var/www/app1/
app
|-- app
|   |-- __init__.py
|   |-- settings.py
|   |-- urls.py
|   `-- wsgi.py
|-- db.sqlite3
|-- manage.py
`-- polls
    |-- admin.py
    |-- apps.py
    |-- __init__.py
    |-- migrations
    |   `-- __init__.py
    |-- models.py
    |-- tests.py
    |-- urls.py
    `-- views.py

ここで app/url.pyは

from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path('', include('polls.urls')),
    path('polls/', include('polls.urls')),
    path('admin/', admin.site.urls),
]

polls/url.pyは

from django.urls import path

from . import views

urlpatterns = [
    path('', views.index, name='index'),
]

polls/views.pyは

from django.shortcuts import render
from django.http import HttpResponse

def index(request):
    return HttpResponse("Hello, world. You're at the polls index.")

そして、許可するドメインをapp/settings.pyの28行目あたりに追加する。今回はapp1.localhost.comとして設定している。

ALLOWED_HOSTS = [
    'app1.localhost.com'
]

Apacheの設定

Python3を使いたいのでwsgi.confを次のように書き換える。

LoadModule mod_wsgi /usr/local/lib/python3.4/dist-packages/mod_wsgi/server/mod_wsgi-py34.cpython-34m.so

今回の場合、静的コンテンツをプロジェクト内ディレクトリに置きたくなかったので、/var/www/app1/indexに置くことにした。

WSGIDaemonProcessはpython-path=/var/www/app1/appを指定しないと、appモジュールが存在しないとエラーを吐き続けていたので記述した。これが分からず1時間くらい苦労した。

WSGIPythonPathは(デーモンモードの場合)必要がなかった。というかVirtualHost内に記述するとエラーとなった。

WSGIScriptAliasに関しては、もともとXXXX.wsgiしか許可できないようになっているっぽいので、プロジェクトファイル内のwsgi.pyをRequire all grantedとしている。

<VirtualHost *:80>
    ServerName app1.localhost.com
    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/app1/index
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

    WSGIScriptReloading On
    WSGIDaemonProcess app1.localhost.com user=www-data group=www-data threads=5 python-path=/var/www/app1/app
    WSGIProcessGroup app1.localhost.com
    WSGIScriptAlias / /var/www/app1/app/app/wsgi.py
    #WSGIPythonPath /var/www/app1/app/

    <Directory /var/www/app1/app/app/>
        <Files wsgi.py>
                Require all granted
        </Files>
    </Directory>

    Alias /static/ /var/www/app1/index/
    <Directory /var/www/app1/index>
        Require all granted
    </Directory>
</VirtualHost>

これで,http://app1.localhost.com/ http://app1.localhost.com/polls http://app1.localhost.com/admin に参照できるようになる。 また、/var/www/app1/index/に置いたファイルに対して、http://app1.localhost.com/static/****で参照できるようになる。 めでたし。

参考

https://torina.top/detail/289/

https://www.yoheim.net/blog.php?q=20170206

https://qiita.com/roy29fuku/items/578de62fbdd65f8ffbaa

Tags
javascript(109)
linux(54)
node.js(53)
amazon%20aws(47)
typescript(44)
%E3%82%A2%E3%83%AB%E3%82%B4%E3%83%AA%E3%82%BA%E3%83%A0(36)
%E7%94%BB%E5%83%8F%E5%87%A6%E7%90%86(30)
html5(29)
php(24)
centos(24)
python(22)
%E7%AB%B6%E6%8A%80%E3%83%97%E3%83%AD%E3%82%B0%E3%83%A9%E3%83%9F%E3%83%B3%E3%82%B0(21)
mac(21)
mysql(20)
canvas(19)
opencv(17)
%E9%9B%91%E8%AB%87(16)
docker(16)
wordpress(15)
atcoder(14)
apache(12)
%E6%A9%9F%E6%A2%B0%E5%AD%A6%E7%BF%92(12)
%E3%83%87%E3%83%BC%E3%82%BF%E3%83%99%E3%83%BC%E3%82%B9(12)
amazon%20s3(12)
red%20hat(12)
prisma(12)
ubuntu(11)
github(10)
git(10)
vue.js(10)
%E7%94%BB%E5%83%8F%E5%87%A6%E7%90%86100%E6%9C%AC%E3%83%8E%E3%83%83%E3%82%AF(10)
mariadb(10)
react(9)
aws%20cdk(9)
css3(8)
%E5%8F%AF%E8%A6%96%E5%8C%96(8)
%E5%B0%8F%E3%83%8D%E3%82%BF(8)
nestjs(8)
amazon%20lightsail(7)
next.js(7)
%E3%83%96%E3%83%AD%E3%82%B0(6)
cms(6)
oracle(6)
perl(6)
gitlab(6)
iam(5)
amazon%20ec2(5)
%E8%B3%87%E6%A0%BC%E8%A9%A6%E9%A8%93(5)
aws%20amplify(5)
curl(4)
Author
githubzennqiita
ただの備忘録です。

※外部送信に関する公表事項