最近因为新买Mac M系列芯片笔记本,一直也没搭建过PHP的开发环境,花了一点时间特意在本机做了一次环境搭建测试具体如下。
工具列表如下:
Xcode
VS Code
Homebrew
OpenSSL & wget
MySQL
Postgres
PHP多版本
Nginx
xcode-select --install
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"
brew install openssl
brew install wget
brew install mysql
brew services start mysql
brew services list
# Intel x86 Chipset code /usr/local/etc/my.cnf |
# Apple Silicon M1 Chipset code /opt/homebrew/etc/my.cnf |
# Default Homebrew MySQL server config
[mysqld]
# Only allow connections from localhost
bind-address = 127.0.0.1
mysqlx-bind-address = 127.0.0.1
# Add mode only if needed
sql_mode = "ONLY_FULL_GROUP_BY,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION"
mysql_secure_installation
brew services restart mysql
PS:MySQL8认证需要更新每个用户mysql_native_password,但是用户名和密码前没有空格,请务必注意。
mysql> SHOW VARIABLES LIKE 'validate_password%';
mysql> SET GLOBAL validate_password.policy=LOW;
brew install postgresql
brew services start postgresql
brew services list
psql postgres
确认用户列表
postgres-# \du
在Mac电脑上安装多个PHP版本时不能使用PHP默认的自制内核,而需要使用shivammathur/php。
brew tap shivammathur/php
brew install shivammathur/php/[email protected]
brew install shivammathur/php/[email protected]
brew install shivammathur/php/[email protected]
brew install shivammathur/php/[email protected]
brew install shivammathur/php/[email protected]
brew unlink php
brew link --overwrite --force [email protected]
对于php-fpm的每个版本更新,都需要提供唯一的端口。更改每个php-fpm的端口以匹配其php版本号。例如:[email protected],我使用的端口为9074。
此外,如果希望php-fpm在你的用户帐户上运行,而不是_www。那么文件夹路径的差异取会决于计算机的Mac处理器(Intel x86或Apple M1 ARM),这一点也是需要非常注意的。
# Intel x86 Chipset code /usr/local/etc/php/7.4/php-fpm.d/www.conf |
# Apple Silicon M1 Chipset code /opt/homebrew/etc/php/7.4/php-fpm.d/www.conf |
内容如下:
# default
user = _www
group = _www
listen = 127.0.0.1:9000
# change to
user =
group = staff
listen = 127.0.0.1:9074
另外,在启动php-fpm前,可以在php.ini中将upload_max_filesize和post_max_size上限进行调整,比如:10M。
不同芯片的路径如下:
# Intel x86 Chipset /usr/local/etc/php/7.2/php.ini /usr/local/etc/php/7.3/php.ini /usr/local/etc/php/7.4/php.ini /usr/local/etc/php/8.0/php.ini /usr/local/etc/php/8.1/php.ini /usr/local/etc/php/8.2/php.ini |
# Apple Silicon M1 Chipset /opt/homebrew/etc/php/7.2/php.ini /opt/homebrew/etc/php/7.3/php.ini /opt/homebrew/etc/php/7.4/php.ini /opt/homebrew/etc/php/8.0/php.ini /opt/homebrew/etc/php/8.1/php.ini /opt/homebrew/etc/php/8.2/php.ini |
命令如下:
brew services start [email protected]
brew services start [email protected]
brew services start [email protected]
brew services start [email protected]
brew services start [email protected]
brew services start [email protected]
启动完成后,确认端口与进程:
Sudo lsof -i -n -P|grep php-fpm
具体操作如下:
# Intel x86 Chipset alias php72="/usr/local/opt/[email protected]/bin/php" alias php73="/usr/local/opt/[email protected]/bin/php" alias php74="/usr/local/opt/[email protected]/bin/php" alias php80="/usr/local/opt/[email protected]/bin/php" alias php81="/usr/local/opt/[email protected]/bin/php" alias php82="/usr/local/opt/[email protected]/bin/php" |
# Apple Silicon M1 Chipset alias php72="/opt/homebrew/opt/[email protected]/bin/php" alias php73="/opt/homebrew/opt/[email protected]/bin/php" alias php74="/opt/homebrew/opt/[email protected]/bin/php" alias php80="/opt/homebrew/opt/[email protected]/bin/php" alias php81="/opt/homebrew/opt/[email protected]/bin/php" alias php82="/opt/homebrew/opt/[email protected]/bin/php" |
刷新环境变量:
source ~/.bash_profile
source ~/.zshrc
刷新后可以测试一下别名是否能够切换不同的php版本,具体如下:
php72 -v
php73 -v
php74 -v
php80 -v
php81 -v
php82 -v
默认php -v命令:
此处也提供一个更简单的手动切换php版本方法:
在~/.bash_profile加入如下代码即可。
# Make PHP switching versions easy
function phpv() {
brew unlink php
brew link --overwrite --force "php@$1"
php -v
}
# bash function
phpv 7.4
同于手动切换版本命令:
# brew
brew unlink php
brew link --overwrite --force [email protected]
关于升级的问题,如果出现升级错误,可以使用以下方式:
brew reinstall shivammathur/php/[email protected]
这个方法是提供给喜欢在WebStorm中调试程序的小伙伴用的,不过我也喜欢,很是方便,具体配置如下:
brew link --overwrite --force [email protected]
pecl uninstall -r xdebug
pecl install xdebug
brew link --overwrite --force [email protected]
pecl uninstall -r xdebug
pecl install xdebug
brew link --overwrite --force [email protected]
pecl uninstall -r xdebug
pecl install xdebug
brew link --overwrite --force [email protected]
pecl uninstall -r xdebug
pecl install xdebug
brew link --overwrite --force [email protected]
pecl uninstall -r xdebug
pecl install xdebug
brew link --overwrite --force [email protected]
pecl uninstall -r xdebug
pecl install xdebug
此处对于每个不同的版本都需要更新php.ini文件,例如,我在这里仅仅是更新php7.4的,具体内容如下:
# Intel x86 Chipset
code /usr/local/etc/php/7.4/php.ini
# Apple Silicon M1 Chipset
code /opt/homebrew/etc/php/7.4/php.ini
这里有一个小问题则需要说明:需要删除zend_extension="xdebug。因为在pecl安装过程中会将其添加到php.ini文件的顶部,并且xdebug默认端口是9003。
php.ini具体内容如下:
[xdebug]
zend_extension="xdebug.so"
xdebug.mode=debug
xdebug.client_port=9003
xdebug.idekey=PHPSTORM
当完成xdebug的配置后,并可以关闭所有的php-fpm进程,不过在生产环境不建议开启xdebug,新的mac或者是开发环境可以这么操作,请务必注意。
命令如下:
brew install nginx
sudo nginx
成功后,做测试访问(端口请自行查看配置,默认为8080/80):
http://localhost:8080 |
# Intel x86 Chipset code /usr/local/etc/nginx/nginx.conf |
# Apple Silicon M1 Chipset code /opt/homebrew/etc/nginx/nginx.conf |
在http{}内容块中添加:
# 允许多服务器配置
server_names_hash_bucket_size 512;
# 大文件配置
client_max_body_size 100M;
在server{}内容块中配置:
# From
listen 8080;
server_name localhost;
index index.html;
# To
listen 80;
server_name localhost test.x;
index index.html index.htm index.php;
这下就可以开始配置FastCGI网关了,将其添加到默认的服务器的php-fpm中,建议安装最新版本的php,对于其他的服务器配置可以按项目需求配置。
location ~ \.php$ {
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
fastcgi_pass 127.0.0.1:9074;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
}
charset utf-8;
add_header X-Frame-Options "SAMEORIGIN";
add_header X-XSS-Protection "1;
mode=block"; add_header X-Content-Type-Options "nosniff";
配置完成后可以建立测试文件,具体路径如下:
Intel x86芯片路径: mv /usr/local/var/www/index.html /usr/local/var/www/index.php code /usr/local/var/www/index.php |
Apple Silicon M1 芯片路径: mv /opt/homebrew/var/www/index.html /opt/homebrew/var/www/index.php code /opt/homebrew/var/www/index.php |
此处我使用index.html/index.php
sudo nginx -s reload
访问http://localhost或http://localhost:8080
至此,nginx配置php单站点服务已经基本完成,可以进行初步的php开发。
下一节将讲述多站点配置。