Deployer: Restarting php5-fpm

Created on 27 May 2015  Â·  22Comments  Â·  Source: deployphp/deployer

Hello,

I saw from the examples page a task to restart php5-fpm:

task('reload:php-fpm', function () {
    run('sudo /usr/sbin/service php5-fpm reload');
});

after('deploy', 'reload:php-fpm');
after('rollback', 'reload:php-fpm');

I need to use this to refresh the php5 paths to the new current symlinked folder.

The problem I have though, is that running sudo requires entering the password, which when running from the deploy script can't happen, I'm getting this:

sudo: no tty present and no askpass program specified 

I did add my user to the sudoers file like so:

myUser    ALL=(ALL:ALL) NOPASSWD: ALL

Though this still gives the same error.

Is there some sort of trick to make this work ?

Thank you!

Most helpful comment

Also note that order is important in sudoers config!

When multiple entries match for a user, they are applied in order. 
Where there are multiple matches, the last match is used
(which is not necessarily the most specific match).

http://askubuntu.com/a/100112/286499

All 22 comments

This is a problem in rights for access sudo.
Please check you /etc/sudoers file!

As example (my sudoers):

taleforchild ALL=NOPASSWD:/bin/systemctl restart php-fpm.service

Where:
taleforchild - user in operation system
Working in CentOS 7.0

Yes, it's something related to sudo rights, though I don't get what, hence my post here.

Using myUser ALL=(ALL:ALL) NOPASSWD: ALL should have done the trick...

I tried forge ALL=NOPASSWD:/usr/sbin/service php5-fpm reload , but in CLI this returns:

service php5-fpm reload
reload: Rejected send message, 1 matched rules; type="method_call", sender=":1.305" (uid=1000 pid=8681 comm="reload php5-fpm ") interface="com.ubuntu.Upstart0_6.Instance" member="Reload" error name="(unset)" requested_reply="0" destination="com.ubuntu.Upstart" (uid=0 pid=1 comm="/sbin/init ")

If you use Ubuntu OS:

Command for restart PHP-FMP service:

/etc/init.d/php-fpm restart

And, you should add rights for run this command without sudo password in you system.

Deployer task:

task('reload:php-fpm', function () {
    run('sudo /etc/init.d/php-fpm restart'); // Using SysV Init scripts
});

And you sudoers rule:

you_user_name ALL=NOPASSWD: /etc/init.d/php-fpm restart

If you use another operation system, please write os and version.

P.S. The wrapper scripts (as example service) redirecting call to other executable files, then you must write a real command in /etc/sudoers

Hm I can reload php5-fpm using either of those:

service php5-fpm reload
# or
/etc/init.d/php5-fpm reload
# or 
/usr/sbin/service php5-fpm reload

I tried it with your setup, logged out of the VPS to be sure, then ran sudo /etc/init.d/php-fpm restart, still getting asked for password. :disappointed:

My visudo file:

forge ALL=NOPASSWD: /etc/init.d/php5-fpm restart

Edit: I'm on ubuntu, indeed.

So, the real script for reload php-fpm /etc/init.d/php-fpm.
For run this script, you should have a superadmin rights (sudo).
If asked password (if you called from another user), then the rule of sudoers not working.

Please try (user for available restart without password - new_user, as example):

1 Login to system as root. (create a user, if not exists. Please check in /etc/passwd)
2 Change /etc/sudoers. Adds:

new_user ALL=NOPASSWD: /etc/init.d/php-fpm restart

3 Login to system as new_user (NOT su, can open new console).
4 Try run:

sudo /etc/init.d/php-fpm restart

Should work.

UPD: The latest command (p4), should not ask the password

That's what I did, it doesn't work. I don't understand why... :/
Also the script to reload php-fpm is not /etc/init.d/php-fpm but /etc/init.d/php5-fpm.

Ok, replace /etc/init.d/php-fpm to /etc/init.d/php5-fpm in sudoers

I did, of course, but that still makes me ask for sudo password when running the command.

hm... interesting... in another system (debian, centos) this is a good work.
Can file /etc/sudoers not reading...

When I cat /etc/sudoers I do see my changes in it, so it was written correctly.

