部署完毕后,感觉好简单好简单,但是这么简单的配置竟然浪费了我1天。通过这件事情,我明白了依葫芦画瓢是有前提的,你必须理解他的原理和架构。
因为在linux上不是太熟悉,和操作性太差,先在windows上试手。
在上一篇安装mod_wsgi成功后,部署django 应用程序一直无法打开。
按照google搜索到的文章在windows下一步步安装成功!
才发现如此简单,发现自己在linux下的错误竟如此的荒谬!
在linux上失败,主要的原因为全部照抄。不懂原理。
现在讲讲我所理解的。
关键点:
1,apache的配置
WSGIScriptAlias / "/home/testadmin/helloworld/wsgi.py"
<Directory "/home/testadmin/">
Order Deny,Allow
Allow from all
</Directory>
以上解释:
WSGIScriptAlias 为脚本路径,意思为在浏览器中输入http://localhost/后,apache服务器会调用wsgi.py脚本。
我犯得错误,教程中写的django.wsgi,我也在配置文件中写,我不会看日志文件,所以也不知道在这里发生问题,我只会用排错法。
这个文件必须存在,并且为绝对路径。
Directory为放项目程序的绝对路径,这个路径一定为程序的根路径,不能在后面再加上helloworld文件夹。我犯过这个错误,因为我不知道怎么去做,只好一步步测试,很傻。
2,wsgi.py需要修改
因为刚开始根本就没有接触到这个文件,直接新建的文件,所以肯定无法成功,在widnows上测试成功后,才知道该文件是变量,名字可以任意的,
"""
WSGI config for helloworld project.
It exposes the WSGI callable as a module-level variable named ``application``.
For more information on this file, see
https://docs.djangoproject.com/en/1.7/howto/deployment/wsgi/
"""
import os,sys
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "helloworld.settings")
sys.path.append('/home/testadmin/')
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
其中有一句原来的开发环境中是没有的,如果不添加该句,也是无法部署成功。我亲测过。
该句主要将系统环境变量中增加在apache设置的目录,必须一致。
不知道这句话添加上去具体的作用,但是知道不添加这句话肯定是不行的。
有谁知道的可以告知!
2014年9月11日星期四
今天我在ubuntu上部署python的生产环境(django)
今天我在ubuntu12.04上部署了python的web生产环境。
在部署过程中遇到了很多问题,现在一一记录下来
1,使用哪个web服务器?
官方上说了很多:apache,lighttpd,nginx,TUX_web_server,cherokee。这么多的web server,我只熟悉apache,并且官网教程也推荐apache。就选择他了。
2,使用哪个模块?
模块很多,
在部署过程中遇到了很多问题,现在一一记录下来
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,基本环境就配置好了,剩下的就看个人发挥了。
订阅:
博文 (Atom)