If you're working with multiple PHP projects requiring different versions, it's essential to have the ability to switch between them easily. Ubuntu, a popular Linux distribution, can facilitate this with the help of the ondrej/php PPA, which provides access to multiple PHP versions ranging from 8.3 to 5.6. Here’s a comprehensive guide to help you through the installation process.
You must have sudo or root privileged account access on Ubuntu 20.04.
First of all, open a terminal and update the current packages with the following commands:
sudo apt update && sudo apt upgrade
Installing PHP on Ubuntu 20.04
PHP installation on Ubuntu systems is pretty straightforward. You just need to add the required PPA and you can install any PHP version on the Ubuntu system.
Follow these steps to complete PHP installation on Ubuntu:
- Install a few dependencies required by this tutorial with the below-mentioned command:
sudo apt install software-properties-common ca-certificates lsb-release apt-transport-https
- Add the Ondrej PPA to your system, which contains all versions of PHP packages for the Ubuntu systems.
LC_ALL=C.UTF-8 add-apt-repository ppa:ondrej/php
- Now, update the Apt package manager cache.
sudo apt update
- The SURY repository contains PHP 8.3, 8.2 8.1, 8.0, 7.4,
7.3, 7.2. 7.1, 7.0 & PHP 5.6. As the latest stable version of PHP is
8.0, but a large number of websites still required PHP 7. You can
install any of the required PHP versions on your system.
- Install PHP 8.3:
sudo apt install php8.3
- Install PHP 8.2:
sudo apt install php8.2
- Install PHP 7.4:
sudo apt install php7.4
- Install PHP 5.6 (EOL):
sudo apt install php5.6
Replace version 8.3, 8.2, 7.4, or 5.6 with the required PHP version to install on Ubuntu. Even you can install multiple PHP versions on a single Ubuntu system.
- Install PHP 8.3:
- Some applications required other PHP extensions, that can also be added using the below-mentioned syntax:
sudo apt install php7.4-[extension]
Replace [extension] with the extension you want to install, if you want to add multiple extensions then include them in braces, I am going to install “php-mbstring, php-mysql, php-xml, and php-curl” by running the below-mentioned command:
sudo apt install php7.4-mysql php7.4-mbstring php7.4-xml php7.4-curl
Users have installed different PHP version, need to replace
7.4
with required PHP versions.
Check Active PHP Version
To view the current active PHP version run the following command:
php -v
Ouput:PHP 7.4.29 (cli) (built: Apr 21 2022 10:16:36) ( NTS ) Copyright (c) The PHP Group Zend Engine v3.4.0, Copyright (c) Zend Technologies with Zend OPcache v7.4.29, Copyright (c), by Zend Technologies
Change Default PHP Version for CLI
You can use the update-alternatives command to set the default PHP version. Use this tutorial to read more details about switching PHP version for CLI and Apache.
sudo update-alternatives --config php
There are 4 choices for the alternative php (providing /usr/bin/php). Selection Path Priority Status ------------------------------------------------------------ 0 /usr/bin/php8.1 81 auto mode 1 /usr/bin/php5.6 56 manual mode 2 /usr/bin/php7.2 72 manual mode * 3 /usr/bin/php7.4 74 manual mode 4 /usr/bin/php8.1 81 manual mode Press to keep the current choice[*], or type selection number: 4
The above output shows all the installed PHP versions on your system. Selection number 4 set PHP 8.1 as the default PHP version for the command line.
Uninstall PHP
If any PHP version is no more required, can be removed from the system. That will free the disk space as well as system security.
To uninstall any PHP version just type:
sudo apt remove php5.6
Also, uninstall all the modules for that version with the following command:
sudo apt remove php5.6-*
How To Install MySQL on Ubuntu
To install it, update the package index on your server:
$ sudo apt update
$ sudo apt install mysql-server$
sudo systemctl start mysql.service
$
sudo mysql
$
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password';
$
exit
$
mysql -u root -p
$
ALTER USER 'root'@'localhost' IDENTIFIED WITH auth_socket;
$
sudo mysql_secure_installation
$
sudo mysql
$
mysql -u root -p
$
CREATE USER 'username'@'host' IDENTIFIED WITH authentication_plugin BY 'password';
$
CREATE USER 'sammy'@'localhost' IDENTIFIED BY 'password';
$
CREATE USER 'sammy'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password';
$
ALTER USER 'sammy'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password';
$
GRANT PRIVILEGE ON database.table TO 'username'@'host';
$
GRANT CREATE, ALTER, DROP, INSERT, UPDATE, INDEX, DELETE, SELECT, REFERENCES, RELOAD on *.* TO 'sammy'@'localhost' WITH GRANT OPTION;
$
GRANT ALL PRIVILEGES ON *.* TO 'sammy'@'localhost' WITH GRANT OPTION;
$
FLUSH PRIVILEGES;
$
exit
$ systemctl status mysql.service
How To Install VPS
# Open Terminal
$ ssh root@159.203.146.40
Comments from Facebook