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

Sinkiang發表於2018-10-09

一、Homebrew安裝

homebrew是mac下非常好用的包管理器,會自動安裝相關的依賴包,將你從繁瑣的軟體依賴安裝中解放出來。

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

homebrew的常用命令:

brew update #更新可安裝包的最新資訊,建議每次安裝前都執行下
brew search pkg_name #搜尋相關的包資訊
brew install pkg_name #安裝包
複製程式碼

二、PHP安裝

Mac OSX 10.9以後的系統自帶了PHP、php-fpm,省去了安裝php-fpm的麻煩。

sudo cp /private/etc/php-fpm.conf.default /private/etc/php-fpm.conf
vim /private/etc/php-fpm.conf
複製程式碼

修改php-fpm.conf檔案中的error_log項,預設該項被註釋掉,這裡需要去註釋並且修改為error_log = /usr/local/var/log/php-fpm.log。如果不修改該值,執行php-fpm的時候會提示log檔案輸出路徑不存在的錯誤。

修改之後在終端輸入:

sudo php-fpm
# 幹掉php程式()/pkill強制刪除php
sudo pkill php-fpm
sudo php-fpm 
複製程式碼

三、Nginx安裝

brew search nginx
brew install nginx
複製程式碼

nginx相關命令

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

nginx配置

cd /usr/local/etc/nginx/
mkdir conf.d
vim nginx.conf
vim ./conf.d/default.conf
複製程式碼

nginx.conf

worker_processes 1; 
 
error_log    /usr/local/var/log/nginx/error.log;
 
pid    /usr/local/var/run/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   /usr/local/var/log/nginx/access.log main;
  port_in_redirect off;
  sendfile    on; 
  keepalive_timeout 65; 
 
  include /usr/local/etc/nginx/conf.d/*.conf;
}
複製程式碼

修改nginx配置檔案,再重啟時可能報出如下錯誤:

nginx: [error] invalid PID number “” in “/usr/local/var/run/nginx/nginx.pid”
複製程式碼

解決辦法:

sudo nginx -c /usr/local/etc/nginx/nginx.conf
sudo nginx -s reload
複製程式碼

default.conf

server {
    listen       80;
    server_name  www.myweb.com;
    root         專案地址;

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

    location / {
        index  index.html index.htm index.php;
        autoindex   on;
    try_files $uri /app_dev.php$is_args$args;
        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;
}

複製程式碼

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;
}
複製程式碼

四、MySQL安裝

brew search mysql
brew install mysql@5.7
複製程式碼

其他命令

# 獲取 service 列表
brew services list
# 重啟 mysql 服務
brew services restart mysql
# 停止 mysql 服務
brew services stop mysql
複製程式碼

配置到環境中

echo 'export PATH="/usr/local/opt/mysql@5.7/bin:$PATH"' >> ~/.bash_profile
source ~/.bash_profile 
複製程式碼

PS:如果出現下面這個錯誤:

ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
複製程式碼

就執行下面命令

mysql.server start
複製程式碼

設定MySQL開機自啟動

ln -sfv /usr/local/opt/mysql/*.plist ~/Library/LaunchAgents
launchctl load ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist
複製程式碼

開啟MySQL安全機制

mysql_secure_installation
複製程式碼
jianghehedeMacBook-Pro:~ comet
$ mysql_secure_installation
 
Securing the MySQL server deployment.
 
Connecting to MySQL using a blank password.
 
VALIDATE PASSWORD PLUGIN can be used to test passwords
and improve security. It checks the strength of password
and allows the users to set only those passwords which are
secure enough. Would you like to setup VALIDATE PASSWORD plugin?
 
Press y|Y for Yes, any other key for No: N   // 這個選yes的話密碼長度就必須要設定為8位以上,但我只想要6位的
Please set the password for root here.
 
New password:            // 設定密碼
 
Re-enter new password:     // 再一次確認密碼
By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL without having to have
a user account created for them. This is intended only for
testing, and to make the installation go a bit smoother.
You should remove them before moving into a production
environment.
 
Remove anonymous users? (Press y|Y for Yes, any other key for No) : Y    // 移除不用密碼的那個賬戶
Success.
 
 
Normally, root should only be allowed to connect from
'localhost'. This ensures that someone cannot guess at
the root password from the network.
 
Disallow root login remotely? (Press y|Y for Yes, any other key for No) : n
 
 ... skipping.
By default, MySQL comes with a database named 'test' that
anyone can access. This is also intended only for testing,
and should be removed before moving into a production
environment.
 
 
Remove test database and access to it? (Press y|Y for Yes, any other key for No) : y
 - Dropping test database...
Success.
 
 - Removing privileges on test database...
Success.
 
Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.
 
Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y
Success.
 
All done!
複製程式碼

五、安裝完成。。。。

六、打賞

覺得可以請小弟喝瓶礦泉水吧。嘻嘻嘻

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

相關文章