Linux + Apache + MySQL 環境下OSQA部署


OSQA是開源的問答網站,采用Python的Django框架開發。按照官方的安裝指南,在安裝過程中出現了一些問題,現將試驗成功的方法總結下。

官方的安裝指南: http://wiki.osqa.net/display/docs/Ubuntu+with+Apache+and+MySQL?focusedCommentId=3539023#comment-3539023

 

安裝環境: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)  主頁下載

 http://www.osqa.net/download/

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,就可以看到頁面了。







 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM