从virtualenv中启动wsgi应用程序作为Linux系统服务[英] Start a wsgi app from within virtualenv as a Linux system service

本文是小编为大家收集整理的关于从virtualenv中启动wsgi应用程序作为Linux系统服务的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我目前正在Virtualenv中开发一个瓶应用程序.我打算使用Bjoern WSGI服务器提供它(但这可能并不重要).我还打算用浅色或NGINX反向代理服务该应用程序.无论如何,该应用程序可以作为系统服务从其自身的Virtualenv中运行吗?如果是这样,一个人将如何处理?

推荐答案

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

运行依赖Virtualenv作为守护程序服务的Python服务器,这是我在生产环境中使用的配置.

[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:

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

当然,如果您的端口号没有低于1024打开的端口号,则可以在非特权帐户中编写此启动话品.

本文地址:https://www.itbaoku.cn/post/1515435.html

问题描述

I'm currently developing a bottle app within virtualenv. I intend to serve it using bjoern WSGI server (but that probably doesn't matter too much). I also intend to serve the app with a lighty or nginx reverse proxy. Anyhow, can the app be run from within its own virtualenv as a system service? And if so, how would one go about it?

推荐答案

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?

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 ;

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 

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.