系统环境及版本:
OS:centos6
python:python2.7
nginx:0.8
mysql:5.5.3
uwsgi:1.0
Django:1.3
MySQL-python:1.2.3
注:Nginx 0.8版本已经内置uwsgi模块,所以不需要重新编译Nginx添加uwsgi模块。
本安装过程全部基于源码编译安装,并假设已经安装Nginx、Mysql,如未安装请参考:
Nginx与PHP,php-fpm,mysql安装整合
CentOS 5环境下Nginx+MySQL+PHP Web服务器环境搭建教程
如果不想这么麻烦可以先使用系统自带Python2.6版本,nginx和mysql可通过yum命令安装,如:
# yum install nginx mysql mysql-devel
安装python2.7
注意:yum命令也需要依赖于python,并且有对应的版本,建议不要删除系统自带的python,删除之后yum命令将不可用。
用源码安装python2.7,如果系统自带python为2.6+则无需再安装python。
# wget http://python.org/ftp/python/2.7.2/Python-2.7.2.tgz
# tar -zxpvf Python-2.7.2.tgz
# cd Python-2.7.2
# ./configure --prefix=/usr/local/python2.7
# make
# make install
# ln -sf /usr/local/python2.7/bin/python2.7 /usr/bin/python2.7
注:如果没有指定python安装路径,则默认安装路径在/usr/local/lib/python2.7/
验证是否安装成功
# python2.7
安装Setuptools模块
安装Setuptools模块是为了能够使用easy_install
# wget http://pypi.python.org/packages/2.7/s/setuptools/setuptools-0.6c11-py2.7.egg#md5=fe1f997bc722265116870bc7919059ea
# sh setuptools-0.6c11-py2.7.egg –-prefix=/usr/local/python2.7
安装完成后,easy_install命令位于
/usr/local/python2.7/bin/easy_install
若不想每次执行easy_install需带上路径可ln到/usr/bin:
# ln -sf /usr/local/python2.7/bin/easy_install /usr/bin/easy_install
使用easy_install安装其他python模块
# easy_install ElementTree Markdown html5lib python-openid
安装Django
注:Django 1.4版本无法运行,出现的错误暂未找到解决方法,google之说是要修改网站程序文件。
# wget http://www.djangoproject.com/download/1.3.1/tarball/
# tar zxvf Django-1.3.1.tar.gz
# cd Django-1.3.1
# python2.7 setup.py install
Django安装目录路径:
/usr/local/python2.7/lib/python2.7/site-packages/django/
测试Django创建Django项目:
# /usr/local/python2.7/bin/django-admin.py startproject hello
安装MySQL-python
1)使用easy_install安装
# easy_install MySQL-python
2)使用Distutils安装
# wget http://sourceforge.net/projects/mysql-python/files/mysql-python/1.2.3/MySQL-python-1.2.3.tar.gz/download
# tar zxvf MySQL-python-1.2.3.tar.gz
# cd MySQL-python-1.2.3
# python2.7 setup.py install
注:安装时,python报错,需要执行 sudo apt-get install python-dev
注:如果mysql是自定义源码编译安装,将会出现下面错误提示:
File "/usr/local/src/MySQL-python-1.2.3/setup_posix.py", line 24, in mysql_config
raise EnvironmentError("%s not found" % (mysql_config.path,))
EnvironmentError: mysql_config not found
注:先执行命令 find / -name mysql_config 查看是否存在mysql_config文件,若不存在,需要安装libmysqlclient-dev,执行命令 sudo apt-get install libmysqlclient-dev
或者安装libmysqld-dev,执行命令 sudo apt-get install libmysqld-dev。安装完成后再执行 find / -name mysql_config 命令查看mysql_config文件路径,例:/usr/local/bin/mysql_config。
则需要指定修改site.cfg文件mysql_config路径:
# vi site.cfg
将下面#去掉,并指定mysql_config的路径
# mysql_config = /usr/local/bin/mysql_config
安装uwsgi
注:uwsgi 1.1.2版本无法安装成功.
# wget http://projects.unbit.it/downloads/uwsgi-1.0.4.tar.gz
# tar zxvf uwsgi-1.0.4.tar.gz
# cd uwsgi-1.0.4
# make -f Makefile.PY27
# cp uwsgi /usr/bin/uwsgi
注:Makefile.Py27 (27为python版本号)
部署OSQA
# cd /home
# svn co http://svn.osqa.net/svnroot/osqa/trunk osqa
# cd osqa
编辑settings_local.py
# cp settings_local.py.dist settings_local.py
# vi settings_local.py
修改数据库信息和APP_URL等几个地方:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'osqa',
'USER': 'root',
'PASSWORD': 'password',
'HOST': '',
'PORT': '',
}
}
APP_URL = 'http://www.unixs.net'
LANGUAGE_CODE = 'zh_CN'
PS:mysql在本地的话不要指定HOST和PORT,如果指定了HOST和PORT导入数据库时会出现Django无法链接数据库。
编辑osqa.wsgi
# cp osqa.wsgi.dist osqa.wsgi
# vi osqa.wsgi
修改添加:
os.environ['PYTHON_EGG_CACHE'] = '/var/www/.python-eggs'
sys.path.append('/home')
sys.path.append('/home/osqa')
os.environ['DJANGO_SETTINGS_MODULE'] = 'osqa.settings'
创建.python-eggs
# mkdir -p /var/www/.python-eggs
# chown www:www /var/www/.python-eggs
创建管理员界面目录
# ln -s /usr/local/python2.7/lib/python2.7/site-packages/django/contrib/admin/media /home/osqa/admin_media
创建导入数据库表
先创建好数据库并指定数据库编码:
CREATE DATABASE osqa DEFAULT CHARACTER SET UTF8 COLLATE utf8_general_ci;
在/home/osqa目录下执行:
# python2.7 manage.py syncdb
过程中有选择是否创建管理员用户,选择(no),本过程中选择了创建,但是创建的用户实际上没有存在。
修改目录权限
# chown -R www /home/osqa
# chmod -R g+w /home/osqa/forum/upfiles
# chmod -R g+w /home/osqa/log
创建uwsgi.xml文件
# vi /etc/uwsgi.xml
www
www
127.0.0.1:9090
/home/osqa
/home/osqa/osqa.wsgi
1
20
128
具体参数参见:http://projects.unbit.it/uwsgi/wiki/Doc
添加站点配置
server {
listen 80;
server_name example.com;
location /m/ {
alias /home/osqa/forum/skins/;
}
location /upfiles/ {
alias /home/osqa/forum/upfiles/;
}
location /admin_media/ {
alias /home/osqa/admin_media/;
}
location / {
include uwsgi_params;
uwsgi_pass 127.0.0.1:9090;
uwsgi_param UWSGI_CHDIR /home/osqa;
uwsgi_param UWSGI_PYHOME /usr/local/python2.7;
uwsgi_param UWSGI_SCRIPT osqa_wsgi;
}
}
启动Nginx,uwsgi
# /usr/local/nginx/sbin/nginx -t 测试
# /usr/local/nginx/sbin/nginx 启动
# /usr/bin/uwsgi -x /etc/uwsgi.xml 启动uwsgi