mac php nginx mysql

As a back-end development, you often use a local development environment. Here I install the php + nginx + mysql development environment through the mac brew package management command.

For MAC brew usage, see mac homebrew.

Install PHP

➜  ~ brew search php
==> Formulae
brew-php-switcher          php-code-sniffer           [email protected]                    phplint                    phpmyadmin                 phpunit
php                        php-cs-fixer               [email protected]                    phpmd                      phpstan

==> Casks
eclipse-php                                           netbeans-php                                          phpstorm
➜  ~ brew install [email protected]
...
==> [email protected]
To enable PHP in Apache add the following to httpd.conf and restart Apache:
    LoadModule php7_module /usr/local/opt/[email protected]/lib/httpd/modules/libphp7.so

    <FilesMatch \.php$>
        SetHandler application/x-httpd-php
    </FilesMatch>

Finally, check DirectoryIndex includes index.php
    DirectoryIndex index.php index.html

The php.ini and php-fpm.ini file can be found in:
    /usr/local/etc/php/7.1/

[email protected] is keg-only, which means it was not symlinked into /usr/local,
because this is an alternate version of another formula.

If you need to have [email protected] first in your PATH run:
  echo 'export PATH="/usr/local/opt/[email protected]/bin:$PATH"' >> ~/.zshrc
  echo 'export PATH="/usr/local/opt/[email protected]/sbin:$PATH"' >> ~/.zshrc

For compilers to find [email protected] you may need to set:
  export LDFLAGS="-L/usr/local/opt/[email protected]/lib"
  export CPPFLAGS="-I/usr/local/opt/[email protected]/include"


To have launchd start [email protected] now and restart at login:
  brew services start [email protected]
Or, if you don't want/need a background service you can just run:
  php-fpm

Success.

  • Installation directory : /usr/local/Cellar/[email protected]
  • Conf directory : /usr/local/etc/php
➜  ~ php -v
 PHP 7.1.23 (cli) (built: Feb 22 2019 22:08:13) ( NTS )
 Copyright (c) 1997-2018 The PHP Group
 Zend Engine v3.1.0, Copyright (c) 1998-2018 Zend Technologies

start php-fpm

➜  ~   brew services start [email protected]
==> Successfully started `[email protected]` (label: [email protected])
➜  ~

Install MySql

➜  ~ brew search mysql
==> Formulae
automysqlbackup        mysql++                mysql-cluster          mysql-connector-c++    mysql-search-replace   [email protected]              [email protected]
mysql ✔                mysql-client           mysql-connector-c      mysql-sandbox          mysql-utilities        [email protected]              mysqltuner

==> Casks
mysql-connector-python     mysql-shell                mysql-utilities            mysqlworkbench             navicat-for-mysql          sqlpro-for-mysql
➜  ~ brew install [email protected]
...
==> [email protected]
We've installed your MySQL database without a root password. To secure it run:
    mysql_secure_installation

MySQL is configured to only allow connections from localhost by default

To connect run:
    mysql -uroot

[email protected] is keg-only, which means it was not symlinked into /usr/local,
because this is an alternate version of another formula.

If you need to have [email protected] first in your PATH run:
  echo 'export PATH="/usr/local/opt/[email protected]/bin:$PATH"' >> ~/.zshrc

For compilers to find [email protected] you may need to set:
  export LDFLAGS="-L/usr/local/opt/[email protected]/lib"
  export CPPFLAGS="-I/usr/local/opt/[email protected]/include"

For pkg-config to find [email protected] you may need to set:
  export PKG_CONFIG_PATH="/usr/local/opt/[email protected]/lib/pkgconfig"


To have launchd start [email protected] now and restart at login:
  brew services start [email protected]
Or, if you don't want/need a background service you can just run:
  /usr/local/opt/[email protected]/bin/mysql.server start

Success.

  • Data directory : /usr/local/var/mysql
  • Conf file : /usr/local/etc/my.cnf
  • ERR log : /usr/local/var/mysql/localhost.err

Security Configuration Wizard:


➜  ~ /usr/local/Cellar/[email protected]/5.7.25/bin/mysql_secure_installation
  • Set the password for root
  • Remove anonymous users?
  • Disallow root login remotely?
  • Remove test database and access to it?
  • Reload privilege tables now?

start mysql

