Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

skylerkatz's avatar

Restarting PHP-FPM in Deploy Script

I am trying to figure out how to configure OPCACHE on my server but have so far been unable to get PHP-FPM to reload after deploying.

I tried following this article - https://stevegrunwell.com/blog/restart-php-fpm-during-deployments, but it did not seem to work with php 7.1 (or I did something wrong).

Any help here would be appreciated!

0 likes
19 replies
bashy's avatar
bashy
Best Answer
Level 65

Forge supplies a button to activate opcache now?

php-fpm is restarted with sudo service phpX-fpm reload. Just add that command to sudoers via sudo visudo

6 likes
Lars-Janssen's avatar

@bashy if I activate OPCACHE on forge do I have to add sudo service phpX-fpm reload at the end of my deploy script aswel?

Thanks!

skylerkatz's avatar

@bashy,

Yeah the functionality was released yesterday.

I think the issue is that I added it incorrectly to sudo visudo.

Here is what I have in the file now

forge ALL=NOPASSWD: /usr/sbin/service php7.1-fpm restart

Should that look like something else?

1 like
davielee's avatar

@skylerkatz That looks right, but if you are worried you could disable opcache through the UI and re-enable it, but after that, what @bashy wrote should do the trick for you.

bashy's avatar

@skylerkatz A reload should suffice else you'll get downtime from a restart I think.

1 like
skylerkatz's avatar

Yeah, it turns out I was running the wrong command in the deployment script :)

willvincent's avatar

You can also do it in php without having to restart the php process...

Just call:

opcache_reset();
bashy's avatar

@willvincent And in a deployment system, how would you do that?

I also wouldn't "restart", just "reload"

1 like
willvincent's avatar

@bashy I would probably wrap an artisan command around it that gets fired by the deploy script.

PhilippeTellier's avatar

I don't think a package is really necessary for something that simple. Here is my Command class :

<?php

namespace App\Console\Commands\Opcache;

use Illuminate\Console\Command;
use App\Models\User;

class Clear extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'opcache:clear';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Clear the opcode cache';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        if (function_exists('opcache_reset') && opcache_reset()) {
            $this->info('Opcode cache cleared');
        } else {
            $this->line('Opcode cache: Nothing to clear');
        }
    }
}
3 likes
shez1983's avatar

@bashy sorry to dredge up this post - i can see forge has automatically? added a file

forge ALL=NOPASSWD: /usr/sbin/service php7.1-fpm reload
forge ALL=NOPASSWD: /usr/sbin/service php7.0-fpm reload
forge ALL=NOPASSWD: /usr/sbin/service php5.6-fpm reload
forge ALL=NOPASSWD: /usr/sbin/service php5-fpm reload

in /etc/sudoers.d but this stil gives me the error when i press deploy? i have used lots of variants:

sudo /usr/sbin/service php7.1-fpm reload sudo service php7.1-fpm reload service php-fpm reload

and every time i get interactive authentication required?

2 likes
bashy's avatar

@shez1983 Sorry, I've never actually used Forge so I'm not sure what you're seeing/getting in response to your deployment.

coreproc's avatar

@shez1983

sudo /usr/sbin/service php7.1-fpm reload should work.

I had the same problem a few minutes ago - turns out I was running "restart" instead of "reload"

2 likes
bashy's avatar

@coreproc Already been marked as answered and your reply adds nothing. Restart is pretty much the same as reload but you drop client connections instantly...

aurawindsurfing's avatar

If anyone is looking at it again.

Forge now includes restart fpm line as a default deployement script. In other words all that is needed is to go to the forge server panel and enable OPCACHE.

Nothing else.

Enjoy!

1 like
ocx's avatar

In php.ini file under php-fpm directory set following cgi option resolved this issue withoth reloading/restarting php-fpm after deploy:

cgi.fix_pathinfo=0;

Optionally use too:

cgi.discard_path=0;

After php.ini modified do php-fpm restart:

Ubuntu (php-fpm 7.2):

sudo systemctl restart php7.2-fpm.service

and no need more php-fpm restart after deploying, it works with php-fpm 7.0, php-fpm-7.1, php-fpm-7.2 with 5.6 and 7.3 it is not tested, please write a reply if you tryed.

all1.ai's avatar

It's easy you just have to make sure the sudoers file is correct which forge does for you and places in /etc/sudoers.d/php-fpm. Here is a full working version of this file for every version supported:

forge ALL=NOPASSWD: /usr/sbin/service php8.1-fpm reload
forge ALL=NOPASSWD: /usr/sbin/service php8.0-fpm reload
forge ALL=NOPASSWD: /usr/sbin/service php7.4-fpm reload
forge ALL=NOPASSWD: /usr/sbin/service php7.3-fpm reload
forge ALL=NOPASSWD: /usr/sbin/service php7.2-fpm reload
forge ALL=NOPASSWD: /usr/sbin/service php7.1-fpm reload
forge ALL=NOPASSWD: /usr/sbin/service php7.0-fpm reload
forge ALL=NOPASSWD: /usr/sbin/service php5.6-fpm reload
forge ALL=NOPASSWD: /usr/sbin/service php5-fpm reload
forge ALL=NOPASSWD: /usr/sbin/service php8.2-fpm reload
forge ALL=NOPASSWD: /usr/sbin/service php8.3-fpm reload

Then in forge backend if you go your site and look at the deployment section you should have something like this: (replace WWW with your own site path)

cd
 /home/forge/www
git pull origin $FORGE_SITE_BRANCH

$FORGE_COMPOSER install --no-dev --no-interaction --prefer-dist --optimize-autoloader

( flock -w 10 9 || exit 1
    echo 'Restarting FPM...'; sudo -S service $FORGE_PHP_FPM reload ) 9>/tmp/fpmlock

if [ -f artisan ]; then
    $FORGE_PHP artisan migrate --force
fi

Please or to participate in this conversation.