本文介紹了在Centos系統下搭建Django站點的流程。 html
一.安裝環境 python
本文適合環境: mysql
centOS 5/6 web
python2.7+- sql
Django1.5 shell
mod_wsgi apache
apache2.2/2.4 django
其中因爲mod_python對python2.7沒有提供支持,所以採用mod_wsgi代替mod_python.1.安裝apache服務器: vim
# yum install -y httpd httpd-devel(通常系統已自帶apache服務器,安裝在/etc/httpd目錄下)
http-devel 是爲了apxs,yum後你能夠whereis apxs去尋找他,而後後邊編譯使用。 centos
2.安裝python2.7
(我編譯後不能import zlib,先要
# yum install -y zlib zlib-devel
)
centos系統自帶的python基本是小於2.6版本的,你能夠用自帶的編譯,可能會產生錯誤,儘可能編譯2.7吧。【假設全部下載的壓縮文件放在~(家目錄)目錄下。】
#tar -xzvf Python-2.7.tar.bz2 #cd Python-2.7 #.configure --enable-shared (!!!後面安裝mysql-python時,python必須採用共享方式)加了這個參數--enable-shared會出現運行找不到 libpython2.5.so.1.0 這個庫,在編輯 目錄下把這個文件複製到 /usr/lib下就能夠,而後可加入連接到 /usr/lib64目錄。若是在有錯把/usr/local/lib and /usr/local/lib64 都作lin(快捷方式)
通常安裝路徑是:/usr/local/lib/python2.7。安裝好後執行命令#python,若輸出版本號,說明成功。
錯誤1:/usr/local/lib/python2.7/config/libpython2.7.a: could not read symbols: Bad value collect2ld returned 1 exit status apxs:Error: Command failed with rc=65536
=》由於安裝python的時候,沒有 ./configure --enable-shared
或
更改在編譯目錄下src文件夾下connobject.c 142行左右
!(b == APR_BRIGADE_SENTINEL(b) ||
改成
!(b == APR_BRIGADE_SENTINEL(bb) ||
(我都遇到了)
錯誤2:python: error while loading shared libraries: libpython2.7.so.1.0: cannot open shared object file: No such file or directory =》新建文件: #vim /etc/ld.so.conf.d/python2.7.conf
加入內容: /usr/local/lib保存退出後運行: ldconfig 再執行#python,測試是否成功。
上邊必定要注意,由於編譯wsgi還可能會報同樣的錯。
3. 安裝Django 【下載Django壓縮文件至~目錄】
#tar -xzvf Django1.4 #cd Django1.4 #python setup.py install
測試:
#python >>>import django >>>django.VERSION
4. 安裝mod_wsgi
a。安裝以前,先進行apache的apxs擴展:(上邊已經裝了)
# yum install httpd-devel
錯誤:有可能發現yum命令用不了,提示:No module named yum
=》緣由是yum命令依賴python2.4,如今用了2.7以後,yum命令就用不了了,解決方法以下:
#vim /usr/bin/yum
將 #!/usr/bin/python 修改成 #!/usr/bin/python2.4
其餘錯誤:bad interpreter: No such file or directory
b。安裝mod_wsgi,mod_wsgi壓縮包也在/var目錄下,一樣採用源碼【連接】安裝
在這我卡了好長時間。終於編譯mod_wgsi成功了。。。。。。。。
這塊看你改不改編譯的python做爲你的默認python了,我直接用
# alias python="python2.7" #你能夠把這個寫在/etc/profile最後邊,而後# source /etc/profile這樣能安全點吧。
# ./configure --with-python=/usr/local/bin/python2.7 【關聯python2.7】 # make # make install
有可能報錯。
缺啥就--with-。
找不到就whereis一下
儘可能完整。
5,開始配置。
在httpd.conf文件中加入
LoadModule wsgi_module modules/mod_wsgi.so
重啓即完成安裝。
而後部署django
首先先看一下tree
[root@localhost html]# tree /var/www/html/ #在這django-admin.py startproject mysite /var/www/html/ #除了標記都是生成的。 └── mysite ├── manage.py ├── mysite │ ├── helloworld.py #本身測試寫的一個hello,不寫能夠,成功後會顯示默認 │ ├── helloworld.pyc │ ├── __init__.py │ ├── __init__.pyc │ ├── settings.py │ ├── settings.pyc │ ├── urls.py │ ├── urls.pyc │ ├── wsgi.py │ └── wsgi.pyc #不動 └── wsgi └── django.wsgi #本身寫的django.wsgi 內容
import os import sys sys.path.append('/var/www/html/mysite')#不寫這個老說配置文件再也不環境變量裏,就不在環境變量裏寫了,在這寫 os.environ['DJANGO_SETTINGS_MODULE'] = 'mysite.settings' #配置文件。 os.environ['PYTHON_EGG_CACHE'] = '/tmp/.python-eggs' current_dir = os.path.dirname(__file__) if current_dir not in sys.path: sys.path.append(current_dir) import django.core.handlers.wsgi #!! application = django.core.handlers.wsgi.WSGIHandler() #!!
httpd.conf配置文件中添加的內容,用來鏈接django.wsgi,使工程被apache加載。
我們httpd不是編譯的。有這個目錄。
[root@localhost mysite]# ls /etc/httpd/conf.d/apache會加載他,新建配置文件wsgi.conf
# LoadModule wsgi_module modules/mod_wsgi.so #在httpd.conf寫了的話這就註釋掉。 WSGIScriptAlias / "/var/www/html/mysite/wsgi/django.wsgi" <Directory "/var/www/html/mysite"> #網站家目錄。 Order Deny,Allow Allow from all </Directory>
http.conf
更改
DocumentRoot "/var/www/html/mysite" ServerName 127.0.0.1:80
而後由於靜態文件放在static目錄中,因此要在httpd.conf中加入static的映射:
Alias /static H:/pythondev/webDSS/static
若是使用django的後臺管理的話,還必須在httpd.conf中加入:
Alias /static/admin E:/Python27/Lib/site-packages/django/contrib/admin/media <Directory "E:/Python27/Lib/site-packages/django/contrib/admin/media"> Order Deny,Allow Allow from all </Directory>而後重啓httpd服務。搞定。