➜  ~ brew services start [email protected]
➜  ~ ps -ef | grep mysql
  501 43843     1   0 11:33PM ??         0:00.03 /bin/sh /usr/local/opt/mysql/bin/mysqld_safe --datadir=/usr/local/var/mysql
  501 43936 43843   0 11:33PM ??         0:01.94 /usr/local/opt/mysql/bin/mysqld --basedir=/usr/local/opt/mysql --datadir=/usr/local/var/mysql --plugin-dir=/usr/local/opt/mysql/lib/plugin --log-error=yangliudeMacBook-Pro.local.err --pid-file=yangliudeMacBook-Pro.local.pid
  501 43938 12914   0 11:33PM ttys001    0:00.00 grep --color=auto --exclude-dir=.bzr --exclude-dir=CVS --exclude-dir=.git --exclude-dir=.hg --exclude-dir=.svn mysql

install db

➜  ~ /usr/local/Cellar/[email protected]/5.7.25/bin/mysql_install_db

Connect DB

➜  ~ /usr/local/Cellar/[email protected]/5.7.25/bin/mysql -uroot -P3306 -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 8.0.15 Homebrew

Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>

Install Nginx

➜  ~ brew search nginx
==> Formulae
nginx
➜  ~ brew install nginx
Docroot is: /usr/local/var/www

The default port has been set in /usr/local/etc/nginx/nginx.conf to 8080 so that
nginx can run without sudo.

nginx will load all files in /usr/local/etc/nginx/servers/.

To have launchd start nginx now and restart at login:
  brew services start nginx
Or, if you don't want/need a background service you can just run:
  nginx
==> Summary
🍺  /usr/local/Cellar/nginx/1.15.12: 25 files, 2MB
Removing: /Users/ylspirit/Library/Caches/Homebrew/nginx--1.15.10.mojave.bottle.1.tar.gz... (1.2MB)

Success.

  • Web root directory : /usr/local/var/www
  • Default port : 8080
  • Conf directory: /usr/local/etc/nginx/nginx.conf

start nginx

➜  ~   brew services start nginx

Service `nginx` already started, use `brew services restart nginx` to restart.
➜  ~ ps -fe | grep nginx
    0   938     1   0  7:40PM ??         0:00.02 nginx: master process /usr/local/opt/nginx/bin/nginx -g daemon off;
   -2   959   938   0  7:40PM ??         0:00.00 nginx: worker process
  501 75279 12914   0  8:54AM ttys001    0:00.00 grep --color=auto --exclude-dir=.bzr --exclude-dir=CVS --exclude-dir=.git --exclude-dir=.hg --exclude-dir=.svn nginx
➜  ~

Conf Nginx and PHP-FPM

FastCGI server listening on 127.0.0.1:9000

➜  ~ lsof -i tcp:9000
COMMAND   PID     USER   FD   TYPE            DEVICE SIZE/OFF NODE NAME
php-fpm 79442 ylspirit    7u  IPv4 0x487f46878fa3e39      0t0  TCP localhost:cslistener (LISTEN)
php-fpm 79447 ylspirit    8u  IPv4 0x487f46878fa3e39      0t0  TCP localhost:cslistener (LISTEN)
php-fpm 79448 ylspirit    8u  IPv4 0x487f46878fa3e39      0t0  TCP localhost:cslistener (LISTEN)

Modify nginx configuration

Default configuration:

server {
    listen       80;
    server_name  localhost;
    #charset koi8-r;
    #access_log  logs/host.access.log  main;
    location / {
        root   html;
        index  index.html index.htm;
    }
    #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;
    }
    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}
    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    #location ~ \.php$ {
    #    root           html;
    #    fastcgi_pass   127.0.0.1:9000;
    #    fastcgi_index  index.php;
    #    fastcgi_param  SCRIPT_FILENAME  /scripts$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;
    #}
}

Modified to:

server {
    listen       80;
    server_name  localhost;
    #charset koi8-r;
    #access_log  logs/host.access.log  main;
    location / {
        root   /Users/ylspirit/wwwroot/www;
        index  index.php index.html index.htm;
    }
    #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   /usr/share/nginx/html;
    }
    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}
    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    location ~ \.php$ {
        root           /Users/ylspirit/wwwroot/www;
        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;
    #}
}

restart nginx

➜  nginx git:(master) ✗ brew services restart nginx
Stopping `nginx`... (might take a while)
==> Successfully stopped `nginx` (label: homebrew.mxcl.nginx)
==> Successfully started `nginx` (label: homebrew.mxcl.nginx)

In the web root directory, create the file index.php

➜  vim cat index.php
<?php
   phpinfo();

Visit homepage

So far, PHP + Nginx + MySQL environment has been successfully built.

Add a Comment

Your email address will not be published. Required fields are marked *