macOS PHP 开发环境完整配置教程

7 阅读6分钟

本教程详细记录了在macOS上配置完整的PHP 7.4开发环境,包括MySQL、Redis、Nginx等组件。

目录


环境概览

技术栈版本

组件版本说明
PHP7.4.33使用shivammathur/php tap安装
MySQL8.4.xHomebrew官方版本
Redis8.4.x缓存服务器
Nginx1.29.xWeb服务器

目录结构

~/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/
│   └── mysql@8.4/
└── var/                        # 数据目录
    ├── mysql@8.4/
    └── 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/php@7.4

# 验证安装
/opt/homebrew/opt/php@7.4/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/php@7.4/bin:$PATH"
export PATH="/opt/homebrew/opt/php@7.4/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 mysql@8.4

# 启动MySQL服务
brew services start mysql@8.4

# 验证MySQL运行
brew services list | grep mysql

3.2 MySQL安全配置

# 运行安全配置脚本
/opt/homebrew/opt/mysql@8.4/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/php@7.4/bin/pecl install igbinary

安装说明

  • PECL会从pecl.php.net下载最新稳定版本
  • 编译过程会自动完成,无需额外配置
  • 安装完成后扩展会自动添加到php.ini

6.3 安装LZF压缩扩展

LZF是一个轻量级的压缩算法,用于Redis数据压缩,可以显著减少内存占用。

# 安装LZF扩展
/opt/homebrew/opt/php@7.4/bin/pecl install lzf

安装说明

  • LZF压缩解压速度极快
  • 适合实时数据压缩场景
  • 与igbinary配合使用效果最佳

6.4 安装Redis扩展(整合版)

Redis扩展是PHP连接Redis服务器的客户端库,支持所有Redis命令。

# 安装Redis扩展
/opt/homebrew/opt/php@7.4/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

配置选项说明

选项推荐说明
igbinaryyes二进制序列化,比标准序列化快30-50%
LZFyes轻量级压缩,CPU开销小,适合缓存数据
Zstdno高压缩率算法,但增加复杂度,开发环境不需要
MessagePackno跨语言序列化,纯PHP项目不需要
LZ4no极速压缩,已有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/php@7.4

阶段7:域名配置

7.1 为什么使用.test域名

重要提醒:不要使用.dev域名!

Chrome浏览器从2021年开始对所有.dev域名实施HSTS(HTTP严格传输安全)策略:

  • ❌ .dev域名会强制重定向到HTTPS
  • ❌ 本地开发环境无法提供HTTPS证书
  • ❌ 即使修改hosts文件也无法访问

推荐使用.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 php@7.4
brew services start mysql@8.4
brew services start redis

# 停止服务
brew services stop nginx
brew services stop php@7.4
brew services stop mysql@8.4
brew services stop redis

# 重启服务
brew services restart nginx
brew services restart php@7.4
brew services restart mysql@8.4
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 mysql@8.4

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/php@7.4/*/pecl/*/redis.so

# 重启PHP-FPM
brew services restart shivammathur/php/php@7.4

问题3:MySQL连接失败

错误信息

SQLSTATE[HY000] [1045] Access denied for user

解决方法

# 检查MySQL服务状态
brew services list | grep mysql

# 重启MySQL
brew services restart mysql@8.4

# 检查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/php@7.4

# 检查错误日志
tail -f ~/Sites/logs/php/php-fpm.error.log

# 测试PHP-FPM配置
/opt/homebrew/opt/php@7.4/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 php@7.4 mysql@8.4 redis

# 2. 验证服务状态
brew services list

# 3. 测试开发环境
curl http://local.test/final_verification.php

# 4. 开始开发...

部署前测试

# 1. 检查配置语法
nginx -t
/opt/homebrew/opt/php@7.4/sbin/php-fpm -t

# 2. 重启所有服务
brew services restart nginx php@7.4 mysql@8.4 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. 参考资源


总结

恭喜!你已经成功在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域名 - 专业的本地开发域名

下一步建议

  1. 熟悉环境:访问 local.test/final_verif… 验证所有功能
  2. 创建项目:在 ~/Sites/projects/ 目录下创建你的项目
  3. 添加域名:使用域名管理工具添加新项目域名
  4. 学习优化:根据项目需求调整配置优化性能

祝你开发愉快!🚀


本教程最后更新时间:2026年2月
适用系统:macOS (Apple Silicon / Intel)
适用Homebrew版本:4.0+