Linux+Django+Python+Wsgi配置過程

來源:互聯網
上載者:User

標籤:

Linux下配置Apache+Mod_Wsgi+Django環境

(ybw 2011-09-23)

一.安裝環境

作業系統:CentOS release 5.5

核心版本:Linux Svn-168-1-11 2.6.18-194.el5 #1 SMP Fri Apr 2 14:58:14 EDT 2010 x86_64 x86_64 x86_64 GNU/Linux

Python版本:Python 2.4.3

Apche版本:httpd-2.2.3

Mod_Wsgi版本:mod_wsgi-3.2-1.el5.x86_64.rpm

Django版本:Django-1.2.4

 

二.軟體安裝

Python採用預設rpm安裝路徑

安裝Apache:

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

安裝Mod_Wsgi:

rpm –ivh mod_wsgi-3.2-1.el5.x86_64.rpm

安裝Django:

tar –zxvf Django-1.2.4.tar.gz

cd Django-1.2.4

python setup.py install

 

安裝MySQLdb

wget -q http://peak.telecommunity.com/dist/ez_setup.py

wget http://pypi.python.org/packages/2.4/s/setuptools/setuptools-0.6c11-py2.4.egg

python ez_setup.py

tar –zxvf MySQL-python-1.2.3.tar.gz

cd  MySQL-python-1.2.3

python setup.py build

python setup.py install

 

安裝成功驗證:

Shell> python

Python 2.4.3 (#1, May  5 2011, 16:39:10) 

[GCC 4.1.2 20080704 (Red Hat 4.1.2-50)] on linux2

Type "help", "copyright", "credits" or "license" for more information.

>>> import django

>>> django.VERSION

(1, 2, 4, ‘final‘, 0)

>>> import MySQLdb

>>>

 

shell>ls /etc/httpd/conf.d

proxy_ajp.conf  README  welcome.conf  wsgi.conf

 

shell> ls /etc/httpd/modules/ | grep mod_wsgi

mod_wsgi.so

 

三.項目部署

1.打包壓縮開發代碼:dajie.zip

2.上傳至伺服器並解壓縮至目錄/var/www/html

 Shell> ls /var/www/html/

dajie

3.修改Apche設定檔httpd.conf

Shell> vi /etc/httpd/conf/httpd.conf

###在最後添加如下內容

<VirtualHost *:80>

        ServerName mice.operation.dajie-inc.com

        DocumentRoot /var/www/html/dajie

        WSGIScriptAlias / /var/www/html/dajie/apache/django.wsgi

        <Directory />

                Order deny,allow

                Allow from all

        </Directory>

        <Directory /apache>

                Allow from all

        </Directory>

</VirtualHost>

4.建立並配置wsgi的設定檔:

Shell>cd /var/www/html/dajie

Shell>mkdir apache

Shell>touch django.wsgi

Shell>cat django.wsgi

import os, sys

 

#Calculate the path based on the location of the WSGI script.

apache_configuration= os.path.dirname(__file__)

project = os.path.dirname(apache_configuration)

workspace = os.path.dirname(project)

sys.path.append(workspace)

os.environ[‘DJANGO_SETTINGS_MODULE‘] = ‘dajie.settings‘

os.environ[‘PYTHON_EGG_CACHE‘] = ‘/tmp‘

 

import django.core.handlers.wsgi

application = django.core.handlers.wsgi.WSGIHandler()

print >> sys.stderr, sys.path

shell>chmod a+x django.wsgi

5.修改django項目的設定檔/var/www/html/dajie/settings.py,主要是下面兩處:

DATABASES = {

    ‘default‘: {

        ‘ENGINE‘: ‘django.db.backends.mysql‘,

        ‘NAME‘: ‘dajieod‘,                     

        ‘USER‘: ‘dajieod‘,                      

        ‘PASSWORD‘: ‘dajieod123‘,                  

        ‘HOST‘: ‘127.0.0.1‘,                     

        ‘PORT‘: ‘3306‘,                         }

}

 

TEMPLATE_DIRS = (

    # Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".

    # Always use forward slashes, even on Windows.

    # Don‘t forget to use absolute paths, not relative paths.

    ‘/var/www/html/dajie/templates‘,

)

 

四.啟動項目

shell>service httpd restart

訪問測試:

http://192.168.1.11

 

項目部署ok~

 

五.問題集

1.安裝MySQL-python執行:python setup.py build報錯:EnvironmentError: mysql_config not found

方法解決:ln -s /usr/local/mysql/bin/mysql_config /usr/local/bin/mysql_config
將mysql_confi從你的安裝目錄連結到/usr/local/bin目錄下,這樣就可以在任意目錄下訪問了(也可以放到/usr/bin)

 

2. import MySQLdb報錯:ImportError: libmysqlclient_r.so.16: cannot open shared object file: No such file or directory

解決方案:將mysql/lib下所有關於libmysqlclient的so檔案軟連結到/usr/lib下。
ln -s /usr/local/mysql/lib/mysql/libmysqlclient* /usr/lib
ldconfig
這樣 import MySQLdb 的時候就不會出錯了

 

3.在系統內容中安裝完django、MySQLdb後,執行import都沒有問題,但是啟動Apache後,瀏覽器訪問時卻報錯:

Error loading MySQLdb module: ImportError: libmysqlclient_r.so.16: cannot open shared object file: Permission denied

解決方案:這裡apache的Permission denied問題是由SELINUX引起的,關閉SELINUX即可。關閉Selinux:vi /etc/selinux/config===》SELINUX=disabled,然後重啟系統

(不重啟設定Selinux:setenforce 0   查看Selinux:getenforce)

 

 

 

Linux+Django+Python+Wsgi配置過程

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.