Yi fan

在WSL安装PPress CMS 开源python + flask博客系统,使用gunicorn作为

Gunicorn(Green Unicorn)是一个基于 Python 的 WSGI(Web Server Gateway Interface)HTTP 服务器,常用于部署 Python Web 应用程序,如 Flask、Django 等。以下是使用Gunicorn部署网站的详细步骤:

1. 准备工作

首先,确保你已经安装了 Python和Git在WSL上

下载PPress代码(参考PPress在github上的代码说明)

git clone https://gitee.com/fojie/PPress.git
cd ppress

注意:官方的安装说明有些缺陷,requirements.txt文件中少了一个依赖包cryptography,请将这个依赖包放在requirements.txt的最后一行

安装依赖包

第一步,创建并激活虚拟环境

python3 -m venv prod_venv

source /home/hans/PPress/prod_venv/bin/activate

确认当前python已经是虚拟环境的python

which python3 

第二步,安装依赖

pip intsall requirements.txt

第三步,初始化PPress

执行 Python3 run.py 语句后,web服务启动,在浏览器输入下面网址:

http://127.0.0.1:5000/

选择SqlLite数据即可,点击安装完成初始化

2 安装Gunicorn

第一步,安装gunicorn

使用以下命令安装Gunicorn

  pip install gunicorn
安装完后检查所有上述步骤中安装过的依赖包列表,查看是否完整:
  pip list

第二步,安装gunicorn服务

为了确保应用在服务器重启后自动启动,并且可以方便地管理应用的启动、停止和重启,可以将Gunicorn配置为系统服务。以下是配置步骤:

创建服务文件

创建一个名为 gunicorn.service 的文件,通常位于 /etc/systemd/system/ 目录下:
 sudo vi /etc/systemd/system/ppress_gunicorn.service

编辑服务文件内容

[Unit] Description=Gunicorn instance to serve myproject

After=network.target

[Service]

User=your_username

Group=your_groupname

WorkingDirectory=/path/to/your/app

Environment="PATH=/path/to/your/venv/bin"

ExecStart=/path/to/your/venv/bin/gunicorn -w 4 -b 0.0.0.0:8000 app:app

[Install] WantedBy=multi-user.target
  • User 和 Group:指定运行Gunicorn的用户和用户组。(User: hans, Group: hans)

  • WorkingDirectory:指定应用所在的目录。(/home/hans/PPress)

  • Environment:指定 Python 虚拟环境的路径。(/home/hans/PPress/prod_venv/bin)

  • ExecStart:指定启动Gunicorn的命令。

    /home/hans/PPress/prod_venv/bin/gunicorn,最后两个app,第一个app指的是你项目根目录下的run.py

    如果端口是低位端口则需要root用户权限

实际的配置如下:

[Unit]
Description=Gunicorn instance to serve my PPress website
After=network.target

[Service]
User=hans
Group=hans
WorkingDirectory=/home/hans/PPress
Environment="PATH=/home/hans/PPress/prod_venv/bin"
ExecStart=/home/hans/PPress/prod_venv/bin/gunicorn -w 2 -b 0.0.0.0:8000 run:app

[Install]
WantedBy=multi-user.target

 

重新加载系统服务配置

sudo systemctl daemon-reload 

启动并设置开机自启

sudo systemctl start ppress_gunicorn
sudo systemctl enable ppress_gunicorn  
 

管理服务

  • 停止服务:sudo systemctl stop ppress_gunicorn
  • 重启服务:sudo systemctl restart ppress_gunicorn
  • 查看服务状态:sudo systemctl status ppress_gunicorn

posted on 2025-03-06 12:24  hanswei  阅读(45)  评论(0)    收藏  举报

导航