Mac OS 完整搭建MNMP(2019-02-16)


Mac OS 完整搭建MNMP (Mac+Nginx+Mysql+Php)

Homebrew

Homebrew又叫Brew,是MacOS上的軟件包管理工具,能在Mac中方便的安裝軟件或者卸載軟件,和linux系統中的yum非常相似,只需要一個命令,非常方便。

官方網站:http://brew.sh

安裝方法:

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

檢驗安裝成功方法:

$ brew doctor

查看命令幫助:

$ brew --help
Example usage:
  brew search [TEXT|/REGEX/]
  brew info [FORMULA...]
  brew install FORMULA...
  brew update
  brew upgrade [FORMULA...]
  brew uninstall FORMULA...
  brew list [FORMULA...]

Troubleshooting:
  brew config
  brew doctor
  brew install --verbose --debug FORMULA

Contributing:
  brew create [URL [--no-fetch]]
  brew edit [FORMULA...]

Further help:
  brew commands
  brew help [COMMAND]
  man brew
  https://docs.brew.sh

安裝常用軟件包:

$ brew install wget 
$ brew install autoconf 
$ brew install openssl

Mysql

安裝方法

$ brew install mysql

設置 Mysql 開機啟動方法,如果不需要可跳過:

$ ln -sfv /usr/local/opt/mysql/*.plist ~/Library/LaunchAgents
launchctl load ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist

初始化、啟動方法,如果不需要可跳過:

#正常初始化
/usr/local/opt/mysql/bin/mysql_secure_installation
#若出錯請啟動服務
brew services start mysql

安全配置方法(Terminal 終端會提示[EnglishVersion]):

> Enter current password for root (enter for none):
#直接 Enter,因為沒有設置過
> Change the root password? [Y/n]
#是否更改 root 用戶的密碼
> Remove anonymous users? [Y/n]
#不需要,鍵入 Y
> Disallow root login remotely? [Y/n]
#禁止root遠程登錄,建議鍵入 Y
> Remove test database and access to it? [Y/n]
#刪除測試數據表,建議鍵入 Y
> Reload privilege tables now? [Y/n]
#確保設置生效,鍵入 Y

檢驗安裝成功方法:

#查看端口
ps aux | grep mysql
#測試 Mysql 連接 如果沒有初始化的root的密碼是空,不需要輸入密碼即可進去
mysql -uroot -p你設置的密碼
為了直觀,這里我以密碼:123456為例,測試Mysql連接時鍵入:
mysql -uroot -p123456

安裝 Phpmyadmin 方法:

$ brew install phpmyadmin

Php

Mac HomeBrew [2018-03-31]起棄用homebrew/php,php版本改名(如:php71 => php@7.1),無法像以前直接搜索各個版本對應擴展直接安裝如:brew install php71-redis,可采用pecl安裝php對應擴展,以php@7.1為例

移除原來的homebrew/php tap(沒有的安裝過的忽略):

$ brew untap homebrew/tap

查看所有php的版本:

$ brew search php
==> Formulae
brew-php-switcher      php-code-sniffer       php@7.1                phplint                phpmyadmin
php                    php-cs-fixer           php@7.2                phpmd                  phpunit

==> Casks
homebrew/cask/eclipse-php                                 homebrew/cask/netbeans-php

安裝 Php@7.1:

brew install php@7.1

配置 Php@7.1的環境變量(替換Mac自帶的php環境變量路徑):

$ echo 'export PATH="$(brew --prefix php@7.1)/bin:$PATH"' >> ~/.bash_profile
$ echo 'export PATH="$(brew --prefix php@7.1)/sbin:$PATH"' >> ~/.bash_profile
$ echo 'export PATH="/usr/local/bin:/usr/local/sbib:$PATH"' >> ~/.bash_profile
$ source ~/.bash_profile

查看剛安裝的php版本信息:

$ php -v
PHP 7.1.25 (cli) (built: Dec  7 2018 08:20:45) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.1.0, Copyright (c) 1998-2018 Zend Technologies
    with Zend OPcache v7.1.25, Copyright (c) 1999-2018, by Zend Technologies

采用pecl安裝php對應擴展

查看pecl的版本信息:

$ pecl version
PEAR Version: 1.10.5
PHP Version: 7.1.25
Zend Engine Version: 3.1.0
Running on: Darwin bogon 18.2.0 Darwin Kernel Version 18.2.0: Thu Dec 20 20:46:53 PST 2018; root:xnu-4903.241.1~1/RELEASE_X86_64 x86_64

查看當前php包含那些擴展:

$ php -m
[PHP Modules]
bcmath
bz2
calendar
Core
ctype
curl
date
dba
dom
exif
fileinfo
filter
ftp
gd
gettext
gmp
hash
iconv
intl
json
ldap
libxml
mbstring
mcrypt
mysqli
mysqlnd
odbc
openssl
pcntl
pcre
PDO
pdo_dblib
pdo_mysql
PDO_ODBC
pdo_pgsql
pdo_sqlite
pgsql
Phar
phpdbg_webhelper
posix
pspell
readline
Reflection
session
shmop
SimpleXML
soap
sockets
SPL
sqlite3
standard
sysvmsg
sysvsem
sysvshm
tidy
tokenizer
wddx
xml
xmlreader
xmlrpc
xmlwriter
xsl
Zend OPcache
zip
zlib

[Zend Modules]
Zend OPcache

pecl安裝擴展

安裝xdebug擴展:

$ pecl install xdebug

安裝redis擴展:

$ pecl install redis
Build process completed successfully
Installing '/usr/local/Cellar/php@7.1/7.1.25/pecl/20160303/redis.so'
install ok: channel://pecl.php.net/redis-4.2.0
Extension redis enabled in php.ini

缺什么擴展使用pecl安裝即可。會自動添加到php.ini配置文件中。

php-fpm配置

查看當前php-fpm信息:

$ php-fpm -v
PHP 7.1.25 (fpm-fcgi) (built: Dec  7 2018 08:20:52)
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.1.0, Copyright (c) 1998-2018 Zend Technologies
    with Zend OPcache v7.1.25, Copyright (c) 1999-2018, by Zend Technologies

測試一下php-fpm配置文件,方便找到php-fpm配置文件存放的位置:

$ php-fpm -t
[16-Feb-2019 15:54:11] NOTICE: configuration file /usr/local/etc/php/7.1/php-fpm.conf test is successful

修改php-fpm配置文件:

$ vim /usr/local/etc/php/7.1/php-fpm.conf

找到pid相關,去掉pid = run/php-fpm.pid前的“;”, 那么php-fpm的pid文件就會自動產生在 /usr/local/var/run/php-fpm.pid,保存退出。

php-fpm驗證與啟動

測試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
NOTICE: configuration file /usr/local/etc/php/7.1/php-fpm.conf test is successful

啟動php-fpm

 $ php-fpm -D

或者

$ php-fpm -c /usr/local/etc/php/7.2/php.ini -y /usr/local/etc/php/7.2/php-fpm.conf -D

這時候如果出現不用理會

[16-Feb-2019 16:06:51] NOTICE: [pool www] 'user' directive is ignored when FPM is not running as root
[16-Feb-2019 16:06:51] NOTICE: [pool www] 'group' directive is ignored when FPM is not running as root

查看php-fpm進程:

$ ps aux | grep php-fpm
linyan           22889   0.0  0.0  4268020    780 s000  S+    4:13下午   0:00.00 grep php-fpm
linyan           22873   0.0  0.0  4482608    640   ??  S     4:06下午   0:00.00 php-fpm -D
linyan           22872   0.0  0.0  4482608    636   ??  S     4:06下午   0:00.00 php-fpm -D
linyan           22871   0.0  0.0  4482864   1124   ??  Ss    4:06下午   0:00.02 php-fpm -D

關閉php-fpm:

$ kill 22871 
或者
$ kill -INT `cat /usr/local/var/run/php-fpm.pid`

重啟php-fpm:

$ kill -USR2 `cat /usr/local/var/run/php-fpm.pid`

USR1亦通常被用來告知應用程序重載配置文件;例如,向Apache HTTP服務器發送一個USR1信號將導致以下步驟的發生:停止接受新的連接,等待當前連接停止,重新載入配置文件,重新打開日志文件,重啟服務器,從而實現相對平滑的不關機的更改。

SIGUSR1 用戶自定義信號 默認處理:進程終止
SIGUSR2 用戶自定義信號 默認處理:進程終止

kill 命令介紹

-l  信號,若果不加信號的編號參數,則使用“-l”參數會列出全部的信號名稱
-a  當處理當前進程時,不限制命令名和進程號的對應關系
-p  指定kill 命令只打印相關進程的進程號,而不發送任何信號
-s  指定發送信號
-u  指定用戶 

php-fpm設置開機啟動

n -sfv /usr/local/opt/php72/*.plist ~/Library/LaunchAgents
launchctl load ~/Library/LaunchAgents/homebrew.mxcl.php56.plist

安裝 composer

$ curl -sS https://getcomposer.org/installer | php mv composer.phar /usr/local/bin/composer
#改為國內源
$ composer config -g repo.packagist composer https://packagist.phpcomposer.com

Nginx

安裝 Nginx:

查看版本

$ brew search nginx
$ brew info nginx
ginx: stable 1.15.8 (bottled), HEAD
HTTP(S) server and reverse proxy, and IMAP/POP3 proxy server
https://nginx.org/
Not installed
From: https://github.com/Homebrew/homebrew-core/blob/master/Formula/nginx.rb
==> Dependencies
Required: openssl ✔, pcre ✔
==> Options
--HEAD
    Install HEAD version
==> Caveats
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
==> Analytics
install: 25,969 (30 days), 96,067 (90 days), 438,395 (365 days)
install_on_request: 24,806 (30 days), 89,756 (90 days), 394,102 (365 days)
build_error: 0 (30 days)

執行安裝

$ brew install nginx
==> Downloading https://homebrew.bintray.com/bottles/nginx-1.15.8.mojave.bottle.3.tar.gz
######################################################################## 100.0%
==> Pouring nginx-1.15.8.mojave.bottle.3.tar.gz
==> Caveats
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.8: 25 files, 2MB

啟動 Nginx:

測試配置文件

$ nginx -t
nginx: the configuration file /usr/local/etc/nginx/nginx.conf syntax is ok
nginx: configuration file /usr/local/etc/nginx/nginx.conf test is successful

啟動nginx

$ sudo nginx
或者
$ nginx

查看nginx啟動情況

$ ps aux | grep nginx
linyan           23921   0.0  0.0  4268020    780 s000  S+    4:50下午   0:00.00 grep nginx
linyan           23919   0.0  0.0  4291632   1044   ??  S     4:50下午   0:00.00 nginx: worker process
linyan           23918   0.0  0.0  4291388    536   ??  Ss    4:50下午   0:00.00 nginx: master process nginx

打開瀏覽器訪問:localhost:8080測試

重新加載配置|重啟|停止|退出 nginx

$ nginx -s reload|reopen|stop|quit

也可以使用Mac的launchctl來啟動|停止 nginx

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

設置 Nginx 開機啟動方法

$ ln -sfv /usr/local/opt/nginx/*.plist ~/Library/LaunchAgents
$ launchctl load ~/Library/LaunchAgents/homebrew.mxcl.nginx.plist

設置 Nginx 監聽端口權限

$ sudo chown root:wheel /usr/local/Cellar/nginx/1.15.8/bin/nginx
$ sudo chmod u+s /usr/local/Cellar/nginx/1.15.8/bin/nginx

配置Nginx.conf

創建配置需要用到的目錄:

$ mkdir -p /usr/local/var/logs/nginx #創建日志目錄
$ 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 #創建存放ssl文件目錄
$ sudo mkdir -p /var/www #創建項目目錄,默認是/var/local/usr/www
$ sudo chown :staff /var/www #更改所屬組為所有普通用戶
$ sudo chmod 775 /var/www #更改目錄權限

編輯器編輯 nginx.conf

$ vim /usr/local/etc/nginx/nginx.conf
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/sites-enabled/*;
}

配置php-fpm 配置文件

$ vim /usr/local/etc/nginx/conf.d/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 下面

$ vim /var/www/info.php
<?php phpinfo();?>
$ vim /var/www/index.html
<html><head><meta charset="UTF-8"><title>Default Page</title></head><body><h1 align="center">Default</h1></body></html>
$ vim /var/www/403.html
<html><head><meta charset="UTF-8"><title>403 Page</title></head><body><h1 align="center">Default</h1></body></html>
$ vim /var/www/404.html
<html><head><meta charset="UTF-8"><title>404 Page</title></head><body><h1 align="center">Default</h1></body></html>

 

配置虛擬主機配置文件

$ vim /usr/local/etc/nginx/sites-available/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虛擬主機配置文件

$ vim /usr/local/etc/nginx/sites-available/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/sites-available/phpmyadmin
server {
    listen       306;
    server_name  localhost;
    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;
    }
}

配置 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

創建虛擬主機軟連接並開啟

$ ln -sfv /usr/local/etc/nginx/sites-available/default /usr/local/etc/nginx/sites-enabled/default
$ ln -sfv /usr/local/etc/nginx/sites-available/default-ssl /usr/local/etc/nginx/sites-enabled/default-ssl
$ ln -sfv /usr/local/etc/nginx/sites-available/phpmyadmin /usr/local/etc/nginx/sites-enabled/phpmyadmin
ln 必要參數:

-b 刪除,覆蓋以前建立的鏈接
-d 允許超級用戶制作目錄的硬鏈接
-f 強制執行
-i 交互模式,文件存在則提示用戶是否覆蓋
-n 把符號鏈接視為一般目錄
-s 軟鏈接(符號鏈接)
-v 顯示詳細的處理過程

重新裝載 nginx

$ sudo nginx -s reload

測試 nginx 開啟情況

http://localhost
http://localhost/info.php
http://localhost/404.html
https://localhost/index.html(SSL)
https://localhost/info.php(SSL)
https://localhost/404.html(SSL)
https://localhost:306(SSL)

 

 錯誤問題解決方法:

1 root登陸phpmyadmin時 mysqli_real_connect(): The server requested authentication method unknown to the client [sha256_password]

在MySQL 8.0.11中,caching_sha2_password是默認的身份驗證插件,而不是以往的mysql_native_password。有關此更改對服務器操作的影響以及服務器與客戶端和連接器的兼容性的信息,請參閱caching_sha2_password作為首選的身份驗證插件。

從8.0.11版本起,不再像mysql5.7及以前版本那樣,設置用戶密碼時默認的驗證方式為caching_sha2_password,如果發現升級mysql8.0.11后原有的程序不能連接mysql,可迅速在mysql command line client客戶端用下面的命令設置成mysql5.7及以前版本的密碼驗證方式,同時MYSQL8.0.11下修改密碼的方式與原先也不大一樣,原先的部分修改密碼的命令在mysql8.0.11下不能使用。

有時候只是因為內網訪問使用測試服務器,不想設置復雜的密碼,比如root:123456
但是新版 mysql 加入密碼安全度檢測機制,導致報錯
解決方法如下:

mysql> SHOW VARIABLES LIKE 'validate_password%';

修改變量:

mysql>set global validate_password.policy=0;
mysql>set global validate_password.length=4;

然后退出后再執行

mysql_secure_installation

繼續重置root密碼就可以設置為123456了。

mysql > use mysql  
mysql > ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '你的密碼';  
mysql > FLUSH PRIVILEGES;

重新登陸你的phpmyadmin就可以了!

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM