MNMP (Mac + Nginx + MySQL + PHP) 開發環境搭建

仇諾伊發表於2017-12-13

環境搭建:

安裝homebrew

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

安裝Mysql

先查詢下mysql:brew search mysql 看一下mysql的版本資訊: brew info mysql brew install mysql 設定密碼: 安裝時的訊息有這麼一句話We've installed your MySQL database without a root password. To secure it run:mysql_secure_installation,那就來設定下root的密碼

第一步:開啟mysql服務

mysql.server start

第二步:執行mysql_secure_installation

mysql_secure_installation # 執行後按照提示資訊進行設定,慢慢看下英文,都能看懂的 啟動mysql:

  • brew services start mysql
  • mysql.server start

安裝php

先新增php擴充套件

brew update # 安裝軟體前都要習慣的更新下brew源
brew tap homebrew/dupes
brew tap josegonzalez/homebrew-php
複製程式碼

然後開始安裝:

brew install php70 --with-debug --with-gmp --with-homebrew-curl --with-homebrew-libressl --with-homebrew-libxml2 --with-homebrew-libxslt --with-imap --with-libmysql --with-mssql
複製程式碼

由於Mac自帶了php和php-fpm,因此需要新增系統環境變數PATH來替代自帶PHP版本,我們用的是zsh,所以放進.zshrc中,如果你用的shell是bash,那麼可以把下面的資訊寫入到~/.bash_profile檔案中,如果這個檔案沒有,你自己建一個就行。

echo 'export PATH="$(brew --prefix php70)/bin:$PATH"' >> ~/.zshrc  #for php
echo 'export PATH="$(brew --prefix php70)/sbin:$PATH"' >> ~/.zshrc  #for php-fpm
echo 'export PATH="/usr/local/bin:/usr/local/sbib:$PATH"' >> ~/.zshrc #for other brew install soft
source ~/.zshrc
複製程式碼

下面先來看下php-fpm的配置檔案,路徑在/usr/local/etc/php/7.0/php-fpm.conf,

13 [global]
 14 ; Pid file
 15 ; Note: the default prefix is /usr/local/var
 16 ; Default Value: none
 17 ;pid = run/php-fpm.pid
 18 
 19 ; Error log file
 20 ; If it's set to "syslog", log is sent to syslogd instead of being written
 21 ; in a local file.
 22 ; Note: the default prefix is /usr/local/var
 23 ; Default Value: log/php-fpm.log
 24 ;error_log = log/php-fpm.log
複製程式碼

自己看下上面的資訊,去掉17行和24行前面的分號,使用php-fpm -t測試下配置是否正確,按提示資訊是不管它也可以,預設就是在/usr/local/var路徑下的,不過還是設定下吧;

php-fpm -t
[24-Oct-2016 11:20:31] NOTICE: configuration file /usr/local/etc/php/7.0/php-fpm.conf test is successful
複製程式碼

php-fpm的一些管理:

#測試php-fpm配置
php-fpm -t

#啟動php-fpm
php-fpm -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`

#也可以用上文提到的brew命令來管理php-fpm
brew services start|stop|restart php70

#還可以用這個命令來管理php-fpm
php70-fpm start|stop|restart
複製程式碼

安裝Nginx

重啟:sudo nginx -s reload 和前面一樣先brew search nginx查詢nginx, 看下資訊brew info nginx 然後安裝brew install nginx

  • brew install nginx
  • #測試配置是否有語法錯誤 nginx -t
  • #開啟 nginx sudo nginx
  • #重新載入配置|重啟|停止|退出 nginx nginx -s reload|reopen|stop|quit
  • sudo nginx

配置nginx,讓它監聽php-fpm的程式,這樣當使用者開啟瀏覽器訪問的時候,身為反向代理的nignx就能把東西讓php去執行了。

我們要配置nginx.conf檔案,建立一個php-fpm檔案(監聽php-fpm), 還要約定下將nginx.pid檔案,log日誌,以及以後我們要配置的站點.conf的路徑,我們的路徑約定還是按照brew預設的目錄來設定,如下:

# nginx.conf,已經被建立好了,我們一會要更改下
/usr/local/etc/nginx/nginx.conf

# php-fpm,這個我們就放在和nginx.conf一樣的路徑下吧,這個要我們自己建立
/usr/local/etc/nginx/php-fpm

# 日誌檔案放在/usr/local/var/log/nginx中,預設已經有了access.log和error.log檔案了
/usr/local/var/log/nginx/

# nginx.pid檔案,放在/usr/local/var/run/下面,和php-fpm.pid放一堆
/usr/local/var/run/

# 以後要配置的站點.conf, 我們就放在/usr/local/etc/nginx/servers/下面,這個servers資料夾本身就存在的
/usr/local/etc/nginx/servers/

# 站點的根目錄,也就用brew給我們設定的吧
/usr/local/var/www/

複製程式碼
  • vim /usr/local/etc/nginx/nginx.conf
worker_processes  1;

error_log   /usr/local/var/log/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/log/nginx/access.log  main;

    sendfile        on;
    keepalive_timeout  65;
    port_in_redirect off;

    include /usr/local/etc/nginx/servers/*;
}

複製程式碼
  • vim /usr/local/etc/nginx/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;
    }
複製程式碼
  • /usr/local/etc/nginx/servers/default.conf
server {
    listen       80;
    server_name  www.ya.com;
    root         /usr/local/var/www/local_yaspace;

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

    location / {
        try_files $uri $uri/ /index.php?$query_string;
        index  index.html index.htm index.php;
        autoindex   on;
        include     /usr/local/etc/nginx/php-fpm;
    }

 # 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;
        }

    error_page  404     /404.html;
    error_page  403     /403.html;
}
複製程式碼
  • servers sudo nginx -t 測試下配置檔案
  • sudo nginx # 已經開啟的用sudo nginx -s reload 重啟下

相關文章