I’ve almost always used MAMP (Mac, Apache, MySQL, PHP) to test and develop websites locally before publicizing online. It’s a pretty simple and straightforward solution when you want a local temporary web server that you can turn on and off as you please. Though you can get the same local web server using already built in features on your Mac (which in fact uses Apache, like MAMP). The setup is maybe a bit more advanced than the installation of MAMP but knowing how to, for example, enable PHP on the pre-installed OS X web server can be good to know when you have to write some quick changes at your parents house.
This guide aims to describe how to start the Mac OS X web server, enable PHP and install and configure MySQL on your local Lion (10.7) installation.
- Mac OS X Lion
- MySQL (x86, 64-bit), DMG Archive (download)
1. Start the built-in web server
In order to start the default web server on your system, you’ll need to activate it from the System Preferences. You can do this from the terminal app too but I’ll show how to do it the visual way.
- Chose System Preferences from the Apple Menu ().
- Got to Sharing.
- Check the Web Sharing box.
You’ll notice that the righthand part of the window changes, showing two links: one with your username in it and one with only the address to your computer. To check that the web server is started you can click on the link with only the address. A browser window should pop up directing you to the address and show the text “It works!”. This webpage is opened from the computers web folder which you can find at
Theres also the possibility to create individual user web directories. I prefer to have this enabled since I can conveniently find my personal user web directory at
~/Sites In Lion, this isn’t enabled by default so you’ll have to activate it for every user that wants it:
- (Optional) Click the big Create Personal Website Folder to enable your user website at http://youraddress/~username
2. Enable PHP
Per default PHP isn’t enabled in the built-in Apache installation but it is installed. To verify your PHP version you can open up the Terminal.app and write
To enable PHP you can, in the Terminal.app, follow these steps:
sudo pico /etc/apache2/httpd.confand enter your account password.
- Use the arrow keys to navigate down to the line
#LoadModule php5_module libexec/apache2/libphp5.so
- Uncomment the line by removing the
- Restart Apache by typing
sudo apachectl restart
You can verify that PHP works by creating a document in your
~/Sites folder named
test.php with the contents
<?php phpinfo(); ?> and navigate to
http://youraddress/~yourusername/test.php with a web browser. If a, oh so pretty, test page with a bunch of information about PHP shows up you’re good to go. Note that you have to create the personal website folder (as described above) for this to work. If you didn’t, just use the computers web folder and skip the username in the address field when browsing.
Lion in fact ships with the database PostgreSQL and you can view the installation version by writing
psql --version in the Terminal.app. However, since for example the publishing tool WordPress as of the time writing only supports MySQL, the PostgreSQL installation is to no help.
I’ll describe how to install MySQL using the ‘official’ packages. There’s many other ways to install MySQL, one of my personal favorites is by using Homebrew, especially if you want other packages on your system. The ‘official’ installation is more straight on subject though and you’ll can get an optional prefPane and other nifty features like startup scripts:
- If you haven’t already, go to the MySQL download page and get the latest x86, 64-bit .dmg version for Mac OS X (as of the time writing this is 5.5.18 and the title says Mac OS X ver. 10.6 (x86, 64-bit), DMG Archive but it will work just fine on 10.7 Lion).
- Mount the DMG-file (double-click on it) and start the setup through, in my case, mysql-5.5.18-osx10.6-x86_64.pkg found on the image.
- Follow the onscreen instructions to complete the installation.
Now MySQL is installed and you can use the command
sudo /usr/local/mysql/bin/mysqld_safe & to start the MySQL daemon and shut it down safely with
sudo /usr/local/mysql/bin/mysqladmin shutdown
3.1 Add MySQL to the PATH environment variable (optional)
The commands listed above are long and hard to remember so I recommend adding /usr/local/mysql/bin/ to your PATH environment variable, for example you then just have to write mysqladmin and not /usr/local/mysql/bin/mysqladmin to access the administration program. Write the following commands in the Terminal.app:
echo 'export PATH=/usr/local/mysql/bin:$PATH' >> ~/.bash_profile
The first command adds the path to the file .bash_profile and the second reloads the file (yes, there’s two dots with a space between them in the second command).
3.2 Install the Preference Pane and Startup Scripts (optional)
To add the MySQL preference pane:
- Go to the mounted .dmg and double-click the MySQL.prefPane file.
- Click the Install button.
A new preference pane appears on your where you can start/stop the server. If you want to use the Automatically Start MySQL Server on Startup option you’ll need to install the startup scripts:
- Go to the mounted .dmg and double-click the MySQLStartupItem.pkg file.
- Follow the onscreen instructions.
3.3 Configure PHP to use correct mysql.sock
Per default PHP tries to use /var/mysql/mysql.sock when connecting to MySQL. When installing MySQL the mysql.sock is located in /tmp/mysql.sock. If we moved this file the preference pane would stop working so what we want to do is tell PHP to use /tmp/mysql.sock. To do this, once again open up the Terminal.app:
sudo cp /private/etc/php.ini.default /private/etc/php.ini
sudo pico /private/etc/php.ini
- Search for mysql.default_socket by pressing
control+w, writing it and press
- Change the row to
mysql.default_socket = /tmp/mysql.sock
sudo apachectl restart
3.4 Change root password for MySQL
The root password is left blank on new installations, this is not acceptable. To change the password write this in Terminal.app (if you’ve added mysql to the PATH):
sudo mysqladmin -u root password [new password]
Replacing [new password] with any secure password you can come up with.
3.5 Test PHP – MySQL connection
- Create a test.php file somewhere accessible from a browser with the following contents:
$address = 'localhost';
$user = 'root';
$password = 'yourrootpassword';
mysql_connect($address, $user, $password) or die(mysql_error());
echo "Successfully connected to MySQL";
- Replace yourrootpassword with the password you specified in 3.4
- View test.php in a browser too see the result
Now you should have a working server with PHP and MySQL running to play around with. Maybe you want to install phpMyAdmin to manage the database or install WordPress to develop themes and plugins.
In order to shut down or start the web server and MySQL daemon you just go to the System Preferences and enable/disable the services.