macOS搭建PHP开发环境

0.如何设置和关闭终端( terminal)代理&curl报错

设置系统全局变量http:https代理

第一种方法虽然可以pod成功, 但是curl www.google.com依旧还是不行
cd /etc
以管理员身份编辑
sudo vim bashrc
输入密码进入后, G$ 定位到文件末尾 按下键盘字母i开始输入, 在最下面位置粘贴
export http_proxy=127.0.0.1:1087
export https_proxy=127.0.0.1:1087
保存退出
esc  :wq
让修改的文件生效
source bashrc
验证:
curl www.google.com

可参考“https://www.jianshu.com/p/7b79d82f5972”这简文章解决

1.安装brew

  • 1.1 获取install文件
    把官方脚本拿下来
    curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install >> brew_install

当前命令执行完后,会在/usr/local/Homebrew下生成一个brew_install文件

  • 1.2 更改脚本中的资源链接,替换成清华大学的镜像
    vim /usr/local/Homebrew/brew_install

就是把这两句 
BREW_REPO = “https://github.com/Homebrew/brew“.freeze 
CORE_TAP_REPO = “https://github.com/Homebrew/homebrew-core“.freeze 
更改为这两句 
BREW_REPO = “https://mirrors.ustc.edu.cn/brew.git “.freeze 
CORE_TAP_REPO = “https://mirrors.ustc.edu.cn/homebrew-core.git“.freeze 
  • 1.3 执行脚本
    /usr/bin/ruby brew_install
  • 1.4 brew常用命令

brew update #更新可安装包的最新信息,建议每次安装前都运行下
brew search pkg_name #搜索相关的包信息
brew install pkg_name #安装包 

2.安装nginx

brew search nginx
brew install 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 warn; 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; port_in_redirect off; sendfile    on; keepalive_timeout 65; include /usr/local/etc/nginx/conf.d/*.conf; 

新建:conf.d/default.conf

server { listen 8080; server_name localhost; root /Users/user_name/nginx_sites/; # 该项要修改为你准备存放相关网页的路径 location / { index index.php; autoindex on; } #proxy the php scripts to php-fpm  location ~ \.php$ { include /usr/local/etc/nginx/fastcgi.conf; fastcgi_intercept_errors on; fastcgi_pass 127.0.0.1:9000; } } 

3.安装php-fpm

Mac OSX 10.9以后的系统自带了PHP、php-fpm,省去了安装php-fpm的麻烦。
这里需要简单地修改下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文件输出路径不存在的错误。

停止php-fpm:sudo pkill php-fpm

  • 配置php-fpm:
    sudo php-fpm报错:

$ sudo php-fpm
Password: [28-Mar-2019 20:07:37] WARNING: Nothing matches the include pattern '/private/etc/php-fpm.d/*.conf' from /private/etc/php-fpm.conf at line 125. [28-Mar-2019 20:07:37] ERROR: No pool defined. at least one pool section must be specified in config file [28-Mar-2019 20:07:37] ERROR: failed to post process the configuration [28-Mar-2019 20:07:37] ERROR: FPM initialization failed 
  • 解决方法:
    cd /private/etc/php-fpm.d
    sudo cp www.conf.default www.conf

$ cd /private/etc/php-fpm.d
$ ls
www.conf.default $ sudo cp www.conf.default www.conf
Password: 

4.安装mysql

安装:brew install mysql@5.7
启动:mysql.server start
关闭:mysql.server stop
配置:
修改密码:ALTER user ‘root’@’localhost’ IDENTIFIED BY ‘newpassward’

注意:安装的版本,如果不选版本默认安装的8.*,可能会造成navicat连接不上的问题!!

Jiaheng:var jiaheng$ brew search mysql ==> Formulae
automysqlbackup            mysql-connector-c          mysql@5.5 mysql                      mysql-connector-c++ mysql@5.6 mysql++ mysql-sandbox              mysql@5.7 mysql-client               mysql-search-replace       mysqltuner
mysql-cluster              mysql-utilities ==> Casks
homebrew/cask/mysql-connector-python     homebrew/cask/navicat-for-mysql
homebrew/cask/mysql-shell                homebrew/cask/sqlpro-for-mysql
homebrew/cask/mysql-utilities
Jiaheng:var jiaheng$ brew install mysql@5.7 Updating Homebrew... ==> Auto-updated Homebrew! Updated 1 tap (homebrew/core). ==> Updated Formulae
gradle ==> Downloading https://homebrew.bintray.com/bottles/mysql@5.7-5.7.25.high_sierr ######################################################################## 100.0% ==> Pouring mysql@5.7-5.7.25.high_sierra.bottle.tar.gz ==> /usr/local/Cellar/mysql@5.7/5.7.25/bin/mysqld --initialize-insecure --user=j ==> Caveats
We've installed your MySQL database without a root password. To secure it run:
    mysql_secure_installation

MySQL is configured to only allow connections from localhost by default

To connect run:
    mysql -uroot

A "/etc/my.cnf" from another install may interfere with a Homebrew-built
server starting up correctly.

mysql@5.7 is keg-only, which means it was not symlinked into /usr/local,
because this is an alternate version of another formula.

If you need to have mysql@5.7 first in your PATH run:
  echo 'export PATH="/usr/local/opt/mysql@5.7/bin:$PATH"' >> ~/.bash_profile

For compilers to find mysql@5.7 you may need to set:
  export LDFLAGS="-L/usr/local/opt/mysql@5.7/lib"
  export CPPFLAGS="-I/usr/local/opt/mysql@5.7/include"


To have launchd start mysql@5.7 now and restart at login:
  brew services start mysql@5.7
Or, if you don't want/need a background service you can just run: /usr/local/opt/mysql@5.7/bin/mysql.server start ==> Summary
🍺 /usr/local/Cellar/mysql@5.7/5.7.25: 319 files, 234.4MB 

最后,运行php环境:

  • 重启nginx:sudo nginx -s reload
  • 启动PHP服务:sudo php-fpm;

    image.png

5.配置虚拟机

  • 5.1新建conf配置
    例如:/usr/local/etc/nginx/conf.d/test

server {
  listen    80;
  server_name tianhong;

  root /Volumes/HHD/project/test

  location / {
    index index.php;
    autoindex on;
  }

  #proxy the php scripts to php-fpm
  location ~ \.php$ {
    include /usr/local/etc/nginx/fastcgi.conf;
    fastcgi_intercept_errors on;
    fastcgi_pass  127.0.0.1:9000;
  }

} 
  • 5.2配置/etc/hosts
    添加:127.0.0.1 test
    这样在浏览器就可以直接访问: http://test

作者:左木北鱼
链接:https://www.jianshu.com/p/5198efd4508e
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。