[PYTHON] Django --Apache mod_wsgi virtualhost deployment

Django --Apache mod_wsgi virtualhost deployment

I'll leave it as a memo, but Backlift at the URL below may be easy to remember with the service. Also, the directory structure has changed in the Django version, so that needs to be fixed.

http://jp.techcrunch.com/2013/04/11/20130410yc-backlift-launch/

Directory structure

sample
      |-wsgi
         |-dispatch.wsgi
      |-src
          |-sample
               |-settings.py
               |-・ ・ ・

dispatch.wsgi

#!/usr/bin/python
# -*- coding: utf-8 -*-
import os
import site
import sys
sys.stdout = sys.stderr

PROJECT_ROOT = os.path.abspath(os.path.dirname(os.path.dirname(__file__)))

# Add the virtual Python environment site-packages directory to the path
site.addsitedir(os.path.join(PROJECT_ROOT, 'python', 'lib', 'python2.7', 'site-packages'))

# Avoid ``[Errno 13] Permission denied: '/var/www/.python-eggs'`` messages
os.environ['PYTHON_EGG_CACHE'] = os.path.join(PROJECT_ROOT, 'wsgi', 'egg-cache')

#If your project is not on your PYTHONPATH by default you can add the following
sys.path.append(os.path.join(PROJECT_ROOT, 'src', 'sample'))
os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'

import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()

Place the following files in sites-available

sample

<VirtualHost *:80>
ServerName Server name

    # Log Files
    ErrorLog /var/log/apache2/sample.error-lostquery.log
    CustomLog /var/log/apache2/sample.access-lostquery.log combined

    RewriteEngine On
    RewriteCond  %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-f
    RewriteRule  .*robots\.txt$          ~/sample/www/robots.txt [L]

    Alias /www/ ~/sample/www/

    <Directory ~/sample/www>
    Order deny,allow
    Allow from all
    </Directory>

    # Setup mod_wsgi
    WSGIScriptAlias / ~/sample/wsgi/dispatch.wsgi

    <Directory ~/sample/wsgi>
     Order deny,allow
     Allow from all
    </Directory>
 </VirtualHost>

After activating the site with Apache, restart

Recommended Posts

Django --Apache mod_wsgi virtualhost deployment
CentOS8 + Apache2.4 + pyenv + mod_wsgi + Django project deployment
Django / Apache / mod_wsgi: No module named importlib
Django + Apache with mod_wsgi on Windows Server 2016
CentOS 6.4 with Python 2.7.3 with Apache with mod_wsgi and Django
Run django applications on Windows + Apache + mod_wsgi + services.
Publish your Django app on Amazon Linux + Apache + mod_wsgi
Python3 + Django ~ Mac ~ with Apache
Django Getting Started: 3_Apache integration
macports Apache, Python 3.3 + non-macports mod_wsgi3.4
Create an environment for Django x Apache x mod_wsgi with Vagrant (Ubuntu 16.04)
Set multiple WSGIPythonPath with Apache + mod_wsgi