PHP Code displayed on browser – Linux

Is your PHP code also being displayed on your browser instead of Apache executing it? Well, you are not alone! This generally happens due to some misconfiguration or if PHP is not present. Don’t worry follow the steps given below and you will be able to solve it very soon.

Step 1: Check if PHP is installed.

First of all, check if PHP is installed in your system by running the below command on the terminal:

php -v

It should print something like this on the terminal.

PHP 7.2.10-0ubuntu0.18.04.1 (cli) (built: Sep 13 2018 13:45:02) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies
with Zend OPcache v7.2.10-0ubuntu0.18.04.1, Copyright (c) 1999-2018, by Zend Technologies

Having a different version of PHP installed in your system is not an issue, but if you see PHP is not installed in your system then install PHP by running the following command:

sudo apt-get install php libapache2-mod-php php-mysql

Step 2: Enabling PHP module

If PHP is already installed in your system but still you are facing this issue then it could be because it might not be enabled in your server configuration. Run the given command and then restart your apache server. Don’t forget to replace the PHP version in the command with the version you have installed in your system.

sudo a2enmod php7.2 

Considering dependency mpm_prefork for php7.2:
Considering conflict mpm_event for mpm_prefork:
Considering conflict mpm_worker for mpm_prefork:
Module mpm_prefork already enabled
Considering conflict php5 for php7.2:
Enabling module php7.2.
To activate the new configuration, you need to run:
  systemctl restart apache2

Restart Apache Server after enabling PHP.

sudo service apache2 restart

Now, test your localhost URL and it should work.

Spread the Knowledge

Leave a Reply

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