Linux Apache not executing php files: Solution

If you are running an Apache web server on your Linux system and find that PHP files are not being executed, there are a few potential solutions that can help with this problem. PHP is a common component of web server setups, and is the programming langauge used by WordPress, the most popular content management system in the world. It is commonly installed alongside Apache and MySQL, which is known as LAMP (Linux, Apache, MySQL, PHP).

In this tutorial, we will show how to enable Apache to execute PHP files on Linux. First, we will verify that PHP has been successfully installed on your system, then make sure that the necessary modules have been enabled for Apache to recognize the PHP format. Follow along with us below to start the troubleshooting process and get PHP working with Apache.

In this tutorial you will learn:

  • How to install PHP on Debian and RHEL based distros
  • How to check PHP info for successful installation
  • How to edit the apache2.conf file
  • How to enable the necessary PHP Apache modules
  • How to restart the Apache system service
Linux Apache not executing php files: Solution
Linux Apache not executing php files: Solution
Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Any Linux distro
Software Apache web server, PHP
Other Privileged access to your Linux system as root or via the sudo command.
Conventions # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – requires given linux commands to be executed as a regular non-privileged user

Linux Apache not executing php files: Solution

The steps below run through multiple troubleshooting methods to figure out what the problem is with Apache not executing PHP files. Keep checking after each step to see if your Apache configuration is now executing PHP files.

  1. Installing Apache on your system does not necessarily install PHP. Ensure that PHP is already installed and updated to the latest version. Here is how on Debian Linux and Ubuntu Linux:
    $ sudo apt update
    $ sudo apt install php

    And here is how to install PHP on RHEL based distros like Fedora Linux and CentOS Linux:

    $ sudo dnf install php
  2. Verify that your installation is configured correctly by opening an interactive PHP prompt and using the phpinfo(); command. A successful execution is usually indication enough, but you can also look through the output for any errors.
    $ php -a
    Interactive shell
    php > phpinfo();
    phpinfo command inside PHP interactive shell
    phpinfo command inside PHP interactive shell
  3. If PHP is installed successfully, make sure that you have disabled the mpm_event Apache module and enabled the mpm_prefork and php8.1 modules.
    $ sudo a2dismod mpm_event
    $ sudo a2enmod mpm_prefork
    $ sudo a2enmod php8.1

    Depending on your configuration, you may also need to execute:

    $ sudo a2enmod proxy_fcgi setenvif
    $ sudo a2enconf php8.1-fpm
    We have enabled the php8.1 and php8.1-fpm modules above, but you will need to adapt this command to match your currently installed version of PHP. For example, if you have version 7.4 installed, the command would be sudo a2enmod php7.4. Check your installed version of PHP with the command: php -v.
  4. After making the changes above, you will need to restart Apache for the changes to take effect:

    On RHEL based distros, Arch Linux, and OpenSUSE:
    $ sudo systemctl restart httpd
    On Debian and Ubuntu based distros:
    $ sudo systemctl restart apache2

The steps above cover the most common issues and should enable your Apache web server to begin executing PHP files. If you still encounter the problem, it most likely is an issue with your PHP files themselves. Try generating a phpinfo document to see if it displays correctly on your website. If it does, then the problem lies with your own PHP files.

In recent versions of PHP, short tags like <? are no longer recognized and you must use <?php to mark the beginning of your PHP code. Make sure that your PHP files comply with this new change, among others.

Closing Thoughts

In this tutorial, we saw how to solve the problem of Apache not executing PHP files on a Linux system. The simplest fix is to install or upgrade the installed PHP version. Even with PHP installed, sometimes Apache may not automatically enable the necessary modules to allow it to read and execute PHP files. After configuring the proper modules and restarting Apache, it will now be able to execute PHP files. If you still have a problem, try inspecting your PHP files for errors and do further troubleshooting with a phpinfo page as linked above.

Comments and Discussions
Linux Forum