PHP files displayed as text

8,993

Solution 1

Usually, when I do installations of apache and PHP on ubuntu it works for me seamlessly but this time I had to use this before my PHP files stopped appearing as plaintext.

sudo apt install libapache2-mod-php7.4 

Solution 2

There are a couple of reasons why your PHP may not get interpreted.

  1. You may not have the PHP module for Apache installed or running.

To check the PHP packages installed, type: dpkg -l | grep php

To check which apache modules are installed, type: a2query -m. There should be a PHP module listed and it should be enabled.

You should see libapache2-mod-php listed among the packages. It could be named libapache2-mod-php7.2 or something similar to match your version of PHP.

If it is not present, you will have to install it.

  1. Apache may need to be configured to execute .php files. To quickly check your Apache configuration, type: grep -r php /etc/apache2/*.

You should see SetHandler application/x-httpd-php somewhere, and be sure it is not commented.

Share:
8,993
adamanyn
Author by

adamanyn

Updated on September 18, 2022

Comments

  • adamanyn
    adamanyn almost 2 years

    When I want to execute a php file it's displayed as text in my browser.

    I have a VPS at Hostinger I installed Ubuntu 18.04 with Webmin and LAMP(installed automatically).

    I checked the installation see below.

    apache2 - v and php -v gives me: Apache/2.4.29 and PHP 7.2.24-0ubuntu0.18.04.6

    So everything seem to be installed correctly.

    My default folder is /var/www/html/ I do not us short tag in my code.

    I try to lunch a file called phpinfo.php with content:

    <?php
    phpinfo();
    ?>
    

    And plain text appear on the page.

    If the file begin with html code it's correctly rendered, even with a .php file. But the php part is not executed. e.g:

    <!DOCTYPE html>
    <html lang="en" dir="ltr">
      <head>
        <meta charset="utf-8">
        <title></title>
      </head>
      <body>
        <h1>Hello</h1>
      </body>
    </html>
    
    <?php
    phpinfo();
    ?>
    

    Render : Hello

    But

    <?php
    phpinfo();
    ?>
    
    <!DOCTYPE html>
    <html lang="en" dir="ltr">
      <head>
        <meta charset="utf-8">
        <title></title>
      </head>
      <body>
        <h1>Hello</h1>
      </body>
    </html>
    

    Render the code as plain text.

  • adamanyn
    adamanyn about 4 years
    Thank you for. 1.I have both files present. 2.As mentioned I have a VPS, this is not a local machine.
  • jtessier72
    jtessier72 about 4 years
    Sorry, because you're on a VPS you couldn't be opening the files directly! We'll keep working then! It happens though.
  • jtessier72
    jtessier72 about 4 years
    can you add the output of a2query -m to your question?
  • adamanyn
    adamanyn about 4 years
    I just changed for ubuntu 20.04 and php 7.4, it's works, so the question is closed for me. Thank you.