
Install php5.5 + php-fpm + mysql + nginx on Mac OS X Mavericks
- Transfer
- Tutorial
Every web developer who chooses a Mac, after initial setup of the system, is looking for working tools. And if everything is clear with the IDE and editors, then it’s hard to find anything like this for the convenience of win-dvd OpenServer or Denwer for free. There is an excellent MAMP PRO solution , but it costs two thousand wooden ones. Yes, and working through Apache may confuse some.
While dealing with this issue, I came across an interesting material that talks about how to set up a workspace using the console package manager Homebrew in literally 5-10 minutes. I’m publishing its translation, because someone similar instructions on setting up a web environment on a Mac will definitely come in handy.

“I just got a new MacBook Pro and decided to set it up from scratch, because I have been using the same Time Machine backup for about four years now. A good chance to get rid of the web server / LAMP stack ( L inux A pache M ySQL P HP) and replace it with Nginx and PHP-FPM as an implementation of FastCGI. Below you can read how to configure Nginx, PHP-FPM, MySQL and PhpMyAdmin on OS X 10.9 / Mavericks.
First of all, install the latest version of Xcode via the Mac App Store:
Download Xcode.app (via the Mac App Store)
Once the download is complete, open Xcode in the folder
Open a Terminal window and install Xcode through the following command:
Confirm the installation with the button
Go back to Xcode, click

