Mac 下 Homebrew 搭建 PHP 开发环境

PHP

安装

1
2
3
4
5
6
7
brew install php
# 启动
brew services start php@7.4
# 重启
brew services restart php@7.4
# 查看安装好的php路径
brew --prefix php@7.4

配置

1
2
3
4
5
6
7
8
9
10
11
12
13
# 配置 /usr/local/etc/php/7.4/php-fpm.conf
pid = /usr/local/var/run/php74-fpm.pid
error_log = /Users/sev/Development/logs/php74-fpm.log

vim ~/.bash_profile
export PATH="/usr/local/opt/php@7.4/bin:$PATH"
export PATH="/usr/local/opt/php@7.4/sbin:$PATH"
export LDFLAGS="-L/usr/local/opt/php@7.4/lib"
export CPPFLAGS="-I/usr/local/opt/php@7.4/include"
# 注意:source ~/.bash_profile, 关闭窗口后就失效, 配置打开终端就生效
vim ~/.zshrc
# 在文件尾部添加一行
source ~/.bash_profile

redis 扩展

1
2
3
4
5
6
7
8
9
10
11
# 下载 Redis 包并解压
phpize
./configure --with-php-config=/usr/local/opt/php@7.4/bin/php-config
make
make install

# /usr/local/etc/php/7.4/conf.d/ext-redis.ini
extension=redis.so

# 查看是否安装
php -m | grep redis

替换默认 PHP

1
2
3
4
5
6
7
8
9
10
vim ~/.bash_profile
export PATH=/Applications/MAMP/bin/php/php7.4.12/bin:$PATH

source ~/.bash_profile

# 替换后,PHP 命令行响应慢(原因:尝试执行 DNS 查找本地计算机的主机名)
# 查看 Mac 名称:系统偏好设置 -> 共享 -> sevdeiMac.local
# 配置 hosts,加入以下两行代码
127.0.0.1 sevdeiMac.local
::1 sevdeiMac.local

Nginx

安装

1
2
3
4
5
6
7
brew install nginx
# 查看配置是否正常
sudo nginx -t
# 启动
sudo nginx
# 重启
sudo nginx -s reload

配置 /usr/local/etc/nginx/nginx.conf

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
pid        /usr/local/var/run/nginx.pid;

access_log /Users/sev/Development/logs/nginx_access.log;
error_log /Users/sev/Development/logs/nginx_error.log;

location / {
root html;
index index.html index.htm index.php;
}

location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
#fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}

include /Users/sev/Development/nginx/conf.d/*.conf;

配置 sock 连接 PHP, 方便支持 PHP 多版本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# 配置 /usr/local/etc/nginx/nginx.conf
# 配置用户/组(与 php 运行的用户/组 一致)
user sev staff;

location ~ \.php$ {
root html;
fastcgi_pass /usr/local/var/run/php74-fpm.sock;
fastcgi_index index.php;
#fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}

# 配置 /usr/local/etc/php/7.4/php-fpm.d/www.conf
; listen = 127.0.0.1:9000
listen = /usr/local/var/run/php74-fpm.sock

# 重启 Nginx 和 PHP 服务

Redis

安装

1
2
3
4
5
brew install redis
# 启动
brew services start redis
# 通过配置文件启动 nginx
redis-server /usr/local/etc/redis.conf

配置

1
2
3
4
5
6
7
# 配置 /usr/local/etc/redis.conf
daemonize yes
requirepass 123456

# 开机自启配置
cp /usr/local/Cellar/redis/6.2.4/homebrew.mxcl.redis.plist ~/Library/LaunchAgents
launchctl load ~/Library/LaunchAgents/homebrew.mxcl.redis.plist

MySQL

安装

1
2
3
4
brew install mysql@5.7
# 启动数据库
brew services start mysql@5.7
# 默认无密码

配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
vim ~/.bash_profile
export PATH="/usr/local/opt/mysql@5.7/bin:$PATH"
export LDFLAGS="-L/usr/local/opt/mysql@5.7/lib"
export CPPFLAGS="-I/usr/local/opt/mysql@5.7/include"
# 运行
source ~/.bash_profile

# 修改密码
mysql -u root -p
use mysql;
UPDATE user SET authentication_string=password('123456') WHERE user='root';
FLUSH PRIVILEGES;
# 提示:ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
SET GLOBAL validate_password_policy=LOW;
SET GLOBAL validate_password_length=6;
0%