LoginSignup
12

More than 5 years have passed since last update.

python3 Django1.9 を mod_wsgi で動かす ( デプロイする )

Posted at

mod_wsgiでDjangoデプロイ環境を作る手順についてまとめた。
備忘録メインなので、手順しか書いてない。ちゃんと理解したい方は他の記事も見たほうが良いでしょう。

環境

  • Ubuntu Server 14.04
  • python3.5.1 (on pyenv)
  • Django1.9.4
  • mod_wsgi

環境構築

sshでUbuntuサーバーに接続し、環境を整えていく。

apache

apacheのインストールがまだの場合は下記コマンドでインストールする。

$ sudo apt-get install apache2

python , Django

次にpyenvからpythonをインストールする。下記参照。(pythonのバージョンを3.5.1に直そう)
http://qiita.com/tonkatu05/items/d0422a2050d669c72f54#1-%E7%92%B0%E5%A2%83%E6%A7%8B%E7%AF%89

$ python --version
Python 3.5.1

Djangoもインストールする。

$ pip install django==1.9.4

mod_wsgi

次にmod_wsgiをインストールする。

$ sudo apt-get install libapache2-mod-wsgi-py3

(※ 別の方法 mod_wsgiをソースからインストールする )

まずはHello World から

/home/hoge/hello.pyを作成する。

hello.py

import sys

def application(environ, start_response):
    status = '200 OK'
    output = b'Hello World! python version : ' + sys.version.encode("utf-8")

    response_headers = [('Content-type', 'text/plain'),
                        ('Content-Length', str(len(output)))]
    start_response(status, response_headers)
    return [output]

/etc/apache2/sites-available/python.confを作成する。

python.conf

LoadModule wsgi_module modules/mod_wsgi.so

WSGIScriptAlias / /home/hoge/hello.py
# WSGIScriptAlias / /home/hoge/TestProject/TestProject/wsgi.py
WSGIPythonPath /home/hoge/TestProject:/home/hoge/.pyenv/versions/3.5.1/lib/python3.5/site-packages

<Directory /home/hoge>
   Require all granted
</Directory>

設定を反映させる。

$ sudo a2ensite python.conf

apacheを再起動する。

$ sudo service apache2 restart

ブラウザからアクセスしてみる。

Hello World! python version : 3.4.3 (default, Oct 14 2015, 20:31:36) 
[GCC 4.8.4]

と表示される。
(python のバージョンが謎)

Django を動かす

$ cd 
$ django-admin startproject TestProject

/home/hoge/TestProject/TestProject/wsgi.pyを編集しする。

wsgi.py
"""
WSGI config for TestProject project.

It exposes the WSGI callable as a module-level variable named ``application``.

For more information on this file, see
https://docs.djangoproject.com/en/1.9/howto/deployment/wsgi/
"""

import os
import sys

from django.core.wsgi import get_wsgi_application

sys.path.append('home/hoge/TestProject')
sys.path.append('home/hoge/TestProject/TestProject')

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "TestProject.settings")

application = get_wsgi_application()

/etc/apache2/sites-available/python.confを編集しする。
( WSGIScriptAlias / /home/hoge/hello.py をコメントアウトし、
WSGIScriptAlias / /home/hoge/TestProject/TestProject/wsgi.py をアンコメントする)

python.conf

LoadModule wsgi_module modules/mod_wsgi.so

# WSGIScriptAlias / /home/hoge/hello.py
WSGIScriptAlias / /home/hoge/TestProject/TestProject/wsgi.py
WSGIPythonPath /home/hoge/.pyenv/versions/3.5.1/lib/python3.5/site-packages

<Directory /home/hoge>
   Require all granted
</Directory>
$ sudo service apache2 restart

またブラウザからアクセスしてみる
Djangoの「It worked!」の画面が表示されたら成功。

image

別の方法

mod_wsgiをソースからインストールする

hello.pyの結果を見て分かるように何故かpythonのバージョン3.4.3が使われてた。mod_wsgiをapt-get でインストールするとバージョンを指定できない感じなのかな〜。どういう仕組みか分からないけど、バージョンの差異で何か不便があるかもしれない、、、?(バージョン周りの知識にうとい)

ということでmod_wsgiをソースからインストールする方法も試してみた。こちらは結構手ごわかった。結局pythonの3.5-devというのをインストールすることになった。エラーコードでぐぐってstackoverflowとか頑張って読みながら進めた感じで、自分でも色々意味が分かっていない。

前置きは終わりにして、以下手順で多分ok。

とりあえずインストールしたものを消す。(インストールしていた場合)

$ apt-get remove libapache2-mod-wsgi-py3
$ pyenv uninstall 3.5.1

apache2-devをインストール

$ sudo apt-get install apache2-dev

python3.5-devをインストールしようとすると、

$ CONFIGURE_OPTS="--enable-shared" pyenv install 3.5-dev
Cloning https://hg.python.org/cpython...
error: please install `mercurial` and try again

と言われるので、mercurialをインストール

$ sudo apt-get install mercurial
$ CONFIGURE_OPTS="--enable-shared" pyenv install 3.5-dev

mod_wsgiをインストール

$ cd 
$ wget https://github.com/GrahamDumpleton/mod_wsgi/archive/4.4.13.tar.gz
$ tar zxvf 4.4.13.tar.gz
$ mv mod_wsgi-4.4.13 /usr/local/src
$ cd /usr/local/src/mod_wsgi-4.4.13
$./configure LDFLAGS='-Wl,-rpath=/home/hoge/.pyenv/versions/3.5-dev/lib'
$ make
$ sudo make install

apacheにモジュールを適用

$ sudo a2enmod wsgi

apacheを再起動

$ sudo service apache2 restart

まとめ

文献とか探すのとかけっこう大変で時間かかった。環境構築に詳しい人を隣に置いて作業したい...(_ _)
何か変なことやってたらぜひ教えてください。

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
12