CentOS下Apache+Python+Django+Wsgi環境搭建__Python

來源:互聯網
上載者:User
CentOs下Apache+Python+Django+mod_wsgi環境搭建

 

 

前言:由於對linux系統的不瞭解,裝軟體什麼的總是喜歡用yum命令,結果這次在搭建環境下吃了大虧。網上文章大多是使用mod_python來搭建的,這個只支援到python2.5,現在需要使用python2.7,只好選擇mod_wsgi,貌似mod_wsgi的處理效率還要更高一些。

 

1.      安裝環境:

 

CentOs版本:   CentOs5.7

Python版本:   Python2.7

Apache版本:   Httpd2.2

Django版本: Django1.1

Mod_wsgi版本:    Mod_wsgi-3.2-1.el5.x86_64.rpm

 

 

2.      軟體安裝:

2.1      安裝Apache:

一般伺服器系統會內建Apache,如果沒有可以使用yum安裝一下:

 

 

yum –y install httpd(httpd-2.2.3-53.el5.centos.1.x86_64.rpm)  

 

2.2      安裝Python2.7:

一般linux系統會內建Python,一般是2.4版本的,我們需要重新安裝,這裡注意,不要使用yum安裝,而要用源碼編譯的方法安裝,不然下面在配置之後會出錯。

 

tar -xvf Python-2.7.tar.bz2
  cd Python-2.7
  ./configure  --enable-shared       這裡一定要注意,解壓完之後要設定enable-shared  

make  

make install  

一般安裝到的路徑是: /usr/local/lib/python2.7

安裝好之後可以執行:# python   查看python是否安裝成功,這裡可能會遇到錯誤:

 

錯誤:  

/usr/local/lib/python2.7/config/libpython2.7.a:  could not read symbols: Bad value
  collect2: ld returned 1 exit status
  apxs:Error: Command failed with rc=65536  

   

這是因為安裝python的時候,沒有   ./configure  --enable-shared  

   

加上後重新編譯,然後運行python,  

   

遇到錯誤:  

python: error while loading shared  libraries: libpython2.7.so.1.0:
  cannot open shared object file: No such file or    

解決方案: 

建立下面檔案
  vim /etc/ld.so.conf.d/python2.7.conf
  加入內容:
  /usr/local/lib
  儲存退出後運行:
  ldconfig   

再次執行 python,問題成功解決。  

 

2.3      安裝Django:

Django的安裝比較簡單,和windows下一樣

tar xzvf  Django-1.1.tar.gz 

cd  Django-1.1

sudo python  setup.py install    

 

安裝完之後,執行命令:

# python

>>>import django

>>>django.VERSION  

可以看到Django的版本號碼,則安裝成功。

 

2.4      安裝wsgi

下載wsgi模組,注意要下載linux版本的

http://code.google.com/p/modwsgi/downloads/detail?name=mod_wsgi-3.3.tar.gz&can=2&q=

 

安裝 mod_wsgi 之前先安裝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  

 

安裝完httpd_devel之後,開始安裝mod_wsgi:

./configure --with-python=/usr/local/bin/python2.7

make

make install 

安裝mod_wsgi的時候,建議不要使用yum安裝,因為用yum安裝很可能無法找到正確的python版本和apxs模組,或者本來沒安裝apxs模組,用yum安裝wsgi也可以成功,但實際運行卻會發生很多奇怪的錯誤,所以建議還是源碼編譯安裝。

 

 

3          配置

3.1    配置apache:

Apache的設定檔在: /etc/httpd/conf/httpd.conf

 

添加:

LoadModule  wsgi_module modules/mod_wsgi.so  

注意:LoadModule wsgi_module modules/mod_wsgi.so要跟所有LoadModule 配置放到一起,如果加到最後無法找到這個模組。

這個非常重要,哥哥就是吃這個虧吃大了,調試了一個下午,沒結果,各種蛋疼啊。

 

然後在設定檔中,添加:

 

 

NameVirtualHost  *:8080

Listen  192.168.145.139:8080 

<VirtualHost  *:8080>  ServerName  www.abc.com 

WSGIScriptAlias  / /var/www/html/LTFS_VLA/setting.wsgi  DocumentRoot  /var/www/html/LTFS_VLA 

<Directory />

Options FollowSymLinks

AllowOverride

Order allow,deny

Allow from all

</Directory>

 

<Directory /var/www/html/LTFS_VLA/setting.wsgi>

AllowOverride None

Order Allow,Deny

Allow from all

<Files setting.wsgi>

Allow from all

</Files>

</Directory>  

Alias /ltfs "/var/www/html/LTFS_VLA" 

<Directory "/var/www/html/LTFS_VLA">

Options FollowSymLinks

AllowOverride All

Order allow,deny

Allow from all

</Directory>

 

Alias /admin_media  "/usr/local/lib/python2.7/site-pacages/django/conftrib/admin/media" 

 <Directory "/usr/local/lib/python2.7/site-pacages/django/conftrib/admin/media">

Order allow,deny

Options Indexes

Allow from all

IndexOptions FancyIndexing

</Directory>  

</VirtualHost>  

配置中的路徑和IP連接埠等,根據實際需要自行更改。

 

3.2   配置wsgi:

根據上面的配置,在/var/www/html/LTFS_VLA/  目錄下,建立setting.wsgi檔案,內容如下:

 

import os

import sys

sys.stdout=sys.stderr

from os.path  import abspath,dirname,join

from  django.core.handlers.wsgi import WSGIHandler

sys.path.insert(0,abspath(join(dirname(__file__),"./")))

os.environ["DJANGO_SETTINGS_MODULE"]  =  "gui.settings"

application=WSGIHandler()  

這個檔案的作用是一些環境變數的設定。

 

4          調試

開啟apache服務

 

Service httpd restart  

然後開啟瀏覽器,輸入IP地址就可以了,我這裡輸入的是192.168.145.139:8080。

 

 

以上就是基本的配置過程,

 

 

 

 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.