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

1、Homebrew安裝

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

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

homebrew的經常使用命令:html

brew update #更新可安裝包的最新信息,建議每次安裝前都運行下
brew search pkg_name #搜索相關的包信息
brew install pkg_name #安裝包
複製代碼

2、PHP安裝

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

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文件輸出路徑不存在的錯誤。nginx

修改以後在終端輸入:

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

3、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;
}
複製代碼

4、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) 複製代碼

就執行下面命令git

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!
複製代碼

5、安裝完成。。。。

6、打賞

以爲能夠請小弟喝瓶礦泉水吧。嘻嘻嘻

           github

相關文章
相關標籤/搜索