apache+Django+mod_wsgi(CentOS5.9)

1.安装apache
1.首先卸载环境自带的httpd
    # rpm -e httpd --nodeps

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 install

4.启动apache服务
    # /usr/local/apache2/bin/apachectl start

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
        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成功.

评论
热度(1)
©蛇信子 | Powered by LOFTER