Apacheã®ã¤ã³ã¹ãã¼ã«ããDjangoã®ãããã¤ã¾ã§
Amazon AMI + Apache + mod_wsgi + virtualenv + Django
Djangoã®ãã¥ã¼ããªã¢ã«ãéçºç¨ãµã¼ããåãã(runserver)ã¨ããã¾ã§ã®ãã®ãå¤ãã£ãã®ã§ãæ¬çªãµã¼ãã§åãã¾ã§ã®æé ãã¡ã¢ã
åä½ç°å¢ã¯ãAmazon AMI & Apache (+mod_wsgi)ã
mod_wsgiã¯Apacheã®ã¢ã¸ã¥ã¼ã«ã§ãpythonã®ã¢ããªã±ã¼ã·ã§ã³ãApacheä¸ã§åä½ããããã¡ãã£ã¨å¤ãmod_pythonã¯éæ¨å¥¨ãããã
ä»åã¯ãvirtualenvã使ã£ã¦Djangoãåããã®ã«ãpathã®æå®ã¨ããApacheã®è¨å®ãã¡ã¤ã«ã«æ¸ãå¿
è¦ããã£ã¦ãããã詳ããã¿ã¦ã¿ãã
Apache+mod_wsgiã®ã¤ã³ã¹ãã¼ã«ã¨è¨å®
å¿ è¦ã¢ã¸ã¥ã¼ã«ãã¤ã³ã¹ãã¼ã«
$ sudo yum install httpd httpd-devel mod_wsgi
Apacheã®ããã©ã«ãè¨å®ãã¡ã¤ã«ã¯ã/etc/httpd/conf/httpd.conf ã«ãããã©ã/etc/httpd/conf.d/ ç´ä¸ã«ãã*.conf ã追å ã§èªã¿è¾¼ãã§ãããã
mod_wsgiã®åä½ç¢ºèªã¨ãã¦ã/etc/httpd/conf.d/python.conf ãä½æããã
# mod_wsgiã®èªã¿è¾¼ã¿ LoadModule wsgi_module modules/mod_wsgi.so # /test ã¨ãããªã¯ã¨ã¹ãã«å¯¾ãã¦ã/var/www/cgi-bin/hello.py è¿ãã WSGIScriptAlias /test /var/www/cgi-bin/hello.py
åä½ç¢ºèªç¨ã« /var/www/cgi-bin/hello.py ãä½æã
/var/www/cgi-bin/hello.py
def application(environ, start_response): status = '200 OK' output = 'Hello World!' response_headers = [('Content-type', 'text/plain'), ('Content-Length', str(len(output)))] start_response(status, response_headers) return [output]
ããã§ãapacheãèµ·åãã¦ãhttp://yourServer.com/testã«ãã©ã¦ã¶ã§ã¢ã¯ã»ã¹ããã¨ãHello World!ã¨è¡¨ç¤ºããããããã§mod_wsgiã®åä½ã¯ãªãã±ã¼ã
(ãµã¼ãã¼ã®ååãyourServer.comã®ã¨ã)
# apacheãèµ·å $ sudo /etc/init.d/httpd start
virtualenvä¸ã§Djangoãåãã
pythonã¯ããã±ã¼ã¸ã®ãã¼ã¸ã§ã³ã§ä¾åé¢ä¿ãçµæ§æ¿ããã®ã§ãããã¸ã§ã¯ãåä½ã§virtualenv使ãã®ãã°ãããã¦ãã¦ã
pipãvirtualenvãã¤ã³ã¹ãã¼ã«ããããvirtualenvãä½æãvirtualenvãã¢ã¯ãã£ãã¼ãããããdjangoãã¤ã³ã¹ãã¼ã«ããã
$ sudo easy_install pip $ sudo pip install virtualenv # virtualenvãä½æ $ virtualenv ENV $ cd ENV $ . bin/activate # virtualenvãæå¹ã«ãã $ pip install django
djangoã®ããã¸ã§ã¯ã㯠/var/www/cgi-bin/ ã«ä½æããã
$ sudo su - # cd /var/www/cgi-bin/ # django-admin.py startproject test_proj # cd /var/www/cgi-bin/test_proj/test_proj
ããã¸ã§ã¯ããä½ã£ãã¨ãã«ãèªåã§ä½æããã /var/www/cgi-bin/test_proj/test_proj/wsgi.py ãç·¨éãããã§ãdjangoãåãããã®ã¯virtualenvç°å¢ã ããªã®ã§ãvirtualenvã§ä½¿ç¨ããpythonã®ãã¹ãæãã¦ãããã
/var/www/cgi-bin/test_proj/test_proj/wsgi.py
import os import site import sys # virtualenvã®ããã±ã¼ã¸ãã¹ site.addsitedir("/home/ec2-user/ENV/lib/python2.6/site-packages") sys.path.append('/var/www/cgi-bin/test_proj') sys.path.append('/var/www/cgi-bin/test_proj/test_proj') os.environ.setdefault("DJANGO_SETTINGS_MODULE", "test_proj.settings") # virtualenvã®å®è¡ã³ã¼ãã®ãã¹ activate_env = os.path.expanduser("/home/ec2-user/ENV/bin/activate_this.py") execfile(activate_env, dict(__file__=activate_env)) from django.core.wsgi import get_wsgi_application application = get_wsgi_application()
ãã£ãä½ã£ããapacheè¨å®ãã¡ã¤ã«ã«è¿½å ã§æ¸ãè¾¼ã¿ã
/etc/httpd/conf.d/python.conf
# mod_wsgiã®èªã¿è¾¼ã¿ LoadModule wsgi_module modules/mod_wsgi.so # /test ã¨ãããªã¯ã¨ã¹ãã«å¯¾ãã¦ã/var/www/cgi-bin/hello.py è¿ãã WSGIScriptAlias /test /var/www/cgi-bin/hello.py # /test_django ã¨ãããªã¯ã¨ã¹ãã«å¯¾ãã¦ã/var/www/cgi-bin/test_proj/test_proj/wsgi.py è¿ãã WSGIScriptAlias /test_django /var/www/cgi-bin/test_proj/test_proj/wsgi.py # /var/www/cgi-bin/test_proj/test_projã®ã¢ã¯ã»ã¹å¶éãè¨å® <Directory /var/www/cgi-bin/test_proj/test_proj> Order deny,allow Allow from all </Directory>
# apacheãåèµ·å $ sudo /etc/init.d/httpd restart
ããã§ãhttp://yourServer.com/test_proj ã«ã¢ã¯ã»ã¹ã㦠It worked! ã¨è¡¨ç¤ºãããã°ãªãã±ã¼ã
ãã¾ãåããªãã¨ã
- ãµã¼ãã¼ããã¬ã¹ãã³ã¹ããªãã¨ã(ãã£ã¨èªã¿è¾¼ã¿)
ãµã¼ãã¼ã®ãã¼ãçªå·80ã空ãã¦ãªãããapacheãåãã¦ãªãã
- Not Found (ãã¼ã¸ãè¦ã¤ããã¾ãã)
apacheã¯åãã¦ããã©ããã¡ã¤ã«ããªã or ãã£ã¬ã¯ããªãã¹ãééã£ã¦ãã
/etc/httpd/conf.d/python.conf ã§ãæ£ãããã¡ã¤ã«ã®ãã¹ãæå®ã§ãã¦ããããã¿ãã
- Internal Error (ãµã¼ãã¼å é¨ã¨ã©ã¼)
ãã¹ãæ£ãããã©ãwsgi.pyã®é¨åã§ã¨ã©ã¼ãçºçãã¦ããã
error_logãè¦ã¦ãã¨ã©ã¼ãç¹å®ããã
less /var/log/httpd/error_log
ãã¶ããimport errorãpermission deniedãã¨ã©ã¼ã®åå ã
import errorã¯ãå¿
è¦ãªã¢ã¸ã¥ã¼ã«ãinstallããã
activate_this.pyã«å¯¾ãã¦ãpermission deniedããã£ããããã£ã¬ã¯ããªã«å¯¾ãã¦permissionãå¤æ´ãããã§çµæ§ã¯ã¾ã£ãã
$ sudo chmod 755 ec2-user/