How To Upgrade To PHP7.2 in Ubuntu

Check your PHP version:

$ php -v

1. Add PPA

$ sudo add-apt-repository ppa:ondrej/php
$ sudo apt update

2. Save current PHP packages

Note down the current PHP packages you have, so we can reinstall them for PHP 7.2

$ dpkg -l | grep php | tee packages.txt

This will save your current packages to packages.txt file in your working directory. Note: If working in a local dev environment using Vagrant, I just changed into my sync'd directory at /var/www/html and then ran the code above so that I can easily view the packages.txt file.

3. Install PHP 7.2

$ sudo apt install php7.2 php7.2-common php7.2-cli php7.2-fpm

This will install the bare basic packages you'd need to get started with PHP 7.2.

4. Install additional modules

Take a look at the packages.txt file we created at step #2, and install the additional PHP packages. Your packages.txt file will show packages such as php7.0-mysql, and you need to install their PHP 7.2 counterpart (php7.2-mysql for example). Example below:

$ sudo apt install php7.2-curl php7.2-gd php7.2-mysql php7.2-gd php7.2-imagick php7.2-mbstring php7.2-common php7.2-opcache php7.2-imap php7.2-intl php7.2-json php7.2-readline php7.2-xml php7.2-zip

5. Web server configuration

Make sure that your web server correctly uses the PHP 7.2 sockets/modules. Edit your nginx config to change the socket paths (substituting ‘nginx_vhost' with your correct filename, i.e. ‘default'):

$ sudo nano /etc/nginx/sites-available/nginx_vhost

For example, change the lines below from php7.0 to php7.2:

...
fastcgi_pass unix:/run/php/php7.2-fpm.sock;

...

# Phpmyadmin Configurations
...
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
...

Ctrl+x, then ‘y' to save and exit. Restart PHP and Nginx:

$ sudo systemctl restart php7.2-fpm
$ sudo service nginx restart

6. Change the default PHP version

If you have multiple PHP versions installed on your Ubuntu server, you can set PHP 7.2 as the default:

$ sudo update-alternatives --set php /usr/bin/php7.2

7. Remove old versions (Optional)

If everything is working you can remove the old packages (change php7.0 to whatever old versions you no longer need):

$ sudo apt purge php7.0*

Verify php7.2

$ php -v

ERRORS & SOLUTIONS

AFTER REMOVING OLD VERSIONS OF NGINX IN STEP 7 I GOT A 502 BAD GATEWAY. I rebooted the server and everything worked fine.

REFERENCES

https://ayesh.me/Ubuntu-PHP-7.2
https://thishosting.rocks/install-php-on-ubuntu/

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.