102

What is the easiest way to activate PHP and MySQL on Mac OS 10.6 (Snow Leopard), 10.7 (Lion), 10.8 (Mountain Lion)?

I'd prefer to use any bundled versions before resorting to downloading from the PHP or MySQL websites. That also rules out MAMP and similar.

I'm comfortable at the command line in Terminal.

Matt Sephton
  • 3,711
  • 4
  • 35
  • 46

12 Answers12

142

Open a good text editor (I'd recommend TextMate, but the free TextWrangler or vi or nano will do too), and open:

/etc/apache2/httpd.conf

Find the line:

"#LoadModule php5_module        libexec/apache2/libphp5.so"

And uncomment it (remove the #).

Download and install the latest MySQL version from mysql.com. Choose the x86_64 version for Intel (unless your Intel Mac is the original Macbook Pro or Macbook, which are not 64 bit chips. In those cases, use the 32 bit x86 version).

Install all the MySQL components. Using the pref pane, start MySQL.

In the Sharing System Pref, turn on (or if it was already on, turn off/on) Web Sharing.

You should now have Apache/PHP/MySQL running.

In 10.4 and 10.5 it was necessary to modify the php.ini file to point to the correct location of mysql.sock. There are reports that this is fixed in 10.6, but that doesn't appear to be the case for all of us, given some of the comments below.

Eric Bloch
  • 2,882
  • 2
  • 20
  • 26
TWLATL
  • 2,859
  • 4
  • 25
  • 37
  • there's a typo /etc/apache2/httpd.conf – Matt Sephton Aug 20 '09 at 19:55
  • +1 this is the way to go. I've also found the instructions here to be amazingly helpful (even though it says 10.5, it works for 10.6): http://www.red91.com/2007/12/14/installing-mysql-on-leopard – Dave DeLong Feb 24 '10 at 20:13
  • 10
    if anyone else is wondering where's the `php.ini`, there isn't any! but you can add one to `/etc` by copying **`/etc/php.ini.default`** to `/etc/php.ini` – cregox Apr 06 '10 at 21:23
  • 11
    and the "It works" page is under `/Library/WebServer/Documents/index.html.en` – cregox Apr 06 '10 at 21:32
  • I followed the steps reported here, but still PHP is not working; using a URL like http://example.local/dr61/install.php, the browser shows the code source. I guess it is necessary to change something in the php.ini file. – apaderno Jun 20 '10 at 15:55
  • 1
    At the end, I had to copy the file php.ini.default in php.ini, and PHP files started to be executed as they should be. – apaderno Jun 20 '10 at 16:22
  • 3
    To use MySQL, I had also to change the default socket, which in my case was /tmp/mysql.sock (I installed MySQL 5.5). – apaderno Jun 20 '10 at 16:39
  • 7
    Also, after saving the .conf, you can just use the command `sudo apachectl restart` instead of turning web sharing off-on. – aaaidan Sep 30 '10 at 02:14
  • To test the setup, you can put the line inside ~/Sites/foo.php and browse to http://host/~user/foo.php – Eric Bloch May 23 '11 at 02:46
  • @Cawas what's the difference between `/etc/php.ini.default` and `/private/etc/php.ini.default`? – Behrang Nov 27 '11 at 23:35
  • @BehrangSaeedzadeh `/etc` is just a [symlink](http://en.wikipedia.org/wiki/Symbolic_link) to `/private/etc` – cregox Nov 29 '11 at 19:19
  • Worked like a charm on El Captain. Btw this link helped me a lot too http://coolestguidesontheplanet.com/get-apache-mysql-php-and-phpmyadmin-working-on-osx-10-11-el-capitan/ – pojda Oct 20 '15 at 12:32
17

To complete your setup or MySQL:

sudo vim /etc/profile
  1. Add alias

    alias mysql=/usr/local/mysql/bin/mysql
    alias mysqladmin=/usr/local/mysql/bin/mysqladmin
    
  2. Then set your root password

    mysqladmin -u root password 'yourPassword'
    
  3. Then you can login with

    mysql -u root -p
    
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
mdskinner
  • 498
  • 4
  • 12
4

It's an invisible folder. Just hit Command + Shift + G (takes you to the Go to Folder menu item) and type /etc/.

Then it will take you to inside that folder.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
asdf1
  • 41
  • 1
4

I strongly prefer HomeBrew over MacPorts for installing software from source.

HomeBrew sequesters everything in /usr/local/Cellar so it doesn't spew files all over the place. (Yes, MacPorts keeps everything in /opt/local, but it requires sudo access, and I don't trust MacPorts with root.)

Installing MySQL is as simple as:

brew install mysql
mysql_install_db

To start mysql, in Terminal type:

mysqld&

There's a way to start it upon boot, but I like to start it manually.

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
Duke
  • 7,070
  • 3
  • 38
  • 28
  • "brew link mysql" will create a number of symlinks in /usr/local/bin etc. – Duke Oct 23 '10 at 06:04
  • I'm not sure I understand the 'messily spew files' comment. MacPorts sequesters everything in /opt/local, so it seems equivalent. I suppose occasionally I've seen something placed in /Applications/MacPorts (e.g. AquaTerm for gnuplot), but this is the very rare exception and usually for a good cause (e.g. AquaTerm is a .app bundle), and even then it's keeping it organized under a single directory... – Ethan Jan 19 '11 at 21:01
  • 1
    Oh, and I find it easier to simply add /opt/local/bin to my $PATH, and -I/opt/local/include to CPPFLAGS, -L/opt/local/lib to LDFLAGS, and then not have to remember to set up links for every single package that gets installed... – Ethan Jan 19 '11 at 21:03
  • My use of the symlink was based partially on ignorance; it's completely unnecessary. I just don't trust large installation scripts with root. And if you don't think MacPorts spreads files over many directories, just look at the uninstallation instructions: http://guide.macports.org/chunked/installing.macports.uninstalling.html – Duke Jan 20 '11 at 04:29
3

This help me a lot:

http://maestric.com/doc/mac/apache_php_mysql_snow_leopard

It also works for Mac OS X Lion :D

.:EDIT:. On my case the prefepane only allows to start and stop mysql, but after some issues i've uninstalled him. If you need a application to run queries and create DB, you could use: Sequel Pro (it's free) or Navicat

If you need start and stop mysql in ~/.bash_profile you can add these lines:

#For MySQL
alias mysql_start="/Library/StartupItems/MySQLCOM/MySQLCOM start"
alias mysql_stop="/Library/StartupItems/MySQLCOM/MySQLCOM stop"

After reloaded the console just call:

$mysql_start 

or

$mysql_stop 

agreding the desired action. Hope helped you.

workdreamer
  • 2,836
  • 1
  • 35
  • 37
  • Does the MySQL prefpane work for you in Lion? That is, can it switch MySQL on/off? – Matt Sephton Aug 19 '11 at 08:36
  • 1
    i don't need the prefpane, but this solve your question? : http://stackoverflow.com/questions/3027479/mac-os-x-mysql-preference-pane-doesnt-work/4582737#4582737 – workdreamer Aug 19 '11 at 10:13
  • But the prefpane only allows you to start and stop the mysql. I re-edited my post because here its not formated. – workdreamer Oct 27 '11 at 11:45
1

I would agree with Benjamin, either install MAMP or MacPorts (http://www.macports.org/). Keeping your PHP install separate is simpler and avoids messing up the core PHP install if you make any mistakes!

MacPorts is a bit better for installing other software, such as ImageMagick. See a full list of available ports at http://www.macports.org/ports.php

MAMP just really does PHP, Apache and MySQL so any future PHP modules you want will need to be manually enabled. It is incredibly easy to use though.

simonrjones
  • 1,182
  • 7
  • 11
0

If you've upgraded to Mac OS X 10.8 Mountain Lion, and previously had a working system, all you should need to do is re-enable PHP as in Step 1 of the above chosen answer.

You may also find the following Preference Pane useful for managing "web sharing" (Apache web server), which replaces system functionality removed in OS X 10.8: http://clickontyler.com/blog/2012/02/web-sharing-mountain-lion/

I also had to re-add my virtual hosts include line to the httpd.conf

Matt Sephton
  • 3,711
  • 4
  • 35
  • 46
0

Considering it hasn't been released yet, I'm assuming this is a question for ahead-of-time or you have a developer's build. As Benjamin mentioned, MAMP is the easiest way. However, if you want a native install, the process should be like 10.5. PHP comes installed on OS X by default (not always activated for some), just download the 32-bit version of MySQL, start Apache, and you should be good to go. You may have to tweak Apache for PHP or MySQL, depending on what builds are present. I didn't have to tweak anything to have it working.

machuga
  • 426
  • 2
  • 2
  • Out of interest - Snow Leopard is 64-bit capable, so why choose the 32-bit of MySQL? – Matt Sephton Aug 18 '09 at 12:57
  • 1
    Leopard, and even Tiger for most things I believe, are 64-bit capable. 64-bit MySQL has some issues with some other packages, not directly related to PHP necessarily, but lets say he tried Python down the line - 64bit MySQL causes some serious grief with the MySQLDB package. It's completely usable, just not all things have caught up yet, sadly. – machuga Aug 18 '09 at 19:57
0

In addition to the native versions, but you may want to try BitNami MAMP Stacks (disclaimer, I am one of the developers). They are completely free, all-in-one bundles of Apache, MySQL, PHP and a several other third-party libraries and utilities that are useful when developing locally. In particular, they are completely self-contained so you can have several one installed at the same time, with different versions of Apache and MySQL and they will not interfere with each other. You can get them from http://bitnami.org/stack/mampstack or directly from the Mac OS X app store https://itunes.apple.com/app/mamp-stack/id571310406

Daniel Lopez
  • 3,297
  • 2
  • 30
  • 29
0

ammps was super easy for me and has a nice web-based configuration:

crizCraig
  • 8,487
  • 6
  • 54
  • 53
0

There's a great guide here:

https://discussions.apple.com/docs/DOC-3083

However, it didn't work for me first try. I found this tip: run "httpd -t" in Terminao to check the syntax of your config files. Turns out using copy & paste from the tutorial introduced some strange characters. After fixing this, it worked great. There are some links from the guide for adding MySQL as well.

This worked much better for me than MAMP. With MAMP, I was having delays of about 20 seconds or so before changes to the .php file would be reflected in the browser when you refresh, even if you cleared the cache, history, cookies, etc.

This problem was resolved in MAMP PRO, but MAMP PRO had a new issue of its own: the .php files would be downloaded instead of being rendered as a page in the browser! I contacted support and they didn't know what was going on.

The built-in Apache server didn't have any of these issues. Definitely the way to go. The guide below is almost identical to the one above, but it has user comments that are helpful:

http://osxdaily.com/2012/09/02/start-apache-web-server-mac-os-x/#comment-572991

Vern Jensen
  • 3,449
  • 6
  • 42
  • 59
0

FYI - if anyone experience issues with launching Apache, and getting errors about

/usr/sbin/apachectl: line 82: ulimit: open files: cannot modify limit: Invalid argument

it's because of a recent update to Apache in Snow Leopard. The fix is easy, just open /usr/sbin/apachectl and set ULIMIT=""

http://blog.deversus.com/2010/11/mac-os-1065-apachectl-usrsbinapachectl-line-82-ulimit-open-files-cannot-modify-limit-invalid-argument/

cabgfx
  • 189
  • 4
  • 11