• Ubuntuに関連したハード、ソフトの備忘録サイトです

DjangoアプリケーションのApacheへのデプロイ【暫定投稿】

Djangoの公式サイトに記載されているDjango1.5アプリケーション(投票システム)を本番サーバーにデプロイする方法について、暫定投稿します。

環境としては、サーバーOSはUbuntu13.04、Djangoのバージョンは1.53、Pythonは2.7.4です。
記載されているとおりにプロジェクト、アプリを作成します。ディレクトリ構造は次のようにしてあります。

/home/user/Django/mysite2/mysite2
/polls/static/polls, admin
/templates

この下で、/etc/apache2/site-available/defaultと/home/usr/Django/mysite2/mysite2/wsgi.pyを次のようにしました。これは、Django1.4のマニュアルの和訳によるものです。
サイトは、http://docs.djangoproject.jp/en/latest/howto/deployment/wsgi/modwsgi.htmlです。
なお、http://docs.djangoproject.jp/en/latest/には、1.4の公式マニュアルの和訳があり、少からず参考になります。

まず、defaultは

# WSGIPythonPath /home/michiaki/Django/mysite2

<VirtualHost *:80>
ServerAdmin webmaster@localhost
    ServerName  localhost
    DocumentRoot /home/michiaki/Django/mysite2

    <Directory />
    Options FollowSymLinks
        AllowOverride None
</Directory>

    <Directory /var/www/>
    Options Indexes FollowSymLinks MultiViews
        AllowOverride None
        Order allow,deny
        allow from all
</Directory>

    ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/

    <Directory “/usr/lib/cgi-bin”>
        AllowOverride None
        Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
        Order allow,deny
        Allow from all
</Directory>

    ErrorLog ${APACHE_LOG_DIR}/error-django.log

# Possible values include: debug, info, notice, warn, error, crit,
    # alert, emerg.
    LogLevel warn

    CustomLog ${APACHE_LOG_DIR}/access-django.log combined

    WSGIScriptAlias / /home/michiaki/Django/mysite2/mysite2/wsgi.py
    
    Alias /static/  /home/michiaki/Django/mysite2/polls/static/
  
    <Directory /home/michiak/Django/mysite2>
      <Files wsgi.py>
        Order deny,allow
        Allow from all
      </Files>
    </Directory>

</VirtualHost>

<VirtualHost *:80>
    ServerAdmin webmaster@localhost
    ServerName  www.django.com
    DocumentRoot /var/www/html
    <Directory />
        Options FollowSymLinks
        AllowOverride None
    </Directory>
    <Directory /var/www/>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride None
        Order allow,deny
        allow from all
    </Directory>

    ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
    <Directory “/usr/lib/cgi-bin”>
        AllowOverride None
        Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
        Order allow,deny
        Allow from all
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/error.log

# Possible values include: debug, info, notice, warn, error, crit,
    # alert, emerg.
    LogLevel warn

    CustomLog ${APACHE_LOG_DIR}/access.log combined

</VirtualHost>

wsgi.pyは

“””
This application via the “WSGI_APPLICATION“ setting.Usually you will have the standard Django WSGI application here, but it also
might make sense to replace the whole Django WSGI application with a custom one
that later delegates to the Django one. For example, you could introduce WSGI
middleware here, or combine a Django application with an application of another
framework.
“””
import os
import sys
sys.path.append(“/home/michiaki/Django/mysite2”)←付け加える


# We defer to a DJANGO_SETTINGS_MODULE already in the environment. This breaks
# if running multiple sites in the same mod_wsgi process. To fix this, use
# mod_wsgi daemon mode with each site in its own daemon process, or use
# os.environ[“DJANGO_SETTINGS_MODULE”] = “mysite2.settings”
os.environ.setdefault(“DJANGO_SETTINGS_MODULE”, “mysite2.settings”)# This application object is used by any WSGI server configured to use this

# file. This includes Django’s development server, if the WSGI_APPLICATION
# setting points here.
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()

# Apply WSGI middleware here.
# from helloworld.wsgi import HelloWorldApplication

この後、/usr/local/lib/python2.7/dist-packages/django/contrib/admin/staticの中身を/home/user/Django/mysite2/polls/staticにコピーします。そのうえで、$ sudo service apache2 restartとして、Apacheを再起動させます。ApacheのVirtualHost機能で、/var/www/html以下に通常のサイトも作成できます。

そうすると、http://localhost/adminで次のような画面になります。また、開発段階で作成したstyle.cssも反映されています。

django01

django02

開発サーバーで効いていたstyle.cssがApacheても効いています。ほかにもいろいろな方法があるようですが、手っ取り早い感じです。これで、Pythonを使えばサーバー管理ツールからデスクトップアプリケーション、Webアプリケーション、クラウド・コンピューティングまで広範囲なシステムを開発できます。PHPとはまた異なる楽しさがあります。

※WSGI(Web Server Gateway Interface=ウィズギと呼ぶそうです=)とは
WSGIは、Pythonにおける、WebアプリケーションとWebサーバを接続する標準仕様を定めるものであり、これによって、WSGIに対応したWebアプリケーション(やフレームワーク)は、WSGIに対応した任意のWebサーバ上で運用できるようになる、とのことです。

コメントを残す

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

CAPTCHA