0

First of all, my example of a virtualhost is working without embedding <? php echo 'Hello World'; ?> in index.html.

Apache is on the same machine I am using. PHPis two times availble on this machine.

$ which php
/usr/bin/php
$ which php7.0 
/usr/bin/php7.0

Furthermore: libapache2-mod-php7.0 is already installed.

What I have done until now:

$ sudo mkdir /var/www/www.virtualhost.com/
$ cd /var/www/www.virtualhost.com/
$ sudo cat > index.html
<html>
    <body>
        <h1>My virtualhost</h1>
        PHP Test:<br>
        <?php echo "PHP"; ?>
    </body>
</html>
ctrl+d
$ sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-availble/virtualhost.com.conf

After editing my virtualhost.com.conf file it look's like:

<VirtualHost *:80>

    ServerName www.virtualhost.com
    ServerAlias virtualhost.com
    ServerAdmin webmaster@virtualhost.com
    DocumentRoot /var/www/www.virtualhost.com
    DirectoryIndex index.html index.php

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined


    <Directory /var/www/www.virtualhost.com/>
            Options +Indexes -FollowSymlinks
            AllowOverride None
    </Directory>

    #Include conf-available/serve-cgi-bin.conf
</VirtualHost>

# vim: syntax=apache ts=4 sw=4 sts=4 sr noet

Then I enabled my site, made an entry in my /etc/hosts file and restartet apache2 with following commands:

$ sudo a2ensite virtualhost.com.conf
$ sudo apache2ctl restart

The new entries in my /etc/hosts are:

127.0.0.1       www.testhost.com    
127.0.0.1       www.virtualhost.com

Following modules are activated:

$ a2query -m
authz_host (enabled by maintainer script)
proxy_fcgi (enabled by site administrator)
socache_shmcb (enabled by site administrator)
negotiation (enabled by maintainer script)
filter (enabled by maintainer script)
access_compat (enabled by maintainer script)
authz_core (enabled by maintainer script)
deflate (enabled by maintainer script)
authn_file (enabled by maintainer script)
php7.0 (enabled by maintainer script)
authz_user (enabled by maintainer script)
authnz_fcgi (enabled by site administrator)
mime (enabled by maintainer script)
proxy (enabled by site administrator)
mpm_prefork (enabled by site administrator)
dir (enabled by maintainer script)
alias (enabled by maintainer script)
auth_basic (enabled by maintainer script)
setenvif (enabled by maintainer script)
cgi (enabled by site administrator)
env (enabled by maintainer script)
autoindex (enabled by maintainer script)
authn_core (enabled by maintainer script)
status (enabled by maintainer script)
ssl (enabled by site administrator)

Without the line <?php echo "PHP"; ?> in index.html the site is working. But if I embed this line php's echo is not echoing.

Do I need something like

LoadModule php7_module modules/libphp7.so

in my /etc/apache2/apache2.conf?

Any hint for solving this issue is welcome.

John Goofy
  • 129
  • 4
  • 2
    `.html` files are not pares for php code. rename your file to `index.php` – Gerald Schneider Jul 18 '19 at 13:05
  • @GeraldSchneider Okay, I followed your hint and it works. But now I'm a little bit irritated. I thought one of the goals of PHP is to use it within HTML, e.g. _index.html_, like any comment or other script language? – John Goofy Jul 18 '19 at 13:23
  • Yes, and you do that. You write it in the HTML code. You just have to tell the server that he has to parse the file for PHP code. You do that by using the proper extension (or by forcing it to parse every html file for it, as noted in the answer below, which is not recommended). – Gerald Schneider Jul 18 '19 at 13:26
  • BTW, PHP7.0 is EOL since january 2019. You should upgrade. And you should definitely NOT start learning it with an outdated version. – Gerald Schneider Jul 18 '19 at 13:27
  • @GeraldSchneider Update accomplished: `$ php -v` results in `php -v PHP 7.3.7-1+ubuntu16.04.1+deb.sury.org+1`. – John Goofy Jul 18 '19 at 15:01

1 Answers1

0

In order for the embedded PHP code in a file to be executed, the file needs to be actually run through the PHP interpreter. By default, only files ending in .php (and a couple of other file extensions) are sent through the PHP interpreter, because sending all files through the PHP interpreter by default reduces performance and can, in some circumstances, cause security or functionality problems.

In order to tell your webserver to send all files ending in .html through the PHP interpreter, you should add the following line to your vhost config:

AddHandler application/x-httpd-php70 .html
womble
  • 95,029
  • 29
  • 173
  • 228