mac 下安装brew + php + nginx + redis + mysql (二)

本文介绍了在mac系统中如何使用brew安装PHP、nginx、redis和mysql,以及遇到的问题和解决方案,包括更换官方源、配置环境变量、修改配置文件等步骤。

安装brew, 使用一段时间七月多出现各种问题,又卸载都重装了几次都不成功, 后来发现可能是国内的中科大,清华,阿里的源不对, 换成了官网的才解决,大致过程如下:

1. 安装brew, homebrew 官网复制命令(https://brew.sh/index_zh-cn.html):

   A.  

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

        可能要输入管理员密码并回车确定开始安装, 会自动写入环境,这一步会返回安装路径,添加环境,卸载方法等信息,建议复制保存下来,以后可能会用到.
 

    B. 更新:

 brew update

2. 安装PHP

    A. 查询PHP版本:

 brew search php

    B. 选择安装版本,返回信息回包括安装位置,如何添加到环境,配置文件目录,如何重启等关键信息,记得保存:

brew install php@8.1

    C. 把PHP路径放到环境变量里去,格式是:

echo 'export PATH="你的安装路径:$PATH"' >> ~/.zshrc

       例如,我的路径是cd XXX/php/8.1.7,则为:

        echo 'export PATH="XXX/php/8.1.7/bin:$PATH"' >> ~/.zshrc
        echo 'export PATH="XXX/php/8.1.7/sbin:$PATH"' >> ~/.zshrc

    D. 重新加载:

  source ~/.zshrc

    E. 重启PHP, 根据你的安装返回信息来, 可能需要加"@版本号": 

brew services restart php

或者 

brew services restart php@8.1

   

3. 安装nginx

    A. 安装,保存返回信息: 

brew install nginx

    B. 根据返回信息进入配置文件目录: 

cd XXX/nginx/


   如果不知道在哪里可以自己通过命令查找:

ps -ef | grep "nginx"

    C.编辑配置文件,主要可以更改端口号和项目位置,为了防止端口冲突,我把自己本地都改成了9001:

vim nginx.conf


       如果你要配置多端口, 记得把XXX/nginx/nginx.conf里面的"worker_processes 1 " 改大点, 比如说 "worker_processes 10";

    D. 需要多端口配置:
         进入服务配置目录:

cd XXX/nginx/servers


         创建并编写配置文件,自己起名:

vim XXX.conf  

         例如:

server
    {
        listen 9002;
        #listen [::]:80;
        server_name kaifa-demo.mallzi.com;
        index index.html index.htm index.php default.html default.htm default.php;
        root  你的项目路径;

        location / {
			try_files $uri $uri/ /index.php?$query_string;
		}
		
        #error_page   404   /404.html;

        # Deny access to PHP files in specific directory
        #location ~ /(wp-content|uploads|wp-includes|images)/.*\.php$ { deny all; }

		location ~ [^/]\.php(/|$)
        {
            fastcgi_pass 127.0.0.1:9000;
            fastcgi_index index.php;
			
            #####include fastcgi.conf;
			fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;
			fastcgi_param  QUERY_STRING       $query_string;
			fastcgi_param  REQUEST_METHOD     $request_method;
			fastcgi_param  CONTENT_TYPE       $content_type;
			fastcgi_param  CONTENT_LENGTH     $content_length;

			fastcgi_param  SCRIPT_NAME        $fastcgi_script_name;
			fastcgi_param  REQUEST_URI        $request_uri;
			fastcgi_param  DOCUMENT_URI       $document_uri;
			fastcgi_param  DOCUMENT_ROOT      $document_root;
			fastcgi_param  SERVER_PROTOCOL    $server_protocol;
			fastcgi_param  REQUEST_SCHEME     $scheme;
			fastcgi_param  HTTPS              $https if_not_empty;

			fastcgi_param  GATEWAY_INTERFACE  CGI/1.1;
			fastcgi_param  SERVER_SOFTWARE    nginx/$nginx_version;

			fastcgi_param  REMOTE_ADDR        $remote_addr;
			fastcgi_param  REMOTE_PORT        $remote_port;
			fastcgi_param  SERVER_ADDR        $server_addr;
			fastcgi_param  SERVER_PORT        $server_port;
			fastcgi_param  SERVER_NAME        $server_name;
			fastcgi_param  APP_ENV        	  dev;
			# PHP only, required if PHP was built with --enable-force-cgi-redirect
			fastcgi_param  REDIRECT_STATUS    200;
			
            ######include pathinfo.conf;
			fastcgi_split_path_info ^(.+?\.php)(/.*)$;
			set $path_info $fastcgi_path_info;
			fastcgi_param PATH_INFO       $path_info;
			try_files $fastcgi_script_name =404;
        }

        location ~ /.well-known {
            allow all;
        }

        location ~ /\.
        {
            deny all;
        }
    }

    E. 

echo 'export PATH="XXX/nginx/1.23.0/bin:$PATH"' >> ~/.zshrc

    F. 执行命令启动./zshrc :

source ~/.zshrc

    G. 重启nginx: 

brew services restart nginx

    H. 测试PHP:

 lsof -i:9000

    I.  再打开一个终端窗口,登陆管理员:

sudo su

         测试配置文件:

nginx -t

         如果报错: nginx: [emerg] open() "/opt/homebrew/var/run/nginx.pid" failed......

         赋权: chmod -R 777 /opt/homebrew/var/run/

         再次 nginx -t

         加载:

nginx -s reload​​​​​​​ 

如果报错: nginx: [error] invalid PID number "" in "/opt/homebrew/var/run/nginx.pid"
解决方法一:指定nginx.conf文件:
nginx -c /usr/local/XXXX/nginx.conf
然后重新启动nginx

方法二:
a.  打开nginx.conf配置文件, 打开 “pid        logs/nginx.pid;” 设置
b.  查找nginx进程 : ps -ef | grep nginx
c.  kill 关闭进程
d.  重新启动nginx
​​​​​​​

         再次测试即可成功

         然后测试nginx端口配置:

curl -IL http://127.0.0.1:9002

4. 安装mysql

    A.官网下载dmg并安装,安装时需设置密码,下载地址:MySQL :: Download MySQL Community Server

    B.访达->系统偏好设置->mysql图标(最下面), 打开mysql的app

    

    C.查看mysql安装位置(红框部分),并复制,  通过命令行把mysql添加到环境变量中:
    

echo 'export PATH="/usr/local/mysql-8.0.29-macos12-x86_64:$PATH"' >> ~/.zshrc 

   

    D. 打开mysql->Configuration ,找到datal路径,复制路径,给data文件夹赋权:

   

    sudo chmod -R 777 /usr/local/mysql/data 

    E. 执行命令启动./zshrc :

source ~/.zshrc

    F.  测试: 

mysql -u root -p

5. 安装redis

第一种方法:
   

 brew search redis  //搜索

 brew install redis@6.2   //安装,返回信息很重要,好好保存

 echo 'export PATH="XXX/redis@6.2/bin:$PATH"' >> ~/.zshrc   //写入环境变量

 source ~/.zshrc   //再次加载

 vim XXX/redis.conf   //编辑配置文件

   输入“/requirepass”, “/”表示从前向后搜索,按回车然后"n"下一个,找到“# requirepass foobared” ,改为“requirepass 你的密码”



 brew services restart redis@6.2  //重启redis, 具体看你安装完成后返回的命令redis版本可能有差异:

    把 Redis 配置到 PHP 扩展里, 打开php.ini,写入extension=redis ,重启 PHP,

   

  

如果不起作用,phpinfo 不返回 Redis 信息,执行第二种方法:

    安装 Redis: 
         

sudo pecl install redis

    该命令会生产 redis.so并自动写入 php.ini ,但是我重启后没有再 phpinfo 返回信息里看到 Redis, 后来打开了配置文件,发现自动写入的却是 extension="redis.so", 我改成了extension=redis 再次重启刷新可以看到

如果还不行, 可以第三种方法安装php-redis 扩展:

git clone https://github.com/phpredis/phpredis.git   //下载

cd phpredis  //进入目录

XXX/phpize   //你自己的 phpize 目录

./configure --with-php-config=XXX/php@8.1/bin/php-config //你自己的安装返回信息里找路径

make && make install  //编译安装,这会给你返回 redis.io 的位置,一般会自动写入 php.ini, 打开 php.ini 去掉 "extension = redis.io"的".io"和双引号, 然后重启 redis 和 PHP

如果这些都不行, 去官网下载 Redis 压缩包, 解压后拷贝到 PHP 下, 然后phpize和make编译, 具体自己搜索, 试了太多种方法, 我记不清了

后续补充:
一. 更新 brew报错

    1. brew update , 报错:

 Warning: No remote 'origin' in /opt/homebrew/Library/Taps/homebrew/homebrew-cask, skipping update!
Warning: No remote 'origin' in /opt/homebrew/Library/Taps/homebrew/homebrew-core, skipping update!
Warning: No remote 'origin' in /opt/homebrew/Library/Taps/homebrew/homebrew-services, skipping update!
Already up-to-date.

    2. 执行brew检查程序: brew doctor

      

Please note that these warnings are just used to help the Homebrew maintainers
with debugging if you file an issue. If everything you use Homebrew for is
working fine: please don't worry or file an issue; just ignore this. Thanks!

Warning: Suspicious https://github.com/Homebrew/brew git origin remote found.
The current git origin is:
  https://mirrors.aliyun.com/homebrew/brew.git

With a non-standard origin, Homebrew won't update properly.
You can solve this by setting the origin remote:
  git -C "/opt/homebrew" remote set-url origin https://github.com/Homebrew/brew

Warning: Homebrew/homebrew-cask was not tapped properly! Run:
  rm -rf "/opt/homebrew/Library/Taps/homebrew/homebrew-cask"
  brew tap homebrew/cask

Warning: Homebrew/homebrew-core was not tapped properly! Run:
  rm -rf "/opt/homebrew/Library/Taps/homebrew/homebrew-core"
  brew tap homebrew/core

Warning: Some installed formulae are deprecated or disabled.
You should find replacements for the following formulae:
  redis@6.2

Warning: Broken symlinks were found. Remove them with `brew cleanup`:
  /opt/homebrew/opt/php
  /opt/homebrew/opt/php@8.1

3. 根据返回信息顺序执行

二. 卸载更新 PHP
    1. 卸载 PHP: brew uninstall php@8.0

    2. 根据提示删除文件: sudo rm -rf XXX/php/8.1.7

    3. brew install php@8.1

  

  

    

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值