Skip to content

Instantly share code, notes, and snippets.

@mrevjd
Last active November 18, 2025 22:16
Show Gist options
  • Select an option

  • Save mrevjd/0b582fea40078e4d1ad4182c1b097271 to your computer and use it in GitHub Desktop.

Select an option

Save mrevjd/0b582fea40078e4d1ad4182c1b097271 to your computer and use it in GitHub Desktop.
Update php to 8.x
# 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*
#!/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
@mrevjd
Copy link
Author

mrevjd commented Aug 9, 2022

Default packages

apt -y install \
php8.3 \
php8.3-cli \
php8.3-common \
php8.3-curl \
php8.3-fpm \
php8.3-gd \
php8.3-igbinary \
php8.3-intl \
php8.3-mbstring \
php8.3-memcached \
php8.3-msgpack \
php8.3-mysql \
php8.3-opcache \
php8.3-pgsql \
php8.3-readline \
php8.3-sqlite3 \
php8.3-xml \
php8.3-xsl \
php8.3-zip

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