#!/usr/bin/env bash underline="\033[4m" red="\033[1;31m" green="\033[1;32m" yellow="\033[1;33m" bold="\033[1m" reset="\033[0m" if [ "$1" == "-h" ]; then echo -e "\\033[4msphp_php-fpm.sh \\033[0m" echo "Change php version (php-fpm)" echo echo "USAGE: sphp_php-fpm.sh version (8.1 8.2 8.3)" echo echo " -h display this help" echo exit 0 fi # # mass_dns: warning: Unable to determine any DNS servers. Reverse DNS is disabled. Try using --system-dns or specify valid servers with --dns-servers # homebrew_path=$(brew --prefix) brew_prefix=$(brew --prefix | sed 's#/#\\\/#g') protocol="http" apache_conf_path=$(httpd -V | grep 'SERVER_CONFIG_FILE' | awk -F '\"' '{print $2}') document_root=$(grep -e '^DocumentRoot' "$apache_conf_path" | awk '{print $2}' | sed 's/\"//g') server_name=$(grep -e '^ServerName' "$apache_conf_path" | awk '{print $2}') h=$(hostname) vhost_conf=$(grep -e 'httpd-vhosts.conf' $apache_conf_path | awk '{print $2}') #ssl_conf=$(grep -e 'httpd-ssl.conf' $homebrew_path/etc/httpd/httpd.conf | awk '{print $2}') ssl_conf=$(grep -e 'httpd-ssl.conf' $apache_conf_path | awk '{print $2}') if [ -f "$ssl_conf" ]; then apache_port=$(cat $ssl_conf | grep -e '^Listen' | awk '{print $2}') a=$(nmap --script http-methods -p$apache_port --script-args http-methods.url-path=’/page’ $h | grep -A1 "^PORT" | tail -1 | awk '{print $3}') if [[ "$a" == "http" ]] || [[ "$a" == "https" ]]; then protocol=$a; fi fi brew_array=("8.1","8.2","8.3") php_array=("php@8.1" "php@8.2" "php@8.3") php_installed_array=() php_version="php@$1" php_opt_path="$brew_prefix\/opt\/" simple_php_version=$(echo "$php_version" | sed 's/^php@//' | sed 's/\.//') # Current php version (from httpd.conf) ip=$(grep -E SetHandler $apache_conf_path | grep -v \# | grep -E -o "([0-9]{1,3}[\.]){3}[0-9]{1,3}:[0-9]{4}") x=${ip: -2} current__simple_php_version="$x" current_php_version="php@${x:0:1}.${x:1}" echo -e "${underline}Current${reset} php version: ${bold}$current_php_version${reset}" # What versions of php are installed via brew # /opt/homebrew/etc/php/8.0/php-fpm.d/www.conf - listen = 127.0.0.1:9081 echo -e "\nPHP version ${underline}installed${reset}:" for i in ${php_array[*]}; do version=$(echo "$i" | sed 's/^php@//') if [[ -d "$homebrew_path/etc/php/$version" ]]; then php_installed_array+=("$i") #php_port=$(sed -n "/^listen/p" $homebrew_path/etc/php/$version/php-fpm.d/www.conf) php_ipport=$(grep -E ^listen $homebrew_path/etc/php/$version/php-fpm.d/www.conf | awk -F" = " '{print $2}') php_running=$(lsof -i -n -P | grep php-fpm | grep $php_ipport) [ -n "$php_running" ] && echo -e " ● php-fpm version: ${bold}$i${reset} ($php_ipport)" "${green}Running...${reset}" || echo -e "php-fpm version: $version ($php_ipport)" "${red}Stopped.${reset}" echo -e " ● php (cli): /opt/homebrew/opt/$i/bin/php" fi done proxy_pass_match_string="ProxyPassMatch \"^/(.*\.php(/.*)?)$\" \"fcgi://127.0.0.1:90$simple_php_version/opt/homebrew/local/var/www/\$1\"" set_handler_string="SetHandler \"proxy:fcgi://127.0.0.1:90$simple_php_version\"" # 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 if [[ "$php_version" == "$current_php_version" ]]; then echo -e "\nPHP already running version ${bold}$php_version${reset} !" echo "Exiting..." exit 0 else version=$(echo "$php_version" | sed 's/^php@//') echo -e "\nSwitching to ${bold}$php_version${reset}..." echo "Edit your Apache conf..." #sed -i.bak "s/ProxyPassMatch.*/ProxyPassMatch \"^/(.*\.php(/.*)?)$\" \"fcgi:\/\/127.0.0.1:90$simple_php_version\/opt\/homebrew\/local\/var\/www\/\$1\"/" $apache_conf_path #sed -i.bak "s/SetHandler \"proxy:fcgi.*/SetHandler \"proxy:fcgi:\/\/127.0.0.1:90$simple_php_version\"/" $apache_conf_path sed -i.bak "s/fcgi:\/\/127.0.0.1:90$current__simple_php_version/fcgi:\/\/127.0.0.1:90$simple_php_version/" $apache_conf_path echo "Restarting Apache server..." #brew services restart httpd sudo apachectl -k restart echo "Loading PHP info..." echo '' > $document_root/php-info.php && open "$protocol://$server_name/php-info.php" echo "All done!" echo -e "\n${underline}Apache conf files:${reset}" echo "$apache_conf_path" echo "$vhost_conf" echo "$ssl_conf" echo -e "${underline}PHP conf files:${reset}" echo "$homebrew_path/etc/php/$version/php.ini" echo "$homebrew_path/etc/php/$version/php-fpm.d/www.conf" find "$homebrew_path/etc/php/$version/conf.d" -name "*.ini" -print | sort -n fi else echo -e "\nSorry, but $php_version is not installed via brew. Install by running: brew install $php_version" fi else echo -e "\nUnknown version of PHP. PHP Switcher can only handle arguments of:" ${brew_array[@]} fi