Easy bash-script to switch PHP versions on linux

Every webdeveloper needs to swith PHP version on his/her development setup. While you can do that manually every single time by using sudo a2dismod and sudo a2enmod and then restarting your server, it’s much easier and faster to write a quick bash script to do that for you.

Swith PHP version on the terminal

When working with the terminal, I am used to having shortcurts set up to make my work a bit faster an a bit more fun.

If you like this content and found it helpful, please consider supporting me with a small donation.

That is why I have written this tiny bash script to let me change my PHP version right from the terminal without having to disable and enable the PHP versions and then restart the server manually.

Feel free to just copy and paste it and place it as an executable in your /usr/bin  or just /bin  directory on your server.

if ["$1" = ""]
   echo "Enter a PHP version - either 5.6 or 7 at the moment";
echo "Switching PHP version to $1";
if [ "$1" = "5.6" ]
   sudo a2dismod php7.0
   sudo a2enmod php5.6
   sudo service apache2 restart
elif [ "$1" = "7" ]
   sudo a2dismod php5.6
   sudo a2enmod php7.0
   sudo service apache2 restart

I call this file switchphp , and it resides in my /bin  directory.

So copy the script above, save it to your /bin/switchphp  file and make it executable by using sudo chmod +x /bin/switchphp .

You can now switch your PHP versions by using the following commands:

switchphp 7  or switchphp 5.6

Note that you must have both PHP versions installed, of course. :)

Veröffentlicht von

Christian Hänsel

Web-Entwickler, SEO-Experte. Ich liebe technisches SEO, die Entwicklung von neuen Ideen und alles rund um das Internet... ja, ich habe mein Hobby zum Beruf gemacht und gehe auch Montags gerne zur Arbeit.

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert.

Diese Website verwendet Akismet, um Spam zu reduzieren. Erfahre mehr darüber, wie deine Kommentardaten verarbeitet werden.