Last Updated: October 16, 2023
·
48.5K
· angelathewebdev

Installing PHP 5.4, MySQL, PhpMyAdmin and Nginx on Mac using Homebrew

Homebrew

When it comes to developing locally, there are many options for Mac users, including the dummy way of MAMP and XAMPP. However, for those who wants a NMP (Nginx, MySql, PHP) stack, it's not that simple. In this tutorial, I'll walk you through how to install these (plus the popular phpMyAdmin). The following has been tested on Snow Leopard and Mountain Lion.

To start with, we need to install Homebrew, a CLI tool that helps us install packages in a painless way. You can read more about Brew <a href="http://brew.sh/" title="Homebrew" target="_blank">@ their official site</a>. Please note that MacPort is not compatible with Homebrew, if you have MacPort installed, uninstall it before installing Homebrew. To check if MacPort is installed, use which port

To install brew, open your Terminal and type

$ ruby -e "$(curl -fsSL https://raw.github.com/mxcl/homebrew/go)"

This will install brew nicely (note you need Ruby to do it, which comes pre-installed on most Mac). Before you install anything with Homebrew, use

$ brew doctor

to check if you are missing any dependencies or need to remove anything.

The next thing is to install MySql with

$ brew install mysql

The output should be something like this

==> Installing mysql dependency: pidof
==> Downloading https://downloads.sf.net/project/machomebrew/Bottles/pidof-0.1.4
######################################################################## 100.0%
==> Pouring pidof-0.1.4.snow_leopard.bottle.1.tar.gz
/usr/local/Cellar/pidof/0.1.4: 3 files, 24K
==> Installing mysql
==> Downloading https://downloads.sf.net/project/machomebrew/Bottles/mysql-5.6.1
######################################################################## 100.0%
==> Pouring mysql-5.6.13.snow_leopard.bottle.1.tar.gz
==> /usr/local/Cellar/mysql/5.6.13/bin/mysql_install_db --verbose --user=angelaz
==> Caveats
A "/etc/my.cnf" from another install may interfere with a Homebrew-built
server starting up correctly.

To connect:
    mysql -uroot

