mac brew 安裝 lnmp環境 代替Mamp

Thanatos發表於2019-02-16

廢話

用了Mac有一陣了,之前一直使用的Mamp Pro的整合環境,非常強大。可以自由切換php版本、更換Apache 和 Nginx、自定義站點 —破解MAmp pro 連結

安裝Brew

需要Homebrew,更換中國映象。有很多帖子這裡就不說了

安裝php

安裝

brew install php56 --with-debug --with-homebrew-libressl --with-homebrew-curl --with-gmp --with-libmysql --with-imap

加入啟動項,進行配置

    # 加入開機啟動
    mkdir -p ~/Library/LaunchAgents
    cp /usr/local/opt/php56/homebrew.mxcl.php56.plist ~/Library/LaunchAgents/
    sudo aunchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.php56.plist

    # 將php和php-fpm 加入環境變數
    echo `export PATH="$(brew --prefix php56)/bin:$PATH"` >> ~/.zshrc  #for php
    echo `export PATH="$(brew --prefix php56)/sbin:$PATH"` >> ~/.zshrc #for php-fpm

測試安裝

php-fpm -v 

可能會出現如下報錯:

Cannot load Xdebug - it was built with configuration API220131226,NTS, whereas running engine is API220131226,NTS,debug
PHP Warning:  PHP Startup: igbinary: Unable to initialize module
Module compiled with build ID=API20131226,NTS
PHP    compiled with build ID=API20131226,NTS,debug

解決辦法: 將上面的報錯的擴充套件重新安裝編譯一下。(注意。一定要看你的報錯有幾個)

brew reinstall php56-xdebug --build-from-source php56-igbinary --build-from-source

情況分析: 我在第一次安裝的時候出現了。報錯,但是第二次裝的時候沒出現。 分析是因為之前用mamp整合環境,已經存在了對應的擴充套件。參考這個 連結

安裝Nginx

brew install nginx

安裝完成之後,會提示一些資訊:
1、nginx 的配置檔案: /usr/local/etc/nginx/nginx.conf
2、可以在這個目錄下新建自己的虛擬主機: /usr/local/etc/nginx/servers/

修改Nginx配置檔案

1、 新建php-fpm配置,用於解析php指令碼

mkdir /usr/local/etc/nginx/conf.d
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 fastcgi.conf;
}

## 修改nginx.conf
vim /usr/local/etc/nginx/nginx.conf

2、修改nginx。conf

vim /usr/local/etc/nginx/nginx.conf
user  thanatos staff;  ###指定使用者
worker_processes  1;

#pid        logs/nginx.pid;


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

    sendfile        on;
    #tcp_nopush     on;
    autoindex on;       # 開啟目錄結構

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   /Users/thanatos/Web;
            index  index.html index.htm index.php;
            autoindex on;
            include   conf.d/php-fpm;   # include 剛才建立的指令碼
        }
    }


    include servers/*;
}

3、將nginx加入開機開機啟動

    cp /usr/local/opt/nginx/homebrew.mxcl.nginx.plist ~/Library/LaunchAgents/
    sudo launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.nginx.plist

儲存退出,重新起nginx (要使用管理員許可權,不然不能使用80埠)

sudo brew services restart nginx

修改php-fpm 配置檔案 (為了不修改目錄許可權)

/usr/local/etc/php/5.6

主要修改執行的使用者

; Per pool prefix
; It only applies on the following directives:
; - `access.log`
; - `slowlog`
; - `listen` (unixsocket)
; - `chroot`
; - `chdir`
; - `php_values`
; - `php_admin_values`
; When not set, the global prefix (or /usr/local/Cellar/php56/5.6.30_6) applies instead.
; Note: This directive can also be relative to the global prefix.
; Default Value: none
;prefix = /path/to/pools/$pool

; Unix user/group of processes
; Note: The user is mandatory. If the group is not set, the default user`s group
;       will be used.
user = 你的使用者
group = 使用者組

儲存重啟php-fpm

sudo brew services restart php56

安裝mysql

1、安裝mysql比較簡單,主要是配置mysql的登陸

brew install mysql

2、加入開機啟動

    cp /usr/local/opt/mysql/homebrew.mxcl.mysql.plist ~/Library/LaunchAgents/
    sudo launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist

3、修改root密碼 具體就不寫了。

因為,記錄的有點亂,就是自己做一個備忘,有遇到同樣問題的老哥,或者我落了那一步,歡迎糾正

相關文章