I used yast to install lamp modules for php7, apache, etc.
From a terminal in super user, the command a2enmod php7 shows: “php7” already present.
Typing localhost/index.html in the chromium browser renders the file /srv/www/htdocs/index.html.
Typing localhost/index.php causes the file /srv/www/htdocs/index.php to download to /home/username/Downloads/index.php.
index.php contains:
<?php
phpinfo();
?>
I suspect I have a configuration issue. Any help would be appreciated. Thanks.
Yes, I restarted apache. Per the yast http server configuration tool, the server module ‘php7’ is enabled. However, the description is unknown. This seems odd.
Try putting “http” in front to explicitly force using the hypertext protocol using the web server, ie
http://localhost/index.php
Your browser settings may be configured to open files from the file system directly (without the web server) although I would think that simply invoking “localhost” ordinarily should force using the web server.
I tried putting php code into the index.html file and executing it from the user directory.
Here is the file:
<html>
<body>
<h1>
Welcome to the home website
</h1>
<?php phpinfo(); ?>
<?php echo ‘hello world’; ?>
<script language=‘php’>
echo ‘hello world’;
</script>
</body>
</html>
the result of file:///home/user/WebAssets/index.html is:
Welcome to the home website
PHP does not render .html files through the parser, it needs to be index.php or you have to adjust the PHP configuration (I do not not recommend parsing .html files through PHP by default)
Do you see php7 there at the end? If not, type; a2enmod php7 and then **systemctl restart apache2
**
Edit:
Going through the file manager would not parse the PHP file, you need to use your web browser and use http://… to view it.
Interesting
browsing to: http://127.0.0.1/~user/index.php - works
browsing to: localhost/index.php - works
browsing to: file:///home/user./public_html/index.php does not work.
My problem was I was performing the later one and not the former two.
But PHP falls in the category of server side scripting. In other words it is the server software (Apache in this case) that detects that something is to be preprocessed by PHP before it is send to the client. How Apache detects this is in the Apache configuration. This is e.g. what I found in my /etc/opache2/conf.d/php5.conf:
Which indicates, amongst other things, that file names ending in e.g. .php are to be treated specially.
Thus when file names do not match that pattern, like ending in .html, they will not be treated as PHP.
And of course interpreting as file directly by a client (browser) will never succeed in the PHP being interpreted because the browser has no PHP knowledge/module.
The following can be accessed both locally and through your external network interface (the one you use to communicate with other machines)
http://localhost/index.php
When you do a “file://<path>” you’re accessing the file directly through your system file system and completely bypassing the webserver. Because the PHP interpreter is installed as a webserver module, you don’t have any PHP functionality. I imagine if you invoked the file through a PHP interpreter, your file should render as you want.
For your little experiment embedding php in the html page,
Pages served by a webserver are always read from top to bottom. This means that you didn’t place your php script invocation high enough on the page, and is a primary reason for placing it in the <head> section. Except in very special cases, you shouldn’t be invoking your script commands before declaring the script type and possibly libraries to be used.
(Hmmmm… I may actually need to review how this was/is done, haven’t done anything like this server-side in awhile…)
On 03/27/2017 12:26 PM, Parthenolide wrote:
>
> Interesting
> browsing to: http://127.0.0.1/~user/index.php - works
> browsing to: localhost/index.php - works
> browsing to: file:///home/user./public_html/index.php does not work.
> My problem was I was performing the later one and not the former two.
This is why tsu2 asked you to prefix with ‘http’, on the off chance you
were instead using the ‘file:///’ prefix which is incorrect. The file
prefix tells the browser to browse the filesystem, meaning it does not, at
all, go through Apache httpd which is only accessed via a socket.
Browsing the filesystem in a browser is just like browsing the filesystem
in any other tool, like a text editor or IDE, where you want to see the
real contents, not the rendered contents.
> browsing to: http://127.0.0.1/~user/index.html does not render the php
> code.
>
> I’m not sure why you do not recommend running php scripts inside html.
> This seems to be standard practice.
You may, if you’d like, but applying a PHP interpreter to HTML files in
general is a waste of time unless you know that PHP code may reside
within, and could cause all kinds of issues if not intended.
–
Good luck.
If you find this post helpful and are logged into the web interface,
show your appreciation and click on the star below…