macOS搭建PHP开发环境(brew安装nginx、mysql 和多版本php,并配置多个php同时运行的环境)
# 更新Homebrew数据库并安装必要的软件
brew update
brew install nginx mysql
# 安装php7.4和php8.0,并启用shiv-php模块以支持多版本
brew install shivammathur/php/php@7.4 shivammathur/php/php@8.0
brew link --force --overwrite php@7.4
brew link --force --overwrite php@8.0
# 为nginx设置PHP处理,并重新加载nginx配置
echo "server {
listen 8080;
root /var/www/html;
index index.php index.html;
error_log /var/log/nginx/error.log;
error_log /var/log/nginx/access.log;
location ~ \\.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.4/cgi.sock;
include fastcgi_params;
}
}" > /usr/local/etc/nginx/servers/php7.4.conf
echo "server {
listen 8081;
root /var/www/html;
index index.php index.html;
error_log /var/log/nginx/error.log;
error_log /var/log/nginx/access.log;
location ~ \\.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php8.0/cgi.sock;
include fastcgi_params;
}
}" > /usr/local/etc/nginx/servers/php8.0.conf
nginx -t && nginx -s reload
这段代码展示了如何使用Homebrew在macOS上安装Nginx和MySQL,并安装两个不同版本的PHP(7.4和8.0)。然后,它为每个PHP版本创建了Nginx配置文件,并重新加载了Nginx以应用更改。这样,你就可以通过不同的端口访问使用不同PHP版本的网站了。
评论已关闭