Stop Wasting your Time Restarting Services Manually After Deployment: Visudo It!

Here is how I managed to save time on myphp project by enabling restarting services automatically during deployment.

Basic capistrano configuration

When I work on PHP projects I deploy using Capistrano, a tool that enables scripting of deployment tasks.During my previous project, I had to manually log in to my server and restart the php-fpm service after each deployment.I wasted almost one hour of my time every week running sudo service php5-fpm restart 10 times a day.Moreover, every once in a while I forgot to restart the service and I had to spend 30 minutes more to find out why I couldn’t see my new feature on my website.

To save time, I wanted capistrano to do it for me:

task :restart_php do
on roles(:app) do
execute "sudo service php5-fpm restart"
end
end

To do so, I needed superuser permissions.I considered giving sudo rights to the application user, but this would represent a major security issue: say there is a security breach on your application that enables an attacker to take control over the application user, they could take control over the whole server.

A solution is to grant superuser permission on a specific command.

Introducing the /etc/sudoers file and visudo command

Log in to your server as root and run sudo visudo .Visudo enables you to edit the /etc/sudoers file, in which your computer grants superuser permissions.

I added the following line:

www-data ALL=(root) NOPASSWD: /usr/sbin/service php5-fpm restart

The line is divided into 4 parts:

www-data is the user you want to grant permissions to. ALL filters users logged in from ALL hosts name (root) www-data has root permissions on the following command NOPASSWD: /usr/sbin/service php5-fpm restart enables www-data to run only this exact command /usr/sbin/service php5-fpm restart without being asked any password.

You can now understand why this line in the file is the source of root superpowers:

root ALL=(ALL) ALL With great power comes great responsibilities

Before you enable all users to run sudo commands without being asked any password (which is possible but strongly advised against), take caution using visudo: granting superuser commands must be used with parcimony.

Finally, you may ask yourself why I used visudo instead of vim /etc/sudoers file. Never edit directly the /etc/sudoers file.Visudo includes checks before saving the edited file: it prevents it from syntax errors that would cause major superuser problems on your computer.

Post Views: 17

You liked this article? You'd probably be a good match for our ever-growing tech team at Theodo.

Join Us

本文系统(linux)相关术语:linux系统 鸟哥的linux私房菜 linux命令大全 linux操作系统

主题: PHP
分页:12
转载请注明
本文标题:Stop Wasting your Time Restarting Services Manually After Deployment: Visudo It!
本站链接:http://www.codesec.net/view/482492.html
分享请点击:


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 系统(linux) | 评论(0) | 阅读(25)