macOS搭建php开发环境
1、brew、mysql和php的安装
安装brew
/bin/zsh -c "$(curl -fsSL https://gitee.com/cunkai/HomebrewCN/raw/master/Homebrew.sh)"
链接: HomebrewCN.
安装Nginx
brew install nginx
安装mysql
brew install mysql
安装多个php版本
安装php5.6
由于homebrew主库中没有PHP7.2 之前的版本,所以需要先挂在第三方的扩展,具体操作如下:
brew tap shivammathur/php
brew search php
brew install shivammathur/php/php@5.6
安装php7.4
brew install php@7.4
安装php8.2
brew install php
2、配置环境
修改php配置文件
sudo vim /opt/homebrew/etc/php/5.6/php-fpm.conf
下的:
listen = 127.0.0.1:9000
改为
listen = 127.0.0.1:9056
sudo vim /opt/homebrew/etc/php/7.4/php-fpm.d/www.conf
下的:
listen = 127.0.0.1:9000
改为
listen = 127.0.0.1:9074
php8.2不用修配置文件,默认监听9000
配置Nginx环境
sudo vim /opt/homebrew/etc/nginx/nginx.conf
修改后:
#user nobody;
worker_processes 1;
error_log /var/logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/logs/access.log;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
include servers/*;
}
检查一下是否配置正确:
nginx -t
如果报文件权限问题:chmod -R 777 /var/logs
配置站点1:
在cd /opt/homebrew/etc/nginx/servers
下新建站点配置文件如:
sudo vim test1.conf
内容如下:
server {
listen 80;
server_name localhost;
# 配置项目路径
root /Library/WebServer/Documents/php/test1/public;
#access_log logs/host.access.log main;
location / {
index index.html index.htm index.php;
if (!-e $request_filename) {
rewrite ^(.*)$ /index.php?s=/$1 last;
break;
}
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
location ~ \.php$ {
# 9074上面设置的监听端口,加载php7.4
fastcgi_pass 127.0.0.1:9074;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
配置站点2:
在cd /opt/homebrew/etc/nginx/servers
下新建站点配置文件如:
sudo vim test2.conf
内容如下
server {
listen 80;
server_name localhost;
# 配置项目路径
root /Library/WebServer/Documents/php/test2;
#access_log logs/host.access.log main;
location / {
index index.html index.htm index.php;
if (!-e $request_filename) {
rewrite ^(.*)$ /index.php?s=/$1 last;
break;
}
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
location ~ \.php$ {
# 9056上面设置的监听端口,加载php5.6
fastcgi_pass 127.0.0.1:9056;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
3、最后设置php-fpm开机自启
将
/opt/homebrew/opt/php@5.6
下的homebrew.php@5.6.service
将/opt/homebrew/opt/php@7.4
下的homebrew.php@7.4.service
将/opt/homebrew/opt/php@8.2
下的homebrew.php@8.2.service
将/opt/homebrew/opt/php
下的homebrew.php.service
复制到
/Library/LaunchAgents下