Install Nginx, PHP-FPM, MySQL and phpMyAdmin on OS X Yosemite

Recently my APM(Apache+PHP+MariaDB) installation from macPorts broken down. So is the perfect time to get rid of the web server stack and replace it with Nginx and PHP-FPP as FastCGI implementation. Below you can read how to setup Nginx, PHP-FPM, MySQL and PhpMyAdmin on OS X 10.10 (Yosemite).

Xcode

First of all, get the last Xcode version via Mac App Store (or at this time, from Developer Member Page). As soon as you have finished the download, open Xcode and agree to the license and click install on the first window how will open.

If this not happen, open a new terminal window and install the Xcode Command Line Tools:

xcode-select --install

Back in Xcode, hit ⌘ + , to access the Preferences and navigate to the Locations tab. Set the Command Line Tools to the latest version available, Xcode 6.0 (6A254o) in my case:

Xcode 6.0 CLT

Homebrew

Now we need install Homebrew, which is a package manager for OS X. We can use other like MacPorts for need more manual configurations, but is also a good package manager.

First, we need Xquarz:

curl http://xquartz-dl.macosforge.org/SL/XQuartz-2.7.6.dmg -o /tmp/XQuartz.dmg
open /tmp/XQuartz.dmg

Now we need to download and install Homebrew with re following command:

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

Check for any conflicts or problems:

brew doctor

Is in this show an error related with clan location install CLT (Comamnd Line Tools) from this website.

Update and upgrade Homebrew formulas


brew update

brew upgrade

PHP-FPM

Because Homebrew doesn’t have default formulas for PHP-FPM, we need to add this first:

brew tap homebrew/dupes
brew tap josegonzalez/homebrew-php

Now install it with following arguments:

brew install --without-apache --with-fpm --with-mysql php55

Homebrew is downloading now the PHP-FPM source code and compiling it for you. Give it some time, it can take several minutes.

Setup auto start

mkdir -p ~/Library/LaunchAgents
cp /usr/local/Cellar/php55/5.5.14/homebrew.mxcl.php55.plist ~/Library/LaunchAgents/

And start PHP-FPM

launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.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   26420 gil0mendes    6u  IPv4 0x77cdec4167b92755      0t0  TCP 127.0.0.1:9000 (LISTEN)
php-fpm   26422 gil0mendes    0u  IPv4 0x77cdec4167b92755      0t0  TCP 127.0.0.1:9000 (LISTEN)
php-fpm   26423 gil0mendes    0u  IPv4 0x77cdec4167b92755      0t0  TCP 127.0.0.1:9000 (LISTEN)
php-fpm   26424 gil0mendes    0u  IPv4 0x77cdec4167b92755      0t0  TCP 127.0.0.1:9000 (LISTEN)

MariaDB

Next step is to install MySQL:

brew install mysql

Setup auto start

