Alterar a versão padrão do PHP
How to change the PHP version you’re using
If you have multiple PHP versions installed on your Ubuntu server, you can change what version is the default one.
To set PHP 7.0 as the default, run:
update-alternatives –set php /usr/bin/php7.0
To set PHP 7.2 as the default, run:
update-alternatives –set php /usr/bin/php7.2
To set PHP 7.3 as the default, run:
update-alternatives –set php /usr/bin/php7.3
If you’re following our LAMP tutorials and you’re using Apache, you can configure Apache to use PHP 7.2 with the following command:
a2enmod php7.2
And then restart Apache for the changes to take effect:
systemctl restart apache2
How to upgrade to PHP 7.2 or 7.3 on Ubuntu
If you’re already using an older version of PHP with some of your applications, you can upgrade by:
sudo a2dismod php7.0
sudo a2enmod php7.1
sudo systemctl restart apache2
Install PHP 7.2 modules
You may need additional packages and modules depending on your applications. The most commonly used modules can be installed with the following command:
apt-get install php-pear php7.2-curl php7.2-dev php7.2-gd php7.2-mbstring php7.2-zip php7.2-mysql php7.2-xml
Backup everything.
Install the newest PHP and required modules.
Change the default version you’re using.
(Optionally) Remove the older PHP
(Required) Configure your software to use the new PHP version. You’ll most likely need to configure Nginx/Apache, and many other services/applications. If you’re not sure what you need to do, contact professionals and let them do it for you.
Speed up PHP by using an opcode cache
You can improve the performance of your PHP by using a caching method. We’ll use APCu, but there are other alternatives available.
If you have the ‘php-pear’ module installed (we included it in our instructions above), you can install APCu with the following command:
pecl install apcu
There are also other ways you can install APCu, including using a package.
To start using APCu, you should run the following command for PHP 7.2:
echo “extension=apcu.so” | tee -a /etc/php/7.2/mods-available/cache.ini
Or this command for PHP 7.3:
echo “extension=apcu.so” | tee -a /etc/php/7.3/mods-available/cache.ini
And the following command for PHP 7.0:
echo “extension=apcu.so” | tee -a /etc/php/7.0/mods-available/cache.ini
If you’re following our LAMP tutorials and you’re using Apache, create a symlink for the file you’ve just created.
For PHP 7.2:
ln -s /etc/php/7.2/mods-available/cache.ini /etc/php/7.2/apache2/conf.d/30-cache.ini
For PHP 7.3:
ln -s /etc/php/7.3/mods-available/cache.ini /etc/php/7.3/apache2/conf.d/30-cache.ini
For PHP 7.0:
ln -s /etc/php/7.0/mods-available/cache.ini /etc/php/7.0/apache2/conf.d/30-cache.ini
And finally, reload Apache for the changes to take effect:
systemctl restart apache2
To further configure APCu and how it works, you can add some additional lines to the cache.ini file you previously created. The best configuration depends on what kind of server you’re using, what applications you are using etc. Either google it and find a configuration that works for you, or contact professionals and let them do it for you.
That’s it for our basic setup. Of course, there are much more options and configurations you can do, but we’ll leave them for another tutorial.