How to install Apache, PHP and MySQL stack on MacOS Lion
I just had to set up my usual web development stack on my new computer (MacBook Air – not that it mattersm but hey – it’s a keyword). Since it came with Lion pre-installed I had to learn how to do it in the best possible way. Usually I just install XAMPP because it comes with everything built in, but since Mac comes with Apache and PHP pre-installed I though that it doesn’t really make sense this time.
After reading some blog articles about how to do this I understood that I don’t really like any of their suggested ways of doing that. They just were messy in my opinion and would take more time than I would want to spend. So – without further welcoming text – here is my version of how to install/set-up Apache, PHP and MySQL on Mac OS Lion.
1. Installing MySQL via Homebrew
I use homebrew. Mostly because I’m lazy and I don’t like downloading sources from internet and then spending time compiling them. That’s why it made the most sense to install MySQL via homebrew. You can learn about homebrew by clicking on this link.
1.1 Installing homebrew
Everything should be explained here: https://github.com/mxcl/homebrew/wiki/installation
1.2 Installing MySQL
Just run these commands once homebrew is installed.
brew install mysql unset TMPDIR mysql_install_db --verbose --user=`whoami` --basedir="$(brew --prefix mysql)" --datadir=/usr/local/var/mysql --tmpdir=/tmp
The last two commands are necessary to make MySQL run from not-root user.
1.3 Running MySQL
Running MySQL is easy!
mysql.server start
Stopping it, if anything, is even more easy
mysql.server stop
1.4 Helping PHP finding MySQL
PHP will look for MySQL socket in a specific location. Once you’ve started MySQL server run these commands to help PHP find it:
sudo mkdir /var/mysql sudo ln -s /tmp/mysql.sock /var/mysql/mysql.sock
It will just symlink the socket into PHP searched place.
2. Setting up Apache
MAC OS Lion already comes with Apache to make the Web sharing work. Sadly the default DocumentRoot is “/Library/WebServer/Documents” which I really don’t like. I prefer my DocumentRoot to live in “~/Sites”. If you don’t require this then you can skip the next step, because I shall explain how to change the DocumentRoot.
2.1 Changing DocumentRoot to “~/Sites” (or anything that you like)
You will need to edit the httpd.conf file. It is located in “/etc/apache2/httpd.conf”. By default it will only have read permissions so you will have to change that:
sudo chmod u+w /etc/apache2/httpd.conf
Now edit the file in your favourite editor (I used VIM):
sudo vi /etc/apache2/httpd.conf
Find a line that says (around line 238):
DocumentRoot "/Library/WebServer/Documents"
And change the path in the quotes to the path where you want your DocumentRoot to be. In my case:
DocumentRoot "/Users/rolandsatvars/Sites"
Then search for line (around line 266):
<Directory "/Library/WebServer/Documents">
And change the path to the same path what you entered before:
<Directory "/Users/rolandsatvars/Sites">
Now save the file and you’re done – DocumentRoot should be changed!
2.2 Making .htaccess files work
By default apache settings won’t allow you to use .htaccess files, but no worries – we will change that in a bit.
We will need to edit two files now. First one is the httpd.conf again which is located in “/etc/apache2/httpd.conf”. If you didn’t do the previous step then you’ll need to make this file writable. If you did the previous step, then you don’t need to do this as you’ve already done it.
sudo chmod u+w /etc/apache2/httpd.conf
Now open the file in your favourite editor again (I used VIM):
sudo vi /etc/apache2/httpd.conf
Then search for a line:
<Directory "/Library/WebServer/Documents">
or:
<Directory "{YOUR_DOCUMENT_ROOT_PATH}">
depending whether you changed your DocumentRoot or not. It should be around line 266. Then in the <Directory> block find where it says:
AllowOverride None
and change it to:
AllowOverride All
When you’ve done that just save the file and close it. Now we need to move on to the next file which should live in “/etc/apache2/users/{YOUR_USER_NAME}.conf” where {YOUR_USER_NAME} is your user name. Open that file in your editor again:
sudo vi /etc/apache2/users/rolandsatvars.conf
And change:
AllowOverride None
to:
AllowOverride All
And also you will want to change this line if you want to use mod_rewrite:
Options Indexes MultiViews
to:
Options Indexes MultiViews FollowSymLinks
Now save the file and close it. Success!!! Apache should be set up – we only have one more step and then we’re finished configuring things!
3. Enable PHP in Apache
Now we have to make Apache understand what PHP is – by default it won’t know that because required module isn’t loaded. We have to go to our, already known, file httpd.conf in “/etc/apache2/httpd.conf”. Open it:
sudo vi /etc/apache2/httpd.conf
and search for ‘php5_module‘ (it should be around line 111) and you should see line like this:
#LoadModule php5_module libexec/apache2/libphp5.so
Just remove the hash so that line becomes like this:
LoadModule php5_module libexec/apache2/libphp5.so
Now save and close the file again. We can remove write permissions for this file now:
sudo chmod u-w /etc/apache2/httpd.conf
Awesome – we’ve done all the things we needed to do.
4. Starting Apache
To start Apache just go to “System Preferences -> Sharing” and click on “Web Sharing” (if it’s already clicked then un-check it and check it again to restart the server).
That’s it. You should now be able to access your webserver at http://localhost (or your IP address).
Hopefully this will work for you – it did for me. Do ask me any questions if you stumble upon any problems.
Some say...
Thanks for the help! I was able to get this all setup on my machine. I do have one question. Where would the databases be stored for this type of install of Mysql?
No problem!
I don’t remember where the actual files are stored (I should be able to tell this when I get home), but you should be able to get to your databases easily with any database management application.
I would suggest using Sequel Pro (http://www.sequelpro.com/) to connect to MySQL server and manage databases. Use socket connection and input this value “/var/mysql/mysql.sock” for socket field. Username is ‘root’ and password is empty. You might want to change the ‘root’ password though.
Good luck.
Hey, great work. I got mySQL up an running perfectly with your steps. My new problem is I can’t seem to figure out how to connect to it via PHP. localhost doesn’t seem to work. I’m admittedly new to this and have no idea where to go, nor where to find this information. Ideas?
What happens when you try going to http://localhost in your browser?
Have you started the Apache web server? (read “4. Starting Apache” in this article)
It takes me to the Lion Server homepage.
Did you do step 2.1 (changing DocumentRoot)?
If no (and you don’t want to do that for some reason) then:
– go to “/Library/WebServer/Documents”
– backup everything that’s in there (safety first!) and empty that directory (it will, probably, ask for root password)
– create a new file in there called “index.php”
Then when you go to “http://localhost” index.php will be loaded.
Hopefully this will help.
I found the location of the databases on the file system. /usr/local/var/mysql. Just wanted to put this up here in case someone else was needing that information.
Perfect! This was everything I needed to make Lion work for me. Thank you so much.
Spent days on the problem (Snow Leopard) that my PHP couldn’t connect to the socket… but on linking it with a symbolic link I had not thought.
Thank you very much!
Man… I spent some time trying to figure out why my .htaccess were being ignored, until I saw your /etc/apache2/users dir tip.
That was lame. But now is solved, thanks to you.
If anybody had problems on Lion 10.7.3 and XCode 4.3
because brew install xdebug failed ( Error: Failed executing: phpize ), install autoconf
http://jfire.posterous.com/xcode-43-homebrew-and-autoconf
i had to rename those links in /usr/local/bin from autoconf213 to autoconf (all auto.* ofc) but i used different recipe though
Thanks for the perfect tutorial.
Used it to create a screencast: https://secure.vimeo.com/37484330
Hope you don’t mind =)
Nice guide, I have been searching for a solution where .htaccess and mod_rewrite works decently together. Turns out the missing var was FollowSymLinks
In addition my vhost directive has a None instead of all
I’ve followed these steps, and when I go to http://localhost in any browser, I get a “Problem loading page” error. My ~/Sites folder has a index.php in it. I’ve tried restarting my web sharing, and restarting my machine. Can you think of anything else I should look into?
Hey Roland,
i’ve followed all the steps without Problems, but now the only Problem i’ve got, is that i reach a 403 Error:
Forbidden
You don’t have permission to access / on this server.
Any Idea how to Fix it? Thanks for your Help :)
@Tobias, you might try restoring your httpd.conf (the one in /etc/apache2/) with the one that is in /etc/apache2/original
Just a comment: I followed this to a T and then when i tried to start the mysql server, there was a fatal error. After realizing I had the default install of mysql, i went and turned it off using the system preferences > mysql and then clicking shut down. It made me log in as root. So I know there was some conflict with the PID as that error was also in the terminal. Sorry if this is a little bit bird brained, but i don’t know much about all of this.
What can you say?