The first challenge I faced was configuration of Apache (HHTP server). It is coming as part of OS X but not activated by default. Yosemite (10.10) is coming with Apache/2.4.9 , Mavericks (10.9) with Apache/2.2.26
Command and Control
The first confusion was when I tried starting httpd with classical “init.d” approach – look, mom, OS X don’t have init.d..
Instead of Linux tricks, OS X is using (RTFM!) a native apache utility
apachectl
To start HTTP server:
sudo apachectl start
and as expected , to stop
sudo apachectl stop
Another useful command test all configuration files and report on error
apachectl configtest
and test virtual host configuration with
apachectl -S
server configuration files located at /etc/apache2 ( or actually at /private/etc/apache2 but “private” can be dropped as etc is linked back)
Alternative is to use launch daemon – actual Mac replacement for init.d
sudo launchctl load -w /System/Library/LaunchDaemons/org.apache.httpd.plist
Some additional info on launchd and launchctl. TBD
Start on boot
To setup HTTPD running automatically on Mac boot ( it is not by default)
sudo launchctl load -w /System/Library/LaunchDaemons/org.apache.httpd.plist
not flag “-w” which made this load permanent (e.t. autostart on reboot). To disable autostart , unload httpd daemon with
sudo launchctl unload -w /System/Library/LaunchDaemons/org.apache.httpd.plist
Configuration
Enable PHP
LoadModule php5_module libexec/apache2/libphp5.so
Enable User directory
LoadModule userdir_module libexec/apache2/mod_userdir.so
Include /private/etc/apache2/extra/httpd-userdir.conf
Enable Rewrite rules
in config file /etc/apache2/httpd.conf uncoment line
LoadModule rewrite_module libexec/apache2/mod_rewrite.so
Enable Alias subdirectory
IMPORTNAT NOTE: Apache 2.4 introduced major changes in authorisation configuration – check details at http://httpd.apache.org/docs/2.4/upgrading.html#run-time (wasted time after upgrading OSX as it also upgrades Apache version)
Particularly, when moving from Apache 2.2 replace “allow all” construct
Order allow,deny
Allow from all
Require all granted
for example, to allow http://localhost/foo add /etc/apache2/other/foo.cfg file
Alias /foo /Users/stas/Sandbox/foo
<Directory “/Users/stas/Sandbox/foo”>
Options Indexes FollowSymLinks Includes ExecCGI
AllowOverride all
Require all granted
AddType application/x-httpd-php .php
</Directory>
Enable Virtual hosts on localhost
usually it is a best way to debug your web application locally – instead of www.foo.com, create local domain “foo.local”, build virtual host and debug as you would with a real application.
Step 0 : in configuration file /etc/apache2/httpd.conf uncomment lines
LoadModule vhost_alias_module libexec/apache2/mod_vhost_alias.so
Step 1 : create a new DNS entry
add new entry into the /etc/hosts file – see last line (first 3 lines are standard)
127.0.0.1 localhost
255.255.255.255 broadcasthost
::1 localhost
127.0.0.1 foo.local
Step 2: create virtual host definition
<VirtualHost *:80>
ServerAdmin webmaster@foo.local
DocumentRoot “/Users/stas/Sandbox/foo”
ServerName foo.local
ServerAlias www.foo.local
ErrorLog “/private/var/log/apache2/foo.local-error_log”
CustomLog “/private/var/log/apache2/foo.local-access_log” common
</VirtualHost>
note: DocumentRoot must to point to directory with appropriate permissions – like one used in subdirectory example above. Otherwise you’ll receive “forbidden” error on access…
see also http://coolestguidesontheplanet.com/set-virtual-hosts-apache-mac-osx-10-10-yosemite/
Enable localhost server with virtual hosts
Setting up virtual hosts has one drawback -you will lose http://localhost ;-(
to restore it back, add one more virtual host definition:
<VirtualHost *:80> ServerName localhost DocumentRoot /Library/WebServer/Documents/</VirtualHost>
Troubleshooting
Log files located by default at /var/log/apache2/