Now you need to install Homebrew , which is the package manager for OS X. You may have already heard about
First of all, we need Xquarz:
Now we need to download and install Homebrew with the following command:
We will believe in any conflicts and problems:
Update repositories and applications with Homebrew:
Because Homebrew does not have a default repository for PHP-FPM, we need to add it:
Install PHP-FPM with the following arguments:
Homebrew will download the PHP-FPM source code and compile it for you. Give him some time, this may take several minutes.
If you want to use PHP on the command line, you need to update the environment variable
And the start of PHP-FPM:
Make sure PHP-FPM is listening on port 9000:
The output should look something like this:
The next step is to install MySQL:
And start the database server:
For the security of our MySQL server, we will call the binary in the component
Please indicate the current password, if already set.
Press enter, specifying the password for the root user. If desired, save it in the LastPass or 1Password password managers.
Yes, they are not necessary.
Yes, there is no need for root authorization from any other IP except 127.0.0.1.
Yes. We do not need test tables.
Rebooting the privilege table will give us the opportunity to make sure that the changes take effect.
Enter the root password specified above and you will see the MySQL console:
End the session using the command
Install
Set the environment variable $ PHP_AUTOCONF:
Let's install phpMyAdmin:
Install Nginx with the command:
Since we are using port 80, we need to run Nginx as root:
Launch Nginx:
The configuration listens on port 8080 by default instead of the standard port 80 for the HTTP protocol. For now, ignore this:
The answer should look like this:
Stop Nginx again:
Create the folders that we will need during the next Nginx configuration:
Delete the current file
The configuration file is as simple and lightweight as possible: worker settings, log paths / formats, and several includes. Nothing extra unlike
Download my PHP-FPM settings from GitHub:
Clone a test virtual host (including rewrites for 404, and 403
And delete the folder
Create a folder for our SSL certificates and private keys:
Generate 4096bit RSA keys and self-signed certificates with the following command:
Now we need to create symlinks in the folder
Starting Nginx again:
Here it is, everything should work. Click on the links below to verify this:
Due to the fact that sooner or later you will need to restart one or another crashed, you may need additional aliases:
You can either open a new window / session of the Terminal or manually reboot
Now you can use aliases instead of typing long commands
You can start, stop and restart Nginx with the commands:
Quick access to logs:
Config check:
Start, stop and reload PHP-FPM:
Config check:
Start, stop and restart the MySQL server:
Let me know if you're stuck or have any add-ons! ”
On my own, I’ll add that when creating local domains and setting up Nginx to work with them, do not forget to write the pair “IP domain.name” in the file
PS The colleague mrded reports that he wrote a script to automatically install this stuff:
github.com/mrded/brew-emp
While dealing with this issue, I came across an interesting material that talks about how to set up a workspace using the console package manager Homebrew in literally 5-10 minutes. I’m publishing its translation, because someone similar instructions on setting up a web environment on a Mac will definitely come in handy.

“I just got a new MacBook Pro and decided to set it up from scratch, because I have been using the same Time Machine backup for about four years now. A good chance to get rid of the web server / LAMP stack ( L inux A pache M ySQL P HP) and replace it with Nginx and PHP-FPM as an implementation of FastCGI. Below you can read how to configure Nginx, PHP-FPM, MySQL and PhpMyAdmin on OS X 10.9 / Mavericks.
Xcode
First of all, install the latest version of Xcode via the Mac App Store:
Download Xcode.app (via the Mac App Store)
Once the download is complete, open Xcode in the folder
/Applications
and agree to the license. Open a Terminal window and install Xcode through the following command:
xcode-select --install
Confirm the installation with the button
Install
. Go back to Xcode, click
⌘ + ,
to access the settings and go to the Locations tab . Install Command Line Tools on the latest available version, Xcode 5.0.2 (5A3005) in my example:
Homebrew
Now you need to install Homebrew , which is the package manager for OS X. You may have already heard about
apt-get
or aptitude
on Linux distributions to install packages and dependencies for a specific application. brew
it also works only on computers running Mac OS X. It will also make sure you get the latest updates for installed applications, so you won’t have to worry about expired versions or security holes, exploits, and so on. First of all, we need Xquarz:
curl http://xquartz-dl.macosforge.org/SL/XQuartz-2.7.5.dmg -o /tmp/XQuartz.dmg
open /tmp/XQuartz.dmg
Now we need to download and install Homebrew with the following command:
ruby -e "$(curl -fsSL https://raw.github.com/Homebrew/homebrew/go/install)"
We will believe in any conflicts and problems:
brew doctor
Update repositories and applications with Homebrew:
brew update
brew upgrade
PHP-FPM
Because Homebrew does not have a default repository for PHP-FPM, we need to add it:
brew tap homebrew/dupes
brew tap josegonzalez/homebrew-php
Install PHP-FPM with the following arguments:
brew install --without-apache --with-fpm --with-mysql php55
Homebrew will download the PHP-FPM source code and compile it for you. Give him some time, this may take several minutes.
PHP configuration on the command line
If you want to use PHP on the command line, you need to update the environment variable
$PATH
in the file ~/.bash_profile
:echo 'export PATH="$(brew --prefix josegonzalez/php/php55)/bin:$PATH"' >> ~/.bash_profile
Autostart setup
mkdir -p ~/Library/LaunchAgents
cp /usr/local/Cellar/php55/5.5.9/homebrew-php.josegonzalez.php55.plist ~/Library/LaunchAgents/
And the start of PHP-FPM:
launchctl load -w ~/Library/LaunchAgents/homebrew-php.josegonzalez.php55.plist
Make sure PHP-FPM is listening on port 9000:
lsof -Pni4 | grep LISTEN | grep php
The output should look something like this:
php-fpm 69659 frdmn 6u IPv4 0x8d8ebe505a1ae01 0t0 TCP 127.0.0.1:9000 (LISTEN)
php-fpm 69660 frdmn 0u IPv4 0x8d8ebe505a1ae01 0t0 TCP 127.0.0.1:9000 (LISTEN)
php-fpm 69661 frdmn 0u IPv4 0x8d8ebe505a1ae01 0t0 TCP 127.0.0.1:9000 (LISTEN)
php-fpm 69662 frdmn 0u IPv4 0x8d8ebe505a1ae01 0t0 TCP 127.0.0.1:9000 (LISTEN)
MySQL
The next step is to install MySQL:
brew install mysql
Autostart setup
cp /usr/local/opt/mysql/*.plist ~/Library/LaunchAgents
And start the database server:
launchctl load ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist
Secure installation
For the security of our MySQL server, we will call the binary in the component
secure_mysql_installation
to change the root password, delete the anonymous user and disable the remote login feature under the root:mysql_secure_installation
> Enter current password for root (enter for none):
Please indicate the current password, if already set.
> Change the root password? [Y/n]
Press enter, specifying the password for the root user. If desired, save it in the LastPass or 1Password password managers.
> Remove anonymous users? [Y/n]
Yes, they are not necessary.
> Disallow root login remotely? [Y/n]
Yes, there is no need for root authorization from any other IP except 127.0.0.1.
> Remove test database and access to it? [Y/n]
Yes. We do not need test tables.
> Reload privilege tables now? [Y/n]
Rebooting the privilege table will give us the opportunity to make sure that the changes take effect.
Connection check
mysql -uroot -p
Enter the root password specified above and you will see the MySQL console:
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
End the session using the command
\q
:mysql> \q
Bye
phpMyAdmin
Install
autoconf
which is required for phpMyAdmin:brew install autoconf
Set the environment variable $ PHP_AUTOCONF:
echo 'PHP_AUTOCONF="'$(which autoconf)'"' >> ~/.bash_profile
Let's install phpMyAdmin:
brew install phpmyadmin
Nginx
Install Nginx with the command:
brew install nginx
Autostart setup
Since we are using port 80, we need to run Nginx as root:
sudo cp /usr/local/opt/nginx/*.plist /Library/LaunchDaemons/
sudo chown root:wheel /Library/LaunchDaemons/homebrew.mxcl.nginx.plist
Test web server
Launch Nginx:
sudo launchctl load /Library/LaunchDaemons/homebrew.mxcl.nginx.plist
The configuration listens on port 8080 by default instead of the standard port 80 for the HTTP protocol. For now, ignore this:
curl -IL http://localhost:8080
The answer should look like this:
HTTP/1.1 403 Forbidden
Server: nginx/1.4.4
Date: Sun, 08 Dec 2013 03:33:41 GMT
Content-Type: text/html
Content-Length: 168
Connection: keep-alive
Stop Nginx again:
sudo launchctl unload /Library/LaunchDaemons/homebrew.mxcl.nginx.plist
Further customization
nginx.conf
Create the folders that we will need during the next Nginx configuration:
mkdir -p /usr/local/etc/nginx/logs
mkdir -p /usr/local/etc/nginx/sites-available
mkdir -p /usr/local/etc/nginx/sites-enabled
mkdir -p /usr/local/etc/nginx/conf.d
mkdir -p /usr/local/etc/nginx/ssl
sudo mkdir -p /var/www
sudo chown :staff /var/www
sudo chmod 775 /var/www
Delete the current file
nginx.conf
(which will always be available at the address /usr/local/etc/nginx/nginx.conf.default
if you want to look at its code) and load the settings I created using curl
GitHub:rm /usr/local/etc/nginx/nginx.conf
curl -L https://gist.github.com/frdmn/7853158/raw/nginx.conf -o /usr/local/etc/nginx/nginx.conf
The configuration file is as simple and lightweight as possible: worker settings, log paths / formats, and several includes. Nothing extra unlike
nginx.conf.default
.Download PHP FPM
Download my PHP-FPM settings from GitHub:
curl -L https://gist.github.com/frdmn/7853158/raw/php-fpm -o /usr/local/etc/nginx/conf.d/php-fpm
Creating Virtual Hosts
curl -L https://gist.github.com/frdmn/7853158/raw/sites-available_default -o /usr/local/etc/nginx/sites-available/default
curl -L https://gist.github.com/frdmn/7853158/raw/sites-available_default-ssl -o /usr/local/etc/nginx/sites-available/default-ssl
curl -L https://gist.github.com/frdmn/7853158/raw/sites-available_phpmyadmin -o /usr/local/etc/nginx/sites-available/phpmyadmin
Clone a test virtual host (including rewrites for 404, and 403
phpinfo()
) using git
:git clone http://git.frd.mn/frdmn/nginx-virtual-host.git /var/www
rm -rf /var/www/.git
And delete the folder
/var/www/.git
so that git does not track subsequent changes.SSL setup
Create a folder for our SSL certificates and private keys:
mkdir -p /usr/local/etc/nginx/ssl
Generate 4096bit RSA keys and self-signed certificates with the following command:
openssl req -new -newkey rsa:4096 -days 365 -nodes -x509 -subj "/C=US/ST=State/L=Town/O=Office/CN=phpmyadmin" -keyout /usr/local/etc/nginx/ssl/localhost.key -out /usr/local/etc/nginx/ssl/localhost.crt
openssl req -new -newkey rsa:4096 -days 365 -nodes -x509 -subj "/C=US/ST=State/L=Town/O=Office/CN=localhost" -keyout /usr/local/etc/nginx/ssl/phpmyadmin.key -out /usr/local/etc/nginx/ssl/phpmyadmin.crt
Enabling Virtual Hosts
Now we need to create symlinks in the folder
sites-enabled
for virtual hosts in order to enable them:ln -sfv /usr/local/etc/nginx/sites-available/default /usr/local/etc/nginx/sites-enabled/default
ln -sfv /usr/local/etc/nginx/sites-available/default-ssl /usr/local/etc/nginx/sites-enabled/default-ssl
ln -sfv /usr/local/etc/nginx/sites-available/phpmyadmin /usr/local/etc/nginx/sites-enabled/phpmyadmin
Starting Nginx again:
sudo launchctl load /Library/LaunchDaemons/homebrew.mxcl.nginx.plist
Latest tests
Here it is, everything should work. Click on the links below to verify this:
- http: // localhost → “Nginx works” page
- http: // localhost / info →
phpinfo()
- http: // localhost / nope → "Not Found" page
- https: // localhost: 443 → “Nginx works” page (SSL)
- https: // localhost: 443 / info →
phpinfo()
(SSL) - https: // localhost: 443 / nope → “Not Found” page (SSL)
- https: // localhost: 306 → phpMyAdmin (SSL)
Service management
Due to the fact that sooner or later you will need to restart one or another crashed, you may need additional aliases:
curl -L https://gist.github.com/frdmn/7853158/raw/bash_aliases -o /tmp/.bash_aliases
cat /tmp/.bash_aliases >> ~/.bash_aliases
echo "source ~/.bash_aliases" >> ~/.bash_profile
You can either open a new window / session of the Terminal or manually reboot
~/.bash_profile
using the command:source ~/.bash_profile
Now you can use aliases instead of typing long commands
launchctl
, as above.Nginx
You can start, stop and restart Nginx with the commands:
nginx.start
nginx.stop
nginx.restart
Quick access to logs:
nginx.logs.access
nginx.logs.default.access
nginx.logs.phpmyadmin.access
nginx.logs.default-ssl.access
nginx.logs.error
nginx.logs.phpmyadmin.error
Config check:
[sudo] nginx -t
PHP-FPM
Start, stop and reload PHP-FPM:
php-fpm.start
php-fpm.stop
php-fpm.restart
Config check:
[sudo] php-fpm -t
MySQL
Start, stop and restart the MySQL server:
mysql.start
mysql.stop
mysql.restart
Let me know if you're stuck or have any add-ons! ”
On my own, I’ll add that when creating local domains and setting up Nginx to work with them, do not forget to write the pair “IP domain.name” in the file
hosts
using the command sudo vi /etc/hosts
. PS The colleague mrded reports that he wrote a script to automatically install this stuff:
github.com/mrded/brew-emp