Python 3 + mod_wsgi + Django
CentOS 6.5, Python 3.6.1, Django 1.11.1
ç§ãPythonã«ã¤ãã¦æã£ã¦ããç¥èã¨ããã®ã¯この程度ã®ãã¨ãããªãã
ã«ããã¨ããã¬ãã«ã§ãããªããããã®ãã³ãPythonã§ã¡ãã£ã¨ããWebã¢ããªãä½ã£ã¦ã¿ãæ©ä¼ããã£ãã
以ä¸ã¯è¨äºã¿ã¤ãã«ã®ç°å¢ãæ§ç¯ããæã®ã¡ã¢ã§ããã
Python 3.6ãã¤ã³ã¹ãã¼ã«ãã
ç§ã対象ã«ããç°å¢ã«ã¯Python 2.6ãã¤ã³ã¹ãã¼ã«ããã¦ãããããã¯yumãå
é¨çã«ä½¿ç¨ãã¦ãããããã
ããã§ã2.6ã¯æ®ããã¾ã¾ã3.6ãã¤ã³ã¹ãã¼ã«ãããã¨ã«ããã
$ wget https://www.python.org/ftp/python/3.6.1/Python-3.6.1.tgz $ tar zxf Python-3.6.1.tgz $ cd Python-3.6.1 # 64bit ç°å¢ã§ Python3 㨠mod_wsgi ã®çµã¿åããã®å ´åã-fPIC ãå¿ è¦ã¿ãã $ ./configure CFLAGS=-fPIC --enable-shared --prefix=/usr/local $ make $ sudo make install # Python ã® .so ãèªã¿è¾¼ããã¹ãéãã¦ãã $ export LD_LIBRARY_PATH=/usr/local/lib $ /usr/local/bin/python3.6 --version Python 3.6.1
virtualenvã使ã£ã¦Python Webã¢ããªã®ç¨¼åç°å¢ãæ§ç¯ãã
virtualenvã¯Rubyã®rbenvçãªãã®ã
pipã¯gemã¿ãããªããããªï¼
ã¨ããèªèã
ããã§ã¯ã/var/www/python/
以ä¸ã«Pythonã®Webã¢ããªããããã¤ããã¤ããã
ãã®ç°å¢ã venv ã¨ããä»®æ³ç°å¢ã¨ãã¦ãvenvå
ã§ã¯ Python 3.6 ã使ããã¨ã¨ããã
$ curl -kLO https://bootstrap.pypa.io/get-pip.py $ sudo LD_LIBRARY_PATH=/usr/local/lib /usr/local/bin/python3.6 $ sudo LD_LIBRARY_PATH=/usr/local/lib /usr/local/bin/pip3.6 install virtualenv $ sudo mkdir -p /var/www/python $ sudo chown myuid:mygid /var/www/python $ cd /var/www/python $ virtualenv venv --python=/usr/local/bin/python3.6 --no-site-packages $ source venv/bin/activate (venv) $ which python /var/www/python/venv/bin/python
Djangoãã¤ã³ã¹ãã¼ã«ãã
Djangoã£ã¦ã®ã¯ãPython Webã¢ããªã®ãã¬ã¼ã ã¯ã¼ã¯ãRoRã¿ãããªããã
ã¨ããèªèã
# pipã§Djangoãã¤ã³ã¹ãã¼ã« (venv) $ pip install Django # test_siteã¨ããããã¸ã§ã¯ããæ°è¦ä½æ (venv) $ django-admin.py startproject test_site (venv) $ cd test_site # éå½¢ããã¸ã§ã¯ããSQLiteã使ãããã«ãªã£ã¦ããããä»ã¯ä½¿ããªãã®ã§ã³ã¡ã³ãã¢ã¦ã (venv) $ vim test_site/settings.py DATABASES = { # 'default': { # 'ENGINE': 'django.db.backends.sqlite3', # 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'), # } } # ã²ã¨ã¾ãå å ã®HTTPãµã¼ãã¼ãèµ·åãã¦ã¿ãï¼ããã¯ã°ã©ã¦ã³ãèµ·åãããï¼ (venv) $ python manage.py runserver & Performing system checks... System check identified no issues (0 silenced). You have 13 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions. Run 'python manage.py migrate' to apply them. May 5, 2017 - 00:00:00 Django version 1.11.1, using settings 'myproject.settings' Starting development server at http://127.0.0.1:8000/ Quit the server with CONTROL-C. ^M # ãã¼ã«ã«ããHTTPçµç±ã§ã¢ã¯ã»ã¹ãã¦ã¿ã $ curl -s http://localhost:8000/ | grep "<h1>" <h1>It worked!</h1> # åãã¦ããã¿ãããªã®ã§ãã·ã£ãããã¦ã³ $ fg 1 python manage.py runserver ^C(venv) $
mod_wsgiã使ã£ã¦Apacheçµç±ã§ã¢ã¯ã»ã¹ã§ããããã«ãã
mod_wsgiã¯ããããApacheã®æ¡å¼µã¢ã¸ã¥ã¼ã«ã
Apacheã¸ã®ãªã¯ã¨ã¹ããPythonã«å¦çãããããã«å¿
è¦ã
ã½ã¼ã¹ããç´æ¥ã³ã³ãã¤ã«ãããããpipã§ã¤ã³ã¹ãã¼ã«ããæ¹ãè¯ããããã
(venv) $ pip install mod_wsgi
ããã§venvä»®æ³ç°å¢å ã«mod_wsgiãã¤ã³ã¹ãã¼ã«ãããã¯ããªã®ã§ããã¡ã¤ã«ãã¹ãæ¤ç´¢ããã
$ find /var/www/python/venv -name 'mod_*.so' /var/www/python/venv/lib/python3.6/site-packages/mod_wsgi/server/mod_wsgi-xxx.so
ãã®ãã¹ãã³ãããã¦ãhttd.confã«è¨å®ããã
$ sudo vim /etc/httpd/conf/httpd.conf LoadModule wsgi_module /var/www/python/venv/lib/python3.6/site-packages/mod_wsgi/server/mod_wsgi-xxx.so WSGIPythonHome /var/www/python/venv WSGIPythonPath /var/www/python/venv/lib/python3.6/site-packages WSGIScriptAlias /test-site /var/www/python/test_site/test_site/wsgi.py
- WSGIPythonHomeã¯ãApacheã«ä½¿ãããPythonã®ãã¼ã ãã£ã¬ã¯ããªã¼
- WSGIPythonPathã¯ãå®è¡ä¸ã«Pythonã«éããããã¹
- WSGIScriptAliasã¯å¼æ°ã2ã¤åãã1ã¤ãã®URLãã¢ã¯ã»ã¹ããããã2ã¤ãã®wsgiã¹ã¯ãªããã«ç§»è²ããã¨ããè¨å®
ã¨æãããã
ãã¦ãããã§httpd.confã®è¨å®ç¢ºèªãè¡ã£ãã¨ãããä¸è¨ã®ãããªã¨ã©ã¼ãçºçããã
$ apachectl configtest httpd: Syntax error on line 9999 of /etc/httpd/conf/httpd.conf: Cannot load /var/www/python/venv/lib/python3.6/site-packages/mod_wsgi/server/mod_wsgi-xxx.so into server: /var/www/python/venv/lib/python3.6/site-packages/mod_wsgi/server/mod_wsgi-xxx.so: undefined symbol: forkpty
ã©ãããç°å¢ã«ãã£ã¦ãªã®ããPython3ã§mod_wsgiã使ãã¨ãã®ãããªã¨ã©ã¼ãçºçãããããªã
äºåã«Apacheã«ã¨ããã©ã¤ãã©ãªã¼ããã¼ãããã¦ããã¨è¯ããããã
$ sudo vim /etc/sysconfig/httpd # 以ä¸2è¡ã追å export LD_PRELOAD=/usr/lib/libutil.so # ããã¯ãããªãå ´åããã export LD_LIBRARY_PATH=/usr/local/lib # Python ã -fPIC ã§ã³ã³ãã¤ã«ããå ´å㯠.so ãèªã¿è¾¼ã¾ããããã«ãã¦ããï¼è¨å®ç®æã¯ãããããªãã¦ãè¯ãï¼ $ sudo apachectl restart
ãã® libutil.so ã£ã¦ãã¤ã¯ãç§ã®ç°å¢ã«ã¯æ¢ã«å
¥ã£ã¦ããã
Apacheçµç±ã§Djangoã«ã¢ã¯ã»ã¹ãã¦ã¿ã
ãã¦ãã楽ãã¿ã®æéã ã
ã¿ããªå¤§å¥½ãã³ã¼ãã£ã³ã°ã
ã¾ããããã¸ã§ã¯ãåæåæã«èªåçæãããsettings.pyå ã®ALLOWED_HOSTSã¨ããè¨å®é ç®ã«ãéçºä¸ï¼ãããã°ã¢ã¼ãæï¼ã«ã©ãããã¢ã¯ã»ã¹ãããã®ããã¨ããè¨å®ãè¡ãã
$ vim /var/www/python/test_site/test_site/settings.py ALLOWED_HOSTS = ['127.0.0.1', 'localhost']
ALLOWD_HOSTSã®è¨å®ã¯ãããããå®éã«ã¢ã¯ã»ã¹ãããæã®HTTPãããã¼ã®ãã¹ãåã¨æ¯è¼ããããã®ã¨æãããã
ä¸è¨è¨å®ã§ã¯ãã¼ã«ã«ãã¹ãããããã¢ã¯ã»ã¹ã§ããªãã®ã§ãå®éã«ã¯ãã¯ã©ã¤ã¢ã³ãããã¢ã¯ã»ã¹ãããæã®ãµã¼ãã¼ã®ãã¹ãåãè¨å®ãã¦ãããªããã°ãªããªãã ããã
次ã«ãèªåçæãããwsgi.pyãã«ã¹ã¿ãã¤ãºããã
ã¨è¨ã£ã¦ããããã¸ã§ã¯ãã®ãã¹ã追å ããã ãã§ããã
$ vim /var/www/python/test_site/test_site/wsgi.py import os import sys # 追å from django.core.wsgi import get_wsgi_application sys.path.append('/var/www/python/test_site') # 追å sys.path.append('/var/www/python/test_site/test_site') # 追å os.environ.setdefault("DJANGO_SETTINGS_MODULE", "test_site.settings") application = get_wsgi_application()
ãã¹ãç¨ã«view.pyã¨ããåã®ã³ã³ããã¼ã©ã¼ãä½ã£ã¦ã¿ãã
ããã§ã¯ãåä½ç¢ºèªç¨ã«ãåã«Helloããã¹ããè¿ãã ãã¨ããã
$ touch /var/www/python/test_site/test_site/view.py $ vim /var/www/python/test_site/test_site/view.py from django.http import HttpResponse def hello(request): return HttpResponse('Hello Django\n', content_type='text/plain')
æå¾ã«ãurls.pyï¼ãã¡ããèªåçæï¼ãã«ã¹ã¿ãã¤ãºãã¦ãURLãããã³ã°ãè¡ãã
urlpatternsã«ã¯ãurlã¬ã³ã¼ããé
åã§ç»é²ããã
urlã¯å¼æ°ã3ã¤åãã
1ã¤ç®ã¯ãã¢ã¯ã»ã¹ãããURLã ããããã§ã¯æ£è¦è¡¨ç¾ ^$
ã¨ãã¦ãããããã¼ã¸ããã¹ãªãã§ã¢ã¯ã»ã¹ãããæãæå®ãã¦ããã
2ã¤ç®ã¯ããã®URLãã¢ã¯ã»ã¹ãããæã«å®è¡ãããé¢æ°ãæå®ãããããã§ã¯ãã«ã¬ã³ããã£ã¬ã¯ããªã¼ããimportããview.pyã®ä¸ã®helloé¢æ°ãæå®ãã¦ããã
$ vim /var/www/python/test_site/test_site/urls.py from django.conf.urls import url from django.contrib import admin from . import view # 追å urlpatterns = [ url(r'^$', view.hello, name='hello') # å ã ãã£ãè¨å®ãåé¤ãã¦è¿½å ]
åä½ç¢ºèªã
$ curl localhost/test-site/ Hello Django