Using the built in web server in MacOSX 10.11 El Capitan

If you want to do local web development on your machine, or host an internet-available website from your mac, you may be surprised to hear that MacOSX comes with a built-in web server.

By default, it comes with Apache2 and Php5 preinstalled. Here is how to enable them and get web-developing.

Step 1: Start the server.

Although the option to start the web server has been removed from System Preferences, you can still enable it from the terminal (/Applications/Terminal.app). First, open the terminal and type in the following content.

sudo apachectl start

Step 2: Use the server.

Now, if you open your web browser and type in 'http://localhost', then you should see the following page:

Hello world If you see this page, it means that our web server is up and running and ready to accept connections. Now, let's change some files so that we can start developing our website.

The default web directory is held in /Library/Webserver/Documents. In this folder, you should see a file called index.html.en, which is the one with the content 'It works!' which you saw above. We can change this for another file as follows:

Open finder and press shift-command-g. Type in the resulting box the following path.

Go to path

You will see the following folder, which is the root web directory. Simply remove the current files in this folder and add your web content to get started.

Webserver documents

A quick side-note, if you want to make it so you don't have to input your password every time to change a file (you probably want to do this), make the folder writeable by your user by going up a folder (cmd-uparrow), clicking on the Documents folder, right clicking and selecting Get Info. Then click the little padlock in the bottom corner and authenticate. Now, click the + in the users list and add your current user. Next to your user in the list, change Read only to Read & Write. Finally, click the little cog in the bottom corner and select apply to enclosed items.

Your user now has access to the web folder and you can change files as you like - have fun web-developing!

3. Enable PHP 5

If you want to use PHP on your server, you will need to make a change to a config file to enable it.

Using the go function described earlier, go to /etc/apache2. Back up the file httpd.conf by copying it to e.g. httpd-bak-1-6-2016.conf. Now, edit httpd.conf in your favourite text-editor, and find the line containing the following.

#LoadModule php5_module libexec/apache2/libphp5.so

Remove the preceding # on this line in the config file, making it:

LoadModule php5_module libexec/apache2/libphp5.so

Now, save and close the file, and restart apache by entering the following into a terminal.

sudo apachectl restart

Now, remove any other files starting with index, e.g. index.html and make a file called 'index.php' with the following content in /Library/Webserver/Documents:

<?php
phpinfo();
?>

Now, go to http://localhost in your browser, and you should see that your PHP config is working nicely, rendering the page above and displaying your PHP info.

phpinfo

4. Stopping the server.

To stop running the web server, simply type the following command into a terminal:

sudo apachectl stop
Published 2016-09-03