环境概览
技术栈版本
| 组件 | 版本 | 说明 |
|---|---|---|
| PHP | 7.4.33 | 使用 shivammathur/php tap 安装 |
| MySQL | 8.4.x | Homebrew 官方版本 |
| Redis | 8.4.x | 缓存服务器 |
| Nginx | 1.29.x | Web 服务器 |
目录结构
~/Sites/ # 项目根目录
├── localhost/ # 主开发环境
│ ├── index.php
│ ├── phpinfo.php
│ ├── db_test.php
│ ├── redis_test.php
│ └── final_verification.php
├── projects/ # 多项目目录
│ ├── blog/
│ ├── shop/
│ ├── admin/
│ └── api/
├── logs/ # 日志目录
│ ├── php/
│ └── nginx/
└── ssl/ # SSL 证书目录
/opt/homebrew/ # Homebrew 安装目录
├── etc/ # 配置文件
│ ├── php/7.4/
│ ├── nginx/
│ └── [email protected]/
└── var/ # 数据目录
├── [email protected]/
└── log/
前置准备
检查系统环境
# 检查 Homebrew 是否已安装
which brew
# 检查 Homebrew 版本
brew --version
# 检查系统架构
uname -m # 应该是 arm64(M1/M2/M3)或 x86_64(Intel)
更新 Homebrew
# 更新 Homebrew
brew update
# 检查是否有待升级的包
brew upgrade
阶段 1:安装 Homebrew 和基础组件
1.1 安装 Homebrew(如未安装)
# 安装 Homebrew(官方命令)
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
# 添加 Homebrew 到 PATH(Apple Silicon Mac)
echo 'eval "$(/opt/homebrew/bin/brew shellenv)"' >> ~/.zprofile
eval "$(/opt/homebrew/bin/brew shellenv)"
1.2 安装基础依赖
# 安装常用工具
brew install curl
brew install wget
brew install git
阶段 2:安装 PHP 7.4
2.1 添加 PHP 扩展仓库
macOS 的 Homebrew 官方仓库不再提供 PHP 7.4,需要使用 shivammathur/php tap:
# 添加 PHP 版本管理仓库
brew tap shivammathur/php
# 更新 brew
brew update
2.2 安装 PHP 7.4
# 安装 PHP 7.4(包含常用扩展)
brew install shivammathur/php/[email protected]
# 验证安装
/opt/homebrew/opt/[email protected]/bin/php --version
输出示例:
PHP 7.4.33 (cli) (built: Dec 19 2025 22:33:57) ( NTS ) Copyright (c) The PHP Group Zend Engine v3.4.0, Copyright (c) Zend Technologies with Zend OPcache v7.4.33, Copyright (c), by Zend Technologies
2.3 配置环境变量
# 编辑 shell 配置文件
vi ~/.zshrc
# 添加以下内容:
export PATH="/opt/homebrew/opt/[email protected]/bin:$PATH"
export PATH="/opt/homebrew/opt/[email protected]/sbin:$PATH"
# 使配置生效
source ~/.zshrc
2.4 验证 PHP 安装
# 检查 PHP 版本
php --version
# 检查已加载的扩展
php -m
# 查看 PHP 配置信息
php -i
阶段 3:安装和配置 MySQL
3.1 安装 MySQL 8.4
# 安装 MySQL 8.4
brew install [email protected]
# 启动 MySQL 服务
brew services start [email protected]
# 验证 MySQL 运行
brew services list | grep mysql
3.2 MySQL 安全配置
# 运行安全配置脚本
/opt/homebrew/opt/[email protected]/bin/mysql_secure_installation
# 按照提示完成配置:
# 1. 设置 VALIDATE PASSWORD component(可选)
# 2. 设置 root 密码
# 3. 移除匿名用户
# 4. 禁止 root 远程登录
# 5. 移除 test 数据库
# 6. 重新加载权限表
3.3 创建开发数据库和用户
# 连接 MySQL
mysql -u root -p
# 在 MySQL 命令行中执行:
CREATE DATABASE IF NOT EXISTS dev_database CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
CREATE USER IF NOT EXISTS 'dev_user'@'localhost' IDENTIFIED BY '你的密码';
GRANT ALL PRIVILEGES ON dev_database.* TO 'dev_user'@'localhost';
FLUSH PRIVILEGES;
EXIT;
3.4 验证 MySQL 连接
# 使用 root 用户连接
mysql -u root -p -e "SELECT VERSION();"
# 使用开发用户连接
mysql -u dev_user -p -e "USE dev_database; SHOW TABLES;"
3.5 MySQL 配置优化(可选)
# 编辑 MySQL 配置文件
vi /opt/homebrew/etc/mysql/my.cnf
# 添加优化配置:
[mysqld]
# 字符集配置
character-set-server = utf8mb4
collation-server = utf8mb4_unicode_ci
# InnoDB 配置
innodb_buffer_pool_size = 256M
innodb_log_file_size = 64M
# 连接配置
max_connections = 100
wait_timeout = 28800
# 日志配置
slow_query_log = 1
slow_query_log_file = /opt/homebrew/var/log/mysql/slow.log
long_query_time = 2
阶段 4:安装和配置 Nginx
4.1 安装 Nginx
# 安装 Nginx
brew install nginx
# 启动 Nginx 服务
brew services start nginx
# 验证 Nginx 运行
brew services list | grep nginx
# 测试配置语法
nginx -t
4.2 配置 Nginx 虚拟主机
4.2.1 创建主开发环境配置
# 创建主项目目录
mkdir -p ~/Sites/localhost
mkdir -p ~/Sites/logs/{php,nginx}
# 设置权限
chmod 755 ~/Sites
# 创建配置文件
vi /opt/homebrew/etc/nginx/servers/local.dev.conf
完整配置内容:
server {
listen 80;
server_name local.test;
# 网站根目录
root /Users/你的用户名/Sites/localhost;
index index.php index.html index.htm;
# 日志配置
access_log /Users/你的用户名/Sites/logs/nginx/local.test.access.log;
error_log /Users/你的用户名/Sites/logs/nginx/local.test.error.log;
# PHP 处理
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
fastcgi_read_timeout 300;
}
# URL 重写(支持 Laravel 等框架)
location / {
try_files $uri $uri/ /index.php?$query_string;
}
# 隐藏文件保护
location ~ /\. {
deny all;
}
# 静态文件缓存
location ~* \.(js|css|png|jpg|jpeg|gif|ico|woff|woff2)$ {
expires 1y;
add_header Cache-Control "public, immutable";
}
}
4.2.2 创建多项目配置
# 创建多项目目录
mkdir -p ~/Sites/projects/{blog,shop,admin,api}
# 设置权限
chmod 755 ~/Sites
# 创建多项目配置
vi /opt/homebrew/etc/nginx/servers/multi_test.conf
# 博客项目
server {
listen 80;
server_name blog.test;
root /Users/你的用户名/Sites/projects/blog;
index index.php index.html;
access_log /Users/你的用户名/Sites/logs/nginx/blog.test.access.log;
error_log /Users/你的用户名/Sites/logs/nginx/blog.test.error.log;
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
# 商城项目
server {
listen 80;
server_name shop.test;
root /Users/你的用户名/Sites/projects/shop;
index index.php index.html;
access_log /Users/你的用户名/Sites/logs/nginx/shop.test.access.log;
error_log /Users/你的用户名/Sites/logs/nginx/shop.test.error.log;
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
# 管理后台
server {
listen 80;
server_name admin.test;
root /Users/你的用户名/Sites/projects/admin;
index index.php index.html;
access_log /Users/你的用户名/Sites/logs/nginx/admin.test.access.log;
error_log /Users/你的用户名/Sites/logs/nginx/admin.test.error.log;
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
4.3 重启 Nginx
# 测试配置语法
nginx -t
# 重启服务
brew services restart nginx
# 查看服务状态
brew services list
阶段 5:安装和配置 Redis 服务器
5.1 安装 Redis 服务器
# 安装 Redis
brew install redis
# 启动 Redis 服务
brew services start redis
# 验证 Redis 运行
brew services list | grep redis
# 测试 Redis 连接
redis-cli ping
5.2 Redis 基本配置
# 编辑 Redis 配置文件
vi /opt/homebrew/etc/redis.conf
# 常用配置:
bind 127.0.0.1
port 6379
daemonize no
# 内存限制
maxmemory 256mb
maxmemory-policy allkeys-lru
# 日志配置
loglevel notice
logfile /opt/homebrew/var/log/redis.log
# RDB 持久化配置
save 900 1
save 300 10
save 60 10000
阶段 6:PHP 扩展配置(Redis 相关)
本阶段将安装和配置与 Redis 相关的 PHP 扩展,包括 igbinary 序列化加速和 LZF 压缩扩展。
6.1 安装前的系统依赖
# 确保 Homebrew 已更新
brew update
# 安装编译工具(如未安装)
brew install autoconf automake libtool
6.2 安装 igbinary 扩展
igbinary 是一个高性能的二进制序列化扩展,比 PHP 标准序列化快 30-50%,用于提升 Redis 数据序列化的性能。
# 安装 igbinary 扩展
/opt/homebrew/opt/[email protected]/bin/pecl install igbinary
安装说明:
- PECL 会从 pecl.php.net 下载最新稳定版本
- 编译过程会自动完成,无需额外配置
- 安装完成后扩展会自动添加到 php.ini
6.3 安装 LZF 压缩扩展
LZF 是一个轻量级的压缩算法,用于 Redis 数据压缩,可以显著减少内存占用。
# 安装 LZF 扩展
/opt/homebrew/opt/[email protected]/bin/pecl install lzf
安装说明:
- LZF 压缩解压速度极快
- 适合实时数据压缩场景
- 与 igbinary 配合使用效果最佳
6.4 安装 Redis 扩展(整合版)
Redis 扩展 是 PHP 连接 Redis 服务器的客户端库,支持所有 Redis 命令。
# 安装 Redis 扩展
/opt/homebrew/opt/[email protected]/bin/pecl install redis
安装过程中的配置选择:
enable igbinary serializer support? [yes] : yes
enable lzf compression support? [yes] : yes
enable zstd compression support? [no] : no
enable msgpack serializer support? [no] : no
enable lz4 compression support? [no] : no
配置选项说明:
| 选项 | 推荐 | 说明 |
|---|---|---|
| igbinary | yes | 二进制序列化,比标准序列化快 30-50% |
| LZF | yes | 轻量级压缩,CPU 开销小,适合缓存数据 |
| Zstd | no | 高压缩率算法,但增加复杂度,开发环境不需要 |
| MessagePack | no | 跨语言序列化,纯 PHP 项目不需要 |
| LZ4 | no | 极速压缩,已有 LZF 足够 |
6.5 验证扩展安装
# 检查扩展是否正确加载
php -m | grep -E "(redis|igbinary|lzf)"
# 应该显示:
# igbinary
# lzf
# redis
6.6 PHP.ini 基础配置
# 编辑 PHP 配置文件
vi /opt/homebrew/etc/php/7.4/php.ini
# 关键配置:
display_errors = On
error_reporting = E_ALL
log_errors = On
error_log = /Users/你的用户名/Sites/logs/php/php_errors.log
memory_limit = 256M
max_execution_time = 300
upload_max_filesize = 64M
post_max_size = 64M
扩展配置(通常自动添加,无需手动配置):
extension=igbinary.so
extension=lzf.so
extension=redis.so
6.7 PHP-FPM 配置
# 编辑 PHP-FPM 配置
vi /opt/homebrew/etc/php/7.4/php-fpm.d/www.conf
# 配置内容:
[www]
user = 你的用户名
group = staff
listen = 127.0.0.1:9000
pm = dynamic
pm.max_children = 50
pm.start_servers = 5
pm.min_spare_servers = 2
pm.max_spare_servers = 8
php_admin_value[error_log]= /Users/你的用户名/Sites/logs/php/php-fpm.error.log
php_admin_flag[log_errors]= on
6.8 重启 PHP-FPM 服务
# 重启 PHP-FPM 服务
brew services restart shivammathur/php/[email protected]
阶段 7:域名配置
7.1 为什么使用 .test 域名
重要提醒:不要使用 .dev 域名!
Chrome 浏览器从 2021 年开始对所有 .dev 域名实施 HSTS(HTTP 严格传输安全)策略:
- ❌ .dev 域名会强制重定向到 HTTPS
- ❌ 本地开发环境无法提供 HTTPS 证书
- ❌ 即使修改 hosts 文件也无法访问
推荐使用 .test 域名:
- ✅ .test 域名专为本地开发设计
- ✅ 无 HSTS 限制
- ✅ 所有浏览器兼容
- ✅ RFC 规范推荐
7.2 配置 hosts 文件
# 编辑 hosts 文件
sudo vi /etc/hosts
# 添加以下内容:
127.0.0.1 local.test
127.0.0.1 blog.test
127.0.0.1 shop.test
127.0.0.1 admin.test
127.0.0.1 api.test
7.3 验证域名解析
# 测试域名解析
ping local.test # 应该返回:PING local.test (127.0.0.1)
# 测试 HTTP 访问
curl -I http://local.test # 应该返回:HTTP/1.1 200 OK
阶段 8:服务管理和验证
8.1 服务管理命令
# 查看服务状态
brew services list
# 检查端口占用
lsof -i :80,9000,3306,6379
8.2 创建测试页面
8.2.1 PHP 信息页面
<?php
// ~/Sites/localhost/phpinfo.php
phpinfo();
8.2.2 数据库连接测试
<?php
// ~/Sites/localhost/db_test.php
echo "<h1>数据库连接测试</h1>";
try {
$pdo = new PDO("mysql:host=localhost;dbname=dev_database", "dev_user", "你的密码");
echo "✅ MySQL 连接成功<br>";
$stmt = $pdo->query("SELECT VERSION() as version");
$row = $stmt->fetch(PDO::FETCH_ASSOC);
echo "MySQL 版本:" . $row['version'] . "<br>";
} catch (PDOException $e) {
echo "❌ MySQL 连接失败:" . $e->getMessage() . "<br>";
}
8.2.3 Redis 连接测试
<?php
// ~/Sites/localhost/redis_test.php
echo "<h1>Redis 连接测试</h1>";
try {
$redis = new Redis();
$redis->connect('127.0.0.1', 6379);
echo "✅ Redis 连接成功<br>";
$redis->set('test_key', 'Hello Redis!');
$value = $redis->get('test_key');
echo "Redis 读写测试:$value<br>";
// 测试 igbinary 序列化
if (extension_loaded('igbinary')) {
$data = ['test' => 'data'];
$serialized = igbinary_serialize($data);
$redis->set('igbinary_test', $serialized);
$result = igbinary_unserialize($redis->get('igbinary_test'));
echo "igbinary 序列化:" . ($data === $result ? "✅ 成功" : "❌ 失败") . "<br>";
}
} catch (Exception $e) {
echo "❌ Redis 连接失败:" . $e->getMessage() . "<br>";
}
8.2.4 完整环境验证
<?php
// ~/Sites/localhost/final_verification.php
echo "<h1>🎉 PHP 7.4 开发环境完整验证</h1>";
// PHP 版本
echo "<h2>PHP 版本:" . phpversion() . "</h2>";
// 扩展检查
$extensions = ['pdo_mysql', 'mysqli', 'redis', 'curl', 'gd', 'mbstring', 'openssl', 'igbinary', 'lzf'];
echo "<h3>扩展状态:</h3>";
foreach ($extensions as $ext) {
$status = extension_loaded($ext) ? "✅" : "❌";
echo "$status$ext<br>";
}
// 服务端口检查
echo "<h3>服务状态:</h3>";
$ports = [80 => 'Nginx', 9000 => 'PHP-FPM', 3306 => 'MySQL', 6379 => 'Redis'];
foreach ($ports as $port => $service) {
$fp = @fsockopen('127.0.0.1', $port, $errno, $errstr, 1);
$status = $fp ? "✅" : "❌";
echo "$status$service (端口 $port)<br>";
if ($fp) fclose($fp);
}
// 数据库连接
echo "<h3>数据库连接:</h3>";
try {
$pdo = new PDO("mysql:host=localhost;dbname=dev_database", "dev_user", "你的密码");
echo "✅ MySQL 连接成功<br>";
} catch (PDOException $e) {
echo "❌ MySQL 连接失败<br>";
}
// Redis 连接
echo "<h3>Redis 连接:</h3>";
try {
$redis = new Redis();
$redis->connect('127.0.0.1', 6379);
echo "✅ Redis 连接成功<br>";
} catch (Exception $e) {
echo "❌ Redis 连接失败<br>";
}
8.3 浏览器访问测试
配置完成后,在浏览器中访问:
# 主环境验证
http://local.test/final_verification.php
# PHP 信息
http://local.test/phpinfo.php
# 数据库测试
http://local.test/db_test.php
# Redis 测试
http://local.test/redis_test.php
# 多项目
http://blog.test
http://shop.test
http://admin.test
常用命令速查
服务管理
# 启动服务
brew services start nginx
brew services start [email protected]
brew services start [email protected]
brew services start redis
# 停止服务
brew services stop nginx
brew services stop [email protected]
brew services stop [email protected]
brew services stop redis
# 重启服务
brew services restart nginx
brew services restart [email protected]
brew services restart [email protected]
brew services restart redis
# 查看所有服务状态
brew services list
# 检查特定端口
lsof -i :80 # Nginx
lsof -i :9000 # PHP-FPM
lsof -i :3306 # MySQL
lsof -i :6379 # Redis
PHP 相关
# PHP 版本
php --version
# 已加载的扩展
php -m
# PHP 配置信息
php -i
# 查找 php.ini 位置
php --ini
# 特定扩展版本
php -r "echo phpversion('redis');"
MySQL 相关
# 连接 MySQL
mysql -u root -p
mysql -u dev_user -p
# 查看数据库
SHOW DATABASES;
# 选择数据库
USE dev_database;
# 查看表
SHOW TABLES;
# MySQL 版本
mysql -V
# 重启 MySQL
brew services restart [email protected]
Redis 相关
# 测试连接
redis-cli ping
# Redis 命令行
redis-cli
# 基本操作
redis-cli SET key value
redis-cli GET key
redis-cli KEYS *
redis-cli DEL key
# 查看 Redis 信息
redis-cli INFO
# 清空所有数据
redis-cli FLUSHALL
Nginx 相关
# 测试配置语法
nginx -t
# 重载配置
nginx -s reload
# 停止 Nginx
nginx -s stop
# 查看 Nginx 版本
nginx -v
# 检查配置
nginx -T
日志查看
# 实时查看日志
tail -f ~/Sites/logs/php/php_errors.log
tail -f ~/Sites/logs/nginx/local.test.error.log
# 查看错误日志
tail -50 ~/Sites/logs/php/php-fpm.error.log
tail -50 ~/Sites/logs/nginx/local.test.error.log
# Nginx 访问日志
tail -50 ~/Sites/logs/nginx/local.test.access.log
故障排除
问题 1:Nginx 启动失败
错误信息:
nginx: [emerg] unknown "user" variable
解决方法:
# 检查并修复 Nginx 配置
nginx -t
# 查看详细错误
tail -f /opt/homebrew/var/log/nginx/error.log
# 确保使用实际用户名而非变量
# 错误示例:root /Users/$USER/Sites/localhost;
# 正确示例:root /Users/你的用户名/Sites/localhost;
问题 2:PHP 扩展未加载
错误信息:
PHP Fatal error: Uncaught Error: Class 'Redis' not found
解决方法:
# 检查扩展是否安装
php -m | grep redis
# 检查 php.ini 配置
php -i | grep extension_dir
# 确认扩展文件存在
ls /opt/homebrew/Cellar/[email protected]/*/pecl/*/redis.so
# 重启 PHP-FPM
brew services restart shivammathur/php/[email protected]
问题 3:MySQL 连接失败
错误信息:
SQLSTATE[HY000] [1045] Access denied for user
解决方法:
# 检查 MySQL 服务状态
brew services list | grep mysql
# 重启 MySQL
brew services restart [email protected]
# 检查 MySQL 错误日志
tail -f /opt/homebrew/var/mysql/*.err
# 验证用户名密码
mysql -u dev_user -p
问题 4:.dev 域名无法访问
问题原因: Chrome 对 .dev 域名实施了 HSTS 策略
解决方法: 使用 .test 域名替代:
# hosts 文件中使用.test 后缀
127.0.0.1 local.test
127.0.0.1 blog.test
问题 5:端口被占用
错误信息:
nginx: [emerg] bind() to 0.0.0.0:80 failed (48: Address already in use)
解决方法:
# 查找占用端口的进程
lsof -i :80
# 终止占用进程
kill -9 PID
# 或使用其他端口
# 修改 Nginx 配置:listen 8080;
问题 6:PHP-FPM 未运行
解决方法:
# 检查 PHP-FPM 服务
brew services list | grep php
# 手动启动
brew services start shivammathur/php/[email protected]
# 检查错误日志
tail -f ~/Sites/logs/php/php-fpm.error.log
# 测试 PHP-FPM 配置
/opt/homebrew/opt/[email protected]/sbin/php-fpm -t
问题 7:Redis 连接失败
错误信息:
RedisException: Redis server went away
解决方法:
# 检查 Redis 服务状态
brew services list | grep redis
# 启动 Redis
brew services start redis
# 测试连接
redis-cli ping
# 检查 Redis 日志
tail -f /opt/homebrew/var/log/redis.log
最佳实践
1. 定期维护
# 每周检查服务状态
brew services list
# 定期查看日志
tail -f ~/Sites/logs/php/*.log
tail -f ~/Sites/logs/nginx/*.log
# 定期清理过期日志
find ~/Sites/logs -name "*.log" -mtime +7 -delete
2. 性能优化
PHP 优化
# php.ini
opcache.enable = 1
opcache.memory_consumption = 256
opcache.interned_strings_buffer = 16
opcache.max_accelerated_files = 10000
opcache.validate_timestamps = 1
opcache.revalidate_freq = 2
MySQL 优化
# my.cnf
innodb_buffer_pool_size = 512M
innodb_log_file_size = 128M
max_connections = 200
query_cache_size = 64M
Redis 优化
# redis.conf
maxmemory 512mb
maxmemory-policy allkeys-lru
appendonly yes
appendfsync everysec
3. 安全管理
MySQL 安全
-- 使用强密码
CREATE USER 'user'@'localhost' IDENTIFIED BY 'StrongP@ssw0rd!';
-- 定期备份数据库
mysqldump -u root -p dev_database > backup_$(date+%Y%m%d).sql
文件权限
# 项目目录权限
chmod 755 ~/Sites/localhost
chmod 644 ~/Sites/localhost/*.php
# 日志目录权限
chmod 755 ~/Sites/logs
chmod 644 ~/Sites/logs/**/*.log
# 禁止 Web 访问敏感文件
# 在 nginx 配置中添加:
location ~ /\. {
deny all;
}
4. 开发工作流
本地开发
# 1. 启动所有服务
brew services start nginx [email protected] [email protected] redis
# 2. 验证服务状态
brew services list
# 3. 测试开发环境
curl http://local.test/final_verification.php
# 4. 开始开发...
部署前测试
# 1. 检查配置语法
nginx -t
/opt/homebrew/opt/[email protected]/sbin/php-fpm -t
# 2. 重启所有服务
brew services restart nginx [email protected] [email protected] redis
# 3. 全面测试
curl -I http://local.test
curl http://local.test/db_test.php
curl http://local.test/redis_test.php
5. 备份和恢复
数据库备份
# 完整备份
mysqldump -u root -p --all-databases > full_backup_$(date +%Y%m%d).sql
# 单个数据库备份
mysqldump -u root -p dev_database > dev_database_backup_$(date +%Y%m%d).sql
# 恢复数据库
mysql -u root -p < backup_file.sql
配置备份
# 备份所有配置
cp -r /opt/homebrew/etc/nginx ~/backup/nginx_config_$(date +%Y%m%d)
cp -r /opt/homebrew/etc/php/7.4 ~/backup/php_config_$(date +%Y%m%d)
cp -r /opt/homebrew/etc/mysql ~/backup/mysql_config_$(date +%Y%m%d)
附录
A. Homebrew 常用命令
# 安装包
brew install <package_name>
# 卸载包
brew uninstall <package_name>
# 搜索包
brew search <package_name>
# 查看已安装的包
brew list
# 更新包
brew upgrade <package_name>
# 清理旧版本
brew cleanup
# 查看信息
brew info <package_name>
B. 常用文件位置
| 用途 | 路径 |
|---|---|
| PHP 配置 | /opt/homebrew/etc/php/7.4/php.ini |
| PHP-FPM 配置 | /opt/homebrew/etc/php/7.4/php-fpm.d/www.conf |
| Nginx 配置 | /opt/homebrew/etc/nginx/nginx.conf |
| Nginx 虚拟主机 | /opt/homebrew/etc/nginx/servers/ |
| MySQL 配置 | /opt/homebrew/etc/mysql/my.cnf |
| Redis 配置 | /opt/homebrew/etc/redis.conf |
| PHP 错误日志 | ~/Sites/logs/php/php_errors.log |
| Nginx 访问日志 | ~/Sites/logs/nginx/*.access.log |
| 项目根目录 | ~/Sites/ |
C. 参考资源
- Homebrew 官网: https://brew.sh
- PHP 官方文档: https://www.php.net/manual/
- MySQL 官方文档: https://dev.mysql.com/doc/
- Redis 官方文档: https://redis.io/documentation
- Nginx 官方文档: https://nginx.org/en/docs/
总结
恭喜!你已经成功在 macOS 上配置了完整的 PHP 7.4 开发环境,包括:
✅ PHP 7.4.33 - 完整的 PHP 开发环境 ✅ MySQL 8.4.x - 关系型数据库 ✅ Redis 8.4.x - 高性能缓存系统 ✅ Nginx 1.29.x - Web 服务器 ✅ igbinary - 二进制序列化加速 ✅ LZF - 轻量级压缩 ✅ .test 域名 - 专业的本地开发域名
下一步建议
- 熟悉环境:访问 http://local.test/final_verification.php 验证所有功能
- 创建项目:在 ~/Sites/projects/ 目录下创建你的项目
- 添加域名:使用域名管理工具添加新项目域名
- 学习优化:根据项目需求调整配置优化性能
祝你开发愉快!🚀


