macOS使用brew安装nginx、mysql 和多版本php,并配置多个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下

4、重启

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值