图像

Django 1.7 with Apache and mod_wsgi

这个在网上有N多版本,但是都是django的老版本,大家的部署环境也不太一样,Linux OS版本也不同。下面我说一下我的两个部署环境,我本机开发环境是Debian 7,我的服务器部署环境是CentOS 7,都是比较新的环境。下面详细说明:

Debian 7 Wheezy

python 2.7.3 (Debian自带的是python2.6,所以你需要安装新的python);
Apache/2.2.22 (Debian)
Django 1.7.1
mod_wsgi 4.3.0-1

最难搞的可能是mod_wsgi,你不用编译源代码就可以使用:

Debian Packages
Details on Debian packages can be found at:

<a href="http://packages.debian.org/unstable/python/libapache2-mod-wsgi" target="_blank">
http://packages.debian.org/unstable/python/libapache2-mod-wsgi
</a>
Thanks to Bernd Zeimetz for preparing and putting together this distribution.

其它linux版本的我会特别列出。方便不能翻墙的哥们。
mod_wsgi配置,debian安装成功mod_wsgi后,你几乎不需要怎么配置,我把系统自动安装生成的两个配置文件贴出来。
首先是wsgi.load文件

LoadModule wsgi_module /usr/lib/apache2/modules/mod_wsgi.so

接着是wsgi.conf文件

<IfModule mod_wsgi.c>
    #This config file is provided to give an overview of the directives,
    #which are only allowed in the 'server config' context.
    #For a detailed description of all avaiable directives please read
    #http://code.google.com/p/modwsgi/wiki/ConfigurationDirectives

    #WSGISocketPrefix: Configure directory to use for daemon sockets.
    #
    #Apache's DEFAULT_REL_RUNTIMEDIR should be the proper place for WSGI's
    #Socket. In case you want to mess with the permissions of the directory,
    #you need to define WSGISocketPrefix to an alternative directory.
    #See http://code.google.com/p/modwsgi/wiki/ConfigurationIssues for more
    #information

        #WSGISocketPrefix /var/run/apache2/wsgi

    #WSGIPythonOptimize: Enables basic Python optimisation features.
    #
    #Sets the level of Python compiler optimisations. The default is '0'
    #which means no optimisations are applied.
    #Setting the optimisation level to '1' or above will have the effect
    #of enabling basic Python optimisations and changes the filename
    #extension for compiled (bytecode) files from .pyc to .pyo.
    #When the optimisation level is set to '2', doc strings will not be
    #generated and retained. This will result in a smaller memory footprint,
    #but may cause some Python packages which interrogate doc strings in some
    #way to fail.

        #WSGIPythonOptimize 0


    #WSGIPythonPath: Additional directories to search for Python modules,
    #                overriding the PYTHONPATH environment variable.
    #
    #Used to specify additional directories to search for Python modules.
    #If multiple directories are specified they should be separated by a ':'.

        #WSGIPythonPath directory|directory-1:directory-2:...


    #WSGIPythonEggs: Directory to use for Python eggs cache.
    #
    #Used to specify the directory to be used as the Python eggs cache
    #directory for all sub interpreters created within embedded mode.
    #This directive achieves the same affect as having set the
    #PYTHON_EGG_CACHE environment variable.
    #Note that the directory specified must exist and be writable by the user
    #that the Apache child processes run as. The directive only applies to
    #mod_wsgi embedded mode. To set the Python eggs cache directory for
    #mod_wsgi daemon processes, use the 'python-eggs' option to the
    #WSGIDaemonProcess directive instead.

        #WSGIPythonEggs directory

    #WSGIRestrictEmbedded: Enable restrictions on use of embedded mode.
    #
    #The WSGIRestrictEmbedded directive determines whether mod_wsgi embedded
    #mode is enabled or not. If set to 'On' and the restriction on embedded
    #mode is therefore enabled, any attempt to make a request against a
    #WSGI application which hasn't been properly configured so as to be
    #delegated to a daemon mode process will fail with a HTTP internal server
    #error response.

        #WSGIRestrictEmbedded On|Off
    #WSGIRestrictStdin: Enable restrictions on use of STDIN.
    #WSGIRestrictStdout: Enable restrictions on use of STDOUT.
    #WSGIRestrictSignal: Enable restrictions on use of signal().
    #
    #Well behaved WSGI applications neither should try to read/write from/to
    #STDIN/STDOUT, nor should they try to register signal handlers. If your
    #application needs an exception from this rule, you can disable the
    #restrictions here.

        #WSGIRestrictStdin On
        #WSGIRestrictStdout On
        #WSGIRestrictSignal On

    #WSGIAcceptMutex: Specify type of accept mutex used by daemon processes.
    #
    #The WSGIAcceptMutex directive sets the method that mod_wsgi will use to
    #serialize multiple daemon processes in a process group accepting requests
    #on a socket connection from the Apache child processes. If this directive
    #is not defined then the same type of mutex mechanism as used by Apache for
    #the main Apache child processes when accepting connections from a client
    #will be used. If set the method types are the same as for the Apache
    #AcceptMutex directive.

        #WSGIAcceptMutex default
    #WSGIImportScript: Specify a script file to be loaded on process start.
    #
    #The WSGIImportScript directive can be used to specify a script file to be
    #loaded when a process starts. Options must be provided to indicate the
    #name of the process group and the application group into which the script
    #will be loaded.

        #WSGIImportScript process-group=name application-group=name

    #WSGILazyInitialization: Enable/disable lazy initialisation of Python.
    #
    #The WSGILazyInitialization directives sets whether or not the Python
    #interpreter is preinitialised within the Apache parent process or whether
    #lazy initialisation is performed, and the Python interpreter only
    #initialised in the Apache server processes or mod_wsgi daemon processes
    #after they have forked from the Apache parent process.
        #WSGILazyInitialization On|Off
