0

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!)
  • According to your question "...just trying to get Users/myusername/Sites/foo and ~/bar [sic!] to work" getting Users/myusername/Sites (accessed by localhost/~username) itself to work isn't required. This site is rather useful for "personal" websites. Additionally: your Enable PHP interpretation within HTML files directive is in the vhost config file and won't affect .../~username sites! – klanomath Mar 5 '18 at 3:39
0

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.

  • @ehounder 1. Enabling user folders (e.g localhost/~myusername) is not required to run foo or bar.localhost even if their DocumentRoot folders are in your Sites folder! 2. Probably it's better to split your PHP directive problem from this question and add a separate question here. – klanomath Mar 5 '18 at 4:07
  • First many thanks @klanomath, localhost/~myusername is working. However, php is apparently not executing in either the example I posted or your example. Otherwise, reading your explanation which I'll have to read again, the default /Library/Websites/Documents setup now seems better. Since I don't want other users: My goal is for all of my PhoneGap hybrid mobile app devices to be able connect to my central www.domainname.com VPN MySQL database remote server (if that's the definition of a remote server). So Xcode project files are in a Sites/PhoneGap directory - with foo-type https-vhosts.conf – ehounder Mar 5 '18 at 4:12
  • Ok I'll split the PHP directive off - didn't register that at first read. – ehounder Mar 5 '18 at 4:37

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .