You are not logged in.

#1 2019-12-01 04:27:51

Registered: 2018-12-20
Posts: 7

[SOLVED] [LAMP] PHP scripts in html files

Hi! I set up a LAMP stack by following the Apache, MariaDB, PHP pages on the wiki to the best of my ability. I think it's working correctly as I can open php files in my user directory. i.e. ~/public_html/some_file.php loads correctly if I visit localhost/~lardandweed/some_file.php in firefox.

However, I couldn't get php scripts to work in html files. So I did the following:

  • Add a .htaccess file to ~/public_html with the following contents

    AddHandler application/x-httpd-php .html
  • Append the following to /etc/httpd/conf/httpd.conf

    <Directory "/home/lardandweed/public_html">
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
  • Restart httpd.service

With this, php scripts are working in html files. Tested this with a simple test.html:

    echo "Hello world!";

I'd like to check if this is a good/safe method? If not, could someone point me to a source with a better approach?

Last edited by lardandweed (2019-12-03 22:43:12)


#2 2019-12-03 01:54:10

From: London, UK
Registered: 2010-12-14
Posts: 16

Re: [SOLVED] [LAMP] PHP scripts in html files

While this will work, I wouldn't recommend taking this approach. My comments are based on not knowing what your plans are with this project. It is much better to stick to accepted standards. Parsing HTML as PHP will firstly lead to more overhead on your stack as every HTML file regardless of whether PHP code is present will be parsed as PHP. Secondly, any developers coming into your environment will certainly not be used to running PHP in this manner and maintenance may become an issue. Thirdly, you run the risk of exposing your PHP code in a scenario where you move your code to another server and forget that you are parsing HTML as PHP or a simple AllowOverride None directive could disable this exception.

I certainly wouldn't feel comfortable doing this even if it was just locally.


#3 2019-12-03 22:42:39

Registered: 2018-12-20
Posts: 7

Re: [SOLVED] [LAMP] PHP scripts in html files

Hi! Thanks for the tip.

I'm learning php at the moment, and intend to stick with php in .php files. But the books I'm using mentioned that its possible to put php scripts within .html files, so I was wondering why my system wasn't able to do so. Led me down this rabbit hole...

Thanks for the advice anyway. Much appreciated!


#4 2019-12-03 22:44:50

Inspector Parrot
Registered: 2011-11-29
Posts: 23,696

Re: [SOLVED] [LAMP] PHP scripts in html files

Perhaps this is obvious alread, but just in case, php can absolutely be embedded within html.  A file can have a first line of `<DOCTYPE html>` and include mostly regular html.  But if it also includes php scripts within that html, the file extension is best set to .php.

"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman


Board footer

Powered by FluxBB