Enable PHP in Apache2

Habdul Hazeez Jun 30, 2022
  1. Enable PHP in Apache2 Using a2enmod
  2. Enable PHP in Apache2 With LoadModule
  3. Enable PHP in Apache2 by Creating a Symbolic Link
Enable PHP in Apache2

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:

sudo a2enmod phpX.X

Here, X.X is the current version of PHP.

Now, if you have PHP5, you can do the following:

  1. Open your terminal.
  2. Type sudo a2enmod php5 to enable PHP5.
  3. 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

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 avatar Habdul Hazeez avatar

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.