- How to install and configure PHP
- Prerequisites
- Install PHP
- Install optional packages
- Configure PHP
- Test your setup
- Further reading
- Enable PHP in Apache2
- Enable PHP in Apache2 Using a2enmod
- Enable PHP in Apache2 With LoadModule
- Enable PHP in Apache2 by Creating a Symbolic Link
- Installing PHP Module for Apache on Ubuntu
- Installing Apache
- Installing PHP
- Install Apache PHP Module
How to install and configure PHP
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.
Prerequisites
Before installing PHP you should install Apache (or a preferred web server) and a database service such as MySQL.
- To install the Apache package, please refer to our Apache guide.
- To install and configure a MySQL database service, refer to our MySQL guide.
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 the php-cli package. You can install it by running the following command:
sudo apt install php-mysql
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:
You can save the content in a file – phpinfo.php for example – and place it under the DocumentRoot directory of the Apache2 web server. Pointing your browser to http://hostname/phpinfo.php will display the values of various PHP configuration parameters.
Further reading
- For more in depth information see the php.net documentation.
- There are a plethora of books on PHP 7 and PHP 8. A good book from O’Reilly is Learning PHP, which includes an exploration of PHP 7’s enhancements to the language.
- Also, see the Apache MySQL PHP Ubuntu Wiki page for more information.
Enable PHP in Apache2
- Enable PHP in Apache2 Using a2enmod
- Enable PHP in Apache2 With LoadModule
- Enable PHP in Apache2 by Creating a Symbolic Link
This article will teach you how to enable PHP in Apache2 using a2enmod , LoadModule , and a symbolic link. If you get a module error about PHP, we will teach you how apt-get can fix it.
Enable PHP in Apache2 Using a2enmod
To enable PHP with a2enmod , you will need to type the command:
Here, X.X is the current version of PHP.
- Open your terminal.
- Type sudo a2enmod php5 to enable PHP5.
- Type sudo service apache2 reload .
The last command will reload the Apache2 configuration. However, if you have other PHP versions like PHP7 or PHP8.1, you can use either the following to enable PHP:
sudo a2enmod php7 sudo a2enmod php8.1
After each command, ensure you reload the Apache2 configuration using sudo service apache2 reload . Meanwhile, if you get an error that the PHP module does not exist, install the module for your current PHP.
The following will do that. Do not forget to replace X.X with your PHP version number.
apt-get install libapache2-mod-phpX.X
Enable PHP in Apache2 With LoadModule
The LoadModule will allow you to add your PHP to the list of active modules. You can do this by setting the absolute path of your PHP module file in httpd.conf .
The following is how you do it for PHP5.x, PHP7.x, and PHP8.x. The X is your PHP version number, and /path/to/mods-available/ is the directory of mods-available .
# For PHP5.x LoadModule php5_module /path/to/mods-available/libphpX.so # For PHP7.x LoadModule php7_module /path/to/mods-available/libphpX.so # For PHP8.x LoadModule php_module /path/to/mods-available/libphpX.so
Enable PHP in Apache2 by Creating a Symbolic Link
With ln -s , you can create a symbolic link from the mods-available directory to mods-enabled . This allows you to use the PHP in the mods-enabled directory.
The following is how you create a symbolic link based on your PHP version. Replace path/to/mods-available/ and path/to/mods-enabled/ based on your system.
# For PHP5.x, PHP7.x, PHP8.x # X is your PHP version number ln -s /path/to/mods-available/libphpX.so /path/to/mods-enabled/libphpX.so
Habdul Hazeez is a technical writer with amazing research skills. He can connect the dots, and make sense of data that are scattered across different media.
Installing PHP Module for Apache on Ubuntu
Apache Web Server is one of the more dominant web servers on the net. Most of those installations are running some form of PHP applications, whether Laravel, WordPress, or some other custom application.
Apache was originally used as a static web server, hosting HTML files, for example, directly the client. However, due to its support for modules the web servers capabilities have grown to run as an application server as well.
Out of the box, a default Apache2 web server installation on Ubuntu 18.04, 18.10, 19.04, and 19.10 will not run a PHP application. The PHP module is not included.
This tutorial will show you how to configure an Apache web server for running PHP applications.
Installing Apache
To install Apache on your Ubuntu server, run the following command.
Installing PHP
Install PHP is simply enough. It can Ben installed using the Apt package manager from Ubuntu’s repositories.
PHP can also be installed from source, if you require a more recent version. That task, unfortunately, is outside of the scope of this tutorial.
To install PHP, run the following command:
Additional PHP modules can be installed with the language itself.
Install Apache PHP Module
The PHP module for Apache is not bundled with Apache. As such, it must be installed in addition to the Apache package.
sudo apt install libapache2-mod-php
Once installed the module will have to be enabled. We accomplish this using the a2enmod command.
Lastly, the Apache web service requires a reboot before the enabled mod will be loaded.
sudo systemctl restart apache2