apache wsgi 配置 django 多环境的方法,包括print utf8中文不报错的,上传中文附件名不报错

apache的配置

################################################################################################

Listen 8001
<VirtualHost *:8001>
ServerName www.liangtuba.com

ServerAlias liangtuba.com
DocumentRoot "/home/bygk/workspace/zcmsn/"
WSGIScriptAlias / /home/bygk/workspace/zcmsn/zcmsn/zcmsn/zcmsn.wsgi
Alias "/static" "/home/bygk/workspace/zcmsn/zcmsn/zcmsn/staticroot"
<Location "/static">
SetHandler None
</Location>

Alias "/site_media" "/home/bygk/workspace/zcmsn/zcmsn/zcmsn/media"
<Location "/site_media">
SetHandler None
</Location>

ErrorLog /var/log/apache2/zcmsn_error.log

# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel info

CustomLog /var/log/apache2/zcmsn_access.log combined

 

#301重定向

<Directory  /home/bygk/workspace/zcmsn/zcmsn/zcmsn>
Order deny,allow
Allow from all

 

Options -Indexes +FollowSymlinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^liangtuba.com$ [NC]
RewriteRule ^zcmsn.wsgi/(.*)$ http://www.liangtuba.com/$1 [L,R=301]
</Directory>

 

 

 

</VirtualHost>

 

####################################################################################################

 

wsgi的配置

####################################################################################################

#coding=utf8
import os
import sys
sys.stdout = sys.stderr #输出日志到apache,django中print的utf8格式的中文,任意格式的非中文到error.log中,注意这里不管制定没有制定错误日志文件,都是输出到error.log,ubuntu的apache error.log在/var/log/apache2/error.log
sys.path.insert(0, '/home/bygk/workspace/zcmsn/virtualenv/lib/python2.7/site-packages')

from site import addsitedir
from django.core.handlers.wsgi import WSGIHandler


os.environ['DJANGO_SETTINGS_MODULE'] = 'zcmsn.settings'
sys.path.append('/home/bygk/workspace/zcmsn/zcmsn/zcmsn')
sys.path.append('/home/bygk/workspace/zcmsn/zcmsn')
sys.path.append('/home/bygk/workspace/zcmsn')

#addsitedir('/usr/local/lib/python2.7/dist-packages')
addsitedir('/home/bygk/workspace/zcmsn/virtualenv/lib/python2.7/site-packages')
application = WSGIHandler()

####################################################################################################

 

 

 

 

 

 

一个完整的apache配置:

 

 

#########################################################

 

<VirtualHost *:80>
ServerName www.liangtuba.com
ServerAlias liangtuba.com
DocumentRoot "/opt/workspace/sinapps/yuanxiao/1/stock"
WSGIScriptAlias / /opt/workspace/sinapps/yuanxiao/1/stock/stock_wsgi.py
Alias "/static" "/opt/workspace/sinapps/yuanxiao/1/stock/static"
<Location "/static">
SetHandler None
</Location>

Alias "/media" "/opt/workspace/sinapps/yuanxiao/1/stock/media"
<Location "/media">
SetHandler None
</Location>


ErrorLog /var/log/httpd/stock_error.log

# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel info

CustomLog /var/log/httpd/stock_access.log combined

<Directory /opt/workspace/sinapps/yuanxiao/1/stock>
Order deny,allow
Allow from all

 

#301重定向【没有www的301到有www的】

Options -Indexes +FollowSymlinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^liangtuba.com$ [NC]
RewriteRule ^stock_wsgi.py/(.*)$ http://www.liangtuba.com/$1 [L,R=301]
</Directory>

#图片防盗链

SetEnvIfNoCase Referer "^http://www.liangtuba.com/" local_ref=1
<FilesMatch ".(gif|jpg|jpeg|png|GIF|JPG|JPEG|PNG)">
Order Allow,Deny
Allow from env=local_ref
</FilesMatch>

</VirtualHost>

 

 

#########################################################

 

 

 

备注 :

   

1.apache配置wsgi问题(多站点,不同虚拟环境的配置)

2.admin登录不进去的问题(too many values unpack)
python manager.py retpassword 用户名

3.session混乱的问题
settings.py加入一句
ESSION_COOKIE_NAME ="zcmsnsid"



当django部署到apache上时,有时候发现中文附件名没办法上传,解决办法
vi /etc/apache2/envvars
最后添加
export LANG='en_US.UTF-8'
export LC_ALL='en_US.UTF-8'

重启apache即可


分享到: 微信 更多