<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:media="http://search.yahoo.com/mrss/"><channel><title><![CDATA[server - Stapps.io]]></title><description><![CDATA[server - Stapps.io]]></description><link>https://blog.stapps.io/</link><generator>Ghost 0.11</generator><lastBuildDate>Fri, 02 Jan 2026 08:41:36 GMT</lastBuildDate><atom:link href="https://blog.stapps.io/tag/server/rss/" rel="self" type="application/rss+xml"/><ttl>60</ttl><item><title><![CDATA[Easy PHP version switching on Ubuntu (allows for running multiple versions at the same time)]]></title><description><![CDATA[<p>This article assumes you're using PHP FPM, we'll also show you how to switch PHP versions on the command line at the bottom as well. </p>

<p>You'll need to install the different PHP versions and make sure modules and config matches up. <br>
If you already have php7.4 installed and you're</p>]]></description><link>https://blog.stapps.io/easy-php-version-switching-on-ubuntu/</link><guid isPermaLink="false">a1b72215-48f1-46f6-b391-83406253b011</guid><category><![CDATA[php]]></category><category><![CDATA[tips]]></category><category><![CDATA[server]]></category><dc:creator><![CDATA[Andrew Stilliard]]></dc:creator><pubDate>Mon, 18 Mar 2024 23:57:53 GMT</pubDate><content:encoded><![CDATA[<p>This article assumes you're using PHP FPM, we'll also show you how to switch PHP versions on the command line at the bottom as well. </p>

<p>You'll need to install the different PHP versions and make sure modules and config matches up. <br>
If you already have php7.4 installed and you're looking to install 8.3 you could add 8.3 in while leaving 7.4 by installing it by version number with your OS package manager, e.g. with <code>apt</code> on Ubuntu:  </p>

<pre><code class="language-bash">sudo apt install php8.3-{cli,fpm}  
</code></pre>

<p>However, before installing the new version, you'll need to make sure your PHP pools don't reference a central/single sock file as installing the new fpm may auto switch it to the new version. E.g.  </p>

<pre><code class="language-bash">sudo vim /etc/php/7.4/fpm/pool.d/www.conf  
</code></pre>

<p>Check if it references <code>/var/run/php/php-fpm.sock</code> and update it to the version it's on like <code>/var/run/php/php7.4-fpm.sock</code>. <br>
You'll then also need to make this update in your nginx or apache file and reload php-fpm first, then nginx or apache. </p>

<p>Once you've installed your new PHP versions, check both php fpm versions are running with:  </p>

<pre><code class="language-bash">systemctl status php7.4-fpm  
systemctl status php8.3-fpm  
</code></pre>

<p>To help compare config files you could use a tool like <code>meld</code>, <code>vimdiff</code> or <code>diff</code>, e.g.:  </p>

<pre><code class="language-bash">sudo meld /etc/php/7.4/cli/php.ini /etc/php/8.3/cli/php.ini  
</code></pre>

<p>Make sure to compare both the <code>cli</code> and <code>fpm</code> config files.</p>

<p>Also to help compare PHP modules you can use <code>meld</code>, <code>vimdiff</code> or <code>diff</code> again like so:  </p>

<pre><code class="language-bash">meld &lt;(php7.4 -m) &lt;(php8.3 -m)  
</code></pre>

<p>Then install missing ones, for example if you spot <code>gd</code>, <code>gmp</code>, <code>intl</code>, <code>xml</code>, <code>curl</code> and <code>bcmath</code> missing you could install them with:  </p>

<pre><code class="language-bash">sudo apt install php8.3-{gd,gmp,intl,xml,curl,bcmath}  
</code></pre>

<p>Then compare the modules again.</p>

