美文网首页
2024-04-16 macOS搭建php开发环境

2024-04-16 macOS搭建php开发环境

作者: TsingQue | 来源:发表于2024-04-15 16:45 被阅读0次

    macOS搭建php开发环境
    1.安装brew
    /bin/zsh -c "$(curl -fsSL https://gitee.com/cunkai/HomebrewCN/raw/master/Homebrew.sh)"
    2.安装Nginx
    brew install nginx
    3.安装mysql
    brew install mysql
    4.安装php
    由于homebrew主库中没有PHP7.2 之前的版本,并且7.2,7.3版本也被标记成过时版本;所以需要先挂在第三方的扩展,具体操作如下:

    brew search php
    php5.6
    brew install shivammathur/php/php@5.6
    php7.3
    brew install shivammathur/php/php@7.3
    php7.4
    brew install shivammathur/php/php@7.4
    php8.2
    

    默认新版8以上直接安装

    brew install php
    
    5.修改php设置
    sudo vim /usr/local/etc/php/5.6/php-fpm.conf  下的:```
    
    注意:5.6版本的配置文件路径和其他版本不一样
    listen = 127.0.0.1:9000
    改为
    listen = 127.0.0.1:9056
    
    sudo vim /usr/local/etc/php/7.3/php-fpm.d/www.conf  下的:
    listen = 127.0.0.1:9000
    改为
    listen = 127.0.0.1:9073
    
    sudo vim /usr/local/etc/php/7.4/php-fpm.d/www.conf  下的:
    listen = 127.0.0.1:9000
    改为
    listen = 127.0.0.1:9074
    
    sudo vim /usr/local/etc/php/8.2/php-fpm.d/www.conf  下的:
    listen = 127.0.0.1:9000
    改为
    listen = 127.0.0.1:9082
    
    6.修改nginx配置
    
    sudo vim /usr/local/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  logs/access.log  main;
     
        sendfile        on;
        #tcp_nopush     on;
     
        #keepalive_timeout  0;
        keepalive_timeout  65;
     
        #gzip  on;
     
        include servers/*;
    }
    
    

    配置站点test1:
    在cd /usr/local/etc/nginx/servers 下新建站点配置文件如:
    sudo vim test1.conf
    内容如下:

    server {
        listen       80;
        server_name  localhost;
        # 配置项目路径
        root   /Users/xxx/xxx/php/2023/test1; 
     
        #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;
        }
    }
    

    配置站点test2:
    在cd /usr/local/etc/nginx/servers 下新建站点配置文件如:
    sudo vim test2.conf
    内容如下:

    server {
        listen       80;
        server_name  localhost;
        # 配置项目路径
        root   /Users/kbq/workspace/php/2023/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$ {
            # 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;
        }
    }
    

    检查配置
    nginx -t
    如果报文件权限问题:
    sudo chmod -R 777 /var/logs

    设置php-fpm开机自启
    未验证成功

    将/usr/local/opt/php@5.6 下的homebrew.php@5.6.service
    将/usr/local/opt/php@7.3 下的homebrew.php@7.3.service
    
    将/usr/local/opt/php@7.4 下的homebrew.php@7.4.service
    将/usr/local/opt/php@8.2 下的homebrew.php@8.2.service
    将/usr/local/opt/php 下的homebrew.php.service
    

    复制到

    /Library/LaunchAgents
    

    最后重启系统

    手动启动php-fpm
     brew services start php@5.6
    
     brew services start php@7.3
    
     brew services start php@7.4
    
     brew services start php@8.2
    

    验证是否启动成功

    lsof -i :9056
    
    lsof -i :9073
    
    lsof -i :9074
    
    lsof -i :9082
    

    终端切换php版本
    解除之前版本链接:brew unlink php

    增加新版本链接:

    brew link --overwrite php@5.6
    
    brew link --overwrite php@7.3
    
    brew link --overwrite php@7.4
    
    brew link --overwrite php@8.2
    

    将php加入环境变量

    将如下加入 /Users/xxx/.bash_profile
    
    export PATH=${PATH}:/usr/local/opt/php@5.6/bin
    export PATH=${PATH}:/usr/local/opt/php@5.6/sbin
    alias php56="/usr/local/opt/php@5.6/bin/php"
     
    export PATH=${PATH}:/usr/local/opt/php@7.3/bin
    export PATH=${PATH}:/usr/local/opt/php@7.3/sbin
    alias php73="/usr/local/opt/php@7.3/bin/php"
     
    export PATH=${PATH}:/usr/local/opt/php@7.4/bin
    export PATH=${PATH}:/usr/local/opt/php@7.4/sbin
    alias php74="/usr/local/opt/php@7.4/bin/php"
     
    export PATH=${PATH}:/usr/local/opt/php@8.2/bin
    export PATH=${PATH}:/usr/local/opt/php@8.2/sbin
    alias php82="/usr/local/opt/php@8.2/bin/php"
    
    使配置生效 source .bash_profile
    
    校验配置是否成功,终端输入
    
    php56 -v
    
    php73 -v
    
    php74 -v
    
    php82 -v
    

    显示如下类似内容,则为成功

    PHP 8.2.12 (cli) (built: Nov  6 2023 02:54:37) (NTS)
    Copyright (c) The PHP Group
    Zend Engine v4.2.12, Copyright (c) Zend Technologies
        with Zend OPcache v8.2.12, Copyright (c), by Zend Technologies
    安装composer
    php56 -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
    php56 -r "if (hash_file('sha384', 'composer-setup.php') === 'e21205b207c3ff031906575712edab6f13eb0b361f2085f1f1237b7126d785e826a450292b6cfd1d64d92e6563bbde02') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
    php56 composer-setup.php
    php56 -r "unlink('composer-setup.php');"
    sudo mv composer.phar /usr/local/bin/composer56
    

    检验是否成功

    ~/ composer74
    Failed loading /usr/local/Cellar/php/7.3.3/lib/php/pecl/20180731/xdebug.so:  dlopen(/usr/local/Cellar/php/7.3.3/lib/php/pecl/20180731/xdebug.so, 0x0009): tried: '/usr/local/Cellar/php/7.3.3/lib/php/pecl/20180731/xdebug.so' (no such file)
       ______
      / ____/___  ____ ___  ____  ____  ________  _____
     / /   / __ \/ __ `__ \/ __ \/ __ \/ ___/ _ \/ ___/
    / /___/ /_/ / / / / / / /_/ / /_/ (__  )  __/ /
    \____/\____/_/ /_/ /_/ .___/\____/____/\___/_/
                        /_/
    Composer version 2.6.5 2023-10-06 10:11:52
    其他版本安装,将php56改为php73、php74、php82,将composer56改为composer73、composer74、composer82
    

    ————————————————

                            版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
    

    原文链接:https://blog.csdn.net/u010926168/article/details/134289062

    相关文章

      网友评论

          本文标题:2024-04-16 macOS搭建php开发环境

          本文链接:https://www.haomeiwen.com/subject/kyvdxjtx.html