环境:CentOS 5.X
django项目目录结构是按照http://djangobook.py3k.cn/2.0/chapter12/一路安装过来的。
目前目录结构:
[root@n66mysite]# pwd
/data/game/project/mysite
[root@n66mysite]# ls
books manage.py mysite
升级python:
由于系统自带python是2.4,我们需要升到2.7
#wget http://www.python.org/ftp/python/2.7/Python-2.7.tgz
#./configure --enable-shared
#make
#make install
替换原来的python
#mv/usr/bin/python{,.bak}
#cp/usr/local/bin/python2.7 /usr/bin/python
[root@n66 ~]#python
Python 2.7(r27:82500, May 9 2013, 17:58:15)
[GCC 4.1.220080704 (Red Hat 4.1.2-52)] on linux2
Type"help", "copyright", "credits" or"license" for more information.
>>>
显示python升级成功
但因为yum使用的是2.4版本,所以还要修改下
#vi/usr/bin/yum
#!/usr/bin/python2.4 #用2.4版本
#cat “/usr/local/lib”>>/etc/ld.so.conf
#ldconfig
安装python模块
#sh setuptools-0.6c11-py2.7.egg
#wget https://pypi.python.org/packages/source/p/pip/pip-1.3.1.tar.gz
#pythonsetup.py install
#python setup.pyinstall
安装django
#pip installDjango==1.5.1
安装apache
#wget http://apache.etoak.com//httpd/httpd-2.2.24.tar.gz
#./configure--prefix=/usr/local/apache2
#make
#make install
安装mod_wsgi
#wget http://modwsgi.googlecode.com/files/mod_wsgi-3.4.tar.gz
#./configure--with-apxs=/usr/local/apache2/bin/apxs
#make
#make installd
配置apache
在httpd.conf里增加一行:
LoadModulewsgi_module modules/mod_wsgi.so
在apache虚拟机主机配置文件里增加以下字段,也可以放在httpd.conf里:
<VirtualHost*:80>
#ServerAdminwebmaster@dummy-host2.example.com
#DocumentRoot"/usr/local/apache2/docs/dummy-host2.example.com"
ServerName 192.168.0.110
ErrorLog "logs/error_log"
CustomLog "logs/access_log"common
<Directory /data/game/project/mysite>
Order deny,allow
Allow from all
</Directory>
WSGIScriptAlias / /data/game/project/mysite/mysite/django.wsgi
</VirtualHost>
其中/data/game/project/mysite/mysite/django.wsgi文件内容如下:
import os
import sys
importdjango.core.handlers.wsgi
os.environ['DJANGO_SETTINGS_MODULE']= 'mysite.settings'
app_path ="/data/game/project/mysite/"
sys.path.append(app_path)
application =django.core.handlers.wsgi.WSGIHandler()
按照官方的配置:
import os,sys
sys.path.append('/data/game/project/mysite/')
os.environ['AJANGO_SETTINGS_MODULE']= 'mysite.settings'
importdjango.core.handlers.wsgi
_application =django.core.handlers.wsgi.WSGIHandler()
defapplication(environ,start_response):
environ['PATH_INFO'] =environ['SCRIPT_NAME'] + environ['PATH_INFO']
return_application(environ,start_response)
不行,不知道为什么,如有懂的,请告知下。
可以用http://192.168.0.110/admin/访问django自带后台管理模版地址了,但奇怪的是样式全丢了。也是google了下,终于找到了解决方法,cd到你的manage.py目录,运行
#python manage.py collectstatic
django会收集必要的静态文件到setting文件里STATIC_ROOT指定的目录,由于我的settings.py里的STATIC_ROOT = ''为空,所以生成的静态目录在:
[root@n66mysite]# ls
admin books manage.py mysite
[root@n66 mysite]#pwd
/data/game/project/mysite
admin目录就是刚生成的.
编辑/data/game/project/mysite/mysite/settings.py文件:
其中
STATIC_ROOT = ''
# URL prefix for static files.
# Example: "http://example.com/static/", "http://static.example.com/"
STATIC_URL = '/static/'
# Additional locations of static files
STATICFILES_DIRS = (
# Put strings here, like "/home/html/static" or "C:/www/django/static".
# Always use forward slashes, even on Windows.
# Don't forget to use absolute paths, not relative paths.
'/data/game/project/mysite/admin/'
)
保持这样。
由先前apache访问日志得知样式访问url是类似以下形式:
/static/admin/css/base.cssHTTP/1.1" 404 2874
所以我们需要在配置虚拟主机文件里增加以下内容:
Alias /static/admin//data/game/project/mysite/admin/
httpd-vhosts.conf全文内容如下(我的虚拟主机配置文件):
NameVirtualHost*:80
<VirtualHost*:80>
#ServerAdminwebmaster@dummy-host2.example.com
#DocumentRoot"/usr/local/apache2/docs/dummy-host2.example.com"
ServerName 115.238.225.50
ErrorLog "logs/error_log"
CustomLog "logs/access_log" common
Alias /static/admin//data/game/project/mysite/admin/
<Directory /data/game/project/mysite>
Order deny,allow
Allow from all
</Directory>
WSGIScriptAlias / /data/game/project/mysite/mysite/django.wsgi
</VirtualHost>
重启apache,再次访问正常。
如有在编译mod_wsgi模块里出现
apxs:Error: Command failed with rc=65536.
make: *** [mod_wsgi.la] Error 1
错误,重新编译python,加上
#./configure --enable-shared
本文档是在搭建后三天写的,可能有遗漏之处。