配置 UCenter Home 2.0 实现个性化二性域名

我的 UCenter Home 2.0 安装在 http://home.taounion.com/,现在准备以 *.taounion.com 作为个人主页的个性域名,如 http://hily.taounion.com/

步骤:

  1. 在域名控制面板中设置 taounion.com 的泛域名解析 *.taounion.com 到你的服务器 IP。
  2. 在 UCenter Home 后台设置中启用“二级域名功能”,并设置二级域名根域名,我的是“taounion.com”。
  3. 修改 lighttpd 的配置文件,将所有 *.taounion.com 解析到 UCenter Home 的虚拟目录中,如下:

    $HTTP["host"] =~ ".+\.taounion\.com" {
      server.document-root = "/www/ucenterhome"
      index-file.names = ("index.html", "index.php")
      fastcgi.server = (
        ".php" => ((
          "socket" => "/tmp/php-fpm.socket"
        ))
      )
      url.rewrite-once = (
        "^/(space|network)-(.+)\.html$" => "/$1.php?rewrite=$2",
        "^/(space|network)\.html$" => "/$1.php",
        "^/(\d+)$" => "/space.php?uid=$1",
        "^/([a-z0-9\-]+)$" => "/space.php?domain=$1"
      )

    }

    其中 rewrite 并不是必须的,前三条都是做页面伪静态化用的,最后一条:

    "^/([a-z0-9\-]+)$" => "/space.php?domain=$1"

    是为了个性化域名跳转后的效果更好,因为默认情况下 http://hily.taounion.com/ 会跳转到 http://home.taounion.com/space.php?domain=hily,很不简洁,我希望结果是 http://home.taounion.com/hily

  4. 配合上一步,修改 index.php 中的个性域名跳转:

    if(!isset($_GET[‘do’]) && $_SCONFIG[‘allowdomain’]) {
            $hostarr = explode(‘.’, $_SERVER[‘HTTP_HOST’]);
            $domainrootarr = explode(‘.’, $_SCONFIG[‘domainroot’]);
            if(count($hostarr) > 2 && count($hostarr) > count($domainrootarr) && $hostarr[0] != ‘www’ && !isholddomain($hostarr[0])) {
                    //showmessage(‘enter_the_space’, $_SCONFIG[‘siteallurl’].’space.php?domain=’.$hostarr[0], 0);
                    showmessage(‘enter_the_space’, $_SCONFIG[‘siteallurl’].$hostarr[0], 0);
            }
    }

  5. 重启 lighttpd,完工!

— EOF —

lighttpd + webpy 启动时出错

调了大半天,火挺大,还好总算解决了。

运行环境:

  • gentoo
  • python 2.5.2
  • lighttpd 1.4.21
  • webpy 0.31

直接跑 /work/www/test/main.py 没问题,交给跑 fastcgi 就有问题,错误提示:

2009-02-22 06:09:47: (log.c.97) server started
2009-02-22 06:09:47: (mod_fastcgi.c.1051) the fastcgi-backend /work/www/test/main.py failed to start:
2009-02-22 06:09:47: (mod_fastcgi.c.1055) child exited with status 1 /work/www/test/main.py
2009-02-22 06:09:47: (mod_fastcgi.c.1058) If you’re trying to run PHP as a FastCGI backend, make sure you’re using the FastCGI-enabled version.
You can find out if it is the right one by executing ‘php -v’ and it should display ‘(cgi-fcgi)’ in the output, NOT ‘(cgi)’ NOR ‘(cli)’.
For more information, check http://trac.lighttpd.net/trac/wiki/Docs%3AModFastCGI#preparing-php-as-a-fastcgi-programIf this is PHP on Gentoo, add ‘fastcgi’ to the USE flags.
2009-02-22 06:09:47: (mod_fastcgi.c.1365) [ERROR]: spawning fcgi failed.

看了下lighttpd 的代码,child exited with status 1 是程序正常退出,看来不像是 lighttpd 的问题了,问题可能出在 flup 上?

一直在 lighttpd 和 flup 上纠结了很久,还是没找出问题所在。

最后把 main.py 写成最简单的:

import web

urls = (
    ‘/(.*)’, ‘hello’
)
app = web.application(urls, globals())

class hello:       
    def GET(self, name):
        if not name:
            name = ‘world’
        return ‘Hello, ‘ + name + ‘!’

if __name__ == “__main__”:
    app.run()

好家伙,居然可以跑起来!看样子是程序的问题了。

不断删除程序代码后,最终定位问题位于 import MySQLdb 语句上,每当执行这句时就出错,注释掉就正常了。

可是用 python 命令行测试 import MySQLdb 又正常,见鬼了!

难道是 www 用户的 python 环境变量设置有问题?在 webpy 时直接输出后如下,和在 python 命令行里执行的一样:

[”, ‘/usr/lib64/python2.5/site-packages/MySQL_python-1.2.2-py2.5-linux-x86_64.egg’, ‘/usr/lib64/python2.5/site-packages/flup-1.0.1-py2.5.egg’, ‘/usr/lib64/portage/pym’, ‘/usr/lib64/python25.zip’, ‘/usr/lib64/python2.5’, ‘/usr/lib64/python2.5/plat-linux2’, ‘/usr/lib64/python2.5/lib-tk’, ‘/usr/lib64/python2.5/lib-dynload’, ‘/usr/lib64/python2.5/site-packages’]

好吧,彻底没折,直接让程序报错好了:

#!/usr/bin/env python

import sys
import web

urls = (
    ‘/(.*)’, ‘hello’
)
app = web.application(urls, globals())

class hello:
    def GET(self, name):
        if not name:
            try:
                import MySQLdb
            except Exception, e:
                return str(e);
        return ”

if __name__ == “__main__”:
    app.run()

这下子真相大白:

Can’t extract file(s) to egg cache The following error occurred while trying to extract file(s) to the Python egg cache: [Errno 13] Permission denied: ‘/root/.python-eggs’ The Python egg cache directory is currently set to: /root/.python-eggs Perhaps your account does not have write access to this directory? You can change the cache directory by setting the PYTHON_EGG_CACHE environment variable to point to an accessible directory.

见鬼了,egg 也有缓存?干啥用滴? 暂且不管干什么用的,为什么默认要把 PYTHON_EGG_CACHE 设置到 /root 目录下呢,因为我是以 root 用户安装的?好傻。 用 baidu 和 google 查找“The following error occurred while trying to extract file(s) to the Python egg”,结果就多了。 可以修改缓存文件夹的位置或权限,也可以把 egg 直接解压。 权限已经让我纠结这么久了,我就不用这种方式了,直接解压: 解压前先从 easy-install.pth 中删除 MySQLdb 的 egg 包路径:

# cd /usr/lib64/python2.5/site-packages/
# mkdir t
# unzip MySQL_python-1.2.2-py2.5-linux-x86_64.egg -d t
# rm MySQL_python-1.2.2-py2.5-linux-x86_64.egg
# mv t MySQL_python-1.2.2-py2.5-linux-x86_64.egg

从 sys.path 中可以看到,我的 flup 也是 egg,顺带也把它解压了:

# mkdir t
# unzip flup-1.0.1-py2.5.egg -d t
# rm flup-1.0.1-py2.5.egg
# mv t flup-1.0.1-py2.5.egg

终于搞定!

— EOF —