PHP is a general-purpose scripting language well-suited for Web development since PHP scripts can be embedded into HTML. This guide explains how to install and configure PHP in an Ubuntu System with Apache2 and MySQL.
Table of Contents
Install PHP
PHP is available on Ubuntu Linux, but unlike Python (which comes pre-installed), must be manually installed.
To install PHP – and the Apache PHP module – you can enter the following command into a terminal prompt:
sudo apt install php libapache2-mod-php

Install Optional Packages
The following packages are optional and can be installed if you need them for your setup.
- PHP-CLI
You can run PHP scripts via the Command Line Interface (CLI). To do this, you must first install thephp-cli
package. You can install it by running the following command:
sudo apt install php-cli
PHP-CGI
You can also execute PHP scripts without installing the Apache PHP module. To accomplish this, you should install the php-cgi
package via this command:
sudo apt install php-cgi
PHP-MySQL
To use MySQL with PHP you should install the php-mysql
package, like so:
sudo apt install php-mysql
PHP-PgSQL
Similarly, to use PostgreSQL with PHP you should install the php-pgsql
package:
sudo apt install php-pgsql
Configure PHP
If you have installed the libapache2-mod-php
or php-cgi
packages, you can run PHP scripts from your web browser. If you have installed the php-cli
package, you can run PHP scripts at a terminal prompt.
By default, when libapache2-mod-php
is installed, the Apache2 web server is configured to run PHP scripts using this module. First, verify if the files /etc/apache2/mods-enabled/php8.*.conf
and /etc/apache2/mods-enabled/php8.*.load
exist. If they do not exist, you can enable the module using the a2enmod
command.

Once you have installed the PHP-related packages and enabled the Apache PHP module, you should restart the Apache2 web server to run PHP scripts, by running the following command:
sudo systemctl restart apache2.service
Test your setup
To verify your installation, you can run the following PHP phpinfo
script:
<?php
phpinfo();
?>
You can save the content in a file – phpinfo.php
for example – and place it under the DocumentRoot
directory of the Apache2 web hosting server. Pointing your browser to http://hostname/phpinfo.php
will display the values of various PHP configuration parameters.