Search code examples
phpapache.htaccessubuntuphp-8

htaccess to allow php 8 to execute in html files


ubuntu 22.04.1 LST, Apache/2.4.52 (Ubuntu), PHP 8.1.2

I'm doing this on my local computer, not on an actual web-server. I'm going through this just to do it and see how it is done.

I've installed apache2 on my computer which is running Ubuntu. I also have PHP8.1 installed. I followed a couple of tutorials on setting up apache2 on Ubuntu. Everything works fine except I can't get the .htaccess entry correct to allow php to run inside my html files. I've done some apache .conf and .htaccess stuff before but am definitely a beginner.

my .conf file:

<Directory /var/www/johndcowan/public_html>
   AllowOverride All
</Directory>
my .htaccess file:

AddHandler application/x-httpd-php8 .html
My test index.html file:

<html>
  <head>
    <title> Ubuntu rocks! </title>
  </head>
  <body>
    <p>I'm running this website on an Ubuntu Server server!</p>               
   
    <?php print "<p>This is printed with php tags</p>"; ?>
     
  </body>
</html>

When I load my webpage I see this:

I'm running this website on an Ubuntu Server server!
This is printed with php tags
"; ?> 

So, it's not displaying the <?php print " but it is showing the rest of the php code.

I haven't been able to find any info on using php8 or php81 in the .htaccess file. I've tried using different versions, but none work:

AddHandler application/x-httpd-php .html
AddHandler application/x-httpd-php5 .html
AddHandler application/x-httpd-php7 .html

The setup is seeing my .htaccess file. If I change the AddHandler to an AddType and reload my test page, I get prompted to Save or Download the page.

Any thoughts on what I can do/try differently?

Any help is appreciated.

****** UPDATED AFTER SOLVING ******

I Googled on "How to enable PHP in apache2". It led me to a thread on StackOverflow: https://stackoverflow.com/questions/36057615/ubuntu-apache-module-php7-does-not-exist

I had to install a php module package and enable a php-module-8.1, then this worked.

$ sudo apt-get install libapache2-mod-php
... 
# in the output I saw this line:
apache2_invoke: Enable module php8.1

Then, just to be sure, I ran:

$ sudo a2enmod php8.1
...
# and I saw this line in the outpu:
Module php8.1 already enabled

Note I tried to install specific versions of the mod-php package but I kept getting a E: Unable to locate package libapache2-mod-php8 The same error for mod-php7 and mod-php5. Using just the generic ...mod-php worked and it knew to get the 8.1 version.

I reloaded my webpage and it worked as expected.


Solution

  • I Googled on "How to enable php in apache2". I found a thread on Stackoverflow: Ubuntu Apache: "Module php7 does not exist"

    It led me to run this:

    $ sudo apt-get install libapache2-mod-php
    ...
    # in the output I saw this line:
    apache2_invoke: Enable module php8.1
    

    Then, just in case, I ran the following to enable php8.1 module:

    $ sudo a2enmod php8.1
    ...
    Module php8.1 already enabled
    

    I reloaded my webpage and it works great as expected now.