Last active
November 18, 2025 22:16
-
-
Save mrevjd/0b582fea40078e4d1ad4182c1b097271 to your computer and use it in GitHub Desktop.
Update php to 8.x
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # PHP Upgrade 7.4 to 8.1 | |
| # This can be used for other versions. | |
| # Just need to replace the from version and/or the new version | |
| apt update | |
| apt -y install software-properties-common | |
| add-apt-repository ppa:ondrej/php | |
| apt update | |
| # Lists all packages installed in relation to php7.4 | |
| dpkg -l | grep php7 | cut -f3 -d' ' | xargs | sed 's/7.4/8.1/g' | |
| # If upgrading to 8.x use the below as JSON is now builtin | |
| apt install $(dpkg -l | grep php7 | cut -f3 -d' ' | grep -v json | xargs | sed 's/7.4/8.1/g') | |
| # If upgrading to 7.4 use the below | |
| apt install $(dpkg -l | grep php7 | cut -f3 -d' ' | xargs | sed 's/7.4/8.1/g') | |
| update-alternatives --config php | |
| cp /etc/php/7.4/fpm/php.ini /etc/php/8.1/fpm/php.ini | |
| cp /etc/php/7.4/fpm/php-fpm.conf /etc/php/8.1/fpm/ | |
| cp /etc/php/7.4/fpm/pool.d/www.conf /etc/php/8.1/fpm/pool.d/ | |
| sed -i 's/7.4/8.1/g' /etc/php/8.1/fpm/php-fpm.conf | |
| sed -i 's/7.4/8.1/g' /etc/php/8.1/fpm/pool.d/www.conf | |
| # Update nginx virtual hosts to use php8.1 | |
| sed -i 's/7.4/8.1/g' /etc/nginx/sites-enabled/* | |
| service php8.1-fpm restart | |
| service nginx reload | |
| # Stop php7.4 | |
| service php7.4-fpm stop | |
| # Disable php7.4-fpm | |
| systemctl disable php7.4-fpm | |
| # Remove at later date | |
| apt purge php7.4* |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env bash | |
| # This is for Ubuntu or Debian based systems | |
| # These must be set before running | |
| OLD_VER='' | |
| NEW_VER='' | |
| ESCAPED_OLD=${OLD_VER//./\\.} | |
| NEW_SEM_VER=( $(IFS="." echo "$NEW_VER") ) | |
| NEW_MAJOR=${NEW_SEM_VER[0]} | |
| NEW_MINOR=${NEW_SEM_VER[1]} | |
| NEW_REVISION=${NEW_SEM_VER[2]} | |
| # PHP Upgrade version | |
| # This can be used for other versions. | |
| # Just need to replace the from version and/or the new version | |
| if [[ $OLD_VER && $NEW_VER ]] | |
| then | |
| # Ubuntu | |
| # apt update | |
| # apt -y install software-properties-common | |
| # add-apt-repository ppa:ondrej/php | |
| # apt update | |
| # Debian | |
| apt update | |
| apt -y install lsb-release ca-certificates curl | |
| curl -sSLo /tmp/debsuryorg-archive-keyring.deb https://packages.sury.org/debsuryorg-archive-keyring.deb | |
| dpkg -i /tmp/debsuryorg-archive-keyring.deb | |
| sh -c 'echo "deb [signed-by=/usr/share/keyrings/debsuryorg-archive-keyring.gpg] https://packages.sury.org/php/ $(lsb_release -sc) main" > /etc/apt/sources.list.d/php.list' | |
| apt update | |
| if [[ $OLD_VER -eq '7.4' ]] | |
| then | |
| # Lists all packages installed in relation to php7.4 | |
| dpkg -l | grep php7 | cut -f3 -d' ' | xargs | sed "s/$OLD_VER/$NEW_VER/g" | |
| else | |
| # If upgrading to 8.x use the below as JSON is now builtin and mcrypt has been deprecated | |
| apt -y install $(dpkg -l | grep php$MAJOR | cut -f3 -d' ' | grep -v json | grep -v mcrypt | xargs | sed "s/$ESCAPED_OLD/$NEW_VER/g") | |
| fi | |
| update-alternatives --set php $(which php$NEW_VER) | |
| cp /etc/php/$OLD_VER/fpm/php.ini /etc/php/$NEW_VER/fpm/php.ini | |
| cp /etc/php/$OLD_VER/fpm/php-fpm.conf /etc/php/$NEW_VER/fpm/ | |
| cp /etc/php/$OLD_VER/fpm/pool.d/www.conf /etc/php/$NEW_VER/fpm/pool.d/ | |
| sed -i "s/$ESCAPED_OLD/$NEW_VER/g" /etc/php/$NEW_VER/fpm/php-fpm.conf | |
| sed -i "s/$ESCAPED_OLD/$NEW_VER/g" /etc/php/$NEW_VER/fpm/pool.d/www.conf | |
| # Update nginx virtual hosts to use php$NEW_VER | |
| sed -i "s/$ESCAPED_OLD/$NEW_VER/g" /etc/nginx/sites-enabled/* | |
| service php$NEW_VER-fpm restart | |
| service nginx reload | |
| # Stop php$OLD_VER | |
| service php$OLD_VER-fpm stop | |
| # Disable php$OLD_VER-fpm | |
| systemctl disable php$OLD_VER-fpm | |
| # Remove at later date | |
| apt -y purge php$OLD_VER* | |
| apt -y autoremove | |
| else | |
| echo "" | |
| echo "OLD_VER and NEW_VER need to be set before running this script" | |
| echo "" | |
| fi |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Default packages