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:

sudo apachectl 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.

mac-web-server-apache-working

To restart the Apache server, use the command:

sudo apachectl restart

To stop the Apache server, use the command:

sudo apachectl stop

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:

sudo nano /etc/apache2/httpd.conf

This will open the Apache config file.

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

#LoadModule php5_module libexec/apache2/libphp5.so

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

LoadModule php5_module libexec/apache2/libphp5.so

mac-web-server-activate-php-module

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

sudo apachectl restart

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:

sudo nano /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>

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

4. Next, type the command:

nano /Users/username/Sites/phpinfo.php

and paste the line:

<?php phpinfo(); ?>

Save and exit the file.

Restart Apache server

sudo apachectl restart

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:

/usr/local/mysql/bin/mysqladmin -u root password 'yourpasswordhere'

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:

sudo rm /usr/local/mysql
sudo rm -rf /usr/local/mysql*
sudo rm -rf /Library/StartupItems/MySQLCOM
sudo rm -rf /Library/PreferencePanes/My*
rm -rf ~/Library/PreferencePanes/My*
sudo rm -rf /Library/Receipts/mysql*
sudo rm -rf /Library/Receipts/MySQL*
sudo rm -rf /private/var/db/receipts/*mysql*

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:

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.

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:

sudo mkdir /var/mysql
sudo ln -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.

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.