Apache in the Land of Mac

November 15, 2014
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

in config file /etc/apache2/httpd.conf  uncoment line 

LoadModule php5_module libexec/apache2/libphp5.so

Enable User directory

If you’d like to classical unix http://localhost/~userdirectory functionality, uncomment following lines in /etc/apache2/httpd.conf 

LoadModule userdir_module libexec/apache2/mod_userdir.so
Include /private/etc/apache2/extra/httpd-userdir.conf 

Userdirectory located by default at /Users/username/Sites/. You probably will get 403 access denied error – fix it using those recommendations.

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

with new declaration

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/

Useful References

Finding your way in OS X Yosemiteā€ˇ

November 12, 2014
This is a kind of confession – I quite often find myself lost on OS X configurations files, installed packages and so on.. And it is embarrassing… really embarrassing…

I started developing software in mid-80s, initially as a hobby, but eventually hobby somehow self-converted into my full-time profession. I started from Assembler 360 (on Soviet clone of IBM 360/370), then moved to Assemblers of IBM PC, following by kernel development for Windows and Linux, embedded systems, VoIP, video streaming, network analysis and many more cool applications/devices/systems…. Bottom line, have quite a baggage to brag about…

Until I got my first Mac – quite late, when it harder to teach old dog new tricks… Actually I was resisting for a while idea to move to Mac – close software, close hardware, no alternative but buy from a single vendor and soon..  But a resistance to Apple is futile – once you forced to work with Mac ( it was AirBook) for a week, you just can’t go back and start wonder how people can ever use anything else but Mac OS ;-).. The only problem is that “old tricks” ( Linux/Windows configurations experience) are often confuse my memory when I need to do something on the OS X, and somehow I think that some old dogs may face quite similar problems… So maybe making my embarrassment public, I can help a few lost souls as well…

Enough with an introduction… This will be a sub-blog about problems i faced while working with OS X, using it as development platform of various application – Java, PHP, C++

Environment (to be updated):
– MacBook Pro with Retina Display ( upgrade from original AirBook)
– OS X Yosemite (10.10)  (upgrade from original 10.7)
– some software installed with MacPort
– some software installed with Fink
– development environment – Eclipse (Luna – 4.4.1)
– Java – JDK 8u25

Expected topics:
– Who installed this shit – finding your way in OS X package managers.
– Configuration for local PHP development & testing
– Configuration for local web app development with Java
– Configuration for Android development ( including NDK debugging).