要使用 Gunicorn 部署 Python 应用程序,您需要首先安装 Gunicorn。在命令行中输入以下命令以安装 Gunicorn:
pip install gunicorn
安装完成后,您可以使用以下步骤设置 Gunicorn:
打开终端或命令提示符。
导航到您的 Python 应用程序的根目录。例如,如果您的应用程序位于 /home/user/myapp,则输入 cd /home/user/myapp。
使用以下命令启动 Gunicorn。将 <your_module> 替换为您的应用程序的主模块(通常包含 wsgi.py 文件):
gunicorn <your_module>:application
例如,如果您的主模块是 wsgi.py,则命令如下:
gunicorn wsgi:application
-b 或 --bind 选项指定端口号。例如,要将端口更改为 8080,请输入:gunicorn -b 0.0.0.0:8080 <your_module>:application
-w 或 --workers 选项指定工作进程的数量。例如,要使用 4 个工作进程,请输入:gunicorn -w 4 <your_module>:application
-k 或 --threads 选项)或启用访问日志(使用 -l 或 --log-level 选项)。有关详细信息和其他可用选项,请参阅 Gunicorn 文档。注意:在生产环境中部署 Gunicorn 时,建议使用反向代理服务器(如 Nginx 或 Apache)与 Gunicorn 配合使用,以便更好地处理 SSL/TLS、负载均衡和其他高级功能。