且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

从virtualenv作为Linux系统服务启动wsgi应用程序

更新时间:2023-10-20 18:18:10

根据我的经验,我建议您可以使用 Supervisord 将Web服务器作为守护程序运行.尽管您可以在/etc/init.d中编写一些Linux服务脚本,但是它们确实很难正确执行.这是 nginx的示例init.d脚本,以在Ubuntu中将其作为服务运行.你不想写一个,是吗?

According to my experience, I suggest that you can use Supervisord to run your web server as daemon service. Although you can write some Linux service scripts in /etc/init.d, but they are really difficult to do it correctly. Here is an example init.d script for nginx to run it as a service in Ubuntu. You don't want to write one, do you?

要运行具有virtualenv作为守护程序的守护程序服务的python服务器,这是我在生产环境中使用的配置.

To run a python server which depends on virtualenv as daemon service with supervisord, here is the configuration I am using in a production environment.

[program:web01]
command=/home/victorlin/tg2env/bin/paster serve production.ini ;
process_name=%(program_name)s ;
directory=/home/victorlin/ ;
user=victorlin ;
priority=999 ;
redirect_stderr=true ;
stdout_logfile=/home/victorlin/logs/web01_out.txt ;
stderr_logfile=/home/victorlin/logs/web01_err.txt ;
environment=PYTHON_EGG_CACHE=/home/victorlin/.python-eggs ;

您可以使用/path/to/virtualenv/bin/python在命令字段中运行自己的python脚本.并且,要在启动时运行受监管的主机,可以在根帐户中编写如下的crontab:

You can use /path/to/virtualenv/bin/python to run your own python script in the command field. And, to run the supervisord on start-up, you can write crontab like this in your root account:

@reboot /usr/local/bin/supervisord -c /home/root/supervisord.conf 

当然,如果您要打开的端口号小于1024,则可以使用非特权帐户编写此启动crontab.

Of course, if you don't have port numbers lower than 1024 to open, you can write this start-up crontab in a non-privilege account.