It is very strange indeed. :(

Please try:

you_user_name ALL=NOPASSWD: /etc/init.d/php5-fpm
                           ^
                           | no space

my sudoers file (ubuntu 14.04)

deploy_user ALL=(ALL) NOPASSWD:/etc/init.d/php5-fpm *, /etc/init.d/nginx *

Tried without a space:

forge ALL=NOPASSWD:/etc/init.d/php5-fpm restart

And running sudo /etc/init.d/php5-fpm restart as the forge user, still asks me for a password.
Running /etc/init.d/php5-fpm restart without sudo, doesn't though. But when I commented the line in the sudoers file, it still doesn't ask root password for it.. so I don't know...

Edit:

I changed the task to:

task('reload:php-fpm', function () {
    run('/etc/init.d/php5-fpm restart');
});

#with:
after('deploy', 'reload:php-fpm');

Though I get: RuntimeException, when it get's to the reload task. Nothing more in the logs file... :/

Please try run deploy with -vvv option and show out to here. @nWidart

This is the error with stacktrace, when running -vvv:

➤ Executing task reload:php-fpm
⤷ on [staging]
Run: /etc/init.d/php5-fpm restart



  [RuntimeException]  



Exception trace:
 () at phar:///usr/local/bin/dep/src/Server/Remote/PhpSecLib.php:114
 Deployer\Server\Remote\PhpSecLib->run() at phar:///usr/local/bin/dep/src/functions.php:289
 run() at /Users/nicolaswidart/Sites/Asguard/presentation-site/deploy.php:29
 {closure}() at n/a:n/a
 call_user_func() at phar:///usr/local/bin/dep/src/Task/Task.php:66
 Deployer\Task\Task->run() at phar:///usr/local/bin/dep/src/Executor/SeriesExecutor.php:40
 Deployer\Executor\SeriesExecutor->run() at phar:///usr/local/bin/dep/src/Console/TaskCommand.php:116
 Deployer\Console\TaskCommand->execute() at phar:///usr/local/bin/dep/vendor/symfony/console/Symfony/Component/Console/Command/Command.php:257
 Symfony\Component\Console\Command\Command->run() at phar:///usr/local/bin/dep/vendor/symfony/console/Symfony/Component/Console/Application.php:874
 Symfony\Component\Console\Application->doRunCommand() at phar:///usr/local/bin/dep/vendor/symfony/console/Symfony/Component/Console/Application.php:195
 Symfony\Component\Console\Application->doRun() at phar:///usr/local/bin/dep/vendor/symfony/console/Symfony/Component/Console/Application.php:126
 Symfony\Component\Console\Application->run() at phar:///usr/local/bin/dep/src/Deployer.php:91
 Deployer\Deployer->run() at phar:///usr/local/bin/dep/bin/dep:62
 require() at /usr/local/bin/dep:4

@nWidart restarting php-fpm can close connections and/or connection attempts (tcp syn).
You can use cachetool to clear opcode cache (apc or opcache). Also, there's a deployer recipe for it deployphp/recipes

Oh ok, thanks.

I've installed deployer and this recipes repository with composer , globally, do I include the file like so, or ?:

require 'recipes/recipes/cachetool.php';

Or is it recommended to install on a per project basis ?

Edit:
Requiring require 'recipes/recipes/cachetool.php'; throws not found exception.

Though recipe/common.php can.

I've tried relative to the deployer package, require '../../deployphp/recipes/recipes/cachetool.php';, still nothing.

You only need to require the recipes in composer, usually you won't need
deployer as a dependency because you use it as an app.
And yes, it should be per project.
Hope it solves your problem.
On qua, 27/05/2015 at 21:50 Nicolas Widart [email protected] wrote:

Oh ok, thanks.

I've installed deployer and this recipes repository with composer ,
globally, do I include the file like so, or ?:

require 'recipes/recipes/cachetool.php';

Or is it recommended to install on a per project basis ?

—
Reply to this email directly or view it on GitHub
https://github.com/deployphp/deployer/issues/302#issuecomment-106040911.

@nWidart so it's turn out?
Here is my sudoers on debian:

elfet   ALL=(ALL) NOPASSWD: /usr/sbin/service php5-fpm reload

Still not working, no.

I tried the cache tool, but that throws: Objectcachetooldoes not exist in Collection.

I required the package in the project and required: vendor/deployphp/recipes/recipes/cachetool.php.

Edit: this is the stacktrace:

Exception trace:
 () at /Users/nicolaswidart/.composer/vendor/deployer/deployer/src/Collection/Collection.php:27
 Deployer\Collection\Collection->get() at /Users/nicolaswidart/.composer/vendor/deployer/deployer/src/functions.php:420
 get() at /Users/nicolaswidart/Sites/Asguard/presentation-site/deploy.php:64
 {closure}() at n/a:n/a
 call_user_func() at /Users/nicolaswidart/.composer/vendor/deployer/deployer/src/Task/Task.php:66
 Deployer\Task\Task->run() at /Users/nicolaswidart/.composer/vendor/deployer/deployer/src/Executor/SeriesExecutor.php:40
 Deployer\Executor\SeriesExecutor->run() at /Users/nicolaswidart/.composer/vendor/deployer/deployer/src/Console/TaskCommand.php:116
 Deployer\Console\TaskCommand->execute() at /Users/nicolaswidart/.composer/vendor/symfony/console/Symfony/Component/Console/Command/Command.php:257
 Symfony\Component\Console\Command\Command->run() at /Users/nicolaswidart/.composer/vendor/symfony/console/Symfony/Component/Console/Application.php:874
 Symfony\Component\Console\Application->doRunCommand() at /Users/nicolaswidart/.composer/vendor/symfony/console/Symfony/Component/Console/Application.php:195
 Symfony\Component\Console\Application->doRun() at /Users/nicolaswidart/.composer/vendor/symfony/console/Symfony/Component/Console/Application.php:126
 Symfony\Component\Console\Application->run() at /Users/nicolaswidart/.composer/vendor/deployer/deployer/src/Deployer.php:91
 Deployer\Deployer->run() at /Users/nicolaswidart/.composer/vendor/deployer/deployer/bin/dep:63


deploy [-p|--parallel]

My sudoer configure is

lynn ALL=(ALL) NOPASSWD:/usr/bin/service php5-fpm restart

using sudo service php5-fpm restart works for me.

If u getting error "sorry, you must have a tty to run sudo", just comment this line Defaults requiretty in sudoers file. Work for me.

Also note that order is important in sudoers config!

When multiple entries match for a user, they are applied in order. 
Where there are multiple matches, the last match is used
(which is not necessarily the most specific match).

http://askubuntu.com/a/100112/286499

Was this page helpful?
0 / 5 - 0 ratings