Skip to content

Instantly share code, notes, and snippets.

@blanklin030
Last active July 27, 2020 07:43
Show Gist options
  • Save blanklin030/a0417fdc69df6844b26e680844eeb08c to your computer and use it in GitHub Desktop.
Save blanklin030/a0417fdc69df6844b26e680844eeb08c to your computer and use it in GitHub Desktop.
通过brew在mac上安装及配置php+mysql+nginx

清理旧安装

brew remove mysql nginx php
brew cleanup

安装php

  • 安装php
brew install php@7.1
  • 设置系统目录
open -e ~/.bash_profile
export PATH="/usr/local/Cellar/php@7.1/7.1.20/bin:$PATH"
source ~/.bash_profile
  • php安装文件目录
/usr/local/Cellar/php@7.1/7.1.20
  • php配置文件目录
/usr/local/etc/php/7.1/
  • 安装pear(pecl)
curl -O https://pear.php.net/go-pear.phar
sudo php -d detect_unicode=0 go-pear.phar

详情访问

运行第二步会有一些pear的配置,可直接默认安装,一直按回车,由于墙的存在,无法直接使用pecl install xxx,可访问http://pecl.php.net/ 下载需要的扩展后使用phpize安装. 提示no releases available for package错误,先pear upgrade
提示 Error getting channel info from pear.php.net: Connection to ssl://pear.php.net:443' failed: Operation timed out

run php -r "print_r(openssl_get_cert_locations());"
//check default_cert_file path
download certificate from http://curl.haxx.se/ca/cacert.pem
// rename it and place it at default_cert_file path
 try pecl list-all

  • 启动php-fpm
mv /private/etc/php-fpm.d/www.conf.default  /private/etc/php-fpm.d/www.conf
mv /private/etc/php-fpm.conf.default /private/etc/php-fpm.conf
vim /private/etc/php-fpm.conf
#找到error_log的配置,修改路径为 /usr/local/log/php-fpm.log
sudo mkdir /usr/local/log
sudo touch /usr/local/log/php-fpm.log
sudo php-fpm -D //以daemon方式启动
brew services start php@7.2 //正常启动php

异常错误

安装ningx

brew install nginx

nginx配置文件

  • nginx安装文件目录
/usr/local/Cellar/nginx
  • nginx配置文件目录
/usr/local/etc/nginx
  • 日志文件目录
 /usr/local/var/log/nginx
  • 系统hosts位置
/private/etc/hosts
  • 修改nginx.conf配置以支持php-fpm,在server里添加
location ~ \.php$ {
    fastcgi_pass      127.0.0.1:9000;
    fastcgi_index     index.php;
    fastcgi_param     SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include           fastcgi_params;
}
  • 重启nginx配置
sudo nginx -s reload
  • 重启nginx出错,提示如下:
nginx: [error] open() "/usr/local/var/run/nginx.pid"

原因:sudo nginx (执行该命令之后,nginx 会在 /usr/local/var/run/ 路径下创建一个名为nginx.pid 的文件) sudo nginx -s stop (执行该命令之后,nginx 会将 /usr/local/var/run/ 路径下名为nginx.pid 的文件删除掉。但前提是/usr/local/var/run/ 路径下必须存在 nginx.pid 文件)

//启动nginx
sudo nginx
brew services start nginx //正常启动php

nginx主进程是需要root用户启动的

安装mysql

  • 安装mysql
brew install mysql
  • 配置环境变量
open -e ~/.bash_profile
export PATH="/usr/local/Cellar/mysql/5.7.18_1/bin:$PATH"
source ~/.bash_profile
  • 启动mysql服务
mysql.server start
  • 初始化mysql配置
mysql_secure_installation

这里可以设置MySQL的访问密码,注意这里如果是开发环境就选1,再回车。另外mysql8版本连接客户端会有一个问题,如下:

Authentication plugin 'caching_sha2_password' cannot be loaded: dlopen(/usr/local/mysql/lib/plugin/caching_sha2_password.so, 2): image not found
  • 原因 密码加密方式【caching_sha2_password】,客户端不支持 在数据库服务器上登录:
mysql>use mysql; 
mysql>select user, host, plugin, authentication_string from user\G; 
*************************** 2. row *************************** 
                 user: root 
                 host: % 
               plugin: caching_sha2_password 
authentication_string: $A$005$XN:@GbgA#f7W+*'3rfILovff0TIgd2lrblzTBREzWsJSvRFNwV0Eu/C/XX9 

  • 解决方法
# 需要改my.cnf
vim /etc/my.cnf
# 在[mysqld]添加跳过所有表的授权指令
skip-grant-tables
# 连接mysql,修改密码
ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY 'root'; 
flush privileges;
# 重启mysql
brew services restart mysql
@blanklin030
Copy link
Author

ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'new password';

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment