MacOS – Solve /Users/theusername/Sites/foo ~/bar localhost/~theusername High Sierra Apache server connection Internal Server Error

apachehigh sierramacosPHP

Battle of VirtualHost /Users/myusername/Sites/foo ~/bar Internal Server Error goin on for days now tryin to attach localhost/~myusername to Apache/2.4.28 http server on macOS 10.13.3 High Sierra theater of operations not going according to any plan:

Apache not working with user Sites folder on macOS 10.13 High Sierra

Set up Virtual Hosts on macos High Sierra 10.13 in Apache

Set up localhost on macOS High Sierra (Apache, MySQL, and PHP 7) with SSL/HTTPS

My question is of course very similar to Fyfe’s: Apache not working with user Sites folder on macOS 10.13 High Sierra except I don’t need to maintain any /Library/Webserver/Documents as there were none, just want Users/myusername/Sites to be operational.

In fact, I see this Apache server-username problem all over the place, what’s more central to web development than one just connecting to one’s freaking server? So that it seems there are only two kinds of developers ~/foo:“I just did this and it works perfectly!” and ~/bar:”I did all that and nothing works.” Well, Fyfe’s question of Sep 27 ’17 has 15 answers and counting, latest being Feb 1 ’18 with no green checkmark.

Again, I had not setup or been using Apache, php, or vhosts in Sierra, so don’t need to maintain /Library/Webserver/Documents as there were none, VirtualHost *:80 command added below to keep option open, although just trying to get Users/myusername/Sites/foo and ~/bar to work.

sudo apachectl configtest 
Syntax OK

php -v
PHP 7.1.7

/etc/apache2/extra/https-vhosts.conf

#Enable PHP interpretation within HTML files
<FilesMatch ".+\.html$">
  SetHandler application/x-httpd-php
</FilesMatch>

<VirtualHost *:80>
ServerName localhost
DocumentRoot /Library/WebServer/Documents/
</VirtualHost>

#VIRTUAL HOST ENTRY FOR foo.localhost
<VirtualHost *:80>
  DocumentRoot "/Users/myusername/Sites/foo"
  ServerName foo.localhost
  ErrorLog "/private/var/log/apache2/foo-error_log"
  CustomLog "/private/var/log/apache2/foo-access_log" common
</VirtualHost>

#VIRTUAL HOST ENTRY FOR bar.localhost
<VirtualHost *:80>
  DocumentRoot "/Users/myusername/Sites/bar"
  ServerName bar.localhost
  ErrorLog "/private/var/log/apache2/bar-error_log"
  CustomLog "/private/var/log/apache2/bar-access_log" common
</VirtualHost>

/etc/hosts

##
# Host Database
#
# localhost is used to configure the loopback interface
# when the system is booting.  Do not change this entry.
##
127.0.0.1       localhost
255.255.255.255 broadcasthost
::1             localhost

#Local myusername/Sites
127.0.0.1       foo.localhost
127.0.0.1       bar.localhost

#Map your IP address to localhost
127.0.0.1 apple.com www.apple.com

/etc/apache2/httpd.conf

# This is the main Apache HTTP server configuration file.  It contains the
# configuration directives that give the server its instructions.
# See <URL:http://httpd.apache.org/docs/2.4/> for detailed information.

ServerRoot "/usr"

# User home directories
Include /private/etc/apache2/extra/httpd-userdir.conf

# Virtual hosts
Include /private/etc/apache2/extra/httpd-vhosts.conf

#User _www
#Group _www

User myusername
Group staff

LoadModule vhost_alias_module libexec/apache2/mod_vhost_alias.so
LoadModule negotiation_module libexec/apache2/mod_negotiation.so
LoadModule dir_module libexec/apache2/mod_dir.so
LoadModule userdir_module libexec/apache2/mod_userdir.so
LoadModule alias_module libexec/apache2/mod_alias.so
LoadModule rewrite_module libexec/apache2/mod_rewrite.so
LoadModule php7_module libexec/apache2/libphp7.so
LoadModule hfs_apple_module libexec/apache2/mod_hfs_apple.so

DocumentRoot "/Library/WebServer/Documents"
<Directory "/Library/WebServer/Documents">

/etc/apache2/extra/httpd-userdir.conf

