Skip to content

(Resolved) Apache Displaying Plain-text PHP Code

Advertisement

When setting up a web server, you might run into a problem where your browser shows the raw PHP code instead of the web page. This usually means your server isn’t processing PHP files correctly. Let’s look at why this happens and how to fix it on Debian and RHEL-based systems.

Why Does This Happen?

This problem often occurs because:

  • The PHP module isn’t installed or configured correctly with Apache.
  • The .php file isn’t linked to the PHP module in the Apache configuration.
  • There’s a mistake in your .htaccess file.

1. Installing and Configuring PHP

First, make sure PHP and the Apache PHP module are installed.

  • On Debian-based Systems (e.g., Ubuntu):
    sudo apt update
    sudo apt install php libapache2-mod-php
    
  • On RHEL-based Systems (e.g., CentOS, Fedora):
    sudo yum install php php-common
    sudo yum install php-fpm
    
  • Enable PHP in Apache: For both systems, make sure the PHP module is enabled:
    sudo a2enmod phpX.X
    

    Replace X.X with your PHP version, like 8.2.

2. Check Apache Configuration for PHP Files

Your Apache configuration should tell the server to process .php files with the PHP module.

  • On Debian-based Systems: Open the Apache configuration file:
    sudo nano /etc/apache2/apache2.conf
    

    Make sure it includes:

    
    <FilesMatch ".+\.php$">
        SetHandler application/x-httpd-php
    </FilesMatch>
    
    
  • On RHEL-based Systems: Open the Apache configuration file:
    sudo nano /etc/httpd/conf/httpd.conf
    

    Make sure it includes:

    
    <FilesMatch ".+\.php$">
        SetHandler application/x-httpd-php
    </FilesMatch>
    
    

3. Verify .htaccess File

If you use an .htaccess file, check for directives that might stop PHP files from running correctly. Look for lines like php_value or php_flag and make sure they are set correctly or temporarily comment them out to test.

4. Restart Apache

After making changes, always restart Apache.

  • On Debian-based Systems:
    sudo systemctl restart apache2
    
  • On RHEL-based Systems:
    sudo systemctl restart httpd
    

5. Double Check File Extensions

Ensure your PHP files have the .php extension. Apache only processes files as PHP if they have the correct file extension.

6. Check for Errors

Look at the Apache error logs for clues:

  • On Debian-based Systems:
    sudo tail /var/log/apache2/error.log
    
  • On RHEL-based Systems:
    sudo tail /var/log/httpd/error_log
    

Conclusion

If your browser shows raw PHP code instead of the web page, it can be frustrating. But by checking if PHP is installed properly, verifying Apache’s configuration, and looking into the .htaccess file, you can fix this issue and make sure your server processes PHP files correctly.

Rahul

I, Rahul Kumar am the founder and chief editor of TecAdmin.net. I am a Red Hat Certified Engineer (RHCE) and working as an IT professional since 2009..

Leave a Reply

Your email address will not be published. Required fields are marked *


Back to top