Restart nginx without sudo?

前端 未结 3 983
攒了一身酷
攒了一身酷 2021-01-30 05:42

So I want to be able to cap:deploy without having to type any passwords. I have setup all private keys so I can get to the remote servers fine, and am now using svn over ssh, so

3条回答
  •  清歌不尽
    2021-01-30 06:10

    Create a rake task in Rails_App/lib/capistrano/tasks/nginx.rake and paste below code.

    namespace :nginx do
      %w(start stop restart reload).each do |command|
        desc "#{command.capitalize} Nginx"
        task command do
          on roles(:app) do
            execute :sudo, "service nginx #{command}"
          end
        end
      end
    end
    

    Then ssh to your remote server and open file

      sudo vi /etc/sudoers
    

    and the paste this line (after line %sudo ALL=(ALL:ALL) ALL)

      deploy ALL=(ALL:ALL) NOPASSWD: /usr/sbin/service nginx *
    

    Or, as in your case,

      deploy ALL=(ALL:ALL) NOPASSWD: /etc/init.d/nginx *
    

    Here I am assuming your deployment user is deploy.

    You can add here other commands too for which you dont require to enter password. For example

      deploy ALL=(ALL:ALL) NOPASSWD: /usr/sbin/service nginx *, /etc/init.d/mysqld, /etc/init.d/apache2
    

提交回复
热议问题