Skip to content

Instantly share code, notes, and snippets.

@wahyudibo
Last active April 25, 2020 23:13
Show Gist options
  • Star 12 You must be signed in to star a gist
  • Fork 10 You must be signed in to fork a gist
  • Save wahyudibo/5c8560ce63c0d57ac3e13855f07578fb to your computer and use it in GitHub Desktop.
Save wahyudibo/5c8560ce63c0d57ac3e13855f07578fb to your computer and use it in GitHub Desktop.
Install nginx, php 70 (with fpm), and mysql in Mac OS X
server {
listen 80;
server_name localhost;
root /Users/wahyudibo/Projects/mylabs/php/nginx;
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
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;
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}

Install MySQL

brew install mysql
mysql_secure_installation
brew services start mysql

Install PHP 7.0

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

add this to shell profile (for zsh)

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

start php70 services

brew services start php70

check php version. it should be using php7.0 and using local php

which php 
/usr/local/bin/php

php -v 
PHP 7.0

check if php-fpm is already running

lsof -Pni4 | grep LISTEN | grep php

php-fpm    1908 wahyudibo    6u  IPv4 0x23a0912e4e76185f      0t0  TCP 127.0.0.1:9000 (LISTEN)
php-fpm    1938 wahyudibo    0u  IPv4 0x23a0912e4e76185f      0t0  TCP 127.0.0.1:9000 (LISTEN)
php-fpm    1939 wahyudibo    0u  IPv4 0x23a0912e4e76185f      0t0  TCP 127.0.0.1:9000 (LISTEN)

Install Nginx (with multiple servers)

brew install nginx
brew services start nginx

change nginx.conf to the one attached in here. We will use port 80 and setup multiple servers. Then

sudo nginx

setup servers file just like the default.conf that attached in here. it will work for php-fpm. Then, except for localhost, you must register your servername to hosts file located in /private/etc/hosts

127.0.0.1 server.name

copy index.html from nginx default dir to your dir. then change file ownership

sudo cp /usr/local/var/www/index.html your-custom-folder
cd your-custom-folder
sudo chown wahyudibo:staff index.html

to reload or quit nginx

sudo nginx -s reload (do this after change conf file)
sudo nginx -s quit (to stop nginx)

to start nginx automatically (do this if brew services is not start nginx automatically)

sudo cp /usr/local/opt/nginx/*.plist /Library/LaunchDaemons
sudo launchctl load -w /Library/LaunchDaemons/homebrew.mxcl.nginx.plist

Notes

  • to know about nginx configuration, use nginx -V
  • if in error log nginx show 403 forbidden, set user in nginx.conf to match user in root folder. in this case : wahyudibo staff
  • test with php file contains <?php phpinfo(); ?> and load file via browser
user wahyudibo staff;
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;
gzip on;
include servers/*.conf;
}
@ClaudeHangui
Copy link

"copy index.html from nginx default dir to your dir. "
To which directory are you referring to ?

@maxim-grin
Copy link

Shouldn't you exchange the order of mysql_secure_installation and
brew services start mysql ?
Maybe it's only my case, but mysql_secure_installation didn't work before mysql was started

@quincykwende
Copy link

Yes same for me. May be this is because, MySql has no password at the time

@CoedNaked
Copy link

You must start mysql first before running mysql_secure_installation. If the server is not running then there is no mysql to connect to for the script to configure, the script is setup to connect to a running mysql server using no password.

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