Mac os 使用 (homebrew) 從零搭建 PHP,nginx,MySQL,Redis (LNMP) 開發環境

Walking發表於2019-06-21
brew是mac os 的軟體包的管理器,安裝homebrew方法, 具體安裝方法請查閱官方網站: http://brew.sh

安裝php (以php7.1為例)

brew update                        #更新brew可安裝包,建議每次執行一下
brew search php71                  #搜尋php7.1
brew tap josegonzalez/php          #安裝擴充套件<gihhub_user/repo>   
brew tap                           #檢視安裝的擴充套件列表
brew install php71                 #安裝php7.1
brew remove  php71                 #解除安裝php7.1
brew upgrade php71                 #升級php7.1
brew options php71                 #檢視php7.1安裝選項
brew info    php71                 #檢視php7.1相關資訊
brew home    php71                 #訪問php7.1官方網站
brew services list                 #檢視系統通過 brew 安裝的服務
brew services cleanup              #清除已解除安裝無用的啟動配置檔案
brew services restart php71        #重啟php-fpm

安裝 homebrew-cask

homebrew cask 是 homebrew 的擴充套件,藉助它可以方便地在 mac as 上安裝圖形介面程式,即我們常用的各類應用。
brew tap phinze/homebrew-cask && brew install brew-cask 
cask 常用命令
brew cask search        #列出所有可以被安裝的軟體
brew cask search php    #查詢所有和php相關的應用
brew cask list          #列出所有通過cask安裝的軟體
brew cask info phpstorm #檢視 phpstorm 的資訊
brew cask uninstall firefox  #解除安裝火狐

安裝msyql

