Skip to content

Instantly share code, notes, and snippets.

@dtomasi dtomasi/default
Last active Oct 11, 2019

Embed
What would you like to do?
Brew Nginx PHP7

Install NGINX with PHP7-FPM on Mac OS X with Homebrew

Install Commandline Tools

xcode-select --install

Install Homebrew

ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

Check Installation

brew doctor

Install brew services

brew tap homebrew/services

Install bash completion (Optional)

brew install bash-completion

Update Brew and Packages if allready installed

brew update && brew upgrade

Setup Environment

sudo nano ~/.bash_profile

Add following lines

  ##
  # Homebrew
  ##
  export PATH="/usr/local/bin:$PATH"
  export PATH="/usr/local/sbin:$PATH"
  
  ##
  # Homebrew bash completion
  ##
  if [ -f $(brew --prefix)/etc/bash_completion ]; then
    source $(brew --prefix)/etc/bash_completion
  fi

DNSMasq

DNSMasq is used to resolve all domains that end with .dev to 127.0.0.1. So you don´t need to touch hosts-File anymore.

Install

brew install dnsmasq

Configure

curl -L https://gist.githubusercontent.com/dtomasi/ab76d14338db82ec24a1fc137caff75b/raw/550c84393c4c1eef8a3e68bb720df561b5d3f175/dnsmasq.conf -o /usr/local/etc/dnsmasq.conf

sudo curl -L https://gist.githubusercontent.com/dtomasi/ab76d14338db82ec24a1fc137caff75b/raw/550c84393c4c1eef8a3e68bb720df561b5d3f175/dev -o /etc/resolver/dev

Start, Stop and Restart

# Start
sudo brew services start dnsmasq

# Stop
sudo brew services stop dnsmasq

# Restart
sudo brew services restart dnsmasq

Test

dig testing.a.domain.that.should.point.to.localhost.dev @127.0.0.1

PHP-FPM

Install php70

  brew tap homebrew/dupes && \
  brew tap homebrew/php && \
  brew install --without-apache --with-fpm --with-mysql php70

Configure

sudo nano /usr/local/etc/php/7.0/php-fpm.d/www.conf

  user = YOUR_USERNAME
  group = YOUR_GROUP || staff

Testing

start php-fpm

sudo brew services start php70

show running processes

lsof -Pni4 | grep LISTEN | grep php

NGINX

Install NGINX

brew tap homebrew/nginx && \
brew install nginx

Test Installation

  ## Start Nginx
  sudo brew services start nginx
  
  ## Check if Nginx is running on default port
  curl -IL http://127.0.0.1:8080

Output should look like this

HTTP/1.1 200 OK
Server: nginx/1.10.0
Date: Sat, 07 May 2016 07:36:32 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Tue, 26 Apr 2016 13:31:24 GMT
Connection: keep-alive
ETag: "571f6dac-264"
Accept-Ranges: bytes

Stop Nginx

sudo brew services stop nginx

####Configure

Create missing directories

  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

Configure nginx.conf

# Remove default
rm /usr/local/etc/nginx/nginx.conf
# Copy mine
curl -L https://gist.githubusercontent.com/dtomasi/ab76d14338db82ec24a1fc137caff75b/raw/c7c99476e6d8bd5b23e814c5593861adb9b54765/nginx.conf -o /usr/local/etc/nginx/nginx.conf

Start and Test Nginx

  ## Start Nginx
  sudo brew services start nginx
  
  ## Check if Nginx is running on default port
  curl -IL http://localhost

  ## Output should look like this
  HTTP/1.1 200 OK
  Server: nginx/1.10.0
  Date: Sat, 07 May 2016 08:35:57 GMT
  Content-Type: text/html
  Content-Length: 612
  Last-Modified: Tue, 26 Apr 2016 13:31:24 GMT
  Connection: keep-alive
  ETag: "571f6dac-264"
  Accept-Ranges: bytes

Setup SSL

Create a folder for our SSL certificates and private keys:

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

Generate 4096 bit 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

Setup example virtual hosts

These are working presets. But you need to edit Document-Root

curl -L https://gist.githubusercontent.com/dtomasi/ab76d14338db82ec24a1fc137caff75b/raw/c7c99476e6d8bd5b23e814c5593861adb9b54765/default -o /usr/local/etc/nginx/sites-available/default && \
curl -L https://gist.githubusercontent.com/dtomasi/ab76d14338db82ec24a1fc137caff75b/raw/c7c99476e6d8bd5b23e814c5593861adb9b54765/default-ssl -o /usr/local/etc/nginx/sites-available/default-ssl

Activate Virtual Hosts

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

