您的位置:首页 > 运维架构 > Apache

ubuntu下安装apache2.2+mod_wsgi+django(二)

2013-05-24 14:57 204 查看
http://blog.csdn.net/huangxiansheng1980/article/details/7207075

在上一篇博文:
ubuntu下安装apache2.2+mod_wsgi+django(一)
中已经建立了一个apache2.2+mod_wsgi_python+django的完整环境,并且建立了一个简单的网站,使用一个main.wsgi的python脚本生成动态的内容。今天接上次的实验,我继续讨论python开发web。

这次我们另外新建一个虚拟主机,在
/etc/apache2/sites-availabe/
下面创建一个配置文件-testserver
配置如下:
<VirtualHost 127.0.0.4:80>

ServerName 127.0.0.4
DocumentRoot /var/www/test
WSGIScriptAlias /dynamic /var/www/test/dynamic
<Directory />
Options indexes

AllowOverride None

Order deny,allow

Allow from all
</Directory>
<Directory /var/www/test/dynamic>
Options FollowSymLinks ExecCGI
Order allow,deny
Allow from all
</Directory>

<Directory /var/www/test/static>
Options indexes
AllowOverride None
Order deny,allow
Allow from all
</Directory>

</VirtualHost>
好了,解释一下上述配置文件的作用。
虚拟主机对应url为127.0.0.4:80
虚拟主机虚拟目录为/var/www/test
其中有2个子目录,dynimic和static,下面一行告诉apache,/var/www/test/dynamic下面视为python脚本程序。

WSGIScriptAlias /dynamic /var/www/test/dynamic

所以你要是在这个文件放一个静态的html文件,那么访问的时候就会出错,查看apache给出的error日志

/var/log/apache2/error.log

就会告诉你类似这样的错误:
:15:32 2012] [error] [client 127.0.0.4] mod_wsgi (pid=1444, process='', application='127.0.0.4|/static/index.html'): Failed to parse WSGI script file 'C:/Program Files/Apache Software Foundation/Apache2.2/htdocs/test/static/index.html'.
[Tue Jan 17 13:15:32 2012] [error] [client 127.0.0.4] mod_wsgi (pid=1444): Exception occurred processing WSGI script 'C:/Program Files/Apache Software Foundation/Apache2.2/htdocs/test/static/index.html'.
[Tue Jan 17 13:15:32 2012] [error] [client 127.0.0.4] File "C:/Program Files/Apache Software Foundation/Apache2.2/htdocs/test/static/index.html", line 1
[Tue Jan 17 13:15:32 2012] [error] [client 127.0.0.4] <html><body><h1>It works! 127.0.0.2</h1></body></html>
[Tue Jan 17 13:15:32 2012] [error] [client 127.0.0.4] ^
[Tue Jan 17 13:15:32 2012] [error] [client 127.0.0.4] SyntaxError: invalid syntax

看到了吧,语法错误,那是因为apache把你的html文件视为python解释执行。

本人曾今就犯了这样的错误,希望也能犯这个错误,为什么? 我是不是有病? 不是。因为我觉得很多东西,你犯了错误,你才能明白这个东西是怎么工作的,如果所有的东西都是别人告诉你怎么做是对的,那么你其实没有真正的了解这个东西,当下次出现一些错误时你就不知道怎么办了。或者你要做的东西发生了点变化,同样你又不知道怎么办了。好了,扯得有点远了,回到正题吧。

上面根目录下还有一个子目录就是static,这里我们打算放一些静态的html网页,收到客户端请求这些页面时,直接返回这些网页给客户端就好。所以我们的static这个目录不需要下面这句:
WSGIScriptAlias /static /var/www/test/static
如果你写了这句就会产生上面的那个错误。

同样我们的dynamic不可以把WSGIScriptAlias这行去掉,否则,即使你的dynamic中的文件是以py为后缀的文件,它不会因为你是py后缀的文件就去调用python解释器去执行这个脚本。但是不会像前面那样出现错误,而是直接把脚本中内容直接返回给浏览器,这个比较危险,因为你的程序代码就被公布于众了。

有了前面的解释你知道接下给做些什么了吧。
首先创建目录:

sudo mkdir /var/www/test
sudo mkdir /var/www/test/dynamic
sudo mkdir /var/www/test/static

在static 下面新建一个index.html,输入下面内容
<html>
<body>
<h1>It works! static web
</h1>
</body>
</html>

让后去dynamic下面新建一个文件test,不用什么后缀,你要是想加一个也无所谓,内容如下:

def application(environ, start_response):
status='200 OK'
output=‘<html><head>’ +\
'<br></head><body>' +\
'<p align=center>Hello world.</p>' +\
'<p align=center>' +\
showtime() +\
'</p></body></html>'
response_headers=[('Content-type', 'text/plain'),
('Content-Length', str(len(output)))]
start_response(status, response_headers)
return [output]

from time import gmtime, strftime
def showtime():
t = strftime("%Y-%m-%d %H:%M:%S", gmtime())
return t

激活我们的网站:
sudo a2ensite testserver
加载我们的网站:
sudo service apache2 reload

现在,你打开浏览器,输入
http://127.0.0.4/static/index.html,
你就可以看到It works! static web。

输入
http://127.0.0.4/dynamic/test
你就可以看到hello world, 下面一行显示当前的时间。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: