mac 下搭建PHP与nginx的开发环境

记录一下mac搭建php环境的步骤,支持多站点不同PHP版本,终端切换PHP版本

首先介绍一下本文所依赖的目录

站点存放目录

/Users/iuu/Sites/

目录结构如下

Sites
├── php74.test.com # php7.4环境网站
│   ├── 404.html
│   ├── 50x.html
│   ├── index.html
│   ├── index.php
│   └── log # 网站日志存放目录
│       ├── access.log
│       └── error.log
└── php82.test.com  # php8.2环境网站
    ├── 404.html
    ├── 50x.html
    ├── index.html
    ├── index.php
    └── log # 网站日志存放目录
        ├── access.log
        └── error.log

brew services 常用命令

# 启动
 brew services start  xx
# 停止
 brew services stop  xx
 # 状态
 brew services info  xx
 # 服务列表
 brew services list

网站目录配置

在站点目录创建站点文件夹

/Users/iuu/Sites/php70.test.com

在站点目录创建站点日志文件夹

/Users/iuu/Sites/php70.test.com/log/

php安装与配置

添加 shivammathur/php 仓库源

github https://github.com/shivammathur/homebrew-php

brew tap shivammathur/php

搜索可安装的 PHP 版本

brew search shivammathur/php

比如你要这些 PHP 版本就这样操作:

brew install shivammathur/php/php@8.1
brew install shivammathur/php/php@8.2
brew install shivammathur/php/php@8.3

查看php安装信息:

brew info php@7.4

php安装目录为:

/opt/homebrew/opt/php@你的版本/

php-fpm 配置文件为:

/opt/homebrew/etc/php/你的版本/

配置php-fpm:

# 修改/opt/homebrew/etc/php/你的版本/php-fpm.conf 中 error_log 配置:
error_log = log/php-fpm-82.log

# 修改/opt/homebrew/etc/php/你的版本/php-fpm.d/www.conf  中 user 配置:
 user = _www
 group = _www
# 改为:
; eg ; user = _www
; eg ; group = _www
# 修改同文件中的 listen  这里端口我为了统一PHP版本我改的是 90[PHP版本]
listen = 127.0.0.1:9082

php-fpm的启动与停止

# 启动
brew services start shivammathur/php/php@8.2
# 停止
brew services stop shivammathur/php/php@8.2 
# 状态
brew services info shivammathur/php/php@8.2 

终端切换php版本

# 8.2 切 8.3
 brew unlink php 
 brew link --overwrite --force shivammathur/php/php@8.3
# 8.3 切 8.2
  brew unlink php 
  brew link --overwrite --force shivammathur/php/php@8.2
  brew unlink php@8.2 && brew link --force php@8.2
# 8.2切 7.4
 brew unlink php@8.2
 brew link --overwrite --force shivammathur/php/php@7.4 
# 7.4 切 8.3
 brew unlink php@7.4   
 brew link --overwrite --force shivammathur/php/php@8.3

nginx安装与配置

安装 nginx :

brew install nginx 

安装完nginx后 查看配置文件信息:

brew info nginx

默认 nginx 站点目录为 :

/opt/homebrew/var/www

默认nginx 端口与配置文件为:

8080
/opt/homebrew/etc/nginx/nginx.conf 

默认nginx会加载该目录的所有配置文件: (注意为了统一网站配置文件以后新建站点,站点的配置文件我都放在这里了)

/opt/homebrew/etc/nginx/servers/

贴一下我的nginx 主配置文件

# /opt/homebrew/etc/nginx/nginx.conf 


#user  nobody;
worker_processes  1;

#error_log  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  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       8080;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.php index.html index.htm;
        }

        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;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        location ~ \.php$ {
           try_files $uri =404;
           root           html;
           fastcgi_pass   127.0.0.1:9082;
           fastcgi_index  index.php;
           fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
           include        fastcgi_params;
        }


       access_log  /opt/homebrew/var/www/log/access.log;
       error_log   /opt/homebrew/var/www/log/error.log;

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # 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/*;
}

接下来创建一个站点:

在 /opt/homebrew/etc/nginx/servers/ 目录下创建一个名称为 php74.test.com.conf 配置文件,内容为:

server {
        listen       80; # 端口
        server_name  php74.test.com; # 站点名称
 
        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   /Users/iuu/Sites/php74.test.com;
            index  index.php index.html index.htm;
        }

        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;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        location ~ \.php$ {
             try_files $uri =404;
           root           /Users/iuu/Sites/php74.test.com;
           fastcgi_pass   127.0.0.1:9074;
           fastcgi_index  index.php;
           fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
           include        fastcgi_params;
        }


       access_log  /Users/iuu/Sites/php74.test.com/log/access.log;
       error_log   /Users/iuu/Sites/php74.test.com/log/error.log;
        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }

不同人配置不同 视情况修改

nginx 校验配置文件是否有效

nginx -t

nginx 启动 与停止

# 启动
brew services start nginx
# 停止
brew services stop nginx
# 重启
brew services restart nginx
# 状态
brew services info nginx

重新加载配置文件

nginx -s reload

修改hosts 文件实现域名访问

# 新增hosts 记录
127.0.0.1       php70.test.com

启动nginx 启动对应版本的 php-fpm 即可访问对于网址

# nginx 程序日志位置
/opt/homebrew/var/log/nginx
# php-fpm 日志位置
/opt/homebrew/var/log 

(为什么指定了php-fpm日志名称还会出现php-fpm.log文件?) 答:因为brew services 启动 php-fpm 的时候带了一个 StandardErrorPath 字段指定了日志

# 当启动php-fpm 或nginx 可以进这个目录看 brew services 的启动文件
~/Library/LaunchAgents 

参考 https://www.jianshu.com/p/6c3b26490861

北溟有鱼QAQ博客
请先登录后发表评论
  • 最新评论
  • 总共0条评论