安装brew

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

安装完毕后,把源改成中科大的

cd "$(brew --repo)"
git remote set-url origin https://mirrors.ustc.edu.cn/brew.git

cd "$(brew --repo)/Library/Taps/homebrew/homebrew-core"
git remote set-url origin https://mirrors.ustc.edu.cn/homebrew-core.git

cd "$(brew --repo)"/Library/Taps/caskroom/homebrew-cask
git remote set-url origin https://mirrors.ustc.edu.cn/homebrew-cask.git

# 2选1
# for bash user
echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.ustc.edu.cn/homebrew-bottles' >> ~/.bash_profile
source ~/.bash_profile
# for zsh user
echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.ustc.edu.cn/homebrew-bottles' >> ~/.zshrc
source ~/.zshrc

安装 MySQL(MariaDB)

brew install mariadb

# 设置下,否则mysql_的命令不会出现
# ls /usr/local/Cellar/mariadb/ 看下是不是10.2.8,如果是别的版本记得更换下下面的路径
echo 'export PATH="$PATH:/usr/local/Cellar/mariadb/10.2.8/bin"' >> ~/.bash_profile

修改完必要的 /usr/local/etc/my.cnf 配置后(其实本地测试没什么好修改的,我就用的默认的)执行 mysql_install_db 命令初始化数据库(如果 /usr/local/var/mysql/ 目录已经有了,可以忽略这个)

启动服务器:

brew services start mariadb

下次开机会自动启动,看所有服务列表:

brew services list

如果要修改MySQL管理员密码,可以使用

mysql_secure_installation

命令(先把服务器启动起来)

安装PHP

先执行下面

brew install homebrew/php/php-install

这个命令,然后

brew search php

你会发现就有好多不同版本的 php 了,比如你要装 php7.1 版本的话,可以

brew search php71

,然后选择你需要的扩展模块安装就可以了,比如:

brew install homebrew/php/php71 homebrew/php/php71-redis homebrew/php/php71-swoole homebrew/php/php71-yaml

你可能没找到 mysql,php-fpm 等这些扩展,这个是 homebrew/php/php71 里就包含了的。

启动 php-fpm

在启动 php-fpm 前建议修改下配置

vim /usr/local/etc/php/7.1/php-fpm.d/www.conf

将 group 改成 staff,也可以将 user 改成自己的账户名。这样做可能会存在一点点安全风险,但是如果你用到php需要写一些文件的话,就会方便很多,否则连自己操作这个文件都没有权限,略麻烦。

启动的话非常简单,此时使用命令

brew services list

会看到 php71 是 stopped 的,执行

brew services start php71

就可以了。

安装nginx

执行执行

brew install nginx

就可以快速的安装好了,brew 安装好的 nginx 默认值监听 8080 端口的,这显然太麻烦了,果断改到80端口,但是由于 Mac 系统的限制,低于1000的端口不能由用户启动,所以需要再配置下。

执行

sudo vim /Library/LaunchDaemons/homebrew.mxcl.nginx.plist

输出密码,后把下面的内容写进去

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
  <dict>
    <key>Label</key>
    <string>homebrew.mxcl.nginx</string>
    <key>RunAtLoad</key>
    <true/>
    <key>KeepAlive</key>
    <false/>
    <key>ProgramArguments</key>
    <array>
        <string>/usr/local/opt/nginx/bin/nginx</string>
        <string>-g</string>
        <string>daemon off;</string>
    </array>
    <key>WorkingDirectory</key>
    <string>/usr/local</string>
  </dict>
</plist>

保存,然后执行下面代码加载 nginx

sudo launchctl load -w /Library/LaunchDaemons/homebrew.mxcl.nginx.plist

这样重启nginx

sudo launchctl stop homebrew.mxcl.nginx
sudo launchctl start homebrew.mxcl.nginx

看看有没有启动成功:

ps -ef | grep nginx

,如果有进程就表示启动成功了。

小建议,brew 安装的 nginx 默认是8个线程,个人觉得测试有点浪费了,所以可以自己设置下,

vim /usr/local/etc/nginx/nginx.conf

将 worker_processes 改成你要的值(我的是4)

安装phpMyAdmin

本人比较懒,直接使用

brew install homebrew/php/phpmyadmin

安装了个 phpMyAdmin,然后将下列配置文件写入

/usr/local/etc/nginx/servers/phpmyadmin.conf

重启 nginx 即可

upstream php-handler {
    server 127.0.0.1:9000;
}
server {
    listen 80;
    server_name 127.0.0.1;
    root /usr/local/share/phpmyadmin/;
    charset utf-8;

    gzip off;
    proxy_buffering off;
    index index.html index.php;

    location ~ .php$ {
        fastcgi_pass php-handler;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        #fastcgi_buffering off;
        fastcgi_buffers 10 256k;
        fastcgi_buffer_size 512k;
        fastcgi_keep_conn on;
        #fastcgi_request_buffering off;
        client_max_body_size 128m;
        fastcgi_temp_file_write_size 512k;
        include fastcgi_params;
    }
}

另外,如果要用 phpMyAdmin 执行超长的任务需要注意,它默认是最大300秒的执行时限的,请修改

/usr/local/etc/phpmyadmin.config.inc.php

文件,在最后加入如下内容即可:

$cfg['ExecTimeLimit'] = 0;  # 或你认为的最大值

另外,关于php.ini的一些设置,

vim /usr/local/etc/php/7.1/php.ini

,可以根据自己的需要调节,比如:

max_execution_time 30  # 最大执行时间,可以根据自己的需要修改
memory_limit = 128M    # 内存限制
post_max_size = 8M     # 将会影响上传文件大小
date.timezone          # 默认时区,可以改成 PRC(中华人民共和国缩写),即 date.timezone = PRC

改好后,使用

brew services restart php71

重启 php-fpm 即可。

至此,php,nginx,mysql都已经安装好了。如果你是用PhpStorm的开发者,推荐我调的代码风格和配色,预览和下载地址:https://www.queyang.com/blog/archives/638

see