Python Django mod_wsgi Windows 部署过程 备忘
部署环境:Windows2003Server
1.安装Apache2.2
2.安装PYTHON2.7.4
注意把PYTHON的安装目录添加到环境变量,否则命令行下不支持python
3.安装DJANGO
解压到C盘,进入django目录,执行命令
python setup.py install
4.下载mod_wsgi.so,复制粘贴到APACHE安装目录modules目录
5.配置httpd.conf添加
Listen 80 #必须添加这个端口的监听 否则apache不会启动这个端口 Listen 8181 #加载 mod_wsgi LoadModule wsgi_module modules/mod_wsgi.so ####### PHP需要用到的 ############# #加载 mod_fcgid 因为我安装的是 php vc9 LoadModule fcgid_module modules/mod_fcgid.so AddHandler fcgid-script .fcgi .php #php.ini的存放目录 FcgidInitialEnv PHPRC "C:/php" # 设置PHP_FCGI_MAX_REQUESTS大于或等于FcgidMaxRequestsPerProcess,防止php-cgi进程在处理完所有请求前退出 FcgidInitialEnv PHP_FCGI_MAX_REQUESTS 1000 #php-cgi每个进程的最大请求数 FcgidMaxRequestsPerProcess 1000 #php-cgi最大的进程数 FcgidMaxProcesses 3 #最大执行时间 FcgidIOTimeout 120 FcgidIdleTimeout 120 #php-cgi的路径 FcgidWrapper "C:/php/php-cgi.exe" .php AddType application/x-httpd-php .php .html .htm ####### PHP需要用到的 ############# <VirtualHost *:8181> ServerName 服务器名称 ServerAlias 服务器名称:端口 DocumentRoot E:/PYTHON/infimob/src WSGIScriptAlias / E:/PYTHON/infimob/wsgi/infimob.wsgi Alias /robots.txt E:/PYTHON/infimob/src/infimob/static/robots.txt Alias /favicon.ico E:/PYTHON/infimob/src/infimob/static/favicon.ico Alias /media/ E:/PYTHON/infimob/src/infimob/media/ #静态文件不要用mod_wsgi Serve,在DJANGO里配置 # Alias /static/ E:/PYTHON/infimob/src/infimob/static/ <Directory E:/PYTHON/infimob/src/infimob/static> Order deny,allow Allow from all </Directory> <Directory E:/PYTHON/infimob/src/infimob/media> Order deny,allow Allow from all </Directory> <Directory "E:/PYTHON/infimob/src"> Order Deny,Allow Allow from all </Directory> <Directory "E:/PYTHON/infimob/wsgi"> Order Deny,Allow Allow from all </Directory> #跟apache默认的日志分离 ErrorLog C:/Apache2.2/logs/pythonerror.log # Possible values include: debug, info, notice, warn, error, crit, # alert, emerg. LogLevel warn CustomLog C:/Apache2.2/logs/pythonaccess.log combined </VirtualHost>
6.配置Python工程
拷贝整个工程源码到合适的目录,在工程目录下创建目录wsgi,在该目录下新建infimob.wsgi(工程名称.wsgi),内容如下:
# complete_project.wsgi is configured to live in projects/complete_project/deploy. # If you move this file you need to reconfigure the paths below. import os, sys sys.path.append('E:/PYTHON/infimob/src') os.environ['DJANGO_SETTINGS_MODULE'] = 'infimob.settings' import django.core.handlers.wsgi application = django.core.handlers.wsgi.WSGIHandler()
更改工程settings.py
DEBUG = TEMPLATE_DEBUG = False #用来接收报错信息 ADMINS = ( ('Neil Cheung', '[email protected]'), ) #使用GMAIL服务器发送邮件 EMAIL_HOST = 'smtp.gmail.com' EMAIL_HOST_USER = '[email protected]' EMAIL_HOST_PASSWORD = 'password' EMAIL_PORT = 587 EMAIL_USE_TLS = True #一定要配置好这个,否则等着500吧 ALLOWED_HOSTS = ['localhost','127.0.0.1','210.209.124.131']
重启Apache即可
DJANGO解码出错UnicodeDecodeError解决办法:
在python的Lib\site-packages文件夹下新建一个sitecustomize.py,内容为
# encoding=utf8 import sys reload(sys) sys.setdefaultencoding('utf8')
生产环境配置过程中遇到3个头疼的问题,记录如下:
1.Apache启动后,http://localhost:8181/admin直接500
原因:settings.py文件里没有配置ALLOWED_HOSTS,这是一个安全选项
解决办法:
#一定要配置好这个,否则等着500吧 ALLOWED_HOSTS = ['localhost','127.0.0.1','210.209.124.131']
2.访问路径最后必须要添加斜杠(/),否则就报500
原因:默认情况下是不需要添加斜杠的,APPEND_SLASH会自动补,原因是urls.py文件里添加了多余的配置
解决办法:从urls.py入手检查。。去掉多余的,不知道用途的配置
3.djangoTinyMce弹出框空白,然后自动下载弹出的页面
原因:Apachehttpd.conf配置文件里配置mod_wsgi的时候配置了serve静态文件的代码
解决办法:去掉server静态文件的代码即可
#静态文件不要用mod_wsgi Serve,在DJANGO里配置 # Alias /static/ E:/PYTHON/infimob/src/infimob/static/ # <Directory E:/PYTHON/infimob/src/infimob/static> # Order deny,allow # Allow from all # </Directory>