How to Enable PHP in Apache on Fedora
When you install Apache (httpd) and PHP on a Fedora system, it is common for the web server to serve static files perfectly while treating your PHP scripts as plain text. If you create a test.php file containing <?php phpinfo(); ?> and see only the raw code in your browser instead of the formatted output, Apache simply isn't configured to process the .php extension. This usually means the necessary modules aren't loaded or the handler mapping is missing.
To resolve this on Fedora, you need to ensure that the libphp package provides a shared object that Apache can load. The most robust method involves using an IncludeOptional directive with specific module files rather than relying solely on generic glob patterns like conf.modules.d/*.conf. This approach gives you explicit control over which PHP components are active.
First, verify that the PHP module files exist in your configuration directory. On Fedora systems, these are typically located in /etc/httpd/conf.d/. You should look for a file named php.conf or similar, which contains directives to load the necessary modules. If this file is missing, you can create it manually. The core requirement is loading the mod_php module:
LoadModule php_module lib64/php/libphp.so
Once the module is loaded, you must enable the handler mapping for the .php extension. Add the following line to your configuration file or a dedicated include file within /etc/httpd/conf.d/:
AddType application/x-httpd-php .php
This directive tells Apache's MIME type handler that any request ending in .php should be processed by the PHP interpreter rather than served as static text. Without this, Apache defaults to serving files with text/plain or leaves them untouched if no specific handler is registered.
You also need to ensure that the directory allowing overrides permits PHP configuration changes. In your httpd.conf, you likely have a <Directory "/var/www/html"> block. As shown in your provided configuration, you have set AllowOverride None. If you intend to use .htaccess files to manage PHP settings later, change this to AllowOverride All or at least AllowOverride FileInfo. However, if you prefer managing everything centrally in the main config file (which is more performant), keep it as None and place all necessary directives in /etc/httpd/conf.d/php.conf.
Another critical detail involves the order of operations. Apache processes configuration files in a specific sequence. If your httpd.conf includes conf.modules.d/*.conf, ensure that any PHP-related modules are loaded before you attempt to define handlers for them. While modern Fedora versions often handle this automatically, explicit loading is safer.
Finally, after making these changes, restart the Apache service to apply them. On Fedora, run:
sudo systemctl restart httpd
If you encounter issues where PHP still displays as text, check the error logs located in /var/log/httpd/error_log. Look for messages indicating that the module failed to load or that the shared object file could not be found. In some cases, the path to libphp.so might differ slightly depending on your Fedora version (e.g., /usr/lib64/php/modules/ instead of lib64/php/libphp.so). Adjust the LoadModule directive accordingly.
By explicitly loading the PHP module and registering the .php extension handler, you bridge the gap between your web server and your scripts, allowing Apache to execute them correctly. This method ensures a clean configuration without relying on implicit defaults that can break easily during system updates.