Skip to content

Instantly share code, notes, and snippets.

@OsamaShabrez
Created August 3, 2018 13:41
Show Gist options
  • Save OsamaShabrez/a80a2d7e8f8aaa367f5b80550228ebb3 to your computer and use it in GitHub Desktop.
Save OsamaShabrez/a80a2d7e8f8aaa367f5b80550228ebb3 to your computer and use it in GitHub Desktop.
Install PHP7.1 + Nginx + MySQL on macOS Sierra
alias nginx.start='sudo launchctl load /Library/LaunchDaemons/homebrew.mxcl.nginx.plist'
alias nginx.stop='sudo launchctl unload /Library/LaunchDaemons/homebrew.mxcl.nginx.plist'
alias nginx.restart='nginx.stop && nginx.start'
alias php-fpm.start="launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.php56.plist"
alias php-fpm.stop="launchctl unload -w ~/Library/LaunchAgents/homebrew.mxcl.php56.plist"
alias php-fpm.restart='php-fpm.stop && php-fpm.start'
alias mysql.start="launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist"
alias mysql.stop="launchctl unload -w ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist"
alias mysql.restart='mysql.stop && mysql.start'

Install PHP + Nginx + MySQL on macOS

Requirements

  • An Intel CPU

  • OS X 10.10 or higher

  • Command Line Tools (CLT) for Xcode: xcode-select --install, developer.apple.com/downloads or Xcode

  • A Bourne-compatible shell for installation (e.g. bash or zsh)

Homebrew

Homebrew - is a package manager for macOS like apt for Debian.

To install Homebrew run this command in the terminal:

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

Run brew doctor and fix all the warnings (outdated Xcode/CLT and unbrewed dylibs are very likely to cause problems) if they will be.

If you already had Homebrew installed, update the existing Homebrew installation as well as the installed packages:

$ brew update && brew upgrade

back to topics

PHP-FPM

Homebrew doesn't come with PHP by default, so you'll need to add a repository for it:

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

Now you can install php:

$ brew install php71 --without-apache --with-fpm --with-mysql

Update the $PATH environment variable, if you want to use the PHP CLI:

$ echo 'export PATH="/usr/local/sbin:$PATH"' >> ~/.bash_profile && . ~/.bash_profile

To setup auto start create a directory for the LaunchAgents and add a symlink for the start/stop service:

$ mkdir -p ~/Library/LaunchAgents
$ ln -sfv /usr/local/opt/php71/homebrew.mxcl.php71.plist ~/Library/LaunchAgents/

Now you could launch php-fpm:

$ launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.php71.plist

back to topics

MySQL

To install MySQL run this command:

$ brew install mysql

To setup auto start add a symlink for the start/stop service:

