Skip to content

Instantly share code, notes, and snippets.

@renekreijveld
Last active January 10, 2024 15:54
Show Gist options
  • Star 11 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save renekreijveld/66183c2bf308d393aa64137f13db3096 to your computer and use it in GitHub Desktop.
Save renekreijveld/66183c2bf308d393aa64137f13db3096 to your computer and use it in GitHub Desktop.
Easy PHP version switching command-line script for devilbox
#!/bin/bash
# sphp - Easy PHP version switching command-line script for devilbox
# Written by: René Kreijveld, email[at]renekreijveld.nl
# Version
version=1.1
# Available PHP versions in devilbox
php_array=(5.2 5.3 5.4 5.5 5.6 7.0 7.1 7.2 7.3 7.4 8.0)
# Devilbox folder
devilbox=/Users/yourusername/devilbox
# Has the user submitted a version required
if [[ -z "$1" ]]; then
echo "PHP version missing"
echo "Usage: sphp <version>"
echo "Version should be one of:" ${php_array[@]}
exit
fi
# Has the user submitted a non-existing version
if [[ ! "${php_array[@]}" =~ "$1" ]]; then
echo "Unknown PHP version"
echo "Usage: sphp <version>"
echo "Version should be one of:" ${php_array[@]}
exit
fi
echo "spsp version ${version}, switching to PHP version $1"
cd ${devilbox}
# Disable all PHP versions
for i in "${php_array[@]}"
do
perl -pi -e "s/PHP_SERVER=$i/#PHP_SERVER=$i/g" .env
perl -pi -e "s/##PHP_SERVER=/#PHP_SERVER=/g" .env
done
# Enable requested PHP version
perl -pi -e "s/#PHP_SERVER=$1/PHP_SERVER=$1/g" .env
echo
echo "Stopping docker containers:"
echo
docker-compose stop
echo
echo "Cleanup docker containers:"
echo
docker-compose rm -f
echo
echo "Restart docker containers:"
echo
docker-compose up -d httpd php mysql
echo
echo "PHP is now version $1"
@renekreijveld
Copy link
Author

renekreijveld commented Jul 22, 2020

Installation instructions

  1. Save this script in a folder that is in your search path on a Linux or macOS machine
  2. Make the script executable
chmod +x sphp
  1. Edit the sphp script and make the following adjustments:
    3.1. Edit line #8 to add or remove PHP versions of your devilbox, check these in your devilbox .env file
    3.2. Edit line #10 to match the path of your devilbox folder
    3.3. Edit line #49 to start the services you want
  2. Save the sphp script

Usage

sphp <php version number>

Example

sphp 7.3

This switches devilbox to PHP version 7.3 and restarts the docker containers.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment