在ubuntu服务器启动时启动unicorn app服务器

时间:2013-04-03 04:23:02

标签: ruby-on-rails ruby ruby-on-rails-3 unicorn

我使用ruby企业版运行我的rails应用程序,将unicorn作为app server。我运行这个命令

bundle exec unicorn -D -c /home/ubuntu/apps/st/config/unicorn.rb

我需要在系统重启或启动后立即运行此命令。我在ubuntu 10.04 LTS EC2实例上运行应用程序。我尝试了几个提到on this site以及as this site的例子,但它并不适用于我。任何抬头

4 个答案:

答案 0 :(得分:16)

尝试将其作为暴发户。为此,您需要使用以下内容在 / etc / init / 目录中创建 myapp.conf 文件:

description "myapp server"

start on runlevel [23]
stop on shutdown
exec sudo -u myuser sh -c "cd /path/to/my/app && bundle exec unicorn -D -c /home/ubuntu/apps/st/config/unicorn.rb"

respawn

之后,您应该可以使用以下命令启动/停止/重启您的应用程序:

start myapp
stop myapp
restart myapp

使用 ps -aux | grep myapp 检查您的应用是否正在运行。

答案 1 :(得分:13)

您可以使用this file作为模板,设置此文件中提到的相应路径,将其设置为/etc/init.d/my_unicorn_server可执行文件和符号链接。现在您可以使用以下命令启动服务器:

sudo service my_unicorn_server start

然后你可以这样做:

sudo update-rc.d my_unicorn_server defaults

在系统重启时自动启动unicorn服务器。

答案 2 :(得分:3)

在我的情况下,我只是想快点,所以我将启动命令放在/etc/rc.local中,如下所示。请注意,我使用的是RVM

# By default this script does nothing.
cd <your project dir>
/usr/local/rvm/gems/ruby-2.2.1/wrappers/bundle exec unicorn -c <your project dir>/config/unicorn.conf -D
test -e /etc/ssh/ssh_host_dsa_key || dpkg-reconfigure openssh-server
exit 0

确保您的启动命令高于退出0.重启后,直接点击应用程序的URL或使用ps -aux | grep unicorn命令检查它是否正在运行。

注意*以前我使用的是Phusion Passenger但是我很难看到它的错误日志,所以我回到了独角兽。我也尝试了@warantesbr没有成功,我想它失败了,因为我的整个环境都是使用root访问设置的。

答案 3 :(得分:2)

如果您使用的是unicorn_init脚本
您可以配置cron作业以在重新启动时启动unicorn服务器

crontab -e

并添加

@reboot /bin/bash -l -c 'service unicorn_<your service name> start >> /<path to log file>/cron.log 2>&1'