Install php5.5 + php-fpm + mysql + nginx on Mac OS X Mavericks

Original author: frdmn
  • 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.

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 /Applicationsand 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:

Xcode.app → Preferences → Location |  Command Line Tools

Homebrew



Now you need to install Homebrew , which is the package manager for OS X. You may have already heard about apt-getor aptitudeon Linux distributions to install packages and dependencies for a specific application. brewit 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 $PATHin 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_installationto 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 autoconfwhich 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.defaultif you want to look at its code) and load the settings I created using curlGitHub:
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/.gitso 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-enabledfor 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:



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_profileusing 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 hostsusing 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

Also popular now: