Linux下将Python的Django项目部署到Apache服务器
这几天花了点时间,将把django开发好的web项目部署到Apache上,参考了官方的一些文档和互联网上的文档,还是花了比较多的时间,这里把配置的过程说一下。
方便有需要的朋友,可以参考,少走弯路!
1.django项目部署环境说明
操作系统:RedHatEnterpriseLinuxServerrelease5.3(Tikanga)x86_64
apache版本:httpd-2.2.3-22.el5
mod_wsgi版本:mod_wsgi-3.2-1.el5fedoraepel可以下载
Django版本:1.2.3
python版本:2.5
这里假定Django和Apache已经安装好,并且Django的项目已经开发好。
以上软件包都是通过yum包安装,软件包都是系统标准目录结构!
django开发好的项目目录是/var/www/html/server,项目目录结构如下(标准django项目目录结构)
#tree-dserver/ server/ |--__init__.py |--manage.py |--settings.py |--backend |--static ||--images ||--locale ||--plugins |`--themes ||--default ||`--images ||--gray ||`--images |`--icons |--template `--view
2.Apache和mod_wsgi配置
修改wsgi配置(/etc/httpd/conf.d/wsgi.conf)
#cat/etc/httpd/conf.d/wsgi.conf LoadModulewsgi_modulemodules/mod_wsgi.so WSGIScriptAlias/"/var/www/html/server/django.wsgi" <Directory"/var/www/html/server"> OrderDeny,Allow Allowfromall </Directory>
项目目录中的django.wsgi这个文件是需要新建的,后面会说到如何新建这个文件。
apache使用的标准配置,Apache的DocumentRoot指向的是/var/www/html目录
3.新建django.wsgi文件
在项目目录/var/www/html/server下新建一个django.wsgi,文件内容如下:
#cat/var/www/html/server/django.wsgi #-*-coding:utf-8-*- importos importsys os.environ['DJANGO_SETTINGS_MODULE']='settings' os.environ['PYTHON_EGG_CACHE']='/tmp/.python-eggs' current_dir=os.path.dirname(__file__) ifcurrent_dirnotinsys.path:sys.path.append(current_dir) importdjango.core.handlers.wsgi application=django.core.handlers.wsgi.WSGIHandler()
第三行os.environ['DJANGO_SETTINGS_MODULE']='settings',这个settings就是指项目目录下的setting.py文件.
第四行os.environ['PYTHON_EGG_CACHE']='/tmp/.python-eggs',指定解压egg文件的cache目录,确保运行apache的用户,能够对此目录有读写权限.
第五,六行将当前目录自动加入到python的搜索路径,如果项目中有自己写的模块,方便使用和发布
最后,这个django.wsgi文件名可以随便取,例如test.wsgi、app.wsgi等等,但是一定要与/etc/httpd/conf.d/wsgi.conf配置文件中配置保持一致。
如果您这里新建的文件名不是django.wsgi而是test.wsgi,那么/etc/httpd/conf.d/wsgi.conf中的配置就应该修改为
WSGIScriptAlias/"/var/www/html/server/test.wsgi"
4.修改django项目中的setting.py文件
找到项目目录下的setting.py,对于这里就是/var/www/html/server/setting.py。找到其中的TEMPLATE_DIRS,修改为:
TEMPLATE_DIRS=("/var/www/html/server/template",)
注意:模板目录在这里一定要用绝对路劲,而不能用相对路径,当然也有方法动态设置模板路劲
PS:关于mod_wsgi
目前mod_wsgi有两种工作模式:
第一种是嵌入模式,类似于mod_python,直接在apache进程中运行,这样的好处是不需要另外增加进程,但是坏处也很明显,所有内存都和apache共享,如果和mod_python一样造成内存漏洞的话,就会危害整个apache。而且如果apache是用workermpm,mod_wsgi也就强制进入了线程模式,这样子对于非线程安全的程序来说就没法用了。
这种模式下需要在apache的vhost中如下设置:
<spanstyle="font-family:'timesnewroman',times;">WSGIScriptAlias/path/path-to-wsgi</span>
即可生效,对于小型脚本的话,直接用这种模式即可。
第二种是后台模式,类似于FastCGI的后台,mod_wsgi会借apache的外壳,另外启动一个或多个进程,然后通过socket通信和apache的进程联系。
这种方式只要使用以下配置即可:
#启动WSGI后台,site1是后台名字 WSGIDaemonProcesssite1processes=1threads=15display-name=%{GROUP} #分配当前上下文应该使用哪个WSGI后台,可以放在Location里面指定 WSGIProcessGroupsite1 #根据当前上下文的ProcessGroup分配到对应的后台 WSGIScriptAlias/path/path-to-wsgi
在这种模式下,我们可以通过调节processes和threads的值来设置三种MPM的模式:prefork','worker','winnt'。
winnt模式
WSGIDaemonProcessexamplethreads=25 wsgi.multithreadTrue wsgi.multiprocessFalse
此时processes=1,但是multiprocess为false
如果显式地指出processes为1那么:
WSGIDaemonProcessexampleprocesses=1threads=25 wsgi.multithreadTrue wsgi.multiprocessTrue
worker模式
WSGIDaemonProcessexampleprocesses=2threads=25 wsgi.multithreadTrue wsgi.multiprocessTrue
preforker模式
WSGIDaemonProcessexampleprocesses=5threads=1 wsgi.multithreadFalse wsgi.multiprocessTrue
后台模式由于是与apache进程分离了,内存独立,而且可以独立重启,不会影响apache的进程,如果你有多个项目(django),可以选择建立多个后台或者共同使用一个后台。
比如在同一个VirtualHost里面,不同的path对应不同的django项目,可以同时使用一个Daemon:
<spanstyle="font-family:'timesnewroman',times;">WSGIDaemonProcessdefaultprocesses=1threads=1display-name=%{GROUP} WSGIProcessGroupdefault WSGIScriptAlias/project1“/home/website/project1.wsgi” WSGIScriptAlias/project2“/home/website/project2.wsgi”</span>
这样子两个django都使用同一个WSGI后台。
也可以把不同的项目分开,分开使用不同的后台,这样开销比较大,但就不会耦合在一起了。
display-name是后台进程的名字,这样方便重启对应的进程,而不需要全部杀掉。
<spanstyle="font-family:'timesnewroman',times;">WSGIDaemonProcesssite1processes=1threads=1display-name=%{GROUP} WSGIDaemonProcesssite2processes=1threads=1display-name=%{GROUP} <Location“/project1″> WSGIProcessGroupsite1 </Location> WSGIScriptAlias/project1“/home/website/project1.wsgi” <Location“/project1″> WSGIProcessGroupsite2 </Location> WSGIScriptAlias/project2“/home/website/project2.wsgi”</span>
对于django1.0以下的版本,由于官方认定不是线程安全的,所以建议使用多进程单线程模式
processes=nthreads=1
对于django1.0以后,就可以放心的使用多进程多线程模式:
processes=2threads=64
这样子性能会更好。