Python/Java/RubyのWebアプリケーション開発を行う場合、最終的には当然、本番のサーバーへのデプロイが必要になりますが、サイト管理者にとってはこれがなかなかの難物でした。しかし、NginxにUbuntu14.04LTSでインストールできるuWSGI(ユーウィズギ?)を使うと、いとも簡単にデプロイできます。
Djangoの本家サイトには、1対多のテーブルモデルを使った簡易Web投票システム構築の例題がありますが、これに沿って開発サーバーで確認したものを、Nginxにデプロイしたものが下記の図です。ただし、データベースは例題のSQLite3ではなく、Ubuntu14.04LTSでインストールできるMySQL(当面、MariaDBに変更するつもりはなさそうです)を使用しましたが、Python-MySQL.connectorを使えば、問題なくMySQLサーバーにアクセスでき、特に問題なく例題を最後まで追跡することができました。
これは、Djangoのバックエンドを表示したものです。Nginx+uWSGIで動作しています。今後、次の順序で投稿する予定です。
まあ、何にしてもPython2系列とPython3系列では、仕様がかなり変わっていますので、注意が必要でした。
【1】uWSGIのインストール
そもそも、WSGI(Web Server Gateway Interface=ウィズギ)とは(インターネット上のクライアント→Webブラウザ)→Webサーバーにアクセスした際に、Webサーバーと(各種フレームワークを使った)Webアプリケーションとの間の通信(データのやりとり)の規約を定めたもの、というように理解しております。実際には、Pythonにはwsgirefパッケージ(機能を実現するための多数のモジュール=クラスの集まり)というものがあって、これが、インターネット上のユーザーがWebサーバーにアクセス(Request)した際に、フレームワークを使ったWebアプリケーションとのデータのやりとりを中継しているらしい。
ということです。そこで、インストールですが、
sudo apt-get install uwsgi uwsgi-core uwsgi-plugin-python3 uwsgi-plugin-sqlite3
でインストールするということになっているようですね。
【2】Djangoのsettings.pyとurls.pyの設定
まず、settings.pyについて。
""" Django settings for mysite project. For more information on this file, see https://docs.djangoproject.com/en/1.6/topics/settings/ For the full list of settings and their values, see https://docs.djangoproject.com/en/1.6/ref/settings/ """ # Build paths inside the project like this: os.path.join(BASE_DIR, ...) import os BASE_DIR = os.path.dirname(os.path.dirname(__file__)) # Quick-start development settings - unsuitable for production # See https://docs.djangoproject.com/en/1.6/howto/deployment/checklist/ # SECURITY WARNING: keep the secret key used in production secret! SECRET_KEY = '+i04!lrfy3*tok9o5npf6)3jgc*g8xr7aa9u7ock@t@m4*r=b2' # SECURITY WARNING: don't run with debug turned on in production! DEBUG = True TEMPLATE_DEBUG = True ALLOWED_HOSTS = [] # Application definition INSTALLED_APPS = ( 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'polls', ) MIDDLEWARE_CLASSES = ( 'django.contrib.sessions.middleware.SessionMiddleware', 'django.middleware.common.CommonMiddleware', 'django.middleware.csrf.CsrfViewMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.contrib.messages.middleware.MessageMiddleware', 'django.middleware.clickjacking.XFrameOptionsMiddleware', ) ROOT_URLCONF = 'mysite.urls' WSGI_APPLICATION = 'mysite.wsgi.application' # Database # https://docs.djangoproject.com/en/1.6/ref/settings/#databases DATABASES = { 'default': { # 'ENGINE': 'django.db.backends.sqlite3', # 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'), # 'ENGINE': 'django.db.backends.mysql', 'ENGINE': 'mysql.connector.django', 'NAME': 'django_db', 'USER': 'django', 'PASSWORD': 'seiban', 'HOST': 'localhost', } } # Templates Dir #TEMPLATE_DIRS = [os.path.join(BASE_DIR, 'templates')] TEMPLATE_DIRS = ("/var/www/mysite/templates",) # STATICFILES_DIRS = ("/var/www/mysite/static",) # Internationalization # https://docs.djangoproject.com/en/1.6/topics/i18n/ LANGUAGE_CODE = 'ja' TIME_ZONE = 'Asia/Tokyo' USE_I18N = True USE_L10N = True USE_TZ = True # Static files (CSS, JavaScript, Images) # https://docs.djangoproject.com/en/1.6/howto/static-files/ STATIC_URL = '/static/' # Adding by Nomura SITE_ROOT = "/var/www/mysite" # Adding by Nomura STATIC_ROOT = "/var/www/mysite/static"
SITE_ROOTとSTATIC_ROOTは開発段階では必要ないとのことですが、Nginxにデプロイする際は必須。なお、投票システムの例題は、/var/www/mysiteにインストールしています。なお、DATABASESのところにMySQLを使用する際の設定を追加してあります。
次に、urls.pyについて。
from django.conf.urls import patterns, include, url from django.contrib import admin admin.autodiscover() urlpatterns = patterns('', # Examples: url(r'^$', include('polls.urls')), # url(r'^blog/', include('blog.urls')), url(r'^polls/', include('polls.urls', namespace='polls')), url(r'^admin/', include(admin.site.urls)), url(r'^static/(?P.*)$', 'django.views.static.serve',{'document_root':'/var/www/mysite/static', 'show_indexes': True}), )
最後の一行は、CSSやJava Script、画像データの配信に必要とのことです。これがないと、バックエンドが文字だけになってしまいます。