Thursday 25 July 2013

Setting up Apache for local web development for Mac 10.8.X series.


If you have upgraded from a previous OS X your hosting environment will be whacked and you need to fix certain configuration files to get that environment back, in particular enabling PHP, and if you used the username/Sites document root folder in your home account you will need to add back in a user configuration file.

Apache/WebSharing

The first difference in the new OS X 10.8 is the dropping of the GUI option in System Preferences > Sharing to turn on Web Sharing, it may be gone but the webserverApache is definitely under the hood of the OS and ready to go.
no-web-sharing in Mountain Lion
No Web Sharing Option in System Preferences
Apache is pre-installed in the OS and needs to be enabled via the command line - this needs to be done in Terminal which is found at /Applications/Utilities/Terminal
For those not familiar with the Terminal, it really isn't as intimidating as you may think, once launched you are faced with a command prompt waiting for your commands - just type/paste in a command and hit enter, some commands give you no response - it just means the command is done, other commands give you feedback - lets get to it....
to start it
sudo apachectl start
to stop it
sudo apachectl stop
to restart it
sudo apachectl restart
To find the Apache version
httpd -v
The version installed in Mountain Lion is Apache/2.2.22
apache web serving it works
After starting Apache - test to see if the webserver is working in the browser -http://localhost - you should see the "It Works!" text.
If you want a GUI point and click web sharing toggle switch in System Preferences, this is one from clickontyler. Some people have had issues with this sys pref so use at your own decision.

Document Root

Document root is the location where the files are shared from the file system and is similar to the traditional names of 'public_html' and 'htdocs', OSX has historically had 2 web roots one at a system level and one at a user level - you can set both up or just run with one, the user level one allows multiple acounts to have their own web root whilst the system one is global for all users. It seems there is less effort from Apple in continuing with the user level one but it still can be set up with a couple of extra tweaks.

System Level Web Root

- the default system document root is still found at - 
The files are shared in the filing system at -  
/Library/WebServer/Documents/

User Level Root

Interestingly the user document root level is missing the '~/Sites' folder in the User account on a clean installation, you need to make a "Sites" folder at the root level of your account and then it will work. Upgrading from a previous OS X version preserves the Sites folder but removes the ability to web serve from it - this is where you need to add in a 'username.conf' file.
sites-folder-account
Create a Sites folder at the account root level 
Check that you have a “username.conf” filed under:
/etc/apache2/users/
If you don’t (very likely), then create one named by the short username of the account with the suffix .conf, it's location and permissions/ownership is best tackled by using the Terminal, the text editor 'nano' would be the best to deal with this.  
Launch Terminal, (Applications/Utilities), and follow the commands below, first one gets you to the right spot, 2nd one cracks open the text editor on the command line (swap 'username' with your account's shortname, if you don't know your account shortname type 'whoami' the Terminal prompt):
cd /etc/apache2/users
sudo nano username.conf
Then add the content below swapping in your 'username' in the code below:
<Directory "/Users/username/Sites/">
Options Indexes MultiViews
AllowOverride All
Order allow,deny
Allow from all
</Directory>
Permissions on the file should be:
-rw-r--r--   1 root  wheel  298 Jun 28 16:47 username.conf
If not you need to change...
sudo chmod 644 username.conf
Restart Apache for the new file to be read:
sudo apachectl restart
Then this user level document root will be viewable at:

That should be all.