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.

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:

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.

mac-web-server-apache-working

To restart the Apache server, use the command:

To stop the Apache server, use the command:

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.

1. In the terminal, type:

This will open the Apache config file.

2. Remove down the list until you see the line:

Remove the “#” in front of the line, so it becomes:

mac-web-server-activate-php-module

3. Save the changes (using shortcut key “Ctrl + o”) and exit (using shortcut key “Ctrl + x”). Restart Apache.

The PHP module is now activated.

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.

mac-web-server-create-sites-on-user-home

2. Back to the Terminal, type the command:

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.

Once again, replace the “username” with your login username. Save (Ctrl + o) and exit (Ctrl + x) the file.

4. Next, type the command:

and paste the line:

Save and exit the file.

Restart Apache server

5. In your browser, type “http://localhost/~username/phpinfo.php“. You should see the PHP info page, if everything is running fine.

mac-web-server-test-phpinfo

MySQL is not included in Mountain Lion, so you will need to download and install it manually.

1. Go to MySQL Download site and download the MySQL installer for Mac. For easier installation, you might want to grab the .DMG image than the one in .tar.gz format.

Note: You don’t have to sign up for an account to download the file. Just click the “No thanks, just start my download.” will do.

2. Once the download is completed, open up the installer, you should see two .pkg files and one .prefPane file. Install all three of them.

3. After the installation, you can go to “System Preferences -> mySQL” and start the MySQL instance.

mac-web-server-start-mysql

Setting upi MySQL root password

In the Terminal, type the command:

Replace the “yourpasswordhere” with your own password.

Note: Do not confuse this password with your Mac login account. They are not the same. This is the password for the script to access your database.

Note: Removing MySQL is not as straightforward. Run the commands, line by line, in the terminal:

Open the file “hostconfig” with the command “sudo nano /etc/hostconfig” and remove the line MYSQLCOM=-YES-.

PhpMyAdmin is basically a bunch of PHP files, so installing them is a breeze.

1. Download PhpMyAdmin from its website.

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:

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.

mac-web-server-setup-new-server-phpmyadmin

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

mac-web-server-enter-password-phpmyadmin

Click “Save”.

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

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

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.

28 comments

  1. 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

  2. I got to step 5 but I do NOT see the PHP info. I don’t see index.html files either in; user/myname/sites

    localhost gives me a page where it says: It Works!

    What do I do wrong?

  3. Apologies, I can’t even get to first base here…
    When I browse to localhost I just get:

    The requested URL / was not found on this server

    I do the

    sudo apachectl start

    and I get :

    org.apache.httpd: Already loaded

    But when I browse to localhost… nothing…

    Thanks

  4. Thanks Damian, you got me thinking…
    I’m actually using Lion.
    So I googled for Lion specifically… and found the answer

    So, if anyone else needs to know, on Lion its different, you go to the system preferences and enable ‘web sharing’ to kick this off.

    Thanks again for a great article Damian,
    Vida

  5. Great Tutorial! Easy to follow steps! Everything works and now i can easily test websites before even uploading!

    Thanks!!

  6. Awesome! Thanks! This is my first time doing this and this tutorial is just perfect!

    For those newbies as I am…, when I tried last step that is to log in into phpmyadmin, I didnt know that “root” was username so I got #1045 error.

  7. Finally figured this out. The comments by Natts and Andrey Mironov a huge help. Thanks to all involved.

  8. I can’t get your save and exit instructions to work. ctrl + x doesn’t work after ctrl + o. (I have OS 10.9.2)

  9. 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.

    • The mySQL removal instruction is there just in case you want to remove it. You will need MySQL for MAMP to work.

  10. 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?

    • Ixne on all that! I fixed it! I’m running my own server now from my laptop! So insanely cool! Yahoo!

  11. 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

  12. Awesome tutorial, perfect.
    I just cannot login after installed, i try the user: root and the password I set up in the sql authentication… but even after that I cannot login…

    Cheers,

  13. 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?

    Thank you.

    Scott.

Comments are closed.

Sponsored Stories