本文概述了为 Python 3 Web 应用程序配置专用服务器的过程 Apache 2.4 使用 mod_wsgi。
什么是 mod_wsgi?
Mod_wsgi 是一个 Apache 允许 Python Web 应用程序在服务器上运行的模块。 该模块为 Flask、Django 和其他基于 Python 的框架提供了一个 Web 框架,以便在一组服务器上的集群服务器环境中运行。
先决条件
- 一台 CentOS VPS 或 Ubuntu 云专用服务器
- Apache 2.4、安装了apache-dev/httpd-devel和python3-dev
- 已安装 Python 3、pip3 和 virtualenv
- 访问终端
如果您没有安装上述先决条件,此构建将无法按预期工作。 以下命令可用于安装先决条件
apt install apache2 apache2-dev python3 python3-dev python3-pip
pip3 install virtualenv
第1步
首先下载 mod_wsgi.i 的源文件
root@host [~]# wget https://github.com/GrahamDumpleton/mod_wsgi/archive/4.6.5.tar.gz
接下来,你npack 存档
root@host [~]# tar -xzvf 4.6.5.tar.gz
Enter 源文件目录
root@host [~]# cd mod_wsgi-4.6.5
接下来,确定 Python 解释器的路径。
root@host [~]# which python3
/usr/bin/python3
root@host [~]#
配置安装并为 mod_wsgi 定义 Python 解释器。
root@host [~]# ./configure --with-python=/usr/bin/python3
checking for apxs2... no
checking for apxs... no
checking for gcc... no
checking for cc... no
checking for cl.exe... no
configure: error: in `/usr/src/mod_wsgi-4.6.5':
configure: error: no acceptable C compiler found in $PATH
See `config.log' for more details
现在,运行以下命令来编译包。
root@host [~]# make && make install
安装模块的路径将是t 最后一个命令的输出结尾.
root@host [~]# /usr/lib/apache2/modules/mod_wsgi.so
使用上述路径将模块加载到 Apache 通过将以下代码段添加到 apache.conf 或 httpd.conf 的底部
LoadModule wsgi_module /usr/lib/apache2/modules/mod_wsgi.so
现在测试语法并重新启动 Apache 云服务器上的服务。
基于 Debian/Ubuntu 的系统:
root@host [~]# apache2 -t
root@host [~]# service apache2 restart
基于 RHEL/CentOS 的系统:
root@host [~]# httpd -t
root@host [~]# service httpd restart
第2步
现在 mod_wsgi 已经安装好了,我们来看看配置。 我们将首先创建一个基本的 Python Web 应用程序来详细说明配置 mod_wsgi 的步骤。
首先,让我们使用配置 mod_wsgi 的 Python 版本创建一个虚拟环境。 对于这篇文章, example 应用程序将放置在 /var/www/ 中。 接下来,我们将运行此命令 创建目录 wsgi_test,它将包含虚拟环境。
root@host [~]# virtualenv --python=python3 wsgi_test
接下来,进入这个目录并激活virtualenv。
root@host [~]# cd wsgi_test
root@host [~]# source bin/activate
root@host [~]# pip install flask
root@host [~]# touch wsgi_test.py
root@host [~]# vim wsgi_test.py
将以下配置放在 wsgi_test.py 中 文件
from flask import Flask
app = Flask(__name__)
@app.route("/")
def index():
return "Hello, from WSGI"
现在,让我们创建 wsgi 文件来加载应用程序。
root@host [~]# touch wsgi.py
root@host [~]# vim wsgi.py
接下来,一个将以下信息添加到文件中。 当收到请求并执行应用程序时,此数据将激活虚拟环境。
activate_this = '/var/www/wsgi_test/bin/activate_this.py'
with open(activate_this) as file_:
exec(file_.read(), dict(__file__=activate_this))
import sys
sys.path.insert(0, '/var/www/wsgi_test/')
from wsgi_test import app as application
现在, example 应用程序已经创建,让我们配置 Apache.
我们将首先为此应用程序创建一个虚拟主机。 该文件将被放置在任一
/etc/httpd/conf.d/ 对于基于 RHEL 的系统,或
/etc/apache2/启用站点/基于 Debian。
红色的帽子
对于基于 RHEL 的系统,将文件写入以下路径,将 domain.com 替换为您的域。
/etc/httpd/conf.d/domain.com.conf
Debian
对于基于 Debian 的系统,将文件写入以下路径,将 domain.com 替换为您的域。
/etc/apache2/sites-enabled/domain.com.conf
该文件的内容如下,将 domain.com 替换为您的域。
<VirtualHost *:80>
ServerName domain.com
WSGIDaemonProcess wsgi_test user=www-data group=www-data threads=2
WSGIScriptAlias / /var/www/wsgi_test/wsgi.py
<Directory /var/www/wsgi_test>
Require all granted
</Directory>
</VirtualHost>
对于基于 RHEL 的系统,将用户和组替换为 apache,如下所示:
WSGIDaemonProcess wsgi_test user=apache group=apache threads=2
现在测试并重新启动 Apache.
基于 Debian 的系统:
root@host [~]# apache2 -t
root@host [~]# service apache2 restart
基于 RHEL 的系统:
root@host [~]# httpd -t
root@host [~]# Service httpd restart
您还可以使用 curl 通过 CLI 命令
root@host [~]# curl https://dtnqes.com/images/9/1652376283_lw-logo-stacked-preview.png"},{"@type":"Article","@id":"https://dtnqes.com/article/483565#article","url":"https://dtnqes.com/article/483565","headline":"在 Ubuntu/Centos 上配置 WSGI","datePublished":"2022-05-12T17:24:41+00:00","dateModified":"2022-05-12T17:24:44+00:00","wordCount":422,"keywords":["Apache","Centos","Debian","Install","Python","Ubuntu"],"articleSection":["article"],"isPartOf":{"@id":"https://dtnqes.com/article/483565#webpage"},"mainEntityOfPage":{"@id":"https://dtnqes.com/article/483565#webpage"},"image":{"@id":"https://dtnqes.com/article/483565#thumbnail"},"publisher":{"@id":"https://dtnqes.com/#organization"},"author":{"@id":"https://dtnqes.com/article/483565#author"}},{"@type":"Person","@id":"https://dtnqes.com/article/483565#author","url":"https://dtnqes.com/author/mzdya/","name":"mzdya","image":{"@id":"https://dtnqes.com/article/483565#author_image"}},{"@type":"ImageObject","@id":"https://dtnqes.com/article/483565#author_image","url":"https://secure.gravatar.com/avatar/aa1d5ea659c6e7cee3be89b7061bdff6?s=96&d=mm&r=g","width":96,"height":96,"caption":"mzdya"}]} Related Posts
如何在 Ubuntu 上创建自签名 SSL 证书