Create info.php for testing echo "<?php phpinfo();" > /path/to/your/document/root

Test

sudo brew services restart nginx

curl -IL http://localhost/info.php

# Output should look like this
HTTP/1.1 200 OK
Server: nginx/1.10.0
Date: Sat, 07 May 2016 08:40:36 GMT
Content-Type: text/html; charset=UTF-8
Connection: keep-alive
X-Powered-By: PHP/7.0.6
server {
listen 80;
server_name localhost;
root /Users/YOUR_USERNAME/Sites;
access_log /Library/Logs/default.access.log main;
location / {
include /usr/local/etc/nginx/conf.d/php-fpm;
}
location = /info {
allow 127.0.0.1;
deny all;
rewrite (.*) /.info.php;
}
error_page 404 /404.html;
error_page 403 /403.html;
}
server {
listen 443;
server_name localhost;
root /Users/YOUR_USERNAME/Sites;
access_log /Library/Logs/default-ssl.access.log main;
ssl on;
ssl_certificate ssl/localhost.crt;
ssl_certificate_key ssl/localhost.key;
ssl_session_timeout 5m;
ssl_protocols SSLv2 SSLv3 TLSv1;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
location / {
include /usr/local/etc/nginx/conf.d/php-fpm;
}
location = /info {
allow 127.0.0.1;
deny all;
rewrite (.*) /.info.php;
}
error_page 404 /404.html;
error_page 403 /403.html;
}
nameserver 127.0.0.1
address=/.dev/127.0.0.1
listen-address=127.0.0.1
worker_processes 1;
error_log /Library/Logs/nginx/error.log debug;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /Library/Logs/nginx/access.log main;
sendfile on;
keepalive_timeout 65;
index index.html index.php;
include /usr/local/etc/nginx/sites-enabled/*;
}
location ~ \.php$ {
try_files $uri = 404;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
@baumannsven

This comment has been minimized.

Copy link

commented Aug 2, 2016

By section dnsmasq => configure.
Before download the dev in /etc/resolver, must be create the resolver directory.

@baumannsven

This comment has been minimized.

Copy link

commented Aug 3, 2016

By sites configuration default in line 15 and default-ssl in line 25 replace with:
rewrite (.*) /info.php;

@baumannsven

This comment has been minimized.

Copy link

commented Aug 3, 2016

Replace the correctly curl url for:

  • default
  • default-ssl
@baumannsven

This comment has been minimized.

Copy link

commented Aug 3, 2016

After download site configuration, download php-fpm configuration.

@raymondjplante

This comment has been minimized.

Copy link

commented Jan 2, 2017

MySQL and nginx modules are installed by default in the php70 install (apache is disabled by default).
--without-apache --with-fpm --with-mysql is unneeded as the options no longer exist and you'll get warnings from brew (it will probably still build fine, but it always hurts my OCD).

Just run:

   brew install php70
@ghost

This comment has been minimized.

Copy link

commented Feb 14, 2017

In the section
"Test Installation and Start and Test Nginx"
curl -IL http://127.0.0.1:8080
curl -IL http://localhost

I get curl: (7) Failed to connect to 127.0.0.1 port 8080: Connection refused and curl: (7) Failed to connect to localhost port 80: Connection refused

Help! Everything else went fine with no trouble, I just can't view anything.

@johnwilson

This comment has been minimized.

Copy link

commented Mar 22, 2017

@the-interactive-company you might have manually create the /Library/Logs/nginx/ directory

@chaeMil

This comment has been minimized.

Copy link

commented Mar 22, 2017

i keep getting bad gateway error when trying to run php files. Im on El Capitan.

@datadimension

This comment has been minimized.

Copy link

commented Mar 25, 2017

This is completely full of bugs and direct rip off of https://blog.frd.mn/install-nginx-php-fpm-mysql-and-phpmyadmin-on-os-x-mavericks-using-homebrew/ for an older version of PHP.
It does more damage that usefulness - cannot even get HTML to work now.

@njsubedi

This comment has been minimized.

Copy link

commented Apr 4, 2017

@chaeMil your php-fpm instance is not running.

@tobecwb

This comment has been minimized.

Copy link

commented Jun 1, 2017

Some corrections to make this thing works:

on PHP-FPM, in Configure (sudo nano /usr/loca/etc/php7.0/php-fpm.d/www.conf), use:

user = YOUR_USERNAME
group = staff

(don't need the YOUR_GROUP || staff)

After Stop Nginx, Create missing directories and Configure nginx.conf, edit nginx.conf and make following changes:

worker_process auto;

remove the line error_log /Library/Logs/nginx/error.log debug;

inside http section, insert/update the following lines:

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

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

remove sendfile on;

then, the php-fpm must be configured.

create a file in /usr/local/etc/nginx/conf.d/php-fpm with the following content:

location ~ \.php$ {
    try_files      $uri /index.html index.php;
    fastcgi_pass   127.0.0.1:9000;
    fastcgi_index  index.php;
    fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include        fastcgi_params;
}

and finally, configure the virtual hosts.

edit the files /usr/local/etc/nginx/sites-available/default and /usr/local/etc/nginx/sites-available/default-ssl, and remove the files access_log (access log is already configured in nginx.conf

I don't remember if this is all that I changed, but worked fine after this.

@birchcode

This comment has been minimized.

Copy link

commented Nov 24, 2017

Thank you for wasting my time with this buggy shit.

@apedicdev

This comment has been minimized.

Copy link

commented Nov 24, 2017

@birchcode you may write yours

@fsebbah

This comment has been minimized.

Copy link

commented Feb 20, 2018

@tobecwb : your config are correct just in the nginx.conf at the header it's important to add
user YOUR_NAME YOUR_GROUP;
without this you have an 403 error

Here you find a good config nginx website

http://www.leeladharan.com/installing-php-7-with-nginx-on-mac-os-x

@thanhoangxuannghiep

This comment has been minimized.

Copy link

commented Apr 22, 2018

I cannot tap to homebrew/dupes. When I run command "brew tap homebrew/dupes". I got this message: "Error: homebrew/dupes was deprecated. This tap is now empty as all its formulae were migrated". How's about this issue?

@bindzus

This comment has been minimized.

Copy link

commented Apr 26, 2018

@thanhoangxuannghiep I don't think you need homebrew/dupes anymore, it looks like everything you need is available from Homebrew core.

@pooyagolchian

This comment has been minimized.

Copy link

commented Jun 18, 2018

sudo nginx -t
nginx: [emerg] open() "/usr/local/etc/nginx/conf.d/php-fpm" failed (2: No such file or directory) in /usr/local/etc/nginx/sites-enabled/default:9
nginx: configuration file /usr/local/etc/nginx/nginx.conf test failed

Why php-fpm is not in conf.d folder?

@maitandat1507

This comment has been minimized.

Copy link

commented Oct 7, 2018

Some corrections to make this thing works:

on PHP-FPM, in Configure (sudo nano /usr/loca/etc/php7.0/php-fpm.d/www.conf), use:

user = YOUR_USERNAME
group = staff

(don't need the YOUR_GROUP || staff)

After Stop Nginx, Create missing directories and Configure nginx.conf, edit nginx.conf and make following changes:

worker_process auto;

remove the line error_log /Library/Logs/nginx/error.log debug;

inside http section, insert/update the following lines:

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

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

remove sendfile on;

then, the php-fpm must be configured.

create a file in /usr/local/etc/nginx/conf.d/php-fpm with the following content:

location ~ \.php$ {
    try_files      $uri /index.html index.php;
    fastcgi_pass   127.0.0.1:9000;
    fastcgi_index  index.php;
    fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include        fastcgi_params;
}

and finally, configure the virtual hosts.

edit the files /usr/local/etc/nginx/sites-available/default and /usr/local/etc/nginx/sites-available/default-ssl, and remove the files access_log (access log is already configured in nginx.conf

I don't remember if this is all that I changed, but worked fine after this.

Thanks for your php-fpm configuration content! It save my day!

@maitandat1507

This comment has been minimized.

Copy link

commented Oct 7, 2018

And I think this resource is useful for understanding "location ~ .php$ {}" configuration
https://www.digitalocean.com/community/tutorials/understanding-and-implementing-fastcgi-proxying-in-nginx

@maitandat1507

This comment has been minimized.

Copy link

commented Oct 7, 2018

I cannot tap to homebrew/dupes. When I run command "brew tap homebrew/dupes". I got this message: "Error: homebrew/dupes was deprecated. This tap is now empty as all its formulae were migrated". How's about this issue?

Try to update & upgrade your Homebrew version:
brew update && brew upgrade

@circa42

This comment has been minimized.

Copy link

commented Jan 17, 2019

What if when I run $ curl -IL http://127.0.0.1:80 I get this output?

Server: nginx/1.15.8
Date: Thu, 17 Jan 2019 01:47:38 GMT
Content-Type: text/html; charset=utf-8
Connection: keep-alive```
@rico-et22

This comment has been minimized.

Copy link

commented Jun 10, 2019

Your default virtualhost has a root path of /Users/Dominik/Sites which isn't a default path on many other computers, can you change it to /usr/local/var/www which is default on homebrew nginx?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.