To have launchd start mysql at login:
    ln -sfv /usr/local/opt/mysql/*.plist ~/Library/LaunchAgents
Then to load mysql now:
    launchctl load ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist
Or, if you don't want/need launchctl, you can just run:
    mysql.server start
==> Summary
/usr/local/Cellar/mysql/5.6.13: 9382 files, 355M

Then install default Nginx with

$ brew install nginx

If you're looking to install Nginx with some additional modules, you might want to try the homebrew-nginx formula instead. Installation instructions are <a href="https://github.com/marcqualie/homebrew-nginx" title="Custom Nginx Install" target="_blank">available here</a>

The output should be something like the following

==> Installing nginx dependency: pcre
==> Downloading ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.33.

curl: (28) Connection time-out
Trying a mirror...
==> Downloading http://downloads.sourceforge.net/project/pcre/pcre/8.33/pcre-8.3
######################################################################## 100.0%
==> ./configure --prefix=/usr/local/Cellar/pcre/8.33 --enable-utf8 --enable-unic
==> make
==> make test
==> make install
/usr/local/Cellar/pcre/8.33: 140 files, 4.1M, built in 74 seconds
==> Installing nginx
==> Downloading http://nginx.org/download/nginx-1.4.3.tar.gz
######################################################################## 100.0%
==> ./configure --prefix=/usr/local/Cellar/nginx/1.4.3 --with-http_ssl_module --
==> make
==> make install
==> Caveats
Docroot is: /usr/local/var/www

The default port has been set in /usr/local/etc/nginx/nginx.conf to 8080 so that
nginx can run without sudo.

To have launchd start nginx at login:
    ln -sfv /usr/local/opt/nginx/*.plist ~/Library/LaunchAgents
Then to load nginx now:
    launchctl load ~/Library/LaunchAgents/homebrew.mxcl.nginx.plist
Or, if you don't want/need launchctl, you can just run:
    nginx
==> Summary
/usr/local/Cellar/nginx/1.4.3: 7 files, 936K, built in 47 seconds

And finally, let's install PHP. Because the PHP formula we want (<a href="https://github.com/josegonzalez/homebrew-php" title="Homebrew PHP" target="_blank">Github Repo Link</a>) doesn't come with default Homebrew installation, we can't just do a brew install php. First we need to tap a special channel to obtain the formula

$ brew tap homebrew/dupes
$ brew tap josegonzalez/homebrew-php
$ brew install --without-apache --with-fpm --with-mysql php54

This allows us to install PHP without Apache build and with FPM. For more options (like if you wanna install PHP 5.5 instead of 5.4), see <a href="https://github.com/josegonzalez/homebrew-php" title="Homebrew PHP" target="_blank">formula readme</a> for more information.

And last but not the least, we need to install phpMyAdmin. But before we do, we need to check for the $PHP_AUTOCONF variable. This is required for the phpMyAdmin installation process, or you will get the following error message

Cannot find autoconf. Please check your autoconf installation and the
$PHP_AUTOCONF environment variable. Then, rerun this script.

You can check for the value of $PHP_AUTOCONF by echoing it. The following is the path of my autoconf installation, yours may vary.

$ echo $PHP_AUTOCONF
/usr/bin/autoconf

If you get only a blank line, you need to find out if autoconf is installed. If not, you can install it with brew install autoconf

$ which autoconf
/usr/bin/autoconf

Once you have the value, use an editor of choice to put the variable into your ~/.bash_profile configuration like so

PHP_AUTOCONF = /usr/bin/autoconf

Quit and start the terminal again and do another echo $PHP_AUTOCONF to confirm. Once you have that done, we're ready to install phpMyAdmin

brew install phpmyadmin

And you should expect output like so

==> Installing phpmyadmin dependency: php54-mcrypt
==> Downloading http://www.php.net/get/php-5.4.20.tar.bz2/from/this/mirror
Already downloaded: /Library/Caches/Homebrew/php54-mcrypt-5.4.20
==> PHP_AUTOCONF="/usr/local/opt/autoconf/bin/autoconf" PHP_AUTOHEADER="/usr/loc
==> ./configure --prefix=/usr/local/Cellar/php54-mcrypt/5.4.20 --with-php-config
==> make
==> Caveats
To finish installing mcrypt for PHP 5.4:
  * /usr/local/etc/php/5.4/conf.d/ext-mcrypt.ini was created,
    do not forget to remove it upon extension removal.
  * Restart your webserver.
  * Write a PHP page that calls "phpinfo();"
  * Load it in a browser and look for the info on the mcrypt module.
  * If you see it, you have been successful!
==> Summary
/usr/local/Cellar/php54-mcrypt/5.4.20: 3 files, 52K, built in 36 seconds
==> Installing phpmyadmin
==> Downloading http://downloads.sourceforge.net/project/phpmyadmin/phpMyAdmin/4
    ######################################################################## 100.0%
==> Caveats
Note that this formula will NOT install mysql. It is not
required since you might want to get connected to a remote
database server.

Webserver configuration example (add this at the end of
your /etc/apache2/httpd.conf for instance) :
  Alias /phpmyadmin /usr/local/share/phpmyadmin
  <Directory /usr/local/share/phpmyadmin/>
    Options Indexes FollowSymLinks MultiViews
    AllowOverride All
    Order allow,deny
    Allow from all
  </Directory>
Then, open http://localhost/phpmyadmin

More documentation : file:///usr/local/Cellar/phpmyadmin/4.0.7/share/phpmyadmin/doc/

Don't forget to copy config.sample.inc.php to config.inc.php and :
  - change your secret blowfish
  - uncomment the configuration lines (pma, pmapass ...)

==> Summary
/usr/local/Cellar/phpmyadmin/4.0.7: 1173 files, 28M, built in 83 seconds

Configuration

Now that you have installed everything, time for configuring. All the following steps are optional

Start services when log in

Mac places the services that run at startup in a folder called ~/Library/LaunchAgents. If you wish to start these services at startup, you need to copy their individual .plist files from their directory to the LaunchAgents folder like so

cp /usr/local/opt/nginx/*.plist ~/Library/LaunchAgents/
cp /usr/local/opt/mysql/*.plist ~/Library/LaunchAgents/
cp /usr/local/opt/php54/*.plist ~/Library/LaunchAgents/

Note: After copying them over, change ownership of all .plist files to that of root/admin to prevent any ownership issues.

cd ~/Library/LaunchAgents/
sudo chown root:admin homebrew*

As for starting and stopping a particular service, there are two options

  • Use the executable. $ sudo /usr/local/Cellar/nginx/1.4.3/sbin/nginx -s reload
  • Loading/Unloading the .plist $ sudo launchctl load homebrew.mxcl.nginx.plist

However, the .plist is not always accurate because it success silently and fails silently. If you're using the load/unload method, always check whether the process is running with

ps -auroot | grep nginx #Or whatever the name of the process is

These information can all be found in the output during after installation. If you can't find them by some means, you can use

$ brew list nginx

to get a list of directories related to the package.

PHP FPM

First let's cover some basic configuration. PHP FPM can run on a Unix socket or IP port, with the latter being the default. However, when you have multiple virtual hosts running on different ports, it's best not to use IP port in FPM configuration. Luckily, we can change that in the /usr/local/etc/php/5.4/php-fpm.conf.

Change the configuration from

listen = 127.0.0.1:9000

to the following

listen = /usr/local/var/run/php/php5-fpm.sock

and restart PHP-FPM

PHP Configuration

The php.ini governs how PHP behaves. In this tutorial, I'll only cover a best practice and configuring extensions.

In /usr/local/etc/php/5.4/php.ini, you will find the line

cgi.fix_pathinfo=1

You should change the value to 0. The reason for doing so is outlined in <a href="https://www.digitalocean.com/community/articles/how-to-install-linux-nginx-mysql-php-lemp-stack-on-ubuntu-12-04">this post</a>

<blockquote>
If this number is kept as 1, the php interpreter will do its best to process the file that is as near to the requested file as possible. This is a possible security risk. If this number is set to 0, conversely, the interpreter will only process the exact file path—a much safer alternative.
</blockquote>

Nginx Configuration

Configuration files for Nginx is different from its installation directory. On both Snow Leopard and Mountain Lion, the Nginx configuration files are located in

/usr/local/etc/nginx

First we would need to create 3 directories

  • conf.d holds commonly used configuration
  • sites-enabled holds all virtual host configuration
  • logs for all log files

Then we are going to edit the nginx.conf to the following

user  nobody;
worker_processes  2; #Number of CPU cores you want to allocate times 2

error_log   /usr/local/etc/nginx/logs/error.log;

#/usr/local/var/run/nginx directory should already exists. If not, create it
pid   /usr/local/var/run/nginx/nginx.pid;

events {
  worker_connections  1024;
}

http {
  include   mime.types;
  default_type  application/octet-stream;

  access_log  /usr/local/etc/nginx/logs/access.log;
  log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

  sendfile        on;
  #tcp_nopush     on;

  #keepalive_timeout  0;
  keepalive_timeout  65;

  gzip    on;

        #tell Nginx to include all configuration in there
  include   /usr/local/etc/nginx/sites-enabled/*;
}

Before setting up virtual host, first let's set up PHP-FPM configuration. Create a file called php in conf.d directory and place the following content

location ~ \.php$ {
    try_files   $uri = 404;
    include     fastcgi_params;
    fastcgi_pass    unix:/usr/local/var/run/php/php5-fpm.sock;
    fastcgi_index   index.php;
    fastcgi_param   SCRIPT_FILENAME $document_root$fastcgi_script_name;
}

Then in the sites-enabled directory, create a file called pma and place the following content

server {
  root    /usr/local/share/phpmyadmin;
  index   index.php;
  listen    80;
  error_log /usr/local/etc/nginx/logs/pma.error.log;
  access_log  /usr/local/etc/nginx/logs/pma.access.log;
  server_name pma.host;

  include   /usr/local/etc/nginx/conf.d/php;
}

Test your configuration with

$ sudo nginx -t

If successful, you can restart your Nginx configuration with

$ sudo nginx -s reload

Note: remember to create the host in /etc/hosts and the log files specified.

Fire up your browser, point it to http://pma.host/ and you're ready to Rock n' Roll! (Psssst, the default login for MySQL is root/root)

<a href="http://angelawang.me/wp-content/uploads/Screen-shot-2013-10-19-at-3.32.47-PM.png">Screen shot 2013-10-19 at 3.32.47 PM</a>

15 Responses
Add your response

Great tutorial. I was curious, why do you install mysql twice or is this the correct way to install mysql? You install one time with "brew install mysql" then you do it again when installing php "brew install --without-apache --with-fpm --with-mysql php54". I am trying to set up a local host (user level) using the amp stack (using Apache instead of nginx) with phpmyadmin through homebrew. Any clarification or recourses would be most grateful, thank you.

over 1 year ago ·

So, I'm still learning about server management and all. At the end of your phpmyadmin configuration, you made a note about creating the host in /etc/hosts. I'm not quite sure what to do here…

over 1 year ago ·

Hi @peterluu, the /etc/hosts is used for hostname override on your local machine, so you don't have to use IP address or the default 'localhost' for your web application. In this case, because I'm using a special hostname (pma.host) so I can access PHPMyAdmin using that hostname instead :) Hope it helps

over 1 year ago ·

Hi @brday, this command "brew install --without-apache --with-fpm --with-mysql php54" does not install MySQL. Instead it installs PHP 5.4 with driver support for MySQL. Notice that strings starting with '--' is most likely to be an option name.

over 1 year ago ·

Hi Angela, I managed to go through the tutorial quite nicely. Although I'm stuck at setting up the virtual host -> pma.host.
In the /etc/hosts file I have the entry:
pma.host localhost
but error 404 gets returned when i try to access http://pma.host/ in the browser. Would you happen to know the cause?

over 1 year ago ·

Hi @davidsupan, your entry should be
127.0.0.1 pma.host
instead of using localhost. Because localhost is eventually pointed back to 127.0.0.1, and I don't think the local hosts binding is smart enough to look twice at the /etc/hosts file. Hope it helps :)

over 1 year ago ·

Hi Angela, I am having some problems with a 502 bad gateway when I try to access http://pma.host. I can't seem to restart the php daemon. It keeps returning the following error:

ERROR: unable to bind listening socket for address '/usr/local/var/run/php/php5-fpm.sock': No such file or directory

What do you think the problem could be?

over 1 year ago ·

hi margakob, i am facing the same issue as @hayliwang. my php does not exists in /usre/local/var/run -

over 1 year ago ·

im getting a 502 bad request also. im using mac yosemite. btw, when i run 'echo $PHP_AUTOCONF', still a blankline and when i run 'which autoconf' i got this '/usr/local/bin/autoconf'. also i am confused when i am searching for this file '/usr/local/var/run/php/php5-fpm.sock' -> i dont have the '/usr/local/var/run/php/' directory... what did i do wrong in the setup?

NOTE: phpmyadmin is 502 request.

over 1 year ago ·

@brickgale, the php folder must be created by you. Can you show the content of the Nginx access log?

over 1 year ago ·

@haylichwang After creating the folder and the file (use the mkdir and touch commands individually), you also need to make sure that the permission is correct. If you still encounter such errors, you can take a look at your access/error log located in /usr/local/etc/nginx/logs

over 1 year ago ·

@angelathewebdev, i guess i just need to find this '/usr/local/var/run/php/php5-fpm.sock'? cause i encountered a mysql error which it didn't find mysql.sock and change php.ini. now i am running my laravel app through php artisan serve because i haven't setup the nginx properly yet. maybe the php5-fpm.sock is the problem?

over 1 year ago ·

Just in case, someone encounter the problem with php5-fpm.sock again.
This command: 'php56-fpm start', solve my problem.

over 1 year ago ·

I've found a tool called ServBay.dev can provide much easier way for PHP developers, especially the beginners. It includes all versions of PHP, MariaDB, and PostgreSQL, as well as Redis and Memcached. Users can run multiple versions of PHP instances all at once.
Trust me, it's made my PHP dev life a whole lot smoother. Might be worth a shot for you to check it out!

over 1 year ago ·