Apple’s built-in Web Sharing feature allows you to run a webserver on your own machine.
For ease of use on developing locally I personally prefer using tools like MAMP or XAMPP because they install and make it easier to get databases configured and require a great deal less work then going through terminal and configuring a bunch of things for application development. If you are already a hardcore command line junkie this post is not up your ally but read on if you want some light reading.
How to turn your Mac into a Web Server:
First we have to turn it on:
- From the Apple menu, select System Preferences
- Click “Sharing”, and then click the services tab.
- Select Personal Web Sharing, and then click the Start Button
- Checking that box will turn on Personal Web Sharing and this will basically turn on an Apache Web Server inside of your computer. if you want to see what you have just enabled open up a browser (Safari or Firefox) and look for the address at the bottom. In this example it is http://10.1.10.50 and clicking on that or entering it into your browser will open up the homepage for your newly enabled web server. Providing you haven’t installed any other web servers you can also point your browser to “localhost” without the quote marks.
Where do I save my site files?
You can save your files for a site within your main Library folder or your own users directory (recommended)
- Macintosh HD > Library > WebServer > Documents
- Users Directory > Your Username (homedirectory) Sites
Resources mentioned above:
How do i get rid of the ~username in my url?
This whole blog post evolved cause one our UX designers who does Design and Front-end development (HTML/CSS/Jquery) didn’t want to go and install a web server like MAMP or XAMPP on his machine since he was only really using the Web Server to render files that had PHP includes or other basic usages for cataloging his files locally. He didn’t need mysql and wasn’t working on any dynamic web applications.
For this reason he really just wanted to use his Sites folder on his machine and not have to type in the full path of localhost/~username/sitesfolder/filename and just wanted to be able to get access to it from going to localhost.
to change which folder the Apache web server resolves to you are going to need to edit the Apache HTTPD
to do this open up terminal by going to your Applications/Utilities/Terminal.app
once you are in terminal:
start by typing (without the quotes)
and hit return. This will take you to the apache webserver main folder using the “change directory” command in Unix.
from here we are going to need to edit the Apache HTTPD configuration file.
rather then opening it in pico or a command line editor type the following command to open it with TextEdit which will make it easier to view
(remember do not put the quotes and put all these on one line.
we use the sudo command because we need to access a file that is locked and requires a special user called “root”
the sudo command is short for “switchuser do” as in switch the user and do the command. we trail it with the path to the TextEdit application to make it easier to edit then using Pico or Vi which are command line Visual Editors in Unix which our nice Apple OSX is built on.
My recommendation to you is to immediately do a backup of this file in case you break it by going to File… Save As…
Once you have done that its really easy simply find the line in the code that reads
DocumentRoot “/Library/WebServer/Documents” and comment it out by placing a # in front of it as shown in the picture below. Adding a # before anything in this file tells the file to ignore what is there.
obviously change out the username for what your computers username is.
if you don’t know your computers username go back up to the first picture in this tutorial and find the Web Sharing section inside of the system preferences. In my example the username is craigkeller which can be seen following the ~ above.
The next thing we are going to have to modify is the Directory.
The Directory setting can be found by doing a find for:
and changing it to reflect the path to your sites folder.
As you can see from the photo below i’ve simply commented old the previous and added my own. Commenting out the previous code is always good because you can easily revert it to what it was defaulted to if need be.
This is just one of the ways you can change where your localhost points in your sites folder.
There are other ways to do this such as adding Aliases in your HTTPD configuration file or even creating something called a symlink but for purposes of solving the problem for the non technical I have written the above.
I don’t write alot of these things primarily because time is lacking so If you found this article helpful please comment on it and let me know so that I continue to write more things like this when i stumble on and fix them for others.