Apache配置WSGI

Apache配置WSGI

什么是WSGI

WSGI被称作web服务器网关接口,在笔者看来其实是基于CGI标准针对Python语言做了一些改进,其主要功能是规范了web 服务器与Pythonj应用程序之间的交互方式,为Python在web开发方面提供了便利而已。关于WSGI原生开发可以阅读参考部分的第一个链接。本文主要讲解如何配置WSGI,从而使得Apache服务器能够支持Python程序。

操作环境

操作系统:ubuntu 16
Apache服务器:Apache 2.4.18
Python:2.7.12

安装与加载

安装WSGI模块

# sudo apt install libapache2-mod-wsgi

启用WSGI模块

# sudo a2enmod wsgi

配置WSGI

编辑文件/etc/apache2/mods-enabled/wsgi.conf

<IfModule mod_wsgi.c>                                                                               	WSGIScriptAlias /test /var/www/html/test.wsgi          #添加该行
</IfModule>

编写测试脚本

/var/www/html目录下创建test.wsgi文件,在文件中添加以下代码

def application(environ, start_response):
    status = '200 OK'
    output = 'Hello World!'
     response_headers = [('Content-type', 'text/plain'),
                        ('Content-Length', str(len(output)))]
    start_response(status, response_headers)
    return [output]

访问

在浏览器中访问http://地址/test ,可以看到页面上显示“Hello World!”。

参考

WSGI原生开发

Apache配置WSGI

Django在Aapache上的部署

原文地址:https://www.cnblogs.com/xidongyu/p/9031290.html