PHP works from command line but not in browser

I’ve installed Fedora 35, with Apache, MySQL 7.4 and PHP 8. PHP seems to work from the commandline, but it is not getting displayed within the browser. This code does not display the php info:

<html>
<head>
</head>
<body>
Hi, there!
<?
phpinfo();
?>
</body>
</html>

If I have just:

<? phpinfo(); ?>

in a file named phpinfo.php, I can run

php phpinfo.php

from the command line to show all the information, but it does not work either standalone or within html when viewing it in the browser. I tried Chrome with the same result. I’m not sure if it is because of a setting in php.ini.

1 Like

Have you had a look at tutorials for embedding php in html? Such as this first search hit?
It suggests you need the php tag, <?php phpinfo.php ?>
or instead, browse to phpinfo.php, as the browser then runs your php script.

1 Like

Fedora 35 : Apache httpd : Use PHP Scripts : Server World

Following vgaetera’s link, this did not fix the issue. My default directory is /var/www/example_domain, from where HTML can be displayed in a browser but the PHP script does not seem to be executing in a browser.
In /etc/httpd/conf/httpd.conf I have set

ServerName 127.0.0.1

I wonder if your setting ServerName 127.0.0.1 may not be contributing to the issue. That will always force the browser to use the loopback address and it could easily interfere. I also have never seen a config where the servername was set to an IP address.

IME it is always best to use the same name as you get with the output of ‘hostname’ and also set the listen address to the actual network IP the server uses. For example, my hostname is eagle and the IP is 192.168.2.111 so that is the ServerName and listen IP used in httpd.conf.

This is, of course, assuming that this is only for the local (primary) server IP and not an aliased (hosted) domain/server name where you may have a machine with multiple IPs and web servers running.

EDIT
I just reviewed the httpd.conf file on my system and I found this as the example.

# ServerName gives the name and port that the server uses to identify itself.
#ServerName www.example.com:80
# Listen: Allows you to bind Apache to specific IP addresses and/or
# ports, instead of the default. See also the <VirtualHost>
# directive.
#
# Change this to Listen on a specific IP address, but note that if
# httpd.service is enabled to run at boot time, the address may not be
# available when the service starts.  See the httpd.service(8) man
# page for more information.
#
#Listen 12.34.56.78:80
Listen 80

I reset the ServerName to 192.168.1.64 but ended up re-installing Fedora, generally following the guide at https://computingforgeeks.com/how-to-install-lamp-stack-on-fedora/ , except that I installed MySQL before PHP, and I have it working now. If my index.php page contains

<?php phpinfo(); ?>

and if it is called from the URL line as http://192.168.1.64/index.php or from localhost or from 127.0.0.1 , it pulls up the formatted display. The script does not seem to display if embedded into html, like:

<html>
<head>
</head>
<body>
First Page.
<br>
<?php
phpinfo();
?>
</body>
</html>

I suspect that this

<html>
<head>
</head>
<body>
First Page.
<br>
<?php
phpinfo();
?>
</body>
</html>

should be this

<html>
<head>
</head>
<body>
First Page.
<br>
<?php phpinfo(); ?>
</body>
</html>

Tried that. No difference.

Try to add <!DOCTYPE html> as the first tag.

If this not works you have to configure apache that it lets you use .php as your index file. Probably now it is just .htm & .html files allowed. Search for “configure index file for apache” in web.

Adding the DOCTYPE line at the top did not make a difference.
I created a .htaccess file, alternating these 2 lines, which also did not enable the php to be displayed:

AddType application/x-httpd-php .php .html .htm
AddHandler application/x-httpd-php80 .php

Refreshing the page in FIrefox produced a pop-up asking what to do with the file (no file name showing).

I hope you are restarting php and apache webserver while doing this changes ?!

Yes, httpd was re-started.

Looks like some steps are missing in the above instruction.
It works for me like this:

sudo dnf install httpd php-fpm
sudo systemctl --now enable httpd.service php-fpm.service
sudo tee /var/www/html/info.php << EOF > /dev/null
<?php phpinfo(); ?>
EOF
sudo systemctl restart httpd.service php-fpm.service
curl http://localhost/info.php
1 Like

Yes i could reproduce it too.

As mentioned in @mhdave posted link:

@fedoracalgary can we put @vgaetera post as solution?

1 Like