Pyramid是跟Django和Flask一样非常优秀的Web框架,gunicorn是专业的高性能Web服务器,两者的结合可以说是天作之合!
在FreeBSD下实践pyrmaid下使用gunicorn
首先pkg安装需要的软件包
pkg install git pyhton311 bash
在普通用户下创建python3.11的环境
登录普通账户,然后执行下面语句:
skywalk@fb5:~ $ bash
[skywalk@fb5 ~]$ python3.11 -m venv py311
[skywalk@fb5 ~]$ source py311/bin/activate
(py311) [skywalk@fb5 ~]$ pip config set --user global.index-url https://pypi.tuna.tsinghua.edu.cn/simple/
Writing to /home/skywalk/.config/pip/pip.conf
(py311) [skywalk@fb5 ~]$ pip install pip -U
安装cookiecutter
pip install cookiecutter
执行cookiecutter创建pyramid测试网站
cookiecutter gh:Pylons/pyramid-cookiecutter-starter --checkout 2.0-branch
按照提示回答问题:
cookiecutter gh:Pylons/pyramid-cookiecutter-starter --checkout 2.0-branch
[1/4] project_name (Pyramid Scaffold): pyramiddemo
[2/4] repo_name (pyramiddemo):
[3/4] Select template_language
1 - jinja2
2 - chameleon
3 - mako
Choose from [1/2/3] (1): 2
[4/4] Select backend
1 - none
2 - sqlalchemy
3 - zodb
Choose from [1/2/3] (1): 1
===============================================================================
Documentation: https://docs.pylonsproject.org/projects/pyramid/en/latest/
Tutorials: https://docs.pylonsproject.org/projects/pyramid_tutorials/en/latest/
Twitter: https://twitter.com/PylonsProject
Mailing List: https://groups.google.com/forum/#!forum/pylons-discuss
Welcome to Pyramid. Sorry for the convenience.
===============================================================================
Change directory into your newly created project.
cd pyramiddemo
Create a Python virtual environment.
python3 -m venv env
Upgrade packaging tools.
env/bin/pip install --upgrade pip setuptools
Install the project in editable mode with its testing requirements.
env/bin/pip install -e ".[testing]"
Run your project's tests.
env/bin/pytest
Run your project.
env/bin/pserve development.ini
安装测试网站环境
首先进入测试网站目录
cd pyramiddemo
然后安装测试网站并执行:
python3 -m venv env
env/bin/pip install --upgrade pip setuptools
env/bin/pip install -e ".[testing]"
env/bin/pytest
env/bin/pserve development.ini
看到这样的提示就证明pyramid web服务起来了。
Starting server in PID 66811.
2024-08-01 21:41:44,909 INFO [waitress:485][MainThread] Serving on http://[::1]:6543
2024-08-01 21:41:44,910 INFO [waitress:485][MainThread] Serving on http://127.0.0.1:6543
使用gunicorn启动pyramid网站
安装gunicorn包:
env/bin/pip install gunicorn
启动gunicorn服务器
env/bin/gunicorn --paste production.ini
显示:
env/bin/gunicorn --paste production.ini
[2024-08-01 21:43:50 +0800] [66828] [INFO] Starting gunicorn 22.0.0
[2024-08-01 21:43:50 +0800] [66828] [INFO] Listening at: http://127.0.0.1:8000 (66828)
[2024-08-01 21:43:50 +0800] [66828] [INFO] Using worker: sync
[2024-08-01 21:43:50 +0800] [66829] [INFO] Booting worker with pid: 66829
这样就证明gunicorn启动成功了!
如果需要远程能够访问,可以将web服务绑定到0.0.0.0:
env/bin/gunicorn --paste production.ini -b 0.0.0.0
[2024-08-01 21:46:57 +0800] [66875] [INFO] Starting gunicorn 22.0.0
[2024-08-01 21:46:57 +0800] [66875] [INFO] Listening at: http://0.0.0.0:8000 (66875)
[2024-08-01 21:46:57 +0800] [66875] [INFO] Using worker: sync
[2024-08-01 21:46:57 +0800] [66876] [INFO] Booting worker with pid: 66876