Rostyslav Rava - Personal Website

How to configure PHP with Apache on Ubuntu

While PHP can be installed through the php-cli package and used as a command line interpreter as shown in the post about installing the latest PHP on Ubuntu, its primary purpose is to serve as a Hypertext Preprocessor for web applications. To do this, it needs to work together with web servers like Apache or Nginx. This post provides a step-by-step guide on configuring PHP to work with Apache web server.

This guide assumes that the repositories for PHP and Apache maintained by Ondřej Surý are configured and Apache is already installed, as described in previous posts related to PHP installation and Apache installation.

PHP integrates with Apache through the libapache2-mod-php package, which is automatically installed as a dependency when installing the main php metapackage. The command below installs PHP 8.5 with Apache integration, though the version number can be adjusted as needed.

sudo apt install php8.5

This will install the libapache2-mod-php8.5 package along with other PHP components.

Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following additional packages will be installed:
  libapache2-mod-php8.5 php-common php8.5-cli php8.5-common php8.5-readline
Suggested packages:
  php-pear
The following NEW packages will be installed:
  libapache2-mod-php8.5 php-common php8.5 php8.5-cli php8.5-common php8.5-readline
0 upgraded, 6 newly installed, 0 to remove and 0 not upgraded.
Need to get 6965 kB of archives.
After this operation, 32.5 MB of additional disk space will be used.
Do you want to continue? [Y/n]

After pressing ‘Y’, PHP along with the module required for Apache integration will be installed.

Alternatively, it can be installed with the ‘-y’ parameter to avoid confirmation.

sudo apt -y install php8.5

After installation, the PHP module enablement and Apache restart are done automatically. If the module is not enabled automatically, it can be enabled manually using the command below:

sudo a2enmod php8.5

If the module is already enabled, the output will be like the following:

Considering dependency mpm_prefork for php8.5:
Considering conflict mpm_event for mpm_prefork:
Considering conflict mpm_worker for mpm_prefork:
Module mpm_prefork already enabled
Considering conflict php5 for php8.5:
Module php8.5 already enabled

If the module was previously disabled, the output will be like the following:

Considering dependency mpm_prefork for php8.5:
Considering conflict mpm_event for mpm_prefork:
Considering conflict mpm_worker for mpm_prefork:
Module mpm_prefork already enabled
Considering conflict php5 for php8.5:
Enabling module php8.5.
To activate the new configuration, you need to run:
  systemctl restart apache2

After enabling the module, Apache must be restarted:

sudo systemctl restart apache2

The module can be verified as loaded by checking Apache’s loaded modules:

apache2ctl -M | grep php

The output should show php_module.

$ apache2ctl -M | grep php
 php_module (shared)

To test configuration, a test PHP file needs to be created.

sudo vim /var/www/html/info.php

In Vim, press Ins to enter insert mode, add the following content, then press Esc followed by :wq to save and quit.

<?php
phpinfo();

After saving the file, it will become available to show information about PHP through the URL http://localhost/info.php on the local machine, or on a remote machine by replacing localhost with the actual IP address or domain name. As a response, the PHP information page should be displayed with a title like “PHP Version 8.5.1” showing the exact version of PHP installed and all the details about it.

The phpinfo() function exposes detailed information about the PHP configuration. After verifying that PHP is working correctly, the info.php file should be removed for security reasons:

sudo rm /var/www/html/info.php

By following these steps, PHP and Apache can be integrated with each other on an Ubuntu machine.

Tags: