前段时间由于本人染小恙,在家休息一段时间,也恰逢原先运行的VPS于2011.08.21到期并且不想在续,故到今天8.26已经有近一周无法访问.昨天与今天两天,一直在处理搬迁的问题,当中碰到不少问题.感谢神一样的stackoverflow,最终帮忙解决了这个问题,也在这里记录一下.

目前roboticsfaq.com是运行在hostmonster下面,对于python与django的支持,官方说是默认支持的,但是还是要自己做一些处理,这里不谈如何去购买hostmonster的主机,也不谈如何开通SSH,就讲如何将一个python+django的网站,搬迁入hostmonster.

安装python2.6

下文的yourname就是你的用户名,请自行修改。注意用户名和域名不太一样,cPanel里能查到
登录ssh:

ssh [email protected]

引用

$ mkdir -p ~/.local/lib/python2.6
$ cd .local
$ wget http://www.python.org/ftp/python/2.6.4/Python-2.6.4.tgz
$ tar xzvf Python-2.6.4.tgz
$ cd Python-2.6.4
$ ./configure --prefix=$HOME/.local/lib/python2.6
$ make
$ make install
$ cd ~
$ ln -s .local/lib/python2.6/bin/python bin/python2.6

测试python2.6是否安装成功:

引用

$ python2.6
Python 2.6 (r26:66714, Apr  1 2009, 20:44:00)
[GCC 4.1.2 20080704 (Red Hat 4.1.2-44)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
]]]

此时python2.6已经安装完毕

安装easy_install

设定easy_install的安装配置和路径,.pydistutils.cfg要放在用户的根目录下,也就是/home/yourname/
$ vim .pydistutils.cfg
添加如下内容

引用

[install]
install_lib=/home/yourname/.local/lib/python2.6/site-packages
install_scripts=/home/yourname/bin

引用

$ cd .local
$ wget http://pypi.python.org/packages/source/s/setuptools/setuptools-0.6c11.tar.gz#md5=7df2a529a074f613b509fb44feefe74e
$ tar xzvf setuptools-0.6c11.tar.gzfcgi.py
$ cd setuptools-0.6c11
$ ./configure
$ make
$ make install

安装相关插件

easy_install flup
easy_install mysql-python
easy_install Django
easy_install html5lib
easy_install markdown
easy_install south
easy_install python-openid

你还需要两个组件,第一是fastcgi script,用python编写,它负责加载你的Django应用并启动FastCGI监听器。下面的代码是fastcgi.script。请根据你的 情况修改里面的内容(python路径、项目名等),然后命名为mydjango.fcgi,放在你的public_html目录中。

#!/usr/bin/python2.6

import sys, os

# Add a custom Python path.
sys.path.insert(0, "/home/username/.local/lib/python2.6")

# Switch to the directory of your project. (Optional.)
# os.chdir("/home/user/myproject")

# Set the DJANGO_SETTINGS_MODULE environment variable.
os.environ['DJANGO_SETTINGS_MODULE]=mydjango.settings"

from flup.server.fcgi import WSGIServer
from django.core.handlers.wsgi import WSGIHandler
WSGIServer(WSGIHandler()).run()

给mydjango.fcgi执行权限, chmod 755 mydjango.fcgi
下一步,我们要建立url重写规则,这样就不会在url里看到django.fcgi了。
编辑.htaccess,添加下面的代码:

Addhandler fcgid-script .fcgi
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteRule ^(media/ .*)$ - [L]
RewriteRule ^(adminmedia/ .*)$ - [L}
RewriteCond %{REQUEST_URL} !(myproject.fcgi)
RewriteRule ^(.*)$ myproject.fcgi/$1 [L]

做完以上一切后,理论上就可以运行你的网站了,进入roboticsfaq.com就可以看到与原来一致的网站.当然,你可能还要加载数据库什么的,不在本文记载讨论之列.

参考:

  1. http://stackoverflow.com/questions/800584/wsgiserver-errors-when-trying-to-run-django-app

  2. http://www.iteye.com/topic/544806

  3. [https://docs.djangoproject.com/en/dev/howto/deployment/fastcgi/#running-django-on-a-shared-hosting-provider-with-apache](https://docs.djangoproject.com/en/dev/howto/deployment/fastcgi/#running-django-on-a-shared-hosting-provider-with-apache