Include /private/etc/apache2/users/*.conf
  • PHP 7 commands not working
  • localhost/~myusername => Internal Server Error
  • foo.localhost => google search “foo.localhost” results => poo
  • localhost => Forbidden you don’t have permission to access / on this server

Time to call in air strikes … all I got here is FUBAR

SOLUTION UPDATE:
klanomath took out the first targets, Tanks! localhost/~myusername and http://foo|bar.localhost are working.

However … Arrg … when entering localhost/~myusername php seems to run this /Sites/index.php file, wherein the "Hello From Sites Folder!" and the 7.1.7 phpinfo page comes up:

<?php
    echo "Hello From Sites Folder!";
    phpinfo();
?>

However this php index.html and example.php combination apparently does not execute

<?php
    date_default_timezone_set('UTC');
    $day = date('l');
?>

<!doctype html>
<html>
   <head>
       <title>Hello, World! | Foo</title>
   </head>
   <body>
       <h1>Hello, World!</h1>
           <p>Welcome to <strong>Foo</strong>.</p>
           echo $greeting
   </body>
</html

example.php (both in the foo directory)

<?php
   $greeting = 'Hello, PHP World!';
   echo '<h1>' . $greeting . '</h1>';
?>

wherein double-clicking /Sites/foo/index.html just opens a page printing

Hello, World!

Welcome to Foo.

echo $greeting (instead of Hello, PHP World!)

Best Answer

A simple working configuration starting with the default macOS Apache config files is the following:

/etc/hosts

##
# Host Database
#
# localhost is used to configure the loopback interface
# when the system is booting.  Do not change this entry.
##
127.0.0.1       localhost
127.0.0.1       foo.localhost
127.0.0.1       bar.localhost
255.255.255.255 broadcasthost
::1             localhost

Modifications of the default /private/etc/apache2/httpd.conf file:

LoadModule vhost_alias_module libexec/apache2/mod_vhost_alias.so
...
LoadModule rewrite_module libexec/apache2/mod_rewrite.so
LoadModule php7_module libexec/apache2/libphp7.so
...
DocumentRoot "/Users/<username>/Sites"
<Directory "/Users/<username>/Sites">
...
Include /private/etc/apache2/extra/httpd-vhosts.conf

Virtual hosts configuration /etc/apache2/extra/https-vhosts.conf

#Enable PHP interpretation within HTML files
<FilesMatch ".+\.html$">
  SetHandler application/x-httpd-php
</FilesMatch>

<VirtualHost *:80>
ServerName localhost
DocumentRoot /Users/<username>/Sites
</VirtualHost>

#VIRTUAL HOST ENTRY FOR foo.localhost
<VirtualHost *:80>
  DocumentRoot "/Users/<username>/Sites/foo"
  ServerName foo.localhost
  ErrorLog "/private/var/log/apache2/foo-error_log"
  CustomLog "/private/var/log/apache2/foo-access_log" common
</VirtualHost>

#VIRTUAL HOST ENTRY FOR bar.localhost
<VirtualHost *:80>
  DocumentRoot "/Users/<username>/Sites/bar"
  ServerName bar.localhost
  ErrorLog "/private/var/log/apache2/bar-error_log"
  CustomLog "/private/var/log/apache2/bar-access_log" common
</VirtualHost>

PHP test file index.html in foo/ and bar/:

<html><body><h1>Welcome to foo/bar. It works!</h1></body></html>

<?php

// Show all information, defaults to INFO_ALL
phpinfo();

?>

Enabling userdir stuff isn't required because you don't want to access .../~username/

Then in Safari or Google enter "http://bar|foo.localhost" instead of simply "bar|foo.localhost"

This configuration shouldn't/won't allow access to http://localhost or http://localhost/~username. But in your case this is not required anyway.


If access to http://localhost and http://localhost/~username1|~username2|~username3 etc is required, use the default DocumentRoot /Library/Websites/Documents in httpd.conf and for the vhost localhost. Additionally enable LoadModule userdir_module libexec/apache2/mod_userdir.so and Include /private/etc/apache2/extra/httpd-userdir.conf in httpd.conf. Enable Include /private/etc/apache2/users/*.conf in /private/etc/apache2/extra/httpd-userdir.conf and add respective <⁣username1|2|3>.conf files in /private/etc/apache2/users/ for each user.