We're looking for a clean and isolated way to host several Django sites on a single Apache with vhosts on Ubuntu 14.04.
Following the docs https://docs.djangoproject.com/en/1.10/howto/deployment/wsgi/modwsgi/#using-mod-wsgi-daemon-mode and Where should WSGIPythonPath point in my virtualenv? , we set the following setup :
Have a global virtualenv for mod_wsgi
virtualenv -p /usr/bin/python3 /home/admin/vhosts_venv
. vhosts_venv/bin/activate
pip install mod-wsgi
sudo /home/admin/vhosts_venv/bin/mod_wsgi-express install-module
sudo vi /etc/apache2/mods-available/wsgi_express.load
added :
LoadModule wsgi_module /usr/lib/apache2/modules/mod_wsgi-py34.cpython-34m.so
Then have a vhost venv and a basic app :
virtualenv -p /usr/bin/python3 /home/admin/vhost1_venv
. vhost1_venv/bin/activate
pip install Django
pip install PyMySQL
django-admin startproject vhost1
cd vhost1
python manage.py startapp main
Setup host resolution with :
sudo vi /etc/hosts
updated :
127.0.0.1 localhost vhost1.example.com
Setup Apache vhost with :
<VirtualHost vhost1.example.com:80>
ServerName vhost1.example.com
ServerAlias example.com
ServerAdmin webmaster@localhost
#DocumentRoot /var/www/html
ErrorLog ${APACHE_LOG_DIR}/vhost1_error.log
CustomLog ${APACHE_LOG_DIR}/vhost1_access.log combined
WSGIProcessGroup vhost1.example.com
WSGIScriptAlias / /home/admin/vhost1/vhost1/wsgi.py process-group=vhost1.example.com
WSGIDaemonProcess vhost1.example.com user=www-data group=www-data threads=25 python-path=/home/admin/vhost1:/home/admin/vhost1_venv/lib/python3.4/site-packages:/home/admin/vhosts_venv/lib/python3.4/site-packages
<Directory /home/admin/vhost1>
<Files wsgi.py>
<IfVersion < 2.3>
Order deny,allow
Allow from all
</IfVersion>
<IfVersion >= 2.3>
Require all granted
</IfVersion>
</Files>
</Directory>
</VirtualHost>
Enabled everything with :
sudo a2enmod wsgi_express
sudo a2ensite vhost1
sudo service apache2 restart
When testing it, we get 2 answers for a single curl request, delivered in 2 timing (sometime like 0.5s between each) :
curl vhost1.example.com
It worked! Congratulations on your first Django-powered page.
Of course, you haven't actually done any work yet. Next, start your first app by running python manage.py startapp [app_label].
You're seeing this message because you have DEBUG = True in your Django settings file and you haven't configured any URLs. Get to work!
Directly followed by :
Internal Server Error
The server encountered an internal error or misconfiguration and was unable to complete your request.
Please contact the server administrator at webmaster@localhost to inform them of the time this error occurred, and the actions you performed just before this error.
More information about this error may be available in the server error log. Apache/2.4.7 (Ubuntu) Server at vhost1.example.com Port 80
In /var/log/apache2/error.log
, we get :
[Mon Aug 15 15:37:42.754139 2016] [core:notice] [pid 18622:tid 140151787534208] AH00051: child pid 18717 exit signal Segmentation fault (11) , possible coredump in /etc/apache2
and in /var/log/apache2/vhost1_access.log
:
127.0.0.1 - - [15/Aug/2016:15:37:42 +0200] "GET / HTTP/1.1" 500 2593 "-" "curl/7.35.0"
How to set it up correctly?