<p>Using PHP FPM you can switch PHP version easily by switching which <code>/var/run/php/php7.4-fpm.sock</code> (this may also be <code>/run/php/php7.4-fpm.sock</code>) you reference in either the Apache or Nginx config (or whichever system you use to host. </p>

<p>E.g. in Nginx it's this line:  </p>

<pre><code class="language-bash">fastcgi_pass unix:/var/run/php7.4-fpm.sock  
</code></pre>

<p>Or Apache:  </p>

<pre><code class="language-bash">SetHandler "proxy:unix:/run/php/php7.4-fpm.sock|fcgi://localhost"  
</code></pre>

<p>Your line here may be a little different, the above assumes it's the default version number based sock files but there are alternatives. <br>
Here's 3 additional examples you may find:</p>

<ul>
<li><p>A single <code>/var/run/php-fpm.sock</code> instead of specific version.
If that's the case, it will likely be that your php fpm pool for that version points to that single sock file. Your pool will be configured by a file in <code>/etc/php/VERSION/fpm/pool.d/ e.g. /etc/php/7.4/fpm/pool.d/www.conf</code> <br>
You can either, leave the current one pointing here and make sure your new PHP version has the specific version number in the sock file name and update the reference to it in the nginx/apache config. Or, you could swap which php version uses that sock file by editing the current one to have the version in the file name and the new one to this main sock file. Then restart both versions of PHP FPM. </p></li>
<li><p>Multiple pools, one per site.
If you have multiple sites on the server you may have created a php pool for each of them to isolate them. If so, you'll need to copy over these configs to the new version (or at least the ones you want on the new version, maybe test with one first) so if it was <code>/etc/php/7.4/fpm/pool.d/site.com.conf</code> and you're switching to php8.3 then copy to <code>/etc/php/8.3/fpm/pool.d/site.com.conf</code> <br>
<code>SetHandler "proxy:unix:/run/php/php7.4-fpm-site.com.sock|fcgi://localhost"</code> and update the version number in the sock file if it had one, or add one in so it doesn't clash with the other.</p></li>
<li><p>Listing to a port instead of a sock file.
<code>
listen = 127.0.0.1:7401
</code>
In this example you have ports instead of files but they can be updated mostly the same as the sock files. You'll just need to remember or note down which ports relate to which versions &amp; update your nginx/apache configs as needed.</p></li>
</ul>

<p>Remember to reload nginx/apache after editing the configs and if you updated your php-fpm configs or pools, reload php-fpm for the versions you updated.</p>

<p>Then for the CLI you can switch with:  </p>

<pre><code class="language-bash">sudo update-alternatives --set php /usr/bin/php8.3  
</code></pre>

<p>You may also want to switch the php version <code>phar</code> files use as well, you can do this with by running both the following commands:  </p>

<pre><code class="language-bash">sudo update-alternatives --set phar /usr/bin/phar8.3  
sudo update-alternatives --set phar.phar /usr/bin/phar.phar8.3  
</code></pre>

<p>To make this easier, I added a bash function so I can call <code>php-version 7.4</code> or <code>php-version 8.3</code> to switch between them, and call <code>php-version</code> to have it list the php-versions I have available:  </p>

<pre><code class="language-bash"># switch php version for CLI
function php-version {  
    if [[ "$1" == "" ]]; then
        echo "Available versions:"
        currentVersion=$(php -v | grep -oP '(?&lt;=PHP )\d+.\d+')
        availableVersions=$(ls /usr/bin/php* | grep -oP '(?&lt;=/php)(\d+\.\d+)')
        echo $availableVersions | sed "s/$currentVersion/$currentVersion *current/"
        echo "Set default for cli with: php-version $currentVersion"
    else
        echo "Setting default php cli version to $1"
        sudo update-alternatives --set php /usr/bin/php$1
        sudo update-alternatives --set phar /usr/bin/phar$1
        sudo update-alternatives --set phar.phar /usr/bin/phar.phar$1
        echo "php -v:"
        php -v
    fi
}
</code></pre>

<p>Side note, you can also use <code>sudo update-alternatives --config php</code> to have it interactively show you php versions available and switch that way. </p>]]></content:encoded></item></channel></rss>