Sunday, January 13, 2013

Install php with nginx and php-fpm using homebrew osx

How I setup a osx php development environment with nginx and php-fpm using Homebrew

I didn't find any documentation specific to my situation of developing in osx and using the homebrew package manager. I hope to provide some useful configuration file settings, directory paths, comments, and a reference for myself. 

This is intended for performing development work on a mac, not running a live production system. 

I am running OSX 10.7. 5. 

Install Homebrew if you have not already. 

Add the php repository and install, adding compile flags for php-fpm and other tools you will be working with, such as mysql. For a list of options one may type 'brew options php54'

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

In another terminal start php-fpm

  $ php-fpm 

This will start php-fpm in the foreground, nice for debugging and seeing that everything is working. Later one can use php-fpm -D to daemonize it.  

Start nginx 

 $ sudo nginx

Test it by entering http://localhost into your web browser. A welcome page should appear.  The welcome page is located in /usr/local/Cellar/nginx/1.2.5/html/index.html

Edit the configs

I will use vi but any text editor is fine such as nano. 


The config file for php-fpm is called php-fpm.conf and is found in /usr/local/etc/php/5.4 along with php.ini . I left most of the settings alone, but made a backup copy of the file and set

  pid = run/

which tells it to create in /usr/local/var/run. This might be useful for a startup script, and nice to see something that says php-fpm is running and reading the config file. One can also set the port, error log, and a bunch of other things in php-fpm.conf. 

Set the PHP date.timezone to your time zone

  $ vi /usr/local/etc/php/5.4/php.ini
  date.timezone = Europe/Berlin


nginx.conf is located in /usr/local/etc/nginx. 

Make a backup of nginx.conf

Create a folder for logs 

  $ mkdir /usr/local/var/log/nginx

In nginx.conf update 

  error_log  /usr/local/var/log/nginx/error.log;

In the http section

  access_log  /usr/local/var/log/nginx/localhost.access.log  main;

In the server section

  listen       80;


  root   /Users/homedir/php-projects; 
  index  index.html index.php;


  location ~ \.php$ {
    root           /usr/local/var/www;
    fastcgi_index  index.php;
    fastcgi_param  SCRIPT_FILENAME document_root$fastcgi_script_name;
    include        fastcgi_params;

Complete nginx.conf

Reload nginx

  $ sudo nginx -s reload

Create a test index.php file in your php projects folder and test it out. 



Kevin Renskers said...

Very nice writeup, however there are some problems with your nginx config example. The php part should be:

location ~ \.php$ {
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;

Sara said...

I am looking for a code so i can convert my apps on computer programs so i can use them in both devices or maybe in my ipad, so what i am looking for is a way to download whatsapp for my pc