Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thanhoangxuannghiep/c278ea1d9c9e7355c0973108ab11523f to your computer and use it in GitHub Desktop.
Save thanhoangxuannghiep/c278ea1d9c9e7355c0973108ab11523f to your computer and use it in GitHub Desktop.
How to install PHP, Nginx, Magento with homebrew on MacOS high sierra

After a long time to research and fix many issues. Now, my config has work fine. I want to create this article to share my experiences when I start work on MacOS.

First, I want to introduce about myself. I had learnt and worked on Windows OS when I was 15 years old. Then, on June-2017, I join into a company ecomerce. They develop base on Magento. They require me can be install and using Ubuntu OS. At that moment, I look like a student has just graduated althought I had 3 years experiences.

When I work on Windows OS, of course, I use third party to build enviroment. It's name is xampp. However, on Ubuntu OS, I must learn to install webserver(apache), mysql, record some commands in my memory. After 1 year, I buy for myself a Macbook pro and I decided install Nginx on MacOS. Why I did it? Because I heard from my partner, we just only config once time if we use nginx. Beside that, we will run nginx with php-fpm. It will improve about performance when we run a project has high level complicate.

All right, Talk about myself is enough, Let talk about Nginx, PHP with Homebrew on MacOS high Sierra 10.13.4.

1. Install Commandline Tools

We should install Xcode. Then, we will Install Commandline Tools

xcode-select --install

2. 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

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

3. Install DNSMASQ

Note on this. Until to this moment, I still do not understand what's dnsmasq and how to config it. It's really very comlex. However, I found a solution for this: using hosts file. It's easier and simple.

4. Install PHP

To install PHP

I have read some topics to talk about this step. Almost composer said we should tap formulaes homebrew/dupes, homebrew/php. However, on my Mac, I cannot tap it. I think It had been removed out off homebrew. So, I decided to tap homebrew/core and so lucky when It works fine :))). Alright, let's run these command to install PHP

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

Why did I install PHP70? I have tried to install PHP72, however, when I run magento site, I meet some issues and I guess the root cause is about version PHP. Therefore, I decided install this version.

Config PHP-FPM

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

  user = YOUR_USERNAME
  group = YOUR_GROUP || staff
Testing

Start PHP

sudo brew services start php70

Check version

php -v

Check processing As my way, I run this command:

ps aux | grep -r "php"

4. nginx

Install nginx

To install nginx, run these commands:

brew tap homebrew/nginx && \
brew install nginx
Testing
## 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

Then, to config Nginx. You should stop it.

sudo brew services stop nginx

Configure Nginx

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

To run as normally with port 80. You can open file usr/local/etc/nginx/nginx.conf and edit line 'listen' in section server to 80. Then, 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

Note: Remember using sudo to run the services with port less than 1000

Setup example virtual host

Setup example virtual hosts Because MacOS prevent Domain *.loc, I use virtual domain *.local. Now, you can download my example config.

curl -L https://gist.github.com/thanhoangxuannghiep/c278ea1d9c9e7355c0973108ab11523f/raw/e019dcec0e3f136c1d2cb87c1b901f82b1b6ef5c/nginx.conf -o /usr/local/etc/nginx/nginx.conf
curl -L https://gist.github.com/thanhoangxuannghiep/c278ea1d9c9e7355c0973108ab11523f/raw/e019dcec0e3f136c1d2cb87c1b901f82b1b6ef5c/mage  -o /usr/local/etc/nginx/sites-available/default

After download my config files. You should edit your root, your servername and active your virtual host

ln -sfv /usr/local/etc/nginx/sites-available/default /usr/local/etc/nginx/sites-enabled/default Create info.php for testing echo "<?php phpinfo();" > /path/to/your/document/root Test

sudo brew services restart nginx

curl -IL http://mage.local/info.php

# Output should look like this
HTTP/1.1 200 OK
Server: nginx/1.13.12
Date: Fri, 10 Aug 2018 17:34:21 GMT
Content-Type: text/html; charset=UTF-8
Connection: keep-alive
X-Powered-By: PHP/7.0.30

Done.

server {
listen 80;
server_name mage.local;
root /Users/nghiepthan/Sites/html/pub;
set $MAGE_ROOT /Users/nghiepthan/Sites/html;
access_log /Users/nghiepthan/Sites/html/nginx.access.log;
error_log /Users/nghiepthan/Sites/html/nginx.error.log;
location / {
include /usr/local/etc/nginx/conf.d/php-fpm;
try_files $uri $uri/ /index.php$is_args$args;
}
location /pub/ {
location ~ ^/pub/media/(downloadable|customer|import|theme_customization/.*\.xml) {
deny all;
}
alias $MAGE_ROOT/pub/;
add_header X-Frame-Options "SAMEORIGIN";
}
location /static/ {
# Uncomment the following line in production mode
# expires max;
# Remove signature of the static files that is used to overcome the browser cache
location ~ ^/static/version {
#rewrite ^/static/(version[^/]+/)?(.*)$ /static/$2 last;
rewrite ^/static/(version\d*/)?(.*)$ /static/$2 last;
}
location ~* \.(ico|jpg|jpeg|png|gif|svg|js|css|swf|eot|ttf|otf|woff|woff2)$ {
add_header Cache-Control "public";
add_header X-Frame-Options "SAMEORIGIN";
expires +1y;
if (!-f $request_filename) {
rewrite ^/static/?(.*)$ /static.php?resource=$1 last;
}
}
location ~* \.(zip|gz|gzip|bz2|csv|xml)$ {
add_header Cache-Control "no-store";
add_header X-Frame-Options "SAMEORIGIN";
expires off;
if (!-f $request_filename) {
rewrite ^/static/?(.*)$ /static.php?resource=$1 last;
}
}
if (!-f $request_filename) {
rewrite ^/static/?(.*)$ /static.php?resource=$1 last;
}
add_header X-Frame-Options "SAMEORIGIN";
}
location /media/ {
try_files $uri $uri/ /get.php$is_args$args;
location ~ ^/media/theme_customization/.*\.xml {
deny all;
}
location ~* \.(ico|jpg|jpeg|png|gif|svg|js|css|swf|eot|ttf|otf|woff|woff2)$ {
add_header Cache-Control "public";
add_header X-Frame-Options "SAMEORIGIN";
expires +1y;
try_files $uri $uri/ /get.php$is_args$args;
}
location ~* \.(zip|gz|gzip|bz2|csv|xml)$ {
add_header Cache-Control "no-store";
add_header X-Frame-Options "SAMEORIGIN";
expires off;
try_files $uri $uri/ /get.php$is_args$args;
}
add_header X-Frame-Options "SAMEORIGIN";
}
location /media/customer/ {
deny all;
}
location /media/downloadable/ {
deny all;
}
location /media/import/ {
deny all;
}
# PHP entry point for main application
location ~ (index|get|static|report|404|503|health_check)\.php$ {
try_files $uri =404;
fastcgi_pass 127.0.0.1:9000;
fastcgi_buffers 1024 4k;
fastcgi_param PHP_FLAG "session.auto_start=off \n suhosin.session.cryptua=off";
fastcgi_param PHP_VALUE "memory_limit=756M \n max_execution_time=18000";
fastcgi_read_timeout 600s;
fastcgi_connect_timeout 600s;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
worker_processes auto;
error_log /usr/local/etc/nginx/logs/error.log;
#access_log /usr/local/etc/nginx/logs/access.log;
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;
keepalive_timeout 65;
index index.html index.php;
include /usr/local/etc/nginx/sites-enabled/*;
}
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index /index.php;
fastcgi_read_timeout 240;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment