How to Run Django with mod_wsgi and Apache with a virtualenv Python environment on a Debian VPS

python > How to Run Django with mod_wsgi and Apache with a virtualenv Python environment on a Debian VPS
21.12.2015 18:14:30

Наиболее часто встречающиеся слова в статье:

[virtual] [default] [sampleapp] [mod_wsgi] [/var/www/sampleapp] [configuration] [install] [--version] [application] [Установка]



apt-get install python-pip python-virtualenv


virtualenv --version
pip --version


Установка Django

cd /var/www/html
mkdir sampleapp
cd sampleapp

Тогда давайте создадим новую виртуальную среду с помощью virtualenv .

Чтобы создать новую виртуальную среду введите

virtualenv env



root@django:/var/www/sampleapp# env/bin/pip --version
pip 1.1 from /var/www/sampleapp/env/lib/python2.7/site-packages/pip-1.1-py2.7.egg (python 2.7)
root@django:/var/www/sampleapp# env/bin/python --version
Python 2.7.3


Другой способ состоит в активизации окружающей среды во-первых, с помощью

source env/bin/activate



Installing Django inside virtual environment

env/bin/pip install django


root@django:/var/www/sampleapp# env/bin/python
Python 2.7.3 (default, Mar 13 2014, 11:03:55)
[GCC 4.7.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import django

Установка первого проекта
env/bin/ startproject sampleapp .

Пожалуйста, обратите внимание на завершающий . в команде - без него проект будет создан в
качестве дополнительного подкаталоге. После выполнения этой команды новый каталог SampleApp и сценарий будет создан в / вар / WWW / SampleApp . скрипт используется для выполнения команды Django для данного проекта .

env/bin/python runserver запуск тестового сервера разработчика

Installing mod_wsgi for Apache
The easiest and also recommended way to serve Python applications using Apache is to use mod_wsgi module. It is not installed by default with neither Python nor Apache, so we have to install an additional package.

apt-get install libapache2-mod-wsgi
The next step will be to configure default Apache virtual host that at the beginning of the article served It works! page to serve our Django application.

Configuring mod_wsgi in a default virtual host
The idea behind configuring mod_wsgi for any other virtual host in Apache is the same as the one presented here. We will use the default virtual host for simplicity, since it is the one already provided by a clean Apache installation.

Open the default virtual host configuration file in nano editor

Сделаем так, чтобы Django проект открывался при обращении на 80 порту

nano /etc/apache2/sites-enabled/000-default
and add three following lines just below <VirtualHost *:80>

WSGIDaemonProcess sampleapp python-path=/var/www/sampleapp:/var/www/sampleapp/env/lib/python2.7/site-packages
WSGIProcessGroup sampleapp
WSGIScriptAlias / /var/www/sampleapp/sampleapp/


The first line spawns a WSGI daemon process called sampleapp that will be responsible for serving our Django application. The daemon name can be basically anything, but is good practice to *use descriptive names such as application names here.

If we were using global Python installation and global Django instance, the python-path directive would not be necessary. However, using virtual environment makes it obligatory to specify the alternate Python path so that mod_wsgi will know where to look for Python packages.

The path must contain two directories: the directory of Django project itself - /var/www/sampleapp - and directory of Python packages inside our virtual environment for that project - /var/www/sampleapp/env/lib/python2.7/site-packages. Directories in path definition are delimited using a colon sign.

The second line tells that particular virtual host to use the WSGI daemon created beforehand, and as such, the daemon name must match between those two. We used sampleapp in both lines.

The third line is the most important, as it tells Apache and mod_wsgi where to find WSGI configuration. The supplied by Django contains the barebone default configuration for WSGI for serving Django application that works just fine and changing the configuration in this file is out of this article scope.

After these changes it is necessary to restart Apache

service apache2 restart
After that, upon opening the web browser on your server IP address, without any additional ports, you should see the same Django page as before instead of initial It works! page that we have seen earlier.

That makes our configuration complete.

Please note: using the default virtual host without additional care is not the recommended way of configuring a production server. It is used for demonstration purposes.