Linux + Apache + MySQL 环境下OSQA部署
OSQA是开源的问答网站,采用Python的Django框架开发。按照官方的安装指南,在安装过程中出现了一些问题,现将试验成功的方法总结下。
安装环境:linuxmint11, python2.7, django1.3, apache2.2。
本文中,linux的用户名为neil,在安装过程中一些路径请注意替换为真实路径。
1. 下载OSQA
1) svn下载
sudo apt-get install subversion #下载subversion
svn co http://svn.osqa.net/svnroot/osqa/trunk/ /home/neil/osqa-server #下载osqa到指定文件夹
2) 主页下载
2. 安装Apache
sudo apt-get install apache2 libapache2-mod-wsgi
3. 更新OSQA WSGI脚本
在/home/neil/osqa-server目录下新建一个文件osqa.wsgi,并输入如下内容
import os
import sys
sys.path.append('/home/neil')
sys.path.append('/home/neil/osqa-server')
# The first part of this module name should be identical to the directory name
# of the OSQA source. For instance, if the full path to OSQA is
# /home/osqa/osqa-server, then the DJANGO_SETTINGS_MODULE should have a value
# of 'osqa-server.settings'.
os.environ['DJANGO_SETTINGS_MODULE'] = 'osqa-server.settings'
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
4. 移除默认的Apache配置(可选)
官网说这个服务器只用于osqa,因此可以移除Apache的默认配置。
sudo rm /etc/apache2/sites-available/default\
/etc/apache2/sites-available/default-ssl\
/etc/apache2/sites-enabled/000-default
5. 为Apache添加OSQA的配置
新建并打开一个osqa配置文件
sudo gedit /etc/apache2/sites-available/osqa #官网是用vim打开的,由于我的机器上没有vim,我用的是gedit
填入如下配置信息
# Must be readable and writable by apache
WSGISocketPrefix ${APACHE_RUN_DIR}
#NOTE: all urs below will need to be adjusted if
#settings.FORUM_SCRIPT_ALIAS !='' (e.g. = 'forum/')
#this allows "rooting" forum at [http://example.com/forum], if you like
<VirtualHost *:80>
ServerAdmin forum@example.com
#osqa网站文件所在的目录
DocumentRoot /home/neil/osqa-server
#自定义
ServerName osqa.localhost
#run mod_wsgi process for django in daemon mode
#this allows avoiding confused timezone settings when
#another application runs in the same virtual host
WSGIDaemonProcess OSQA
WSGIProcessGroup OSQA
#force all content to be served as static files
#otherwise django will be crunching images through itself wasting time
Alias /m/ "/home/neil/osqa-server/forum/skins/"
<Directory "/home/neil/osqa-server/forum/skins">
Order allow,deny
Allow from all
</Directory>
Alias /upfiles/ "/home/neil/osqa-server/forum/upfiles/"
<Directory "/home/neil/osqa-server/forum/upfiles">
Order deny,allow
Allow from all
</Directory>
#this is your wsgi script described in the prev section
WSGIScriptAlias / /home/osqa/osqa-server/osqa.wsgi
CustomLog ${APACHE_LOG_DIR}/osqa.access.log common
ErrorLog ${APACHE_LOG_DIR}/osqa.error.log
</VirtualHost>
将配置文件链接到已启用站点的目录,也就是启用该站点
sudo ln -s /etc/apache2/sites-available/osqa /etc/apache2/sites-enabled/osqa
6. 安装MySQL
sudo apt-get install mysql-server mysql-client
安装过程中要求配置mysql的root用户名和密码。
添加为MySQL添加osqa用户,输入如下命令进入mysql命令行操作模式
sudo mysql -u root -p
创建用户
CREATE USER 'osqa'@'localhost' IDENTIFIED BY 'your_osqa_password';
创建数据库
CREATE DATABASE osqa DEFAULT CHARACTER SET UTF8 COLLATE utf8_general_ci;
GRANT ALL ON osqa.* to 'osqa'@'localhost';
7. 安装Python
linux默认是安装了python的,如果没有安装可以输入如下命令
sudo apt-get install python
安装python setup tools,安装了这个就可以很方便的安装一些python的库了。
sudo apt-get install python-setuptools
安装依赖的库
sudo apt-get install python-mysqldb
sudo easy_install South django django-debug-toolbar markdown \
html5lib python-openid
8. 配置OSQA的settings文件
进入/home/neil/osqa-server目录,拷贝settings_local.py.dist并重命名为settings_local.py
cp settings_local.py.dist settings_local.py
打开settings_local.py文件,并修改以下几项
DATABASE_NAME = 'osqa'
DATABASE_USER = 'osqa'
DATABASE_PASSWORD = 'your_osqa_password'
DATABASE_ENGINE = 'mysql'
同样在settings_local.py文件中,修改以下这项,来更新域名。
APP_URL = 'http://osqa.localhost/'
9. 为OSQA数据库建表
输入如下命令,会自动建立必须的表
sudo python manage.py syncdb --all
进行这步操作时,出现错误cannot import name mark_safe,解决方法,打开文件/home/neil/osqa-server/forum/utils/html.py,找到第6行,做如下修改。
#from django.template import mark_safe #原始代码
from django.utils.safestring import mark_safe #修改后
在稍后部署后访问页面出现http 500错误,查看错误日志/var/log/apache2/osqa.error.log,发现该错误url(r'^%s(.*)' % _('nimda/'), admin.site.root),据stackoverflow上的说法,打开/home/neil/osqa-server/forum/urls.py文件,找到第23行,这是django1.0版本的代码,在django1.3版本下不工作。替换成如下代码。
#url(r'^%s(.*)' % _('nimda/'), admin.site.root), #原始代码
url(r'^%s(.*)' % _('nimda/'), include(admin.site.urls)), #修改后
在建完表后,系统会要求建立一个超级管理员帐号,OSQA官网推荐说此时不建立该帐号,等网站架设起来后,通过正常方法建立的第1个用户默认就是超级管理员。
接下来还有一步,不是很了解,原文如下。
With that command you have successfully defined the schema. With South installed, you also have the ability to migrate between databases--a useful feature as OSQA is updated. However, as this is a fresh install, you will need to convince South that the schema is already up to date by "faking" a migration. You can do that with the following command:
sudo python manage.py migrate forum --fake
10. 保证Apache被允许访问OSQA文件
这条命令表示osqa-server文件夹下的所有文件被neil用户和Apache group (www-data)拥有
sudo chown -R neil:www-data /home/neil/osqa-server
允许Apache访问upfiles和log目录
sudo chmod -R g+w /home/osqa/osqa-server/forum/upfiles
sudo chmod -R g+w /home/osqa/osqa-server/log
11. 启动网站
如果是在本地部署,可以往hosts里添加一条指向本地的域名
sudo gedit /etc/hosts #打开hosts
加上以下这条内容,当在浏览器中输入osqa.localhost时就会跳转到本地部署的osqa
127.0.0.1 osqa.localhost
重新启动Apache
sudo /etc/init.d/apache2 restart
一切部署完毕,打开osqa.localhost,就可以看到页面了。