Hello, This is Alex Gorzen from Liquid Web with our video on how to install PHP 7.2 on Ubuntu 16.04.
Here I’m starting with a vanilla install of Ubuntu Server 16 just with the default
LAMP stack enabled.
Make sure your user has sudo permissions or you use the root user itself.
First let's check to see what version the existing PHP install is.
Type php -v and it comes with PHP 7.0 by default.
If it shows "Command PHP not found" then you don't have any version installed.
If you have a live server that you host sites or applications from, now would be a good
time to make a backup of any existing settings before starting the upgrade.
In case there are issues then this would make downgrading easier.
So check your current configuration directory by running php --ini.
This defaults to /etc/php/7.0/cli folder, so we can make a quick backup of the whole
directory by running sudo cp -a /etc/php/7.0 /etc/php/7.0.backup or wherever else you'd
like to place it.
And to save a quick list of the existing modules run php -m > /tmp/7.0.modulelist.txt.
I'll show a command later to help you compare these previous modules with the new ones.
So to actually install PHP 7.2, you have to add a new repository.
The official release is from Ondrej Surý on launchpad.
To add it to your system run the command:
sudo add-apt-repository ppa:ondrej/php
press enter when it prompts you to, and after that finished run
sudo apt-get update
to have your system download a list of available programs it can install.
After that finishes, we can proceed with the install by running sudo apt-get install php7.2.
You can review what it installs and how much space it uses, and then type capital Y to proceed.
After a few moments the install should be complete, so check the PHP version with php -v,
and here it now shows me using PHP 7.2.
However, Apache doesn't know about the new version, so the next step is to change the
Apache configuration so it calls PHP 7.2.
Ubuntu 16.04 uses a few different commands to help manage Apache modules so the way it
utilizes a specific PHP version depends on which module is loaded.
You can view the list of available modules by running:
ls /etc/apache2/mods-available/php*
Here it shows just my 7.0 and 7.2 versions, so you may see other versions in there, but
to view which module is active, run ls /etc/apache2/mods-enabled/php*
and here it shows the 7.0 configuration is being loaded.
So to switch to the newer 7.2 version, run the disable module command:
sudo a2dismod php7.0
then enable 7.2 with:
sudo a2enmod php7.2
after that, let's have apache test the configuration syntax with:
apachectl -t
if that shows the syntax is OK, then restart apache with:
sudo service apache2 restart
So to check your work, rerun the previous ls command to view the enabled modules and
it should show 7.2 active.
If it's not showing it, recheck the spelling and syntax of the last few commands to check
for any mistakes.
Then to test it on your site, I would recommend making a phpinfo file you can view on your domain.
This shows the PHP version as well as information about modules and different variables.
If I create a new phpinfo.php file in my document root directory, which is /var/www/html by default,
then I just add the phpinfo code, <? phpinfo(); ?> save it, then when I try to view that file
in my browser.
I can see it shows the PHP 7.2 version!
The switch to PHP 7.2 doesn't automatically keep all the same modules from a previous
PHP version.
A quick way to install some basic modules is to type:
sudo apt-get install php7.2
But don't press enter, instead press the TAB key twice and it should give a list of available
modules specific to 7.2.
This is not a complete list so depending on the other modules you'll need, you may have
to look up how to install them separately.
So I’ll just choose to install mbstring here. sudo apt-get install php7.2-mbstring
So if you did save a list of modules from a previous PHP version with the command I
mentioned before, you can save another list of the 7.2 modules like:
php -m > /tmp/7.2.modulelist.txt
then compare the files.
You can use the diff command like this if diff /tmp/7.0.modules.txt /tmp7.2.moduleslist.txt or I like the vimdiff command to
get a better visual comparison. vimdiff /tmp/7.0.modules.txt /tmp7.2.moduleslist.txt
On the left is the 7.0 modules and on the right is the 7.2.
So for me I'm missing the mysql modules and soap, while I gained sodium, which is new
to 7.2 so I couldn't have had it before!
To exit vimdiff press the escape key, then :q! and then type it again to exit the second instance as well.
So now if I just install those missing mysql modules, sudo apt-get install php7.2-mysql update my list of modules in php
7.2 to that file again and running the vimdiff command again I see I have all the same old
ones I did before.
I'm now ready to go build my site!
So there was a quick guide for installing PHP 7.2 on Ubuntu 16.
Hopefully that helped!
If you or a friend are in the market for a web host, feel free to talk to a Liquid Web
tech by phone or in a chat 24 hours a day! Thank you for watching!
For more info, visit us at liquidweb.com/kb