Installing PHP security releases on Laravel Forge servers, without the wait
When a PHP security release is tagged, you probably want to update your servers as soon as possible.
While Laravel Forge does let you update PHP, which is great, it often takes a while for security releases to become available. First, the release needs to reach the package repository your server pulls from. Then, the Laravel team need to unlock the ability to update PHP from within the UI (they lock it down to avoid folks clicking "Update PHP" before the new version is actually available).
When the big security release came out a couple of weeks ago, we didn't want to sit around waiting, mostly because we had taken our services down as a precautionary measure. Instead, we ended up pulling in the packages.sury.org repository, where new releases tend to be available much sooner.
I figured I'd write up the process, and the commands we used, in case they're helpful for anyone else.
Adding the repository
You only need to do this once per server. SSH in, then run the following commands to add the repository's signing key and the repository itself:
sudo curl -sSLo /tmp/debsuryorg-archive-keyring.deb https://packages.sury.org/debsuryorg-archive-keyring.deb
sudo dpkg -i /tmp/debsuryorg-archive-keyring.deb
echo "deb [signed-by=/usr/share/keyrings/debsuryorg-archive-keyring.gpg] https://packages.sury.org/php/ $(lsb_release -sc) main" | sudo tee /etc/apt/sources.list.d/php.list
From now on, when you install or update PHP, your server will look at both the repository Forge configured (ppa.setup-php.com, a mirror of the ondrej/php PPA) and packages.sury.org, and install whichever has the newest version available.
If your server is running Ubuntu 26.04, you can skip this step, as Forge already uses packages.sury.org on 26.04 servers.
Updating PHP
Then, whenever a new release comes out, run these commands:
sudo apt-get update
sudo apt-get install -y --only-upgrade $(dpkg-query -W -f='${db:Status-Abbrev} ${Package}\n' 'php*' | awk '/^ii/{print $2}')
sudo systemctl restart 'php*-fpm'
php -v
The apt-get install command finds every PHP package that's currently installed on the server and upgrades it. It doesn't matter if you've got multiple versions of PHP installed, they'll all be updated.
Afterwards, it restarts PHP-FPM for every installed version so the new releases are picked up, and prints the current PHP version so you can check it worked.
However, php -v only shows the server's default PHP version. You can see the versions of every installed PHP instance by running:
for version in $(ls /etc/php); do php$version -v | head -n 1; done
Why can't Forge apply updates automatically?
I haven't seen an official reason, and maybe I'm mistaken, but it's probably because it's your server.
Laravel Forge doesn't know what configuration you're using. Maybe you've strayed from the defaults, to the point where Forge could break something by updating your server. Forge also doesn't know when the best time to apply updates would be. If it applied updates on everyone's servers, took them down at a bad time, and they didn't come back up for some reason, who's to blame? You'd blame Laravel.
If you want updates to be applied automatically, you probably want a managed hosting platform, like Laravel Cloud, where patches are applied behind the scenes without you needing to do anything.