$ ln -sfv /usr/local/opt/mysql/*.plist ~/Library/LaunchAgents

Run the MySQL-server:

$ launchctl load ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist

To secure your MySQL server you should execute the provided secure_mysql_installation binary to change the root password:

$ mysql_secure_installation

> Enter current password for root (enter for none):

Press Enter since you don't have one set.

> Change the root password? [Y/n]

It's up to you ;)

> Remove anonymous users? [Y/n]

They are not necessary, so press Enter.

> Disallow root login remotely? [Y/n]

Enter — No need to log in as root from any other IP than 127.0.0.1.

> Remove test database and access to it? [Y/n]

Enter — You don't need the testing tables.

> Reload privilege tables now? [Y/n]

Enter — Reload the privilege tables to ensure all of the changes made so far will take effect immediately.

When you done, test mysql:

$ mysql -u root -p
> Enter you password

back to topics

Nginx

To install Nginx run this command:

$ brew install nginx

To setup auto start:

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

Why sudo? Because only the root user is allowed to open ports which are < 1024.

Launch the server and test connection:

$ sudo launchctl load /Library/LaunchDaemons/homebrew.mxcl.nginx.plist
$ curl -IL http://127.0.0.1:8080

Next you need to create few folders, which are necessary for the 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

There are two ways to store your projects. First is to store them inside separate directory in home directory:

$ cd ~/projects

The second one is to store you projects in /var/www/ directory:

$ sudo mkdir -p /var/www
$ sudo chown :staff /var/www
$ sudo chmod 775 /var/www

I choose the first way, but it's up to you.

Replace the default nginx.conf with my custom config from GitHubGist:

$ sudo rm /usr/local/etc/nginx/nginx.conf
$ curl -L https://gist.githubusercontent.com/iggyster/f897571cfd420bc2aa238a0bd7954858/raw/e10bf2808bb09747c334a8573e5ba7320bb6fb70/nginx.conf -o /usr/local/etc/nginx/nginx.conf

Also download my custom PHP-FPM config:

$ curl -L https://gist.githubusercontent.com/iggyster/f897571cfd420bc2aa238a0bd7954858/raw/e10bf2808bb09747c334a8573e5ba7320bb6fb70/php-fpm -o /usr/local/etc/nginx/conf.d/php-fpm

And the last step is to setup virtual host. You could do it by your self, or you could download my custom virtual host config and setup it:

$ curl -L https://gist.githubusercontent.com/iggyster/f897571cfd420bc2aa238a0bd7954858/raw/e10bf2808bb09747c334a8573e5ba7320bb6fb70/test -o /usr/local/etc/nginx/sites-available/test
$ ln -sfv /usr/local/etc/nginx/sites-available/test /usr/local/etc/nginx/sites-enabled/

But don't forget to change the path to the root directory of your host and also to add test.php to the root.

After finishing you should restart nginx:

$ sudo launchctl load /Library/LaunchDaemons/homebrew.mxcl.nginx.plist

back to topics

Services control

To be easy control of services like nginx, php-fpm & mysql I propose you to download my alias list:

$ curl -L https://gist.githubusercontent.com/iggyster/f897571cfd420bc2aa238a0bd7954858/raw/e10bf2808bb09747c334a8573e5ba7320bb6fb70/bash_aliases -o /tmp/.bash_aliases
$ cat /tmp/.bash_aliases >> ~/.bash_aliases
$ echo "source ~/.bash_aliases" >> ~/.bash_profile && . ~/.bash_profile

All aliases you could find in More commands section.

back to topics

Update

To update PHP version in future try this commands:

$ brew remove php71
$ brew install php7.X --without-apache --with-mysql --with-fpm
$ rm ~/Library/LaunchAgents/homebrew.mxcl.php71.plist
$ ln -sfv /usr/local/opt/php7X/homebrew.mxcl.php7X.plist ~/Library/LaunchAgents/

And of course update your alias list:

$ sudo nano ~/.bash_aliases

...
alias php-fpm.start="launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.php7X.plist"
alias php-fpm.stop="launchctl unload -w ~/Library/LaunchAgents/homebrew.mxcl.php7X.plist"
...

back to topics

More commands

  • brew install [package] -

  • brew list - Show the list of all installed packages

  • brew search [search term] - List the possible packages that you can install

  • brew info [package] - Display some basic information about the package

  • brew remove [package] - Remove package

  • brew update - Self-update

  • brew upgrade [package] - Update package

  • brew doctor - Self-diagnose

  • brew tap [repo] - Add new Brew repository

  • brew help - List Brew commands

  • nginx.start - Start Nginx service

  • nginx.stop - Stop Nginx service

  • nginx.restart - Restart Nginx service

  • php-fpm.start - Start PHP-FPM service

  • php-fpm.stop - Stop PHP-FPM service

  • php-fpm.restart - Restart PHP-FPM service

  • mysql.start - Start MySQL server

  • mysql.stop - Stop MySQL server

  • mysql.restart - Retstart MySQL server

back to topics

Credits

Great thanks to @frdmn who inspired me to write my own guide.

back to topics

worker_processes 1;
error_log /usr/local/etc/nginx/logs/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 /usr/local/etc/nginx/logs/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;
}
server {
listen 80;
server_name test.local;
root /path/to/host/root/dir/;
access_log /path/to/host/root/dir/nginx.access.log;
error_log /path/to/host/root/dir/nginx.error.log;
location / {
include /usr/local/etc/nginx/conf.d/php-fpm;
try_files $uri $uri/ /test.php?$query_string;
}
}
@gabrielpuzl
Copy link

Hello, its works into php74?

@OsamaShabrez
Copy link
Author

@gabrieldev4 this script is 3years old but it should still work.

@Raensul
Copy link

Raensul commented Aug 4, 2021

Mac OS Big Sur MBP 2018.
It works!

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