共计 8066 个字符,预计需要花费 21 分钟才能阅读完成。
提醒:本文最后更新于2025-07-07 14:55,文中所关联的信息可能已发生改变,请知悉!
Mac OS X
系统自带有 php-fpm
等 php 运行环境,但还需要配置起来才能正常使用。本文简要介绍配置和运行系统自带 php-fpm
的方法与步骤,以及 nginx 的安装与配置。
1 安装 brew
brew 是 mac os x 上最流行的包管理工具,使用它安装软件非常的方便。如果你没有安装,那么安装它:
# 官方脚本 | |
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)" | |
# 国内gitee脚本 | |
/bin/zsh -c "$(curl -fsSL https://gitee.com/cunkai/HomebrewCN/raw/master/Homebrew.sh)" |
brew cask
k 是 brew 的一个子集,也就是一个扩展,主要用于有GUI的软件。安装 brew cask
:
brew install caskroom/cask/brew-cask
1.1 brew 替换为国内源
查看 brew 源:
# 查看 brew.git 当前源 | |
cd "$(brew --repo)" && git remote -v | |
# 查看 homebrew-core.git 当前源 | |
cd "$(brew --repo homebrew/core)" && git remote -v |
brew 替换为阿里源(推荐):
# 修改 brew.git 为阿里源 | |
$ git -C "$(brew --repo)" remote set-url origin https://mirrors.aliyun.com/homebrew/brew.git | |
# 修改 homebrew-core.git 为阿里源 | |
$ git -C "$(brew --repo homebrew/core)" remote set-url origin https://mirrors.aliyun.com/homebrew/homebrew-core.git | |
# zsh 替换 brew bintray 镜像 | |
echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.aliyun.com/homebrew/homebrew-bottles' >> ~/.zshrc | |
source ~/.zshrc | |
# bash 替换 brew bintray 镜像 | |
echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.aliyun.com/homebrew/homebrew-bottles' >> ~/.bash_profile | |
source ~/.bash_profile | |
# 刷新源 | |
brew update |
brew 替换为中科大源:
# 替换 brew 源 | |
git -C "$(brew --repo)" remote set-url origin https://mirrors.ustc.edu.cn/brew.git | |
git -C "$(brew --repo homebrew/core)" remote set-url origin https://mirrors.ustc.edu.cn/homebrew-core.git | |
git -C "$(brew --repo homebrew/cask)" remote set-url origin https://mirrors.ustc.edu.cn/homebrew-cask.git | |
# zsh 替换 brew bintray 镜像 | |
echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.ustc.edu.cn/homebrew-bottles' >> ~/.zshrc | |
source ~/.zshrc | |
# bash 替换 brew bintray 镜像 | |
echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.ustc.edu.cn/homebrew-bottles' >> ~/.bash_profile | |
source ~/.bash_profile | |
# 刷新源 | |
brew update |
brew 替换为清华源:
# 替换 brew 源 | |
git -C "$(brew --repo)" remote set-url origin https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/brew.git | |
git -C "$(brew --repo homebrew/core)" remote set-url origin https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-core.git | |
git -C "$(brew --repo homebrew/cask)" remote set-url origin https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-cask.git | |
# zsh 替换 brew bintray 镜像 | |
echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles' >> ~/.zshrc | |
source ~/.zshrc | |
# bash 替换 brew bintray 镜像 | |
echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles' >> ~/.bash_profile | |
source ~/.bash_profile | |
# 刷新源 | |
$ brew update |
brew 重置为官方源
# 重置 brew.git 为官方源 | |
git -C "$(brew --repo)" remote set-url origin https://github.com/Homebrew/brew.git | |
# 重置 homebrew-core.git 为官方源 | |
git -C "$(brew --repo homebrew/core)" remote set-url origin https://github.com/Homebrew/homebrew-core.git | |
# 重置 homebrew-cask.git 为官方源 | |
git -C "$(brew --repo homebrew/cask)" remote set-url origin https://github.com/Homebrew/homebrew-cask | |
# zsh 注释掉 HOMEBREW_BOTTLE_DOMAIN 配置 | |
vi ~/.zshrc | |
# export HOMEBREW_BOTTLE_DOMAIN=xxxxxxxxx | |
# bash 注释掉 HOMEBREW_BOTTLE_DOMAIN 配置 | |
vi ~/.bash_profile | |
# export HOMEBREW_BOTTLE_DOMAIN=xxxxxxxxx | |
# 刷新源 | |
$ brew update |
1.2 brew 常用命令
brew update #更新brew可安装包,建议每次执行一下 | |
brew search php55 #搜索php5.5 | |
brew tap josegonzalez/php #安装扩展<gihhub_user/repo> | |
brew tap #查看安装的扩展列表 | |
brew install php55 #安装php5.5 | |
brew remove php55 #卸载php5.5 | |
brew upgrade php55 #升级php5.5 | |
brew options php55 #查看php5.5安装选项 | |
brew info php55 #查看php5.5相关信息 | |
brew home php55 #访问php5.5官方网站 |
2 配置启用 php 环境
2.1 创建 php.ini
配置文件
首先查看默认的 php 的编译参数,有些值是编译进执行程序的,无法更改,所以需要按照默认设置来配置。
$ php -i|grep config
在输出结果中找到配置文件(php.ini)的位置,可以看到下面两项的值指定:
'--with-config-file-path=/etc' | |
'--with-config-file-scan-dir=/Library/Server/Web/Config/php' |
根据这个指定,需要在 /etc
目录下创建 php.ini
。Mac 在 /private/etc
和 /etc
下均提供了样例文件 php.ini.default
,两个文件完全相同。选择一个复制为 /etc/php.ini
:
sudo cp /etc/php.ini.default /etc/php.ini
变更 owner
:
# chown <你的用户名> /etc/php.ini | |
# chmod u+w /etc/php.ini |
2.2 创建配置文件 php-fpm.conf
直接运行 php-fpm
,会报错找不到配置文件。
$ php-fpm
可以在 /private/etc/
目录下生成配置文件:
sudo cp /private/etc/php-fpm.conf.default /private/etc/php-fpm.conf | |
sudo chmod 777 /private/etc/php-fpm.conf |
另外,也可以在普通用户有权限的目录里放置配置文件,通过 --fpm-config
参数指定配置文件的位置:
cp /private/etc/php-fpm.conf.default /usr/local/etc/php-fpm.conf
然后可以通过如下命令执行:
$ php-fpm --fpm-config /usr/local/etc/php-fpm.conf
2.3 修改默认的日志和 pid 配置
$ vim /usr/local/etc/php-fpm.conf
修改 error_log
项,默认前缀是 /usr/var
,但该路径并不存在,可以改到其他地方。参考:
pid = /usr/local/var/run/php-fpm.pid | |
error_log = /usr/local/var/log/php-fpm.log |
当然,也可以不修改配置文件中配置项的路径,在php-fpm
的运行参数中(-p)指定放置运行时文件的相对路径前缀。示例:
$ php-fpm --fpm-config /usr/local/etc/php-fpm.conf --prefix /usr/local/var
到此,php-fpm
守护进程已经基本可以正确的启动了。默认监听 9000
端口。
相关命令参考:
启动: php-fpm --fpm-config /usr/local/etc/php-fpm.conf --prefix /usr/local/var | |
停止: sudo killall php-fpm |
php-fpm
开机启动:
ln -sfv /usr/local/opt/php55/*.plist ~/Library/LaunchAgents | |
launchctl load ~/Library/LaunchAgents/homebrew.mxcl.php55.plist |
2.4 编译安装 PHP 扩展的方法参考
创建 config-file-scan-dir
指定的目录:
mkdir -p /Library/Server/Web/Config/php
源码编译安装 php 扩展
这里以 php_discuz
扩展 (https://github.com/potterhe/php_discuz)[https://github.com/potterhe/php_discuz] 为例,介绍编译安装扩展的步骤。
假如扩展源码在 /Users/lzwme/php_discuz
目录,进入该目录并编译:
$ ./configure | |
$ make |
扩展编译后,默认会存储在 /Users/lzwme/php_discuz/modules/discuz.so
。将扩展配置到 php 自动加载配置的目录中:
$ echo "extension=/Users/lzwme/php_discuz/modules/discuz.so" > /Library/Server/Web/Config/php/discuz.ini
测试验证是否成功
$ php -i|grep discuz | |
discuz support => enabled |
执行测试用例
$ php -f /Users/lzwme/php_discuz/discuz.php
使用 brew 安装 php 扩展
以安装 phpredis
为例:
# 查看 php 版本 | |
php -v | |
PHP 5.5.36 (cli) (built: May 29 2016 01:07:06) | |
# 根据 php 版本安装对应版本的扩展 | |
$ brew install homebrew/php/php55-redis --build-from-source |
3 安装和配置 nginx
3.1 安装 nginx
使用 brewhome
安装 nginx:
brew install nginx --with-http_geoip_module --with-http2
3.2 新建 server
3.2.1 配置普通 server
在 /usr/local/etc/nginx/servers/
目录中新建的配置文件,都会被 nginx.conf
加载。
cd /usr/local/etc/nginx/servers | |
vi localhost-80.conf |
在其中输入 server 配置。参考:
server { | |
listen 80; | |
root /Users/lzwme/test; | |
server_name localhost; | |
index index.html index.htm index.php; | |
# 方便本地测试,开启目录浏览遍历 | |
location / { | |
autoindex on; # 开启目录浏览功能 | |
autoindex_exact_size off; # 关闭详细文件大小统计,让文件大小显示MB,GB单位,默认为b | |
autoindex_localtime on; # 开启以服务器本地时区显示文件修改日期 | |
} | |
location ~ \.php$ { | |
#fastcgi_pass unix:/tmp/php-cgi.sock; | |
fastcgi_pass 127.0.0.1:9000; | |
fastcgi_index index.php; | |
include fastcgi.conf; | |
} | |
} |
3.2.2 配置 https
生成私有证书
sudo mkdir /usr/local/etc/nginx/ssl/ | |
cd /usr/local/etc/nginx/ssl/ | |
sudo openssl genrsa -des3 -out localhost.key 1024 | |
sudo openssl req -new -key localhost.key -out localhost.csr | |
sudo openssl rsa -in localhost.key -out localhost_nopwd.key | |
sudo openssl x509 -req -days 365 -in localhost.csr -signkey localhost_nopwd.key -out localhost.crt |
在 /usr/local/etc/nginx/servers/
目录下新建 localhost-443.conf
,内容为:
server { | |
listen 443 ssl http2 default_server; | |
root /Users/lzwme/test; | |
server_name localhost; | |
index index.html index.htm index.php; | |
ssl_certificate ssl/localhost.crt; | |
ssl_certificate_key ssl/localhost_nopwd.key; | |
# 方便本地测试,开启目录浏览遍历 | |
location / { | |
autoindex on; # 开启目录浏览功能 | |
autoindex_exact_size off; # 关闭详细文件大小统计,让文件大小显示MB,GB单位,默认为b | |
autoindex_localtime on; # 开启以服务器本地时区显示文件修改日期 | |
} | |
location ~ \.php$ { | |
fastcgi_pass 127.0.0.1:9000; | |
fastcgi_index index.php; | |
include fastcgi.conf; | |
} | |
} |
关于 https
和 http2
的具体配置,可参考这里:https://lzw.me/a/http2-nginx.html
3.2.3 启动并测试 nginx
Nginx 相关命令:
# 测试配置是否有语法错误 | |
nginx -t | |
# 启动 nginx | |
sudo nginx | |
# 重新加载配置|重启|停止|退出 nginx | |
sudo nginx -s reload|reopen|stop|quit |
启动 nginx,并在 root
指定目录 /Users/lzwme/test
下新建文件 phpinfo.php
:
<?php | |
phpinfo(); | |
?> |
浏览器打开地址 http://localhost:81/phpinfo.php
,查看是否正常。
3.3 配置 nginx 开机自动启动
Nginx 监听 1000
以下的端口需要 root 权限执行,所以应当使用 sudo
执行相关操作。配置 Nginx 开机启动:
sudo ln -sfv /usr/local/opt/nginx/*.plist /Library/LaunchAgents | |
sudo chown root:wheel /Library/LaunchAgents/homebrew.mxcl.nginx.plist |
开启开机自动启动服务:
launchctl load -w /Library/LaunchAgents/homebrew.mxcl.nginx.plist
取消开机自动启动服务:
sudo launchctl unload /Library/LaunchAgents/homebrew.mxcl.nginx.plist
4 安装与配置 Mysql
安装 mysql
brew install mysql
启动 mysql
mysql.server start | |
// mysql 启动|停止|重启|查看状态 | |
mysql.server start|stop|restart|status | |
// 修改 mysql 密码 | |
mysqladmin -S /tmp/mysql.sock -u root -p password 123456 |
测试 mysql
mysql -uroot
进入 mysql 执行环境后,修改 mysql 的 root 账号密码
update mysql.user set authentication_string="new password" where User='root'; | |
flush privileges; | |
quit |
配置 mysql 开机自动启动
ln -sfv /usr/local/opt/mysql/*.plist /Library/LaunchAgents | |
launchctl load /Library/LaunchAgents/ homebrew.mxcl.mysql.plist |
初始化 mysql 配置参考
mysql_secure_installation | |
Enter current password for root (enter for none) | |
回车 | |
> Change the root password? [Y/n] | |
如不愿意使用 root 密码缺省 mysql 的 password 输入n | |
并键入自己想使用的password | |
> Remove anonymous users? [Y/n] | |
Yes. 匿名用户我们并不需要 | |
> Disallow root login remotely? [Y/n] | |
Yes. 我们仅需要127.0.0.1 | |
> Remove test database and access to it? [Y/n] | |
Yes. 无需保留冗余的测试文件 | |
> Reload privilege tables now? [Y/n] | |
Yes.重新加载数据库 |
5 相关参考
- http://blog.csdn.net/pang040328/article/details/41259385
- http://blog.qiji.tech/archives/132
- https://github.com/potterhe/php_discuz
本文更新于:
2016-08-22