PHP installation is required for command line use only Follow instructions at noted here Recent versions of Mac OS already ship with php7.2.x+ Install with root privileges On Debian/Ubuntu see article
here On CentOS Building minimal PHP from source Download latest PHP7.2.x then unzip and build as follows After installing PHP, add php to your environment path for e.g. append the following to your
~/.bashrcA. Windows
B. Mac OS
C. Linux
Add environment path
Introduction
PHP stands for Hypertext Preprocessor, and it’s a script-based server-side programming language.
PHP is often used to automate server tasks. It handles tasks like dynamic content, database requests, and processing and displaying data.
Read our tutorial on how to install PHP 7.4 and 8.0 on Ubuntu 18.04 or 20.04 including integration with Apache and Nginx.
Prerequisites
- A Linux system running Ubuntu 18.04 or 20.04
- Access to an Ubuntu user account with sudo privileges
- Access to a command line/terminal window (Ctrl-Alt-T)
- A running web server (Apache or Nginx)
Installing PHP on Ubuntu
Like many developer tools, PHP has several different release versions. At the time of writing, PHP 7.4. and 8.0 are the currently supported versions of the software.
1. Install PHP with Apache on Ubuntu
If you are running an Apache web server, you want to install PHP with the Apache module. Below you will find installations for PHP 7.4 and PHP 8.0.
1. Ensure you are using the latest Ubuntu updates by entering the following command into a terminal window:
sudo apt update && sudo apt upgrade2. Install software-properties-common to help you manage distributions and independent software sources:
sudo apt install software-properties-common3. Next, add the ondrej/php PPA which provides all the latest releases of PHP for Ubuntu 18.04:
sudo add-apt-repository ppa:ondrej/php4. Update the repository to include the new packages:
5. Now you can install PHP.
- To install PHP 7.4, run the command:
- To install PHP 8.0, run:
Note: The command for installing PHP 8.0 should automatically install the PHP module for Apache 2. If it fails to do so, run the following command instead: sudo apt -y install php8.0 libapache2-mod-php8.0.
6. Verify the installation with:
php -vThe output should display the PHP version you installed. If you installed version 7.4, the output appears as in the image below.
If you installed PHP 8.0, the output shows:
2. Install PHP with Nginx on Ubuntu
If you are using an Nginx server instead of Apache, follow the steps below to install PHP 7.4 or 8.0 to work with Nginx.
- Enter the following command into a terminal window to ensure you are using the latest software:
. 2. To install PHP for Nginx, use one of the following commands:
- For PHP 7.4:
- For PHP 8.0:
The system will reach out to download and install the package and its dependencies.
2. Once the installation finishes, restart the service to apply the changes by entering:
sudo systemctl restart nginx3. Next, you need to enable PHP support by editing the server block. Open the server block with the command:
sudo nano /etc/nginx/sites-available/default4. Add the following code to your server block file for Nginx to make use of PHP:
server
4. Save the file and exit.
5. Finally, restart Nginx on Ubuntu and reload PHP:
sudo systemctl restart nginxsudo systemctl reload php[version_number]-fpmInstalling PHP Modules on Ubuntu
To install additional PHP modules, use the following syntax:
sudo apt install php[version_numeber]-[package_name]You can also install multiple modules at once. For example, to install modules mysql, zip, json, common, and bcmath on PHP 7.4, you would run:
sudo apt install php7.4-{mysql,zip,json,common,bcmath}To list all loaded PHP modules run the command:
php -mThe output lists all compiled PHP modules, as in the example below.
Conclusion
After reading this article, you should have successfully installed PHP 7.4 or 8.0 with Apache or Nginx on your Ubuntu 18.04 and 20.04 system.
One important thing to remember is that PHP works in tandem with an existing server, so you need to install it specifically to your server software. Alternatively, you can always install a stable version from the default Ubuntu software repositories.