Understanding Apache mods-available and mods-enabled
When managing a PHP setup on an Apache server, you will almost certainly encounter a directory structure split between mods-available and mods-enabled. If you find yourself in the scenario described where the php5 module exists in mods-available but is missing from mods-enabled, it usually means the module was installed but never activated for the current virtual host or the main server configuration. This is a common friction point when taking over a legacy deployment or dealing with unresponsive previous administrators who have left configurations in an inconsistent state.
The fundamental difference between these two directories lies in how Apache handles them at runtime. Files residing in mods-available serve as templates or definitions of potential modules. They are not automatically loaded into the process. Conversely, files in mods-enabled are symbolic links pointing to their counterparts in mods-available. Apache's configuration loader specifically scans the mods-enabled directory during initialization; it does not read from mods-available directly. Therefore, moving a file manually or copying it over is the wrong approach. The correct mechanism is using the a2enmod command, which handles the symbolic linking logic safely and consistently.
To enable PHP specifically for your server configuration, execute the following command in your terminal:
sudo a2enmod php
Note that while the source material mentions php5, modern Ubuntu/Debian distributions typically bundle PHP under a generic php package name with version-specific subdirectories (like php7.4 or php8.1) depending on the specific FPM handler and module versions installed. If you strictly need to enable an older php5 module, the command would be sudo a2enmod php5, but this is increasingly rare in current environments due to security patches ending for PHP 5.x years ago. Always verify the exact package name using ls /etc/apache2/mods-available/ before running the enable command.
Once the link is created, Apache will pick up the module only on its next restart or reload cycle. You can apply the changes immediately with:
sudo systemctl reload apache2
# or on some systems
sudo service apache2 reload
If you encounter an error stating that the module could not be enabled because it is already disabled, or if you are using a specific virtual host configuration file (e.g., /etc/apache2/sites-available/default.conf), you might need to ensure that LoadModule directives are explicitly present in your main config file (/etc/apache2/apache2.conf). In many standard distributions, the modular setup handles this automatically via the .load files generated when you run a2enmod. However, if you are maintaining a custom Apache installation or a Docker container where the standard a2enmod script doesn't exist, you must manually create a symlink:
sudo ln -s /etc/apache2/mods-available/php.load /etc/apache2/mods-enabled/
After making these changes, verify that Apache is serving PHP correctly by creating a test file in your web root (e.g., info.php) containing <?php phpinfo(); ?>. If you see the full configuration details without HTML errors, the module is active.
A critical pitfall to avoid is disabling PHP globally across the entire system if only specific applications need it, or conversely, forgetting to enable it for a new virtual host after cloning an existing one. Since another developer previously disabled it, check if they left a custom mod_php configuration that conflicts with other handlers like libphp8-fpm. In shared hosting or containerized environments, ensuring the correct PHP version matches the Apache handler is essential to prevent runtime crashes or 500 Internal Server Errors. If you are deploying Laravel applications, ensure your .htaccess files are configured correctly if using mod_rewrite alongside the enabled PHP module, though this often requires switching to mod_php specifically for simpler setups unless you have a dedicated PHP-FPM pool listening on a specific socket.