我遵循了基本的 CherryPy 教程(http://www.cherrypy.org/wiki/CherryPyTutorial)。没有讨论的一件事是部署。
如何将 CherryPy 应用程序作为守护程序启动并“忘记它”?如果服务器重新启动会发生什么?
有标准食谱吗?也许会创建一个服务脚本(/etc/init.d/cherrypy ...)
谢谢!
我遵循了基本的 CherryPy 教程(http://www.cherrypy.org/wiki/CherryPyTutorial)。没有讨论的一件事是部署。
如何将 CherryPy 应用程序作为守护程序启动并“忘记它”?如果服务器重新启动会发生什么?
有标准食谱吗?也许会创建一个服务脚本(/etc/init.d/cherrypy ...)
谢谢!
Daemonizer 使用起来非常简单:
# this works for cherrypy 3.1.2 on Ubuntu 10.04
from cherrypy.process.plugins import Daemonizer
# before mounting anything
Daemonizer(cherrypy.engine).subscribe()
cherrypy.tree.mount(MyDaemonApp, "/")
cherrypy.engine.start()
cherrypy.engine.block()
总结一下:
/etc/init.d
在该调用中创建一个为您的应用程序命名的文件/bin/sh
sudo vim /etc/init.d/MyDaemonApp
#!/bin/sh
echo "Invoking MyDaemonApp";
/path/to/MyDaemonApp
echo "Started MyDaemonApp. Tremble, Ye Mighty."
使其可执行
sudo chmod +x /etc/init.d/MyDaemonApp
运行update-rc.d
以在正确的运行时目录中创建正确的链接。
sudo update-rc.d MyDaemonApp defaults 80
sudo /etc/init.d/MyDaemonApp
CherryPy 默认包含一个Daemonizer插件,这对于启动它很有用,但到目前为止,对于简单的情况,最简单的方法是使用cherryd 脚本:
> cherryd -h
Usage: cherryd [options]
Options:
-h, --help show this help message and exit
-c CONFIG, --config=CONFIG
specify config file(s)
-d run the server as a daemon
-e ENVIRONMENT, --environment=ENVIRONMENT
apply the given config environment
-f start a fastcgi server instead of the default HTTP
server
-s start a scgi server instead of the default HTTP server
-i IMPORTS, --import=IMPORTS
specify modules to import
-p PIDFILE, --pidfile=PIDFILE
store the process id in the given file
就 init.d 脚本而言,我认为有些示例可以通过 Google 搜索。
并且cherryd
在您的中找到:
virtualenv/lib/python2.7/site-packages/cherrypy/cherryd
或在:https ://bitbucket.org/cherrypy/cherrypy/src/default/cherrypy/cherryd
我编写了一个教程/项目框架,cherrypy-webapp-skeleton,其目标是填补在 Debian* 上为 Web 开发人员部署真实 CherryPy 应用程序的空白。它cherryd
为守护程序特权下降提供了扩展。还有一些重要的脚本和配置文件用于init.d
, nginx
, monit
, logrotate
. 教程部分描述了如何把东西放在一起并最终忘记它。骨架部分提出了一种 CherryPy webapp 项目资产的可能排列方式。
* 它是为 Squeeze 编写的,但实际上它应该与 Wheezy 相同。
有关守护程序选项的信息
使用 Daemonizer 时,文档没有说明选项,例如如何重定向stdout或stderr。您可以从Daemonizer类的源代码中找到这些选项。作为参考,请从我的项目中获取此示例:
# run server as a daemon
d = Daemonizer(cherrypy.engine,
stdout='/home/pi/Gate/log/gate_access.log',
stderr='/home/pi/Gate/log/gate_error.log')
d.subscribe()