MAC安装Nginx Mysql Php的过程记录

    技术2022-07-16  83

    MAC安装Nginx Mysql Php的过程记录

    目录: 1.使用brew安装,首先需要安装brew。 2.安装PHP。 3.安装nginx。 4.安装mysql。

    安装brew

    官网上给出的安装方式是:/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)" 但是,raw.githubusercontent.com 连接不到。

    网上给出在 hosts里修改 ip 地址在这里也是行不通。

    有大神提供国内安装源:/bin/zsh -c "$(curl -fsSL https://gitee.com/cunkai/HomebrewCN/raw/master/Homebrew.sh)" 地址:https://gitee.com/cunkai/HomebrewCN/

    brew search 搜索 brew install 安装 brew uninstall(remove) 卸载

    安装php

    Mac 原有php因版本不适合项目,所以需要安装php7.3。 php -v 查看当前php版本 brew install php@7.3 安装php7.3 brew link php@7.3 添加PHP7.3的软链 系统会提示你将他给出的两行环境变量添加到你的~/.bash_profile 里,例如:export PATH="/usr/local/opt/php@7.3/bin:PATH" export PATH="/usr/local/opt/php@7.3/sbin:PATH"

    vi ~/.bash_profile 打开文件加入这两行内容。保存 source ~/.bash_profile 生成环境变量 php -v 查看是否改为7.3版本

    安装Nginx

    brew install nginx 安装最新版 浏览器输入localhosts:8080 查看是否访问成功!

    下面加入我们自己的项目,使用虚拟域名配置 修改自己项目的文件权限:chmod -R 777 文件目录 配置文件位置:/usr/local/etc/nginx/nginx.conf

    vim /usr/local/etc/nginx/nginx.conf 在 #user nobody; 下添加自己的用户和组 user hui staff; user 自己的用户 自己的组

    配置文件最后一行,有 include servers/*; (没有就添加) 进入到 /usr/local/etc/nginx/servers 创建自己项目的配置文件:touch 项目名自己起.conf 配置文件里的内容:

    server { listen 80; server_name 你的域名; root 你的项目根目录位置; location / { index index.html index.php ; access_log /tmp/access_nginx.log combined; error_log /tmp/error_nginx.log; try_files $uri $uri/ /index.php?$query_string; } location ~ .php$ { root 你的项目根目录位置; fastcgi_pass unix:/usr/local/var/run/php-cgi.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; }

    }

    关于 fastcgi_pass 的内容是 php-fpm 的配置文件里的listen。 Mac自带php配置文件的地址:vim /etc/php-fpm.d/www.conf.default 我自己重装后的地址:vim /usr/local/etc/php/7.3/php-fpm.d/www.conf 文件千万看对 不匹配会报错。

    修改hosts,加入你的域名

    访问项目遇到403 502 ?

    目录权限修改了么?fastcgi_pass正确么?用户和组对么?

    安装MySQL

    brew search mysql : 查找有没有mysql brew install mysql@版本 :安装某个版本的mysql brew install mysql : 安装最新的mysql

    如果是卸载后的重新安装 一定要将之前的数据和文件删除干净 否则会影响新安装mysql的启动。my.cnf 也要删除。要brew link 新mysql

    mysql8 默认的身份验证是:caching_sha2_password。 因项目需要,在my.cnf 中新增一行: default_authentication_plugin=mysql_native_password root用户登录MySQL:mysql -uroot -p

    修改验证方式: ALTER USER ‘root’@‘localhost’ IDENTIFIED WITH mysql_native_password BY ‘用户密码’;

    项目就可以使用mysql啦

    启动 mysql.server start sudo nginx sudo php-fpm

    重启 mysql.server restart sudo nginx -s start

    Processed: 0.009, SQL: 9