部署CherryPy(守护进程)

26 投票
4 回答
13697 浏览
提问于 2025-04-15 14:32

我跟着基本的CherryPy教程学了一下(http://www.cherrypy.org/wiki/CherryPyTutorial)。不过有一点没有讨论,就是如何部署。

我想知道怎么把一个CherryPy应用程序当成后台服务启动,这样我就可以“忘记它”了。如果服务器重启了,那会发生什么呢?

有没有什么标准的方法?比如说,能不能创建一个服务脚本(/etc/init.d/cherrypy...)?

谢谢!

4 个回答

5

我写了一个教程和项目框架,叫做 cherrypy-webapp-skeleton,目的是帮助网页开发者在Debian系统上部署一个真实的CherryPy应用。这个框架包含了扩展版的 cherryd,可以让程序以较低的权限运行。此外,还有一些重要的脚本和配置文件,涉及到 init.dnginxmonitlogrotate。教程部分讲解了如何把这些东西组合在一起,最后你就可以“忘记”它们了。框架部分则提供了一种可能的CherryPy网页应用项目资源的组织方式。


* 这个框架是为Squeeze版本写的,但实际上对于Wheezy版本也应该差不多。

19

使用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()

这里有一个关于SysV风格的不错的使用指南。

简单来说就是:

  1. /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."  
    
  2. 让这个文件可以执行

    sudo chmod +x /etc/init.d/MyDaemonApp

  3. 运行 update-rc.d 来在合适的运行目录中创建正确的链接。

    sudo update-rc.d MyDaemonApp defaults 80

  4. 最后,运行这个文件

    sudo /etc/init.d/MyDaemonApp

14

CherryPy 默认包含一个叫做 Daemonizer 的插件,这个插件很有用,可以帮助你启动 CherryPy。不过,对于简单的情况,最简单的方法是使用 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 脚本,我觉得网上有很多可以找到的例子。

cherryd 这个文件可以在你的:

virtualenv/lib/python2.7/site-packages/cherrypy/cherryd

或者在这个链接找到: https://bitbucket.org/cherrypy/cherrypy/src/default/cherrypy/cherryd

撰写回答