本教程适用于以下操作系统版本

  • Ubuntu 22.04(Jammy Jellyfish)
  • Ubuntu 20.04(Focal Fossa)

本页内容

  • 先决条件
  • 开始之前
  • 安装 Python 编程语言
  • 安装 Apache 网页服务器与 mod_wsgi 模块
  • Apache 中的 WSGI 模块配置
  • 测试 Python 3 WSGI 脚本
  • 下载为虚拟机 / 虚拟机的登录信息
  • 虚拟机的登录信息

Apache 的 mod_wsgi 模块为托管基于 Python 的 Web 应用程序提供了一个接口。由于它提供了高性能的解决方案,因此是专业托管系统的理想选择,同时也同样适用于小型站点。使用 mod_wsgi 可以轻松部署使用 Django、Web.py、Werkzug、Chery.py、TurboGears 和 Flask 等 Python 框架编写的应用程序。

在本教程中,我将向您展示如何在 Ubuntu 22.04 LTS 上使用 Apache 网页服务器安装并配置 mod_wsgi。本指南是 Hitesh Jethva 编写的 Ubuntu 20.04 Apache WSGI 指南的更新版本。

先决条件

  • Ubuntu 22.04 服务器版或桌面版
  • 您必须是系统管理员,并拥有 root 或 sudo 权限才能安装和配置此软件。

开始之前

在开始之前,最好将系统更新到最新版本。您可以使用以下命令更新系统:

sudo apt update -y
sudo apt upgrade -y

更新系统后,请重启系统以使更改生效。

安装 Python 编程语言

默认情况下,Python 已预装在 Ubuntu 22.04 中。如果尚未安装 Python,您可以使用以下命令进行安装:

sudo apt install python3 libexpat1 -y

下面的截图显示了 Python 已安装时该命令的输出。

在 Ubuntu 系统上从零完整配置 Apache 与 mod_wsgi 运行 Python 程序

现在继续进行下一步。

安装 Apache 网页服务器与 mod_wsgi 模块

在这一步中,我们将安装 Apache 网页服务器和 mod_wsgi Python 模块。执行以下命令以安装这两个应用程序。相关的软件依赖包会自动安装。

sudo apt install apache2 apache2-utils ssl-cert libapache2-mod-wsgi-py3 -y

在 Ubuntu 系统上从零完整配置 Apache 与 mod_wsgi 运行 Python 程序

所有软件包安装完成后,您可以进行下一步。

Apache 中的 WSGI 模块配置

接下来,您需要在 Apache 默认 Web 根目录中创建一个新的 Python 脚本,以便通过 WSGI Apache 模块提供服务。这里我们将使用一个小型的“Hello World”脚本进行演示。

您可以使用以下命令创建该 Python 脚本:

sudo nano /var/www/html/wsgitest.py

添加以下几行内容:

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

保存并关闭文件。然后,使用以下命令将该文件的所有权更改为 www-data:

sudo chown www-data:www-data /var/www/html/wsgitest.py
sudo chmod 775 /var/www/html/wsgitest.py

接下来,编辑 Apache 虚拟主机配置文件,以便通过 HTTP 协议提供该文件。

sudo nano /etc/apache2/sites-enabled/000-default.conf

添加以下行:

WSGIScriptAlias /wsgi /var/www/html/wsgitest.py

添加到该行之前

</VirtualHost>

完整的虚拟主机文件如下所示:

        # The ServerName directive sets the request scheme, hostname and port that
        # the server uses to identify itself. This is used when creating
        # redirection URLs. In the context of virtual hosts, the ServerName
        # specifies what hostname must appear in the request's Host: header to
        # match this virtual host. For the default virtual host (this file) this
        # value is not decisive as it is used as a last resort host regardless.
        # However, you must set it for any further virtual host explicitly.
        #ServerName www.example.com

        ServerAdmin webmaster@localhost
        DocumentRoot /var/www/html

        # Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
        # error, crit, alert, emerg.
        # It is also possible to configure the loglevel for particular
        # modules, e.g.
        #LogLevel info ssl:warn

        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined

        # For most configuration files from conf-available/, which are
        # enabled or disabled at a global level, it is possible to
        # include a line for only one particular virtual host. For example the
        # following line enables the CGI configuration for this host only
        # after it has been globally disabled with "a2disconf".
        #Include conf-available/serve-cgi-bin.conf

        WSGIScriptAlias /wsgi /var/www/html/wsgitest.py




# vim: syntax=apache ts=4 sw=4 sts=4 sr noet

在文本编辑器中保存更改并关闭编辑器。下一步是重启 Apache 网页服务器以使更改生效。

systemctl restart apache2

测试 Python 3 WSGI 脚本

现在 Apache HTTP 服务器已配置为通过 HTTP 协议提供我们的 Python 文件。

打开网页浏览器并输入 URL http://your-server-ip/wsgi ,将 your-server-ip 替换为服务器的 IP 地址。您将看到一个白色页面,上面显示“Hello Howtoforge!”字样。

至此,您已经拥有了一个配置正确的 Apache 网页服务器,可以使用 mod_wsgi 运行 Python 应用程序了。下一步是将您的 Python Web 应用程序部署到 /var/www/html/ 目录中,并修改 /etc/apache2/sites-enabled/000-default.conf 文件中的 WSGIScriptAlias 行,使其与您应用程序的启动脚本相匹配。然后再次重启 Apache 以使配置更改生效。

下载为虚拟机

此配置可作为虚拟机下载,格式为 ova/ovf(兼容 VMWare 和 Virtualbox),供 howtoforge 订阅用户使用。

虚拟机的登录信息

  • 用户“admininstrator”的密码为:“howtoforge”。该管理员用户拥有 sudo 权限。

请在首次登录时更改所有密码。

  • 虚拟机的 IP 地址为 192.168.0.100