#!/bin/bash | |
# Creator: Phil Cook | |
# Modified: Andy Miller | |
osx_major_version=$(sw_vers -productVersion | cut -d. -f1) | |
osx_minor_version=$(sw_vers -productVersion | cut -d. -f2) | |
osx_patch_version=$(sw_vers -productVersion | cut -d. -f3) | |
osx_patch_version=${osx_patch_version:-0} | |
osx_version=$((${osx_major_version} * 10000 + ${osx_minor_version} * 100 + ${osx_patch_version})) | |
brew_prefix=$(brew --prefix | sed 's#/#\\\/#g') | |
brew_array=("5.6","7.0","7.1","7.2","7.3","7.4","8.0") | |
php_array=("php@5.6" "php@7.0" "php@7.1" "php@7.2" "php@7.3" "php@7.4" "php@8.0") | |
php_installed_array=() | |
php_version="php@$1" | |
php_opt_path="$brew_prefix\/opt\/" | |
php5_module="php5_module" | |
apache_php5_lib_path="\/lib\/httpd\/modules\/libphp5.so" | |
php7_module="php7_module" | |
apache_php7_lib_path="\/lib\/httpd\/modules\/libphp7.so" | |
php8_module="php_module" | |
apache_php8_lib_path="\/lib\/httpd\/modules\/libphp.so" | |
native_osx_php_apache_module="LoadModule ${php5_module} libexec\/apache2\/libphp5.so" | |
if [ "${osx_version}" -ge "101300" ]; then | |
native_osx_php_apache_module="LoadModule ${php7_module} libexec\/apache2\/libphp7.so" | |
fi | |
# Has the user submitted a version required | |
if [[ -z "$1" ]]; then | |
echo "usage: sphp version [-s|-s=*] [-c=*]" | |
echo | |
echo " version one of:" ${brew_array[@]} | |
echo | |
exit | |
fi | |
php_module="$php5_module" | |
apache_php_lib_path="$apache_php5_lib_path" | |
simple_php_version=$(echo "$php_version" | sed 's/^php@//' | sed 's/\.//') | |
if [[ simple_php_version -ge 70 && simple_php_version -lt 80 ]]; then | |
php_module="$php7_module" | |
apache_php_lib_path="$apache_php7_lib_path" | |
elif [[ simple_php_version -ge 80 ]]; then | |
php_module="$php8_module" | |
apache_php_lib_path="$apache_php8_lib_path" | |
fi | |
apache_change=1 | |
apache_conf_path="/usr/local/etc/httpd/httpd.conf" | |
apache_php_mod_path="$php_opt_path$php_version$apache_php_lib_path" | |
# What versions of php are installed via brew | |
for i in ${php_array[*]}; do | |
version=$(echo "$i" | sed 's/^php@//') | |
if [[ -d "/usr/local/etc/php/$version" ]]; then | |
php_installed_array+=("$i") | |
fi | |
done | |
# Check that the requested version is supported | |
if [[ " ${php_array[*]} " == *"$php_version"* ]]; then | |
# Check that the requested version is installed | |
if [[ " ${php_installed_array[*]} " == *"$php_version"* ]]; then | |
# Switch Shell | |
echo "Switching to $php_version" | |
echo "Switching your shell" | |
for i in ${php_installed_array[@]}; do | |
brew unlink $i | |
done | |
brew link --force "$php_version" | |
# Switch apache | |
if [[ $apache_change -eq 1 ]]; then | |
echo "Switching your apache conf" | |
for j in ${php_installed_array[@]}; do | |
loop_php_module="$php5_module" | |
loop_apache_php_lib_path="$apache_php5_lib_path" | |
loop_php_version=$(echo "$j" | sed 's/^php@//' | sed 's/\.//') | |
if [[ loop_php_version -ge 70 && loop_php_version -lt 80 ]]; then | |
loop_php_module="$php7_module" | |
loop_apache_php_lib_path="$apache_php7_lib_path" | |
elif [[ loop_php_version -ge 80 ]]; then | |
loop_php_module="$php8_module" | |
loop_apache_php_lib_path="$apache_php8_lib_path" | |
fi | |
apache_module_string="LoadModule $loop_php_module $php_opt_path$j$loop_apache_php_lib_path" | |
comment_apache_module_string="#$apache_module_string" | |
# If apache module string within apache conf | |
if grep -q "$apache_module_string" "$apache_conf_path"; then | |
# If apache module string not commented out already | |
if ! grep -q "$comment_apache_module_string" "$apache_conf_path"; then | |
sed -i.bak "s/$apache_module_string/$comment_apache_module_string/g" $apache_conf_path | |
fi | |
# Else the string for the php module is not in the apache config then add it | |
else | |
sed -i.bak "/$native_osx_php_apache_module/a\\ | |
$comment_apache_module_string\\ | |
" $apache_conf_path | |
fi | |
done | |
sed -i.bak "s/\#LoadModule $php_module $apache_php_mod_path/LoadModule $php_module $apache_php_mod_path/g" $apache_conf_path | |
echo "Restarting apache" | |
brew services stop httpd | |
brew services start httpd | |
fi | |
echo "" | |
php -v | |
echo "" | |
echo "All done!" | |
else | |
echo "Sorry, but $php_version is not installed via brew. Install by running: brew install $php_version" | |
fi | |
else | |
echo "Unknown version of PHP. PHP Switcher can only handle arguments of:" ${brew_array[@]} | |
fi |
This comment has been minimized.
This comment has been minimized.
@rhukster would you be able to update your gist to include 7.3 as shown in the latest revision on my fork here: https://gist.github.com/csoseman/f53fc6dd70cf5a382b9fba7f84454961/revisions? 7.3 is now the default brew version for php. This script rocks so thanks for that! |
This comment has been minimized.
This comment has been minimized.
I've now added 7.3 to this... thanks. |
This comment has been minimized.
This comment has been minimized.
Thank you for that script! Probably with a directory check? $FILE = "\/usr\/libexec\/apache2\/libphp7.so
if [ -e $FILE ];
then
native_osx_php_apache_module="LoadModule php7_module libexec\/apache2\/libphp7.so"
else
native_osx_php_apache_module="LoadModule php5_module libexec\/apache2\/libphp5.so"
fi |
This comment has been minimized.
This comment has been minimized.
Hi. I have an issue with homebrew: php links wrong, in services list "php" is written instead of "php@7.3". And I installed sphp from here |
This comment has been minimized.
This comment has been minimized.
In my current httpd.conf I'm including a custom .conf file in which I load the php modules, so I had to modify the apache_conf_path to point to that file. Other than that it worked very well, thanks! |
This comment has been minimized.
This comment has been minimized.
Is it normal that I have to reboot for the change to be effective ? The script works without any error but if I |
This comment has been minimized.
This comment has been minimized.
In your article (https://getgrav.org/blog/macos-catalina-apache-multiple-php-versions) httpd.conf is /usr/local/etc/httpd/httpd.conf Why this file change "apache_conf_path" to "/etc/apache2/httpd.conf" ? |
This comment has been minimized.
This comment has been minimized.
Hi! Works great! But I had to change this line: |
This comment has been minimized.
This comment has been minimized.
Hi ! Am I the only one who has a problem with the icu4c library version when I switch from version 5.6/7.1 to 7.2/7.3/7.4 ? 5.6 and 7.1 works with icu4u 63.1 and 7.2, 7.3 and 7.4 with 64.2? Thank you in advance ! |
This comment has been minimized.
This comment has been minimized.
Hi! FYI, I had problems with Apache restart, and solved them by changing the script to |
This comment has been minimized.
This comment has been minimized.
I've updated the script to include the |
This comment has been minimized.
This comment has been minimized.
Hi @Sinepel, Thank you! |
This comment has been minimized.
This comment has been minimized.
Hi @vlad7code ,@Sinepel, You should upgrade icu4c , and rebuild php 5.6 and 7.1 check ICU version
|
This comment has been minimized.
This comment has been minimized.
Using this script on Catalina 10.15.5 with HOMEBREW_VERSION: 2.4.0 Creator: Phil CookModified: Andy MillerModified Stephen Tothosx_major_version=$(sw_vers -productVersion | cut -d. -f1) brew_prefix=$(brew --prefix | sed 's#/#\/#g') brew_array=("5.6","7.0","7.1","7.2","7.3","7.4") php5_module="php5_module" native_osx_php_apache_module="LoadModule ${php5_module} libexec/apache2/libphp5.so" php_module="$php5_module" Has the user submitted a version requiredif [[ -z "$1" ]]; then if [[ $(echo "$php_version" | sed 's/^php@//' | sed 's/.//') -ge 70 ]]; then apache_change=1 valet_restart=0 Check if valet is already installhash valet 2>/dev/null && valet_installed=1 || valet_installed=0 POSITIONAL=() Check for skip & change flagwhile [[ $# -gt 0 ]]; do
done What versions of php are installed via brewfor i in ${php_array[*]}; do Check if php version support via valetif [[ (" ${valet_support_php_version_array[*]} " != "$php_version") && ($valet_restart -eq 1) ]]; then Check that the requested version is supportedif [[ " ${php_array[]} " == "$php_version" ]]; then
$comment_apache_module_string\
else |
This comment has been minimized.
This comment has been minimized.
For PHP8 its not working for me because instead of |
This comment has been minimized.
This comment has been minimized.
Work well, except when switching 7.x to 8.0: httpd.conf is well modified but php_info() stay at php 7.4. (it's ok for php cli).
|
This comment has been minimized.
This comment has been minimized.
Hi, I don't use macOS, but I was tasked with setting up an environment for someone on one of the new Apple Silicon MacBooks. (don't know if relevant) I found that PHP was installed in |
This comment has been minimized.
This comment has been minimized.
Super useful @Lombra ! I was just about to post this. Also note that I had to update line 52, which is the Apache config path to |
This comment has been minimized.
This comment has been minimized.
I really want to update this with automatic support for both intel + m1 paths. Not had a chance to look at this yet. |
This comment has been minimized.
Do you want to take over the original repo? https://github.com/sgotre/sphp-osx/blob/master/sphp i have no time to fix the bugs in there.