之前就已经写过一篇《在linux服务器上部署django项目》,在那篇博文里,我用的操作系统是ubuntu的,而且没有配置域名,直接在一台服务器下放的一个网站,而且用的是mod_wsgi的全局模式。今天的这篇,我将使用mod_wsgi的daemon mode,而且配置多个网站–一个域名对应多个网站。
我的环境:
centos6.7
Apache/2.2.15
python2.7.12
django1.9.7
mod_wsgi4.5.3
本文的前提是环境已经配好了,如果没有配好,可以参考《在linux服务器上部署django项目》。
接下来,以2个django项目,1个php项目为例,进行网站的配置。
2个django项目的地址:
/mywebsite/django1
/mywebsite/django2
php项目的地址
/mywebsite/myphp
使用mod_wsgi的daemon mode
这个例子,我们将第一个django项目配置到服务器上,使用mod_wsgi的daemon mode。
对应的httpd.conf文件应该这么写:
<VirtualHost *:80>
ServerName lookfor404.com # 域名
WSGIDaemonProcess django1 python-path=/mywebsite/django1
WSGIProcessGroup django1
WSGIScriptAlias / /mywebsite/django1/yourproject/wsgi.py #在 /mywebsite/django1 下,project和app在同一级目录
<Directory /mywebsite/django1/yourproject>
<Files wsgi.py>
Order Deny,Allow
Allow from All
</Files>
</Directory>
#别忘了静态文件配置,这个和一般的php项目是类似的
Alias /static "/your/path/collectedstatic"
<Directory "/your/path/collectedstatic">
Order deny,allow
Allow from all
</Directory>
</VirtualHost>
但是据测试,有时候可能会出现错误,在log里面提示如下:
Permission denied: mod_wsgi (pid=10458): Unable to connect to WSGI daemon process ‘lookfor404’ on ‘/etc/httpd/logs/wsgi.10453.0.1.sock’ as user with uid=11.
逛了半天的stackoverflow,最终看到一个靠谱的答案提到这个网址,
https://code.google.com/archive/p/modwsgi/wikis/ConfigurationIssues.wiki#Location_Of_UNIX_Sockets
解决方法就是,在virtualhost外面加多一句
WSGISocketPrefix /var/run/wsgi
一个域名对应多个网站
接下来,看看怎么用一个域名配置多个网站。
比如,我想要的效果是这样的:访问lookfor404.com/django1,进入django1这个项目;访问lookfor404.com/django2,进入django2这个项目;访问lookfor404.com/myphp,进入myphp这个项目。同样的,直接看配置文件。为了看起来清楚,我把静态文件设置先忽略了。
#----------django1项目配置
<VirtualHost *:80>
ServerName lookfor404.com # 域名
WSGIDaemonProcess django1 python-path=/mywebsite/django1
WSGIProcessGroup django1
#注意,以下发生变化,即第二个参数为访问的路径
WSGIScriptAlias /django1 /mywebsite/django1/yourproject/wsgi.py
<Directory /mywebsite/django1/yourproject>
<Files wsgi.py>
Order Deny,Allow
Allow from All
</Files>
</Directory>
</VirtualHost>
#----------django2项目配置
<VirtualHost *:80>
ServerName lookfor404.com # 域名
WSGIDaemonProcess django2 python-path=/mywebsite/django2
WSGIProcessGroup django2
#如下,即访问url为lookfor404.com/django2
WSGIScriptAlias /django2 /mywebsite/django2/yourproject/wsgi.py
<Directory /mywebsite/django2/yourproject>
<Files wsgi.py>
Order Deny,Allow
Allow from All
</Files>
</Directory>
</VirtualHost>
#----------myphp项目配置
<VirtualHost *:80>
ServerName lookfor404.com # 域名
DocumentRoot /mywebsite/myphp
<IfModule alias_module>
#设置访问url为lookfor404.com/myphp
Alias /myphp "/mywebsite/myphp"
<Directory "/mywebsite/myphp">
AllowOverride all
Order allow,deny
Allow from all
</Directory>
</IfModule>
</VirtualHost>
总结来说,这种配置适用于你只有一个域名,而你有多个网站程序,而且你不使用子域名。对于django项目,通过WSGIScriptAlias的第一个参数就可以设定访问的域名子路径了,对于php项目,使用Alias即可解决问题。
另外,多个域名多个网站这种情况就不说了,直接在servername里设置就行。
django项目出现WSGI Bad Bad Request (400) 错误
最后还要说一下这个错误,明明之前一切配置都顺利,用ip访问也没问题,但是一用域名访问就出错了,原来是新版django的问题,需要到setting.py里面设置可访问的域名:
ALLOWED_HOSTS = [
‘lookfor404.com’, # 只允许主域名
‘.lookfor404.com’, # 允许主域名以及子域名
‘.lookfor404.com.’, # 允许FQDN以及子域名
]
然后重启一下apache就行了。





