PDA

View Full Version : [SOLVED] 18.04LTS Apache2 PHP parsing



Frank P
June 30th, 2020, 10:00 PM
the following lines added to apache2.conf via IncludeOptional prevent indexing and allow parsing of php in html


Options FollowSymLinks
AllowOverride None
</Directory>

<Directory /var/www/>
Options -Indexes +FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all
</Directory>

AddType application/x-httpd-php .php .htm .html
The same code added on a 18.04LTS server prevent indexing but wont parse php in html. It's a long time since I set up the 16.04 Is there a change in 18.04 or am I forgetting something.

apache2 (https://askubuntu.com/questions/tagged/apache2) php (https://askubuntu.com/questions/tagged/php)

SeijiSensei
July 1st, 2020, 02:34 PM
You might be missing the libapache2-mod-php package. It adds the code to the Apache configuration that tells it to parse rather than display .php files. Does it correctly parse files ending in .php but not ones using .html? Or does it not parse PHP files at all?

Also, are you using "short tags" (<? versus <?php)? If so, you might have to enable them in /etc/php/[version_number]/apache2/php.ini. Short tags are now disabled by default.


; short_open_tag
; http://php.net/short-open-tag
short_open_tag = On

Frank P
July 1st, 2020, 02:54 PM
I didn't install the libapache2-mod-php package initially but I did later after going thru one of the tutorials. It runs php code if the file has a php extension; it ignores it in html files. Im using phpInfo() to test it


phpInfo.html
<h1>Testing for parseing php code in html files</h1>
<h3>If you can see results of phpInfo() then all is okay</h3>
<?php
phpinfo()
?>

SeijiSensei
July 1st, 2020, 05:06 PM
https://stackoverflow.com/questions/11312316/how-do-i-add-php-code-file-to-html-html-files

You can try using AddHandler rather than AddType.


AddHandler application/x-httpd-php .html

I haven't done this in years. Is there a strong reason why you don't simply use .php as the file extension?

Frank P
July 2nd, 2020, 01:57 AM
I added the handler; still not parsing. I'll try researching some more tutorials to see if can find anything and come back later.
I have done that before but I find it easier to design and maintain if I keep the html and php in separate files and embed the php where I need it.
Thanks
Frank

SeijiSensei
July 2nd, 2020, 07:59 PM
I separate code from content as well. On any website I build, I have an include/handlers and an include/content directory that are run via include() commands. The handlers directory consists of scripts that run before the page is displayed, for instance, to handle POST or GET requests. The handler can choose among a variety of pages in the content directory to display based on the handler's results.

The content pages often contain PHP code as well, for instance, pages that iterate over records retrieved from a database.

Frank P
July 3rd, 2020, 01:13 AM
I'm doing roughly the same as you - using 'include' or 'require' in the html files to process the PHP

Anyway - problem solved


The line endings in my conf file had been set to windows; set back to Unix and everything works
Thanks
Frank