Updating PHP on Laravel Forge.

Today I had the crazy idea of updating PHP on my Laravel Forge-provisioned server. In this article I’ll cover how to update PHP 7.0 to 7.1 on a 7.0-provisioned server.

Required reading:

Start off by reading this article by Bryse Meijer. In it, he explains how to add “ppa:ondrej/php” as an apt repository and install PHP 7.1 (CLI and FPM). He also describes how you can set the socket used by nginx, but we’re not going to be doing this just yet.

If you now run “php -v”, you’ll see that your CLI is already upgraded to 7.1, which is great! Unfortunately, Bryse doesn’t mention you’ll need a couple of extensions to be loaded by PHP FPM, which aren’t automatically installed when you install PHP 7.1.

Downloading extensions:

When SSH’d into your server, navigate to “/etc/php/7.0/mods-available” and list the contents of the directory. You should see about 46 “.ini” files listed. I recommend saving this list somewhere as we’ll be downloading the PHP 7.1 compatible versions of these extensions.

Now that we know what extensions to download, navigate to “/etc/php/7.1/mods-available” and list the contents of the directory. You should see fewer “.ini” files than you did in the 7.0 directory, which we’ll be fixing here. Execute “sudo apt-get install php7.1-<extension-name>” for every extension that’s not in the 7.1 list, but is in the 7.0 directory.

For example, “gd.ini” does exist in the list of 7.0 extensions, but it’s not present in the 7.1 list. Once you execute “sudo apt-get install php7.1-gd” and list the contents again, you should see the “gd.ini” file there now. Repeat this for all the missing extensions.

Enabling PHP 7.1:

Right, so we have all the required extensions we need, let’s go and enable PHP 7.1 for our website! Go to “/etc/nginx/sites-available”, then edit the configuration file for the domain you want to enable PHP 7.1 for with your favorite text editor.

Near the bottom of the file, you should see a line that looks like this: “fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;”. As you might expect, we’ll just replace the “php7.0-fpm.sock with “php7.1-fpm.sock”.

All we need to do now is restart our webserver with “sudo service nginx restart” and we’re done! To verify, add a call to “phpinfo();” followed by “die;” at the top of “/home/forge/<domain>/public/index.php”, navigate to the website and 🎉! PHP 7.1!

Since publishing this article, I noticed that deploying again breaks this (always showing a 500 error in the browser). To temporarily fix this, change “php7.1-fpm.sock” back to “php7.0-fpm.sock”, reload nginx (“sudo service nginx restart”), refresh the browser and then change it back to “php7.1-fpm.sock” and restart nginx again.

My name is Sven, I’m a PHP developer from The Netherlands. If you want to read more from me, feel free to follow me here or on Twitter. Want to take a look at my open source work? Check me out on GitHub!

Show your support

Clapping shows how much you appreciated Sven Luijten’s story.