brew install mysql
mysql 開機啟動:
ln -sfv /usr/local/opt/mysql/*.plist ~/Library/LaunchAgents
launchctl load ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist
安裝完成之後開啟MySQL安全機制:
/usr/local/opt/mysql/bin/mysql_secure_installation
根據終端提示,輸入root密碼, 然後依次確認一些安全選項。
當然brew下載的預設是最新版的mysql8.0+,在brew search mysql 選擇你要安裝的。因為mysql8.0+和mysql5.7的區別還是有點大,目前還是用5.7的版本。
➜  ~ brew search mysql
==> Formulae
automysqlbackup       mysql-client          mysql-connector-c++   mysql-utilities       mysql@5.7 ✔
mysql                 mysql-cluster         mysql-sandbox         mysql@5.5             mysqltuner
mysql++               mysql-connector-c     mysql-search-replace  mysql@5.6

==> Casks
mysql-connector-python      mysql-shell                 mysqlworkbench              navicat-for-mysql
mysql-connector-python      mysql-utilities             mysqlworkbench              sqlpro-for-mysql
mysql-shell                 mysql-utilities             navicat-for-mysql           sqlpro-for-mysql
#檢視一下mysql執行情況
walking          63407   0.0  0.0  4287208    872 s004  S+    6:02下午   0:00.00 grep --color=auto --exclude-dir=.bzr --exclude-dir=CVS --exclude-dir=.git --exclude-dir=.hg --exclude-dir=.svn mysql
_mysql              93   0.0  0.1  4696448  10016   ??  Ss   一09上午   1:09.50 /usr/local/mysql/bin/mysqld --user=_mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --plugin-dir=/usr/local/mysql/lib/plugin --log-error=/usr/local/mysql/data/mysqld.local.err --pid-file=/usr/local/mysql/data/mysqld.local.pid --keyring-file-data=/usr/local/mysql/keyring/keyring --early-plugin-load=keyring_file=keyring_file.so

#連線資料庫mysql
➜  ~ mysql -u root -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2808
Server version: 5.7.24 MySQL Community Server (GPL)

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>

安裝phpmyadmin

brew install phpmyadmin

安裝php擴充套件

通過brew安裝的PHP版本中自帶了pecl,可以直接使用
pecl version 檢視版本資訊
pecl help 可以檢視命令幫助
pecl search xdebug  搜尋可以安裝的擴充套件資訊
pecl install xdebug 安裝擴充套件
也可以使用brew options php71命令來檢視安裝php7.1的選項,這裡我用下面的選項安裝
brew install php71 --with-fpm --with-gmp --with-imap --with-tidy --with-debug --with-mysql --with-libmysql
等待PHP編譯完成,開始安裝PHP常用擴充套件,擴充套件安裝過程中brew會自動安裝依賴包,例如php71-pdo-pgsql 會自動裝上postgresql,這裡我安裝以下PHP擴充套件:
brew install php71-apcu\
 php71-gearman\
 php71-geoip\
 php71-gmagick\
 php71-imagick\
 php71-intl\
 php71-mcrypt\
 php71-memcache\
 php71-memcached\
 php71-mongo\
 php71-opcache\
 php71-pdo-pgsql\
 php71-phalcon\
 php71-redis\
 php71-sphinx\
 php71-swoole\
 php71-uuid\
 php71-xdebug;
由於Mac自帶了php和php-fpm,因此需要新增系統環境變數PATH來替代自帶PHP版本。
echo 'export PATH="$(brew --prefix php71)/bin:$PATH"' >> ~/.bash_profile  #for php
echo 'export PATH="$(brew --prefix php71)/sbin:$PATH"' >> ~/.bash_profile  #for php-fpm
echo 'export PATH="/usr/local/bin:/usr/local/sbib:$PATH"' >> ~/.bash_profile #for other brew install soft
source ~/.bash_profile
修改php-fpm配置檔案,vim /usr/local/etc/php/7.1/php-fpm.conf,找到pid相關大概在25行,去掉註釋 pid = run/php-fpm.pid, 那麼php-fpm的pid檔案就會自動產生在/usr/local/var/run/php-fpm.pid,下面要安裝的Nginx pid檔案也放在這裡。
#測試php-fpm配置
php-fpm -t
php-fpm -c /usr/local/etc/php/7.1/php.ini -y /usr/local/etc/php/7.1/php-fpm.conf -t

#啟動php-fpm
php-fpm -D
php-fpm -c /usr/local/etc/php/7.1/php.ini -y /usr/local/etc/php/7.1/php-fpm.conf -D

#關閉php-fpm
kill -INT `cat /usr/local/var/run/php-fpm.pid`

#重啟php-fpm
kill -USR2 `cat /usr/local/var/run/php-fpm.pid`
啟動php-fpm之後,確保它正常執行監聽9000埠:
➜  ~ lsof -Pni4 | grep LISTEN | grep php
php-fpm     513 walking    7u  IPv4 0x29d33d013e0238b5      0t0  TCP 127.0.0.1:9000 (LISTEN)
php-fpm     665 walking    8u  IPv4 0x29d33d013e0238b5      0t0  TCP 127.0.0.1:9000 (LISTEN)
php-fpm     666 walking    8u  IPv4 0x29d33d013e0238b5      0t0  TCP 127.0.0.1:9000 (LISTEN)
php-fpm   36395 walking    8u  IPv4 0x29d33d013e0238b5      0t0  TCP 127.0.0.1:9000 (LISTEN)
phpstorm  73490 walking  155u  IPv4 0x29d33d013fd005f5      0t0  TCP 127.0.0.1:6942 (LISTEN)
phpstorm  73490 walking  347u  IPv4 0x29d33d0152921f55      0t0  TCP 127.0.0.1:63342 (LISTEN)
PHP-FPM開機啟動:
ln -sfv /usr/local/opt/php71/*.plist ~/Library/LaunchAgents
launchctl load ~/Library/LaunchAgents/homebrew.mxcl.php71.plist

安裝composer

brew install composer
#檢查一下情況
➜  ~ composer -VVV
Composer version 1.8.6 2019-06-11 15:03:05

安裝nginx

brew install nginx
nginx 基本操作
#測試配置是否有語法錯誤
nginx -t

#開啟 nginx
sudo nginx

#重新載入配置|重啟|停止|退出 nginx
nginx -s reload|reopen|stop|quit

#也可以使用Mac的launchctl來啟動|停止
launchctl unload ~/Library/LaunchAgents/homebrew.mxcl.nginx.plist
launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.nginx.plist
nginx監聽80埠需要root許可權執行
sudo chown root:wheel /usr/local/Cellar/nginx/1.17.0/bin/nginx
sudo chmod u+s /usr/local/Cellar/nginx/1.17.0/bin/nginx

配置nginx.conf

建立需要用到的目錄:
mkdir -p /usr/local/var/logs/nginx
mkdir -p /usr/local/etc/nginx/conf.d
mkdir -p /usr/local/etc/nginx/ssl
sudo mkdir -p /var/www
sudo chown :staff /var/www
sudo chmod 775 /var/www

vim /usr/local/etc/nginx/nginx.conf 輸入以下內容:

worker_processes  1;

error_log   /usr/local/var/logs/nginx/error.log debug;

pid        /usr/local/var/run/nginx.pid;

events {
    worker_connections  256;
}

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/logs/access.log  main;

    sendfile        on;
    keepalive_timeout  65;
    port_in_redirect off;

    include /usr/local/etc/nginx/servers/*;
}
設定nginx php-fpm配置檔案
vim /usr/local/etc/nginx/conf.d/php-fpm
#proxy the php scripts to php-fpm
location ~ \.php$ {
    try_files                   $uri = 404;
    fastcgi_pass                127.0.0.1:9000;
    fastcgi_index               index.php;
    fastcgi_intercept_errors    on;
    include /usr/local/etc/nginx/fastcgi.conf;
}
nginx虛擬主機準備工作
#建立 info.php index.html 404.html 403.html檔案到 /var/www 下面
vi /var/www/info.php
vi /var/www/index.html
vi /var/www/403.html
vi /var/www/404.html
建立預設虛擬主機default

vim /usr/local/etc/nginx/servers/default輸入:

server {
    listen       80;
    server_name  localhost;
    root         /var/www/;

    access_log  /usr/local/var/logs/nginx/default.access.log  main;

    location / {
        index  index.html index.htm index.php;
        autoindex   on;
        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;
}
建立ssl預設虛擬主機default-ssl

vim /usr/local/etc/nginx/servers/default-ssl輸入:

server {
    listen       443;
    server_name  localhost;
    root       /var/www/;

    access_log  /usr/local/var/logs/nginx/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;
}
建立phpmyadmin虛擬主機
# vim /usr/local/etc/nginx/servers/phpmyadmin.local #輸入以下配置
server {
    listen       80;
    server_name  phpmyadmin.local;
    root    /usr/local/share/phpmyadmin;

    error_log   /usr/local/var/logs/nginx/phpmyadmin.error.log;
    access_log  /usr/local/var/logs/nginx/phpmyadmin.access.log main;

    #ssl                  on;
    ssl_certificate      ssl/phpmyadmin.crt;
    ssl_certificate_key  ssl/phpmyadmin.key;

    ssl_session_timeout  5m;

    ssl_protocols  SSLv2 SSLv3 TLSv1;
    ssl_ciphers  HIGH:!aNULL:!MD5;
    ssl_prefer_server_ciphers   on;

    location / {
        index  index.html index.htm index.php;
        include   /usr/local/etc/nginx/conf.d/php-fpm;
    }
}

安裝 phpRedisAdmin 檢視化介面

https://github.com/erikdubbelboer/phpRedisAdmin.git git克隆phpredisadmin專案。可以查reids的資訊

Mac os 使用 (homebrew) 從零搭建 PHP,nginx,MySQL,Redis (LNMP) 開發環境

# vim /usr/local/etc/nginx/servers/phpmyredis.local
server {
    listen       80;
    server_name  phpmyredis.local;
    # 我克隆到本地的目錄
    root    /usr/local/share/phpRedisAdmin;

    error_log   /usr/local/var/logs/nginx/phpmyadmin.error.log;
    access_log  /usr/local/var/logs/nginx/phpmyadmin.access.log main;

    #ssl                  on;
    ssl_certificate      ssl/phpmyadmin.crt;
    ssl_certificate_key  ssl/phpmyadmin.key;

    ssl_session_timeout  5m;

    ssl_protocols  SSLv2 SSLv3 TLSv1;
    ssl_ciphers  HIGH:!aNULL:!MD5;
    ssl_prefer_server_ciphers   on;

    location / {
        index  index.html index.htm index.php;
        include   /usr/local/etc/nginx/conf.d/php-fpm;
    }
}
設定SSL
mkdir -p /usr/local/etc/nginx/ssl
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
openssl req -new -newkey rsa:4096 -days 365 -nodes -x509 -subj "/C=US/ST=State/L=Town/O=Office/CN=phpmyadmin" -keyout /usr/local/etc/nginx/ssl/phpmyadmin.key -out /usr/local/etc/nginx/ssl/phpmyadmin.crt
啟動|停止Nginx
launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.nginx.plist
launchctl unload -w ~/Library/LaunchAgents/homebrew.mxcl.nginx.plist
訪問連線測試是否已經搭建起來環境
http://localhost/index.php

如果訪問成功恭喜你已經順利成功搭建好開發環境,可以愉快的開發了。

Walking

相關文章