阅读量:1
以下是在Debian上配置Python服务器的几种常见方法:
简易HTTP服务器(基于Python内置模块)
- 安装Python(若未安装):
sudo apt update,sudo apt install python3。 - 创建Python文件,如
simple_server.py,内容为:
import http.server
import socketserver
PORT = 8000
Handler = http.server.SimpleHTTPRequestHandler
with socketserver.TCPServer(("", PORT), Handler) as httpd:
print(f"Serving at port {PORT}")
httpd.serve_forever()
- 运行服务器:
python3 simple_server.py,可通过http://localhost:8000访问。
使用Gunicorn和Nginx的Web服务器
- 安装必要软件:
sudo apt update,sudo apt install python3-pip python3-venv nginx。 - 创建虚拟环境并激活:
python3 -m venv venv,source venv/bin/activate。 - 安装项目依赖(若有
requirements.txt):pip install -r requirements.txt。 - 安装Gunicorn:
pip install gunicorn。 - 用Gunicorn运行应用,如
gunicorn -w 4 -b 127.0.0.1:8000 app:app(app为Flask等框架的应用对象)。 - 配置Nginx反向代理,编辑
/etc/nginx/sites-available/your_site:
server {
listen 80;
server_name your_domain_or_ip;
location / {
proxy_pass http://127.0.0.1:8000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
- 启用Nginx配置并重启:
sudo ln -s /etc/nginx/sites-available/your_site /etc/nginx/sites-enabled,sudo nginx -t,sudo systemctl restart nginx。 - (可选)将Gunicorn设为系统服务以便后台运行。
以上就是关于“如何在Debian上配置Python服务器”的相关介绍,筋斗云是国内较早的云主机应用的服务商,拥有10余年行业经验,提供丰富的云服务器、租用服务器等相关产品服务。云服务器资源弹性伸缩,主机vCPU、内存性能强悍、超高I/O速度、故障秒级恢复;电子化备案,提交快速,专业团队7×24小时服务支持!
简单好用、高性价比云服务器租用链接:https://www.jindouyun.cn/product/cvm