How to Setup a Web Server in Mac OS X

Spread the love

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:

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.

To restart the Apache server, use the command:

sudo apachectl restart

To stop the Apache server, use the command:

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

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

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.

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:

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

Setting up MySQL

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.

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

Installing PhpMyAdmin

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.

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:

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.

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.

Subscribe to our newsletter!

Our latest tutorials delivered straight to your inbox

Sign up for all newsletters.
By signing up, you agree to our Privacy Policy and European users agree to the data transfer policy. We will not share your data and you can unsubscribe at any time. Subscribe


Damien Oh

Damien Oh started writing tech articles since 2007 and has over 10 years of experience in the tech industry. He is proficient in Windows, Linux, Mac, Android and iOS, and worked as a part time WordPress Developer. He is currently the owner and Editor-in-Chief of Make Tech Easier.

Comments (28)