ln -sfv /usr/local/opt/mysql/*.plist ~/Library/LaunchAgents

And start the database server:

launchctl load ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist

Secure the installation

To secure the MySQL server, we’ll execute the provided secure_mysql_installation binary to change the root password, remove anonymous users and disable remote too login:

mysql_secure_installation

Test connection

mysql -uroot -p

Enter your root password and you should see the MySQL console:

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>

To quit the session use \q:

mysql> \q
Bye

phpMyAdmin

Install autoconf which you need for the installation of phpMyAdmin:

brew install autoconf

Set $PHP_AUTOCONF. For bash users:

echo 'PHP_AUTOCONF="'$(which autoconf)'"' >> ~/.profile

For zsh users:

echo 'PHP_AUTOCONF="'$(which autoconf)'"' >> ~/.zshrc

Let’s start with the installation of phpMyAdmin:

brew install phpmyadmin

Nginx

Install the default Nginx with:

brew install nginx

Setup auto start

Since we want to use port 80 have to start the Nginx process as root:

sudo cp /usr/local/opt/nginx/*.plist /Library/LaunchDaemons/
sudo chown root:wheel /Library/LaunchDaemons/homebrew.mxcl.nginx.plist

Test web server

Start Nginx for the first time:

sudo launchctl load /Library/LaunchDaemons/homebrew.mxcl.nginx.plist

The default configuration is set that it will listen on port 8080 instead of the HTTP standard 80. Ignore that for now:

curl -IL http://localhost:8080

The output should look like:

HTTP/1.1 200 OK
Server: nginx/1.6.0
Date: Tue, 08 Jul 2014 21:40:38 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Tue, 08 Jul 2014 21:35:25 GMT
Connection: keep-alive
ETag: "53bc641d-264"
Accept-Ranges: bytes

Stop Nginx again:

sudo launchctl unload /Library/LaunchDaemons/homebrew.mxcl.nginx.plist

More configurations

nginx.conf

Create some folders which we are going to use in the configuration files:

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

Remove the current default nginx.conf (is also available as /usr/local/etc/nginx/nginx.conf.default in case you want to take a look) and download this custom one via curl from 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

Load PHP FPM

Download PHP-FPM configuration from GitHub:

curl -L https://gist.github.com/frdmn/7853158/raw/php-fpm -o /usr/local/etc/nginx/conf.d/php-fpm

Create default 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 my example virtual host (including 404, 403, and phpinfo() rewrite) using git

git clone https://github.com/gil0mendes/nginx-virtual-host.git /var/www
rm -rf /var/www/.git

Setup SSL

Create folder for our SSL certificates and private keys:

mkdir -p /usr/local/etc/nginx/ssl

Generate 4096bit RSA keys and the self-sign the certificates in one command:

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/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=phpmyadmin" -keyout /usr/local/etc/nginx/ssl/phpmyadmin.key -out /usr/local/etc/nginx/ssl/phpmyadmin.crt

Enable virtual hosts

Now we need to symlink the virtual hosts we want to enable into the sites-enabled folder:

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

Start Nginx again:

sudo launchctl load /Library/LaunchDaemons/homebrew.mxcl.nginx.plist

Final tests

Thats it, everything should be up and running. Click on the links below to ensure that:

Control the services like a boss

Because you probably need to restart the one or other service sooner or later, you probably want to set up some alias.
Download alias:

curl -L https://gist.githubusercontent.com/gil0mendes/2ebff506699fea364adf/raw/ed04e90d7770dbb62c60e1e4a912f75adc46cb5e/aliases -o /tmp/.aliases

For bash:

cat /tmp/.aliases >> ~/.profile

For zsh:

cat /tmp/.aliases >> ~/.zshrc 

Close and open the terminal or type “source ~/.profile” or “source ~/.zshrc” to reload the profile.

Let me know in case you stuck at some point or you have some suggestions!

 

Update 1

This article was based on the article made by Jonas Friedmann.

13 pensamentos sobre “Install Nginx, PHP-FPM, MySQL and phpMyAdmin on OS X Yosemite

  1. Pingback: OSX Tools | A Coder's Life

  2. I have an issue copying php55.plist to LaunchAgents:
    cp: cannot stat ‘/usr/local/Cellar/php55/5.5.14/homebrew.mxcl.php55.plist’: No such file or directory

    thus I cannot start php and run further configuration. Any help is appreciated

    • This tutorial is really outdated and some of the commands can’t work anymore… sorry!

      My suggestion to you (now), is using virtual machines, maybe vagrant. There is a lot of boxes with these services pre-installed.

      Thanks for the comment.

Deixe uma Resposta

Preencha os seus detalhes abaixo ou clique num ícone para iniciar sessão:

Logótipo da WordPress.com

Está a comentar usando a sua conta WordPress.com Terminar Sessão / Alterar )

Imagem do Twitter

Está a comentar usando a sua conta Twitter Terminar Sessão / Alterar )

Facebook photo

Está a comentar usando a sua conta Facebook Terminar Sessão / Alterar )

Google+ photo

Está a comentar usando a sua conta Google+ Terminar Sessão / Alterar )

Connecting to %s