Linux配置實踐:apache+wsgi+虛擬主機配置


<安裝mod_wsgi>html

使用centos光盤中mod_wsgi.rpm包node

    LoadModule wsgi_module modules/mod_wsgi.sopython

編譯完成後引入配置文件:shell

LoadModule wsgi_module modules/mod_wsgi.so


最後的虛擬主機配置:apache

#
# Use name-based virtual hosting.
#
NameVirtualHost *:80
#
# NOTE: NameVirtualHost cannot be used without a port specifier 
# (e.g. :80) if mod_ssl is being used, due to the nature of the
# SSL protocol.
#

#
# VirtualHost example:
# Almost any Apache directive may go into a VirtualHost container.
# The first VirtualHost section is used for requests without a known
# server name.
#
<VirtualHost *:80>
    ServerAdmin centos@nobsu.net
    DocumentRoot /var/www/html
    ServerName centos.nobsu.net
    ErrorLog logs/centos.nobsu.net-error_log
    CustomLog logs/centos.nobsu.net-access_log common
</VirtualHost>
<VirtualHost *:80>
    ProxyPreserveHost On
    ProxyPass / http://192.168.75.1
    ProxyPassReverse / http://192.168.75.1/
    ServerAdmin front@nobsu.net
    ServerName front.nobsu.net
</VirtualHost>

<VirtualHost *:80>
        ServerName py.nobsu.net
        ServerAlias py.nobsu.net
        DocumentRoot "/var/www/mysite"
        WSGIScriptAlias /test "/var/www/mysite/wsgi/django.wsgi"
        Alias /static "/var/www/static/"
        <Directory "/var/www/static/">
           Order deny,allow
           Allow from all
        </Directory>
        <Directory "/var/www/">
           Order Deny,Allow
           Allow from all
        </Directory>
</VirtualHost>


apache+Django+mod_wsgi(CentOS6.3)django

1.安裝apache
1.首先卸載環境自帶的httpd
    # rpm -e httpd --nodepscentos

2.安裝gcc
    # yum install -y *gcc*(此過程在虛擬機的環境爲CentOS上須要大約40分鐘)服務器

3.解壓httpd並進行安裝(/root/tools/)
    # cd /root/tools
    # tar zxvf httpd-2.2.15.tar.gz
    # cd httpd-2.2.15
    # ./configure --prefix=/usr/local/apache2 --enable-rewrite --enable-so(大約兩分鐘,選項的做用主要是容許動態加載模塊,之後咱們要加載mod_wsgi)
    # make(大約三分鐘)
    # make installapp

4.啓動apache服務
    # /usr/local/apache2/bin/apachectl startpython2.7

5.測試
    # firefox localhost
當看到"It works"表明apache配置成功

2.安裝mod_wsgi
1.解壓,安裝
    # tar zxvf mod_wsgi-3.3.tar.gz -C /usr/local/src/
    # cd /usr/local/src/mod_wsgi-3.3/
    # ./configure --with-apxs=/usr/local/apache2/bin/apxs(apache動態添加的一個模塊)
                       --with-python=/usr/local/bin/python2.7(指定python的路徑)
                       --with-mutex-dir=/var/run/mod_wsgi(最大緩衝值的目錄)
    # make 在這一步若是出現make: *** [mod_wsgi.la]錯誤則須要從新編譯安裝python2.7並加上--enable-shared參數.
    # make install

編譯過程遇到"error while loading shared libraries: libpython2.7.so.1.0:cannot open shared object file: No such file or directory"錯誤解決:

緣由
    在系統的lib路徑中找不到這個共享庫. 
    若是編譯時加上了--enable-shared,纔會編譯這個共享庫,默認的位置是python可執行程序所在目錄的lib目錄下,如/usr/local/python27

解決方法
    1.能夠使用以下方式編譯Python解決這個問題
          # ./configure --enable-shared --prefix=/usr/local/python27
          # make && make install

    2.
        # cp /usr/local/python27/lib/libpython2.7.so.1.0 /usr/local/lib
        # cd /usr/local/lib
        # ln -s libpython2.7.so.1.0 libpython2.7.so

    3.
        # whereis libpython2.7.so.1.0
        libpython2.7.so.1:/usr/local/lib/libpython2.7.so.1.0

    4.若是whereis沒有結果,或者仍然沒有解決問題
        # cat /etc/ld.so.conf  在此文件中加入/usr/local/lib
        include ld.so.conf.d/*.conf
        /usr/local/lib 
        # /sbin/ldconfig
        # /sbin/ldconfig -v|grep libpython2.7*
    

    # make && make install

2.配置Apache
    在Apache配置文件httpd.conf中,增長一行:
    LoadModule wsgi_module modules/mod_wsgi.so
    AddType text/html .py

3.修改Virtual Host配置
    Apache 能夠配置不少個 Named-based Virtual Hosts ,能夠在一個服務器上部署多個Web Sites。
    在Apache配置文件/usr/local/apache2/conf/extra/httpd-vhosts.conf增長:
    <VirtualHost *:80>
        ServerName unicorn.test.com
        ServerAlias unicorn.test.cn
        DocumentRoot "/var/www/mysite"
        WSGIScriptAlias /test "/var/www/mysite/wsgi/django.wsgi"
        Alias /static "/var/www/static/"
        <Directory "/var/www/static/">
           Order Deny,Allow
           Allow from all 
        </Directory>
        <Directory "/var/www/">
           Order Deny,Allow
           Allow from all 
        </Directory>
    </VirtualHost>

4.建立測試頁面
1.建立django工程:
    django-admin.py startproject mysite

2.建立django.wsgi文件:
    在mysite文件夾下建立文件夾wsgi;
    在wsgi文件夾下面建立django.wsgi文件,其內容爲:
        import os
        import sys

        sys.stdout = sys.stderr

        from os.path import abspath, dirname, join
        from site import addsitedir

        from django.core.handlers.wsgi import WSGIHandler

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

        os.environ["DJANGO_SETTINGS_MODULE"] = "mysite.settings" #your settings module

        application = WSGIHandler()

重啓apache訪問localhost/test看到django的It worked成功.

相關文章
相關標籤/搜索