How to Setup a Web Server in Mac OS X Mountain Lion

In the recent version of Mac OS X, the web server is one of the component that is built-in by default. Prior to Mountain Lion, users can easily turn on the web server via the “Web Sharing” option in the Sharing Preference pane. That component was removed in Mountain Lion. In this tutorial, we will show you how to activate the web server in Mountain Lion, as well as setting up PHP, MySQL and PhpMyAdmin. At the end of this tutorial, you will have a MAMP (Mac, Apache, MySQL, Php) server running on your Mac.

Starting the Apache server

Apache server is pre-installed in Mac OS X, so there is no need to install it. However, to start the Apache server, we will have to use command line in the Terminal.

1. Open the Terminal (it can be found under the Applications -> Utilities section).

2. Type the following command:

sudoapachectl start

This will start the Apache server. To make sure that it is working, open a browser and type “http://localhost” in the address bar. If you see a “It works!” message, then your Apache server is running fine.

To restart the Apache server, use the command:

sudoapachectl restart

To stop the Apache server, use the command:

sudoapachectl stop

Activating the PHP module

The Apache server is only good enough for you to run static HTML files. If you want to run a more complicated setup, like installing WordPress, you will need to activate the PHP module.

PHP is pre-installed in Mac OS X as well, but it is not included by default.

Configuring Sites folder

By default, Apache serves files that are in the folder location “/Library/WebServer/Documents”. On a multiple users system, you can setup the web server to serve files for different users using the URL “http://localhost/~username“.

1. Open the Finder and go to your Home folder (the folder with a Home icon and your username). Create a new folder “Sites” if it is not available.

2. Back to the Terminal, type the command:

sudonano/etc/apache2/users/username.conf

Replace the “username” with your login username. In my case, it will be “sudo nano /etc/apache2/users/damienoh.conf“.

3. Copy and paste the following code to the conf file.

<Directory "/Users/username/Sites/">
Options Indexes MultiViews
AllowOverride All
Order allow,deny
Allow from all
</Directory>

Installing PhpMyAdmin

2. Extract the compressed file to your Sites folder and rename it as “phpmyadmin”.

3. Open the “phpmyadmin” folder and create a new folder call “config”. Change its permission with the command:

chmod o+w ~/Sites/phpmyadmin/config

4. Next, in your browse, navigate to “http://localhost/~username/phpmyadmin/setup”. This will bring up the setup page where you can connect PhpMyAdmin to your MySQL server.

5. Click the “New Server” button.

6. Go to the “Authentication” tab and enter MySQL root password in the “Password for config auth” field.

Click “Save”.

7. Lastly, enter the following commands in the terminal:

sudomkdir/var/mysql
sudoln-s/tmp/mysql.sock /var/mysql/mysql.sock

Now, go to “http://localhost/~username/phpmyadmin”. You should be able to login and create database now.

Conclusion

It will probably be easier if you install a third party tool like MAMP, but that will add duplicate features to what is already available in your Mac. With a little tinkering, you can easily get your Mac to be a web server for all your web hosting needs.

and if you are getting #2002 error when trying to log in to phpmyadmin as i did, try to change the following key in config.sample.inc.php: from $cfg[‘Servers’][$i][‘host’] = ‘localhost'; to $cfg[‘Servers’][$i][‘host’] = ‘127.0.0.1’; and save it as config.inc.php (in your phpmyadminfolder) hope this saves some time to somebody

First of all this is an excellent tutorial. I’m a newbie developer and just bought a Mac after 20+ years using a PC. Anyhoo…I saw this and decided to try it so that I could run Ajax scripts. Everything worked perfectly just one question…why would I want to remove the MySQL? Your note about removing me caused alarm bells to ring. Should I not have it on my Mac when I’m not using it? Just curious. Thanks again. Great tutorial.

Oh…one last comment and this may be a totally rookie one but…when you get the phyMyadmin to login to create the database…what’s the username and password? Is the user name your home and password the one you created?

Hi, I did everything like the instruction said but I cannot log in using the phpmyadmin. it says there everytime I try to log in was #1045 Cannot log in to mySql server. my server is running I’ve checked it everytime I try to log in. Do you know which problem that cause this kind of error? Thanks

Hi, and thanks for the awesome tutorial. I have followed the instructions to the end and everything worked for me until the final step. When I go to log in to http://localhost/~scott/phpmyadmin using the username root and password root (that i set up in admin) it doesn’t log me in. When I saved the password I did get the following message “Use SSL (localhost [1]) You should use SSL connections if your database server supports it.” Can you please assist?