</IfModule>

各个配置字段的意思很明确,大家不懂就google下。
下面进入正题,apache配置,我直接贴出我的配置文件:

#WSGIPythonPath /var/www/courb
<VirtualHost *:80>
    ServerAdmin dev@connove.com
    ServerName tong.connove.com
    ServerAlias tong.connove.com
    DocumentRoot /var/www/courb
    #Alias /robots.txt /path/to/mysite.com/s/robots.txt
    Alias /favicon.ico /path/to/mysite.com/s/favicon.ico

    Alias /media/ /var/www/courb/media/
    Alias /static/ /var/www/courb/s/

    <Directory /var/www/courb/s>
        Order allow,deny
        Allow from all
    </Directory>

    <Directory /var/www/courb/media>
        Order allow,deny
        Allow from all
    </Directory>

    WSGIScriptAlias / /var/www/courb/courb/wsgi.py
   
    WSGIDaemonProcess courb user=jerry threads=10 python-path=/var/www/courb/courb/:/usr/lib/python2.7/dist-packages
    WSGIProcessGroup courb

    ErrorLog logs/py_error.log
    LogLevel info

    <Directory /var/www/courb/courb>
    <Files wsgi.py>
        Order allow,deny
        Allow from all
    </Files>
    </Directory>    
</VirtualHost>

这里的配置要注意几点:
1. 静态文件的alias要放到前面;
2. linux上WSGIDaemonProcess模式可以充分利用apache的性能;
3. 配置的日志级别为info,便于我们找出问题所在,在第一次部署时,你可以把Django的DEBUG设为True,这样便于调试。等到上线一定记得关闭debug模式;
4. 有问题多看看日志文件,然后就是stackoverflow去逛逛,可以说stackoverflow是我们的百科全书。
配置完以上这些,把你的域名映射到主机上,应用放到正确的路径,然后跑一次

python manage.py collectstatic

就可以重启你的apache了,这东西不难,apache的配置可能你还会遇到权限的问题,这个时候可以多多尝试下,一般都不难,可以看看这个Common Http 404,500 problems with python scripts on Apache or Nginx
一旦你跑起来你的应用并观察它和预期一样,你肯定会觉得mod_wsgi不过如此,貌似很简单,但那是因为你已经搞定了,在你没有搞定的时候,你肯定不会这么想,不要高兴哦,可能客户给我们的部署环境不一样,这个时候你刚刚轻松的心情就会悬起来,因为有的环境你真的搞不定,我不是和你开玩笑。后面我会给你讲讲我的故事。

CentOS 7

为了让应用更快的部署到客户的服务器上,我们在公司内部的CentOS环境上又部署了一次该应用。
以下是它的具体环境:

python 2.7.5 (CentOS);
Apache/2.4.6 (CentOS)
Django 1.7.1
mod_wsgi 3.4

因为CentOS 7的系统比较新,mod_wsgi没有提供7.x系列的rpm地址,我本来准备自己编译的,结果在aliyun的镜像上找到了该rpm,我把地址列出来,大家需要什么安装包都可以在这上面找到。

http://mirrors.aliyun.com/centos/7/os/x86_64/Packages/


安装完成后你可以配置你的conf文件,这里啰嗦几句,CentOS 7已经不提供mysql的package,官方全部采用mariadb,我为了偷懒就用了lampp最新版本里的mysql来代替了,节省了时间,你可以自己搞定mariadb,把它和python结合起来。
下面是centos 7 wsgi对应应用的配置内容:

WSGIPythonPath /opt/lampp/htdocs/courb
WSGISocketPrefix /var/run/wsgi
NameVirtualHost tong.connove.com
<VirtualHost tong.connove.com>
    ServerAdmin dev@connove.com
    ServerName tong.connove.com
    #ServerAlias tong.connove.com
    DocumentRoot /opt/lampp/htdocs/courb
    Alias /robots.txt /opt/lampp/htdocs/courb/s/robots.txt
    Alias /favicon.ico /opt/lampp/htdocs/courb/s/favicon.ico

    Alias /media/ /opt/lampp/htdocs/courb/media/
    Alias /static/ /opt/lampp/htdocs/courb/s/

    <Directory /opt/lampp/htdocs/courb/s>
        Order allow,deny
        Allow from all
    </Directory>

    <Directory /opt/lampp/htdocs/courb/media>
        Order allow,deny
        Allow from all
    </Directory>

    WSGIScriptAlias / /opt/lampp/htdocs/courb/courb/wsgi.py
   
    WSGIDaemonProcess courb user=jerry threads=10 python-path=/opt/lampp/htdocs/courb/courb/:/usr/lib/python2.7/site-packages
    WSGIProcessGroup courb

    ErrorLog logs/py_error
    LogLevel info

    <Directory /opt/lampp/htdocs/courb/courb>
    <Files wsgi.py>
        Order allow,deny
        Allow from all
    </Files>
    </Directory>    
</VirtualHost>

发表评论