How to Fix ‘PHP Source Code Displayed Instead of Rendering’ Issue on AlmaLinux Apache Server

PHP

If you are running an AlmaLinux server with Apache and PHP (including php-fpm) and encountering the issue where the source code of your PHP files is displayed in the browser instead of executing the PHP script, follow the steps below to resolve it.

1. Ensure PHP is Properly Installed and Configured

Start by verifying that PHP is correctly installed on your server. You can check the PHP version by running the command php -v in your terminal. Ensure that the correct version of PHP is installed and that PHP-FPM is properly configured.

2. Verify Apache and PHP Integration

If PHP is installed but not integrated with Apache, the server will serve PHP files as plain text. To fix this, ensure that Apache is configured to handle PHP files. The most common solution is to ensure that Apache uses the correct PHP handler, either through mod_php or php-fpm for PHP processing.

3. Check Apache Configuration

In the Apache configuration file (usually located at /etc/httpd/conf/httpd.conf), make sure that the following lines are included:

LoadModule php_module modules/libphp.so

Also, ensure the following directive is set to allow PHP scripts to run:

AddHandler application/x-httpd-php .php

Restart Apache after making these changes using:

sudo systemctl restart httpd

4. Verify PHP-FPM Configuration

If you are using PHP-FPM, ensure that it is running and correctly linked with Apache. Check the status of PHP-FPM by running:

sudo systemctl status php-fpm

If it is not running, start it with:

sudo systemctl start php-fpm

Additionally, check your php-fpm configuration file (usually located at /etc/php-fpm.d/www.conf) to make sure Apache is properly passing PHP requests to PHP-FPM.

5. Test with a PHP File

Once the configuration is complete, test the PHP setup by creating a simple phpinfo() file in your web directory (e.g., /var/www/html/index.php) with the following content:

Visit this file through your browser (http://your-server-ip/index.php) to ensure that PHP is being processed correctly.

Conclusion

If you still encounter issues after following these steps, ensure that your firewall or SELinux is not blocking the necessary ports for PHP-FPM to function correctly with Apache.

コメント

タイトルとURLをコピーしました