Apache for Django 1.10 的共享主机设置

Shared hosting setup for on Apache for Django 1.10

我们正在寻找一种干净且独立的方式来在 Ubuntu 14.04 上使用虚拟主机在单个 Apache 上托管多个 Django 站点。

遵循文档 https://docs.djangoproject.com/en/1.10/howto/deployment/wsgi/modwsgi/#using-mod-wsgi-daemon-modeWhere should WSGIPythonPath point in my virtualenv? ,我们设置以下设置:

为 mod_wsgi

建立一个全局 virtualenv
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

已添加:

LoadModule wsgi_module /usr/lib/apache2/modules/mod_wsgi-py34.cpython-34m.so

然后有一个 vhost venv 和一个基本的应用程序:

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

设置主机分辨率为:

sudo vi /etc/hosts

已更新:

127.0.0.1       localhost vhost1.example.com

设置 Apache 虚拟主机:

<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>

启用一切:

sudo a2enmod wsgi_express
sudo a2ensite vhost1
sudo service apache2 restart

在测试时,我们得到了单个 curl 请求的 2 个答案,在 2 个时间交付(有时每个之间相隔 0.5 秒):

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!

紧接着:

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

/var/log/apache2/error.log中,我们得到:

[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

并在 /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"

如何正确设置?

总结 Graham Dumpleton 的回答

  • mod_wsgi 4.5.4 被 Python3 破坏了。 4.5.5 修复了它。
  • 可以为虚拟主机设置一个 Python virtualenv(使用软件包 mod_wsgi),但建议使用 python-home 选项设置一个 Python virtualenv,而不是 python-path.
  • 建议根据http://blog.dscpl.com.au/2009/11/save-on-memory-with-modwsgi-30.html设置WSGIRestrictEmbedded On

/etc/apache2/mods-available/wsgi_express.conf 看起来像:

WSGIRestrictEmbedded On

/etc/apache2/sites-available/vhost1.conf 看起来像:

<VirtualHost vhost1.example.com:80>
  ServerName vhost1.example.com
  ServerAdmin webmaster@localhost
  DocumentRoot /var/www/html

  ErrorLog ${APACHE_LOG_DIR}/vhost1_error.log
  CustomLog ${APACHE_LOG_DIR}/vhost1_access.log combined

  WSGIDaemonProcess vhost1.example.com threads=15 python-home=/home/admin/vhost1_venv python-path=/home/admin/vhost1
  WSGIScriptAlias / /home/admin/vhost1/vhost1/wsgi.py process-group=vhost1.example.com application-group=%{GLOBAL}

  <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>