Dreamhost 上编译python并安装django

1. 升级python到2.5.2:

用Putty登录到DreamHost的Shell控制台执行如下的命令:

mkdir opt
cd opt
mkdir packages
cd packages
wget http://www.python.org/ftp/python/2.5.2/Python-2.5.2.tgz
tar -xzvf Python-2.5.2.tgz
cd Python-2.5.2
./configure -prefix=$HOME/opt
make
make install

python 2.5已经安装好了。把python 2.5设置为默认版本:

vim ~/.bash_profile

然后,在~/.bash_profile里加入一条:

export PATH=$HOME/opt/bin/:$PATH

重新载入配置文件:

source ~/.bash_profile  

检验一下是否成功:

$python -V
Python 2.5.2

2. 安装python-Mysql

cd ~/opt/packages
wget http://internap.dl.sourceforge.net/sourceforge/mysql-python/MySQL-python-1.2.2.tar.gz
tar xvzf MySQL-python-1.2.2.tar.gz
cd MySQL-python-1.2.2
python setup.py install

3. 安装django

cd ~/opt/packages
wget http://www.djangoproject.com/download/1.0/tarball/
tar -xzvf Django-1.0.tar.gz
cd Django-1.0
python setup.py install

在python交互环境下查看django的版本,以测试安装配置的准确性。

$python
>>>import django
>>>django.VERSION
(1, 0, 'final')

4. 创建Django项目

mkdir ~/django_projects
cd ~/django_projects
django-admin.py startproject myproject
chmod 600 myproject/settings.py

vim myproject/settings.py :

DATABASE_ENGINE = 'mysql'           # 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.
DATABASE_NAME = 'dbname'             # Or path to database file if using sqlite3.
DATABASE_USER = 'dbuser'             # Not used with sqlite3.
DATABASE_PASSWORD = 'dbpass'         # Not used with sqlite3.
DATABASE_HOST = 'dbhost'             # Set to empty string for localhost. Not used with sqlite3.
DATABASE_PORT = ''             # Set to empty string for default. Not used with sqlite3.

TIME_ZONE = 'Asia/Shanghai'
MEDIA_ROOT = '/home/yourusername/lostk.com/media/'
MEDIA_URL = 'http://www.lostk.com/media/'

TEMPLATE_DIRS = (
    # Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".
    # Always use forward slashes, even on Windows.
    # Don't forget to use absolute paths, not relative paths.
    "/home/yourusername/django_projects/myproject_tpl"
)

~/.bash_profile中加入环境变量:

export PYTHONPATH=$PYTHONPATH:$HOME/django-projects
export DJANGO_SETTINGS_MODULE=myproject.settings

重新载入配置文件:

source ~/.bash_profile  

同步数据库并创建管理员

cd ~/django_projects/myproject
django-admin.py syncdb

5. 配置FastCGI

cd ~/lostk.com/
wget http://svn.saddi.com/py-lib/trunk/fcgi.py
chmod 755 fcgi.py

创建名为dispatch.fcgi, 内容如下:

#!/home/yourusername/opt/bin/python

import sys
sys.path += ['/home/yourusername/django_projects']
from fcgi import WSGIServer
from django.core.handlers.wsgi import WSGIHandler
import os

os.environ['DJANGO_SETTINGS_MODULE'] = 'myproject.settings'
WSGIServer(WSGIHandler()).run()
chmod 755 dispatch.fcgi

创建.htaccess文件内容如下:

RewriteEngine On
RewriteBase /
RewriteRule ^(media/.*)$ - [L]
RewriteRule ^(admin_media/.*)$ - [L]
RewriteRule ^(dispatch\.fcgi/.*)$ - [L]
RewriteRule ^(.*)$ dispatch.fcgi/$1 [L]

重启python激活所有改动

$pkill python

最后用命令测试:

$./dispatch.fcgi

如果返回的是一个django页面的字符串。

再通过浏览器访问http://lostk.com

出现的django提示

配置成功。

Django 管理

ln -s ~/opt/packages/Django-1.0/django/contrib/admin/media ~/lostk.com/admin_media

编辑配置文件

vim ~/django-projects/myproject/settings.py :

ADMIN_MEDIA_PREFIX = '/admin_media/'

INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.admin',
)

更新配置

cd ~/django_projects/myproject
django-admin.py syncdb

vim ~/django_projects/myproject/urls.py

from django.contrib import admin

admin.autodiscover()
(r'^admin/(.*)', admin.site.root),

重启python激活所有改动

$pkill python

访问 lostk.com/admin 即可登录管理

2008-11-05 21:56 119 django dreamhost python