0

使用 Rails 3.2,Capistrano 2,我有以下 nginx 配方:

namespace :nginx do
  desc "Install latest stable release of nginx"
  task :install, roles: :web do
    run "#{sudo} add-apt-repository -y ppa:nginx/stable"
    run "#{sudo} apt-get -y update"
    run "#{sudo} apt-get -y install nginx"
    run "#{sudo} /etc/init.d/apache2 stop" # Stop Apache because we are using nginx, only for production
    start
  end
  after "deploy:install", "nginx:install"
end

这是从头开始设置我的 VPS 的一部分,你注意到我在启动 nginx 之前停止了 Apache,这样就不会出现端口冲突。但这仅在安装了 Apache 时才有效,如果有其他操作系统没有预装 Apache,那么这个部署配方会抛出错误,试图停止不存在的 Apache。

我该如何改进这个脚本?谢谢。

4

2 回答 2

0

如果您只想在找不到 apache 的情况下忽略错误,可以尝试将命令替换为

run "#{sudo} /etc/init.d/apache2 stop || true"
于 2013-10-19T16:12:40.280 回答
0

您可以将命令更改为

run "#{sudo} pkill -9 apache"

如果您使用任何监控工具,例如monitor god,那么他们可能会尝试再次启动 apache。在这种情况下,使用

run "#{sudo} netstat -tulpn | grep ':80 ' && /etc/init.d/apache2 stop"

检查是否有任何进程(不仅仅是 apache)正在侦听端口 80,如果是,则发出 stop apache

于 2013-10-19T16:07:14.843 回答