- Published on
How to install/upgrade PHP on a server
- Author
- Illia Vasylevskyi
I'll document process of upgrading PHP from 8.1 to 8.3 on ubuntu operating system here.
First of all you need to have ppa added:
sudo add-apt-repository ppa:ondrej/php
sudo apt update
Then install common packages (I install fpm, using it behind nginx)
sudo apt install php8.3-fpm php8.3-cli php8.3-{bz2,curl,mbstring,intl}
Install other packages (choose what you need)
sudo apt install php8.3-{xdebug,redis,imagick,dev,gd,igbinary,imap,mysql,opcache,readline,soap,sqlite3,xml,xmlrpc,zip}
Test your new install with php -v
If you upgrading php configuration files for the new PHP 8.3 installation are at /etc/php/8.3 directory. Existing PHP installations should also be in the /etc/php directory. Do not copy existing PHP INI files to /etc/php/8.3. Instead, compare two corresponding PHP INI files and update the PHP 8.3 variant only when necessary.
phpenmod
and phpdismod
scripts provide continent toggles for PHP modules.
For example, the following disables the phar extension for FPM on PHP 8.3:
sudo phpdismod -v 8.3 -s fpm phar
After making changes, restart PHP 8.3-FPM:
sudo systemctl restart php8.3-fpm
Removing old version
To remove old PHP versions, run apt purge with the PHP version prefix. For example, the following removes the packages and configuration for PHP 8.2:
sudo apt purge php8.2*
Running different versions of php
The default php name will be symlinked to the latest PHP version by default, but it is possible to change to where the default php command links. Use update-alternatives command to change the path to which php command should be linked.
sudo update-alternatives --config php
Or without interactive prompt
update-alternatives --set php /usr/bin/php8.2
Also you can use /usr/bin/php8.2
directly instead of php
And for composer if you are not using local phar
/usr/bin/php8.2 /usr/local/bin/composer update
To tell nginx too look at version of php just point it to correct socket:
fastcgi_pass unix:/var/run/php/php8.3-fpm.sock;