Install Nginx and PHP-FPM on OS X

Enabling SPDY and HSTS on Nginx

Install Nginx via Homebrew

$ brew install nginx

Adding Nginx to startup routine

$ ln -sfv /usr/local/opt/nginx/*.plist /Library/LaunchDaemons/
$ sudo chown root:wheel /Library/LaunchDaemons/homebrew.mxcl.nginx.plist

To Start

$ launchctl load -w /Library/LaunchDaemons/homebrew.mxcl.nginx.plist

or

$ sudo nginx

To Stop

$ launchctl unload -w /Library/LaunchDaemons/homebrew.mxcl.nginx.plist

or

$ sudo nginx -s stop

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://127.0.0.1:8080
HTTP/1.1 200 OK
Server: nginx/1.8.0
Date: Thu, 23 Jul 2015 15:01:14 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Tue, 21 Apr 2015 17:50:12 GMT
Connection: keep-alive
ETag: "55368dd4-264"
Accept-Ranges: bytes

Installing PHP with FPM

Search for available PHP formulas (formula’s in homebrews are equivalent to packages in aptitude)

$ brew search php

It will return long list of php package

$ brew tap josegonzalez/php
$ brew tap homebrew/dupes

Before we build PHP, you may like to exercise options using

$ brew options php55

We have built it using

$ brew install php55 --with-fpm  --with-imap  --without-apache --with-debug

After long wait, you can verify php & php-fpm version using php -v and php-fpm -v respectively.

$ php -v
PHP 5.5.27 (cli) (built: Jul 23 2015 08:42:00) (DEBUG)
Copyright (c) 1997-2015 The PHP Group
Zend Engine v2.5.0, Copyright (c) 1998-2015 Zend Technologies

$ php-fpm -v
PHP 5.5.24 (fpm-fcgi) (built: May 19 2015 10:10:19)
Copyright (c) 1997-2015 The PHP Group
Zend Engine v2.5.0, Copyright (c) 1998-2015 Zend Technologies

Adding PHP-FPM to startup routine

$ ln -sfv /usr/local/opt/php55/*.plist /Library/LaunchDaemons/

To Start

$ launchctl load -w /Library/LaunchDaemons/homebrew.mxcl.php55.plist

or

$ php-fpm

To Stop

$ launchctl unload -w /Library/LaunchDaemons/homebrew.mxcl.php55.plist

Make sure PHP-FPM is listening on port 9000

$ lsof -Pni4 | grep LISTEN | grep php
php-fpm   38716 xuri    9u  IPv4 0xeae1b207b42a1721      0t0  TCP 127.0.0.1:9000 (LISTEN)
php-fpm   38717 xuri    0u  IPv4 0xeae1b207b42a1721      0t0  TCP 127.0.0.1:9000 (LISTEN)
php-fpm   38718 xuri    0u  IPv4 0xeae1b207b42a1721      0t0  TCP 127.0.0.1:9000 (LISTEN)
php-fpm   38719 xuri    0u  IPv4 0xeae1b207b42a1721      0t0  TCP 127.0.0.1:9000 (LISTEN)

Config Nginx

$ sudo vim /usr/local/etc/nginx/nginx.conf

Modify this section

location ~ \.php$ {
    root           html;
    fastcgi_index  index.php;
    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
    include        fastcgi_params;
}

... to ...

location ~ \.php$ {
    fastcgi_pass   127.0.0.1:9000;
    fastcgi_index  index.php;
    include fastcgi.conf;
}

Save and reload Nginx service

$ sudo nginx -s reload

Deny access to multiple folders on Nginx

location ~ /(folder1|folder2|folder3) {
    deny all;
    return 404;
}

Define 404 page

server {
    // ...
    error_page  404  /var/www/404.html;

Read more posts

Installation MAMP ( Apache+MySQL+PHP ) on a Mac with OS X 10.8 +
Get MAMP Working OS X 10.10 Yosemite
Installation LAMP ( Apache+MySQL+PHP ) on Ubuntu

0.00 avg. rating (0% score) - 0 votes