Skip to content

Instantly share code, notes, and snippets.

@DeeEmm
Forked from dtomasi/default
Last active February 18, 2025 00:30
Show Gist options
  • Select an option

  • Save DeeEmm/66e454dcc9bfdc51d7d27030d23944b0 to your computer and use it in GitHub Desktop.

Select an option

Save DeeEmm/66e454dcc9bfdc51d7d27030d23944b0 to your computer and use it in GitHub Desktop.
Brew Nginx PHP7

Install NGINX with PHP8.3-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 .test to 127.0.0.1. So you don´t need to touch hosts-File anymore.

NOTE: do not use .dev as this is a valid domain

Install

brew install dnsmasq

Configure

curl -L https://gist.githubusercontent.com/DeeEmm/66e454dcc9bfdc51d7d27030d23944b0/raw/40ad4f8a99934415ebd3e0beb348d01894a047c5/dnsmasq.conf -o /usr/local/etc/dnsmasq.conf

Add to the Local DNS

Edit your network connector in system settings ? network

and add the following to the DNS servers

127.0.0.1

You can check that it has been added by viewing /etc/resolv.conf

Start, Stop and Restart

# Start
sudo brew services start dnsmasq

# Stop
sudo brew services stop dnsmasq

# Restart
sudo brew services restart dnsmasq

Test

dig yourlocaldomain.test @127.0.0.1

You should also be able to test by ping

ping yourlocaldomain.test

PHP-FPM

Install php8.3

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

Configure

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

  user = YOUR_USERNAME
  group = staff

Testing

start php-fpm

sudo brew services start php@8.3

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 port 80
  curl -IL http://127.0.0.1:80

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.github.com/DeeEmm/66e454dcc9bfdc51d7d27030d23944b0/raw/40ad4f8a99934415ebd3e0beb348d01894a047c5/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.github.com/DeeEmm/66e454dcc9bfdc51d7d27030d23944b0/raw/40ad4f8a99934415ebd3e0beb348d01894a047c5/default -o /usr/local/etc/nginx/sites-available/default && \
curl -L https://gist.github.com/DeeEmm/66e454dcc9bfdc51d7d27030d23944b0/raw/40ad4f8a99934415ebd3e0beb348d01894a047c5/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;
}
address=/.test/127.0.0.1
listen-address=127.0.0.1
worker_processes auto;
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 /usr/local/var/log/nginx/access.log;
error_log /usr/local/var/log/nginx/error.log;
keepalive_timeout 65;
index index.html index.php;
include /usr/local/etc/nginx/sites-enabled/*;
}
location ~ \.php$ {
try_files $uri $uri/ /index.html /index.php?$args;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
nameserver 127.0.0.1
@DeeEmm

DeeEmm commented Feb 17, 2025

Copy link
Copy Markdown
Author

Note SSL untested

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment