2014年9月11日星期四

今天我在ubuntu上部署python的生产环境(django)

今天我在ubuntu12.04上部署了python的web生产环境。
在部署过程中遇到了很多问题,现在一一记录下来
1,使用哪个web服务器?
官方上说了很多:apache,lighttpd,nginx,TUX_web_server,cherokee。这么多的web server,我只熟悉apache,并且官网教程也推荐apache。就选择他了。
2,使用哪个模块?
模块很多,

FastCGI, SCGI, or AJP在官方教程中不推荐,

就不用了,选用推荐的wsgi。

3,开始安装,我全部使用命令安装,
apt-get install apache2
apt-get install libapache2-mod-wsgi

4,安装完成后,开始配置,mod-wsgi使用命令安装后默认已经配置好。

5,在/etc/apache2/conf.d下新建文件apache_django_wsgi.conf
该文件的内容为:
AliasMatch /js/.*/.(js) /home/site/media/js/$1 
<Directory "/home/site/media/js "> 
Order allow,deny
Options Indexes
Allow from all
#IndexOptions FancyIndexing
</Directory>
Alias /static/.*/.(css|gif|png|jpg|jpeg) /home/site/media/static/$1 
<Directory "/home/site/media/static "> 
Order allow,deny
Options Indexes
Allow from all
#IndexOptions FancyIndexing
</Directory>
#for testing wsgi
WSGIScriptAlias / "/home/site/test.wsgi"  
#WSGIScriptAlias / "/home/site/django.wsgi"
<Directory "/home/site"> 
Allow from all
</Directory>
开始无法调试成功,原因为在编辑这个文件时WSGIScriptAlias后面的值中有一个空格,取消后正常。

6,放置应用程序的位置就是目录/home/site了,里面要有django.wsgi的文件,另一个文件test.wsgi为测试
django.wsgi的内容为:
import os, sys
#Calculate the path based on the location of the WSGI script.
apache_configuration= os.path.dirname(__file__)
project = os.path.dirname(apache_configuration)
workspace = os.path.dirname(project)
#
sys.stdout = sys.stderr
sys.path.append(workspace)
#print workspace 
sys.path.append(workspace + "site")
os.environ['DJANGO_SETTINGS_MODULE'] = 'mytest.settings '
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
其中mytest.settings的mytest是我的django的项目名 sys.path.append(workspace + "WWW") 中 "WWW"是我的site目录,sys.stdout = sys.stderr 是将标准输出到Apache的error.log中便于调试wsgi脚本
test.wsgi中的内容为:
def application(environ, start_response):
    status = '200 OK'
    output = 'Hello World!'

    response_headers = [('Content-type', 'text/plain'),
                        ('Content-Length', str(len(output)))]
    start_response(status, response_headers)

    return [output]
7,重启apache服务器,如果输出Hello World! 就表明wsgi配置ok啦

8,基本环境就配置好了,剩下的就看个人发挥了。

没有评论:

发表评论