注:本教程在Vultr VPS上测试通过,如需部署请前往Vultr.com
先决条件
运行最新的Arch Linux的Vultr服务器(请参阅本文。)
正在运行的Web服务器,Apache或Nginx
须藤访问:
需要以root身份运行的命令带有前缀 #
。建议以root用户身份运行命令的方式是,以普通用户的身份为每个命令添加前缀 sudo
安装并熟悉文本编辑器,例如vi,vim,nano,emacs或类似的编辑器
在Web服务器上安装Python 3.7
在Apache上
不幸的是,不支持在同一个Arch系统上同时运行两个版本的Apache模块(适用于Python 2.x和3.x),但这很少有问题。
要使用Python 3.x:
# pacman -S mod_wsgi
启用Apache mod_wsgi
通过编辑模块 /etc/httpd/conf/httpd.conf
,并在列表的末尾 LoadModule
命令,添加以下内容:
LoadModule wsgi_module modules/mod_wsgi.so
在Nginx上
要使用Python 3.x:
# pacman -S uwsgi-plugin-python
测试Python
在适当的目录中,创建 test.py
具有以下内容:
#-*- coding: utf-8 -*-
def wsgi_app(environment, start_response):
import sys
output = sys.version.encode('utf8')
status = '200 OK'
headers = [('Content-type', 'text/plain'),
('Content-Length', str(len(output)))]
start_response(status, headers)
yield output
application = wsgi_app
在Apache上
添加到 /etc/httpd/conf/httpd.conf
,或者如果您正在运行多个主机,请编辑适当的配置文件,然后添加适当的
块:
WSGIScriptAlias /wsgi_app /srv/http/test.py
重新启动Apache:
# systemctl restart httpd
在网络浏览器中,访问 https://YOUR-SERVER-WEB-ADDRESS-OR-IP/wsgi_app
,您将看到一个包含python和GCC版本的测试页。
删除 test.py
您刚创建的测试文件,以及 WSGIScriptAlias
在您的Apache配置中。
重新启动Apache:
# systemctl restart httpd
在Nginx上
创建文件 /etc/uwsgi/wsgi_app.ini
具有以下内容:
[uwsgi]
socket = /run/uwsgi/wsgi_app.sock
uid = http
gid = http
plugins = python
chdir = /usr/share/nginx/html/
wsgi-file=test.py
callable = application
启动uWSGI服务 wsqi_app
:
# systemctl start uwsgi@wsgi_app
通过编辑允许Nginx使用uWSGI /etc/nginx/nginx.conf
,对于要测试的每个服务器块,添加以下内容。或者,如果您使用的是虚拟主机,请编辑每个主机的配置文件:
location ~ wsgi_app {
root /usr/share/nginx/html/;
include uwsgi_params;
uwsgi_pass unix:/run/uwsgi/wsgi_app.sock;
}
重新启动Nginx:
# systemctl restart nginx
在网络浏览器中,访问 https://YOUR-SERVER-WEB-ADDRESS-OR-IP/wsgi_app
,您将看到一个包含python和GCC版本的测试页。
删除 test.py
您刚刚创建的文件,以及您刚刚添加到的位置块 /etc/nginx/nginx.conf
对于 wsgi_app
。
重新启动Nginx:
# systemctl restart nginx
停止提供uWSGI wsgi_app
:
# systemctl stop uwsgi@wsgi_app
删除 /etc/uwsgi/wsgi_app.ini
和 test.py
测试您刚创建的文件。
注:本教程在Vultr VPS上测试通过,如需部署请前往Vultr.com