在 Mac OS X Mountain Lion 中将 Web.py应用部署到 Apache 服务器

1. 设置域名:

sudo vi /etc/hosts

在其中添加:

127.0.0.1    www.example.com

 

2. 编辑 httpd.conf

sudo vi /etc/apache2/httpd.conf

取消以下两行注释(或自行编写):

#LoadModule rewrite_module  libexec/apache2/mod_rewrite.so

#LoadModule wsgi_mod  libexec/apache2/mod_wsgi.so

(mod_wsgi.so 如果不存在,请自行下载编译安装)

找到下列行,并取消注释:

#Include /private/etc/apache2/extra/httpd-vhosts.conf

 

3. 在 httpd-vhosts.conf 文件中添加虚拟主机

sudo vi /private/etc/apache2/extra/httpd-vhosts.conf

在其中添加以下内容:(/Users/yourname/www.example.com路径必须先创建)

<VirtualHost  *:80>

  DocumentRoot "/Users/yourname/www.example.com"

  <Directory>

    Options +FollowSymLinks

    AllowOverride  All

    Order allow,deny

    allow from all

     </Drectory>

  DirectoryIndex  index.py

  ServerName www.example.com

</VirtualHost>

完成后,校验一下:

sudo apachectl configtest

如果有错,排除它,直到看到 Syntax OK.

重启服务器:

sudo apachectl reatsrt

 

4. 在DocumentRoot 下创建并编辑 .htaccess 文件

cd ~/www.example.com

sudo vi .htaccess

内容如下:

<Files index.py>
  SetHandler wsgi-script
  Options ExecCGI FollowSymLinks
</Files>


RewriteEngine on
RewriteBase /

RewriteCond %{REQUEST_URI} !^favicon.ico$

RewriteCond %{REQUEST_URI} !^/static

RewriteCond %{REQUEST_URI} !^(/.*)+index.py
RewriteCond %{REQUEST_URI} !^(/.*)+\.(css|png|jpg|js)

RewriteRule ^(.*)$ index.py/$1

 

5. 在当前目录(/Users/yourname/www.example.com/)下创建 index.py

sudo vi index.py

内容如下:

-----------------------------

#!/user/bin/env python
import web

urls = (
  "/(.*)", "index"
)

app = web.application(urls, globals(), autoreload=False)
application = app.wsgifunc()

web.config.debug=True

class Index:
  def GET(self,name):
    if not name:
      name="You"
    return "Hello, "+name+"!"

---------------------------

6. 打开浏览器,在地址栏中输入:

www.example.com

如果看到:

Hello, You!

大功告成!

posted @ 2013-07-24 08:55  artman  阅读(265)  评论(0编辑  收藏  举报