Apache,wsgi,django 程序部署配置方法详解

发布时间:2020-10-21 15:08:02 作者:轻舞肥羊
来源:脚本之家 阅读:244

本文实例讲述了Apache,wsgi,django 程序部署配置方法。分享给大家供大家参考,具体如下:

前面写过一篇文章,ngixn,uwsgi,django,python 环境配置,有兴趣的朋友可以参考 nginx,django部署

后来有人在QQ上问我,用Apache 好部署吗?我当时只在windows下用 APACHE2.3,django1.4,wsgi 部署过,还没有在 linux 下部署。前几天有时间,我在 centos 上测试部署了一下。也不难。唯一的差别是,在windows 下有已经编译好的 wsgi.so 文件,放到  apache 的 modules下,然后在 httpd.conf 中增加

LoadModule wsgi_module modules/mod_wsgi.so

但是在 linux 下,wsgi的安装,都要在源码编译下安装,而且还有几点要注意的。下面就详细介绍我部署的过程。

安装python 2.7 或者你需要的版本

这个就简单带过了,下载安装包之后,windows 可以直接 运行安装包,linux 下 最好编译安装吧。这一步可以参考我上面提到的文章,我已经说得比较清楚。 但我这个centos 环境,是别人已经装好了的,而且比较怪异,安装在:/usr/local/activepython27 ,一般的python 安装在:/usr/bin/python 。其实原因简单,因为centos 自带的python 是 2.4 的版本较低,所以重新装了一个新版本的。

安装wsgi

首先要在google 代码托管处下载.https://code.google.com/p/modwsgi ,如果是windows 的,可以直接下载编译好的文件。linux 的兄弟们,下载源码编译:

wget http://modwsgi.googlecode.com/files/mod_wsgi-3.4.tar.gz
tar zxvf mod_wsgi-3.4.tar.gz
cd mod_wsgi-3.4
./configure

发现什么了,报错了

./configure
checking for apxs2... no
checking for apxs... no
checking Apache version... ./configure: line 1695: apxs: command not found
./configure: line 1695: apxs: command not found
./configure: line 1696: /: is a directory

报错的原因,也很清楚。没有 apxs.下面安装它

yum install httpd-devel

如果是 ubuntu  可能命令为 sudo apt-get install apache2-devsudo apt-get install apache2-threaded-dev,具体的命令可以查一下.

再次编译

[root@29 mod_wsgi-3.4]# ./configure (这里有可能要加上python路径 --with-python=/usr/local/activepython27)
checking for apxs2... no
checking for apxs... /usr/sbin/apxs
checking Apache version... 2.2.3
checking for python... /usr/local/activepython27/bin/python
configure: creating ./config.status
config.status: creating Makefile
[root@29 mod_wsgi-3.4]#
make
make install

得到如下编译结果:

See any operating system documentation about shared libraries for
more information, such as the ld(1) and ld.so(8) manual pages.
----------------------------------------------------------------------
chmod 755 /usr/lib/httpd/modules/mod_wsgi.so
[root@29 mod_wsgi-3.4]#

配置 apache2 配置文件httpd.conf .

LoadModule wsgi_module modules/mod_wsgi.so

然后启动 apache

service httpd start

发现什么鸟,这是只布谷鸟,乱叫,报错了

Could not find platform independent libraries <prefix>
Could not find platform dependent libraries <exec_prefix>
Consider setting $PYTHONHOME to <prefix>[:<exec_prefix>]
ImportError: No module named site

错误的原因,系统有多个python 版本,必须指明用哪一个python .
在apache 配置文件中,继续加入

WSGIPythonHome /usr/local/activepython27  (这是你要采用的python的路径,一般是/usr/bin/python,我这环境别人配成这样了)

用什么方法可以得到这个路径呢,直接执行python 命令就可以得到,前提是,这个python 软连接是你用要的python, 注意一点的是,WSGIPythonHome不要配置到<VirtualHost> </VirtualHost> 之间,否则报错

import sys
sys.prefix 

就可以得到路径/usr/local/activepython27 。

Django应用程序相关配置

1.我的django程序部署在 /opt/www/html/djangocms/crm 这里

 Apache,wsgi,django 程序部署配置方法详解

在工程的conf 目录下加入如下两个文件

apache_django_wsgi.conf 文件内容如下

<VirtualHost *:80>
  ServerName 192.168.1.111
  ServerAlias 192.168.1.111
  DocumentRoot /opt/www/html/djangocms/crm
  WSGIScriptAlias / /opt/www/html/djangocms/crm/conf/django.wsgi
  <Directory "/opt/www/html/djangocms/crm">
   order allow,deny
   Allow from all
  </Directory>
  Alias /static /opt/www/html/djangocms/crm/static
  <Location "/static">
      SetHandler None
  </Location>
  <Directory "/opt/www/html/djangocms/crm/static">
    order Deny,Allow
    Allow from all
  </Directory>
</VirtualHost>

django.wsgi 文件内容

import os
import sys
sys.path.append("/opt/www/html/djangocms")
sys.path.append("/opt/www/html/djangocms/crm")
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "crm.settings")
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()

最后在 apache 配置文件 httpd.conf 中加入:

Include "/opt/www/html/djangocms/crm/conf/apache_django_wsgi.conf"

重启apache

service httpd restart

你就看到你熟悉的django应用程序了。

希望本文所述对大家基于Django框架的Python程序设计有所帮助。

推荐阅读:
  1. mac配置 Django 1.7.1 +mod_wsgi 4
  2. Django用Apache和mod_wsgi部署

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

apache wsgi django

上一篇:云服务器的功能有哪些

下一篇:重庆云服务器哪个品牌好

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》