Mac下搭建PHP+MYSQL+NGINX+PHPMYADMIN开发环境

  1. 安装brew
/bin/zsh -c "$(curl -fsSL https://gitee.com/cunkai/HomebrewCN/raw/master/Homebrew.sh)"

我选择的是USTC镜像,速度很快。若需卸载,可以执行

/bin/zsh -c "$(curl -fsSL https://gitee.com/cunkai/HomebrewCN/raw/master/HomebrewUninstall.sh)"
  1. 配置系统自带php
php -i|grep 'Loaded Configuration File'

我输出/etc/php.ini, vim编辑(sudo)该文件,修改如下

;doc_root=
cgi.fix_pathinfo=0
do_mysql.default_socket= /tmp/mysql.sock 
mysqli.default_socket = /tmp/mysql.sock 

其中,这里的socket路径可以通过后面安装mysql后,登陆mysql后,使用
show variables like '%socket%';查询。

  1. 配置系统自带php-fpm
    我的配置文件在/private/etc/php-fpm.conf, 可以通过copy得到
cp /private/etc/php-fpm.conf.default /private/etc/php-fpm.conf

我只修改了

error_log = /usr/local/var/log/php-fpm/php-fpm.log

检查安装是否正常

php-fpm -v
  1. 安装phpmyadmin
brew install phpmyadmin

修改/etc/phpmyadmin/config.inc.php

$cfg['Servers'][$i]['host'] = '127.0.0.1';
  1. 安装mysql
brew install mysql

修改/usr/local/etc/my.cnf

# Default Homebrew MySQL server config
[mysqld]
# Only allow connections from localhost
bind-address = 127.0.0.1
mysqlx-bind-address = 127.0.0.1
tmpdir=/tmp
user=root

执行mysql_secure_installation, 并进行相关安全性设置。
terminal登陆mysql

mysql -h localhost -u root -p

然后输入

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

注意将password改为你登陆mysql的密码。

  1. 安装nginx
brew install nginx

安装结束后可以看到服务器配置文件在/usr/local/etc/nginx/servers/, 也可以通过brew info nginx查看该路径。
我们在该目录下修改default为

server {
  charset utf-8;
  client_max_body_size 128M;

  listen 80; ## listen for ipv4

    server_name localhost;
  root        /usr/local/var/www;
  index       index.php index.html index.htm;

  access_log /usr/local/var/www/log/access.log;
  error_log   /usr/local/var/www/log/error.log;

  location / {
# Redirect everything that isn't a real file to index.php
    try_files $uri $uri/ /index.php$is_args$args;
  }

  error_page 404 /404.html;

# deny accessing php files for the /assets directory
  location ~ ^/assets/.*\.php$ {
    deny all;
  }


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

  location  /phpmyadmin {
    root /usr/local/share;
    index index.php index.html index.htm;
    location ~ \.php$ {
      try_files $uri =404;
      include fastcgi_params;
      fastcgi_pass   127.0.0.1:9000;
      fastcgi_index  index.php;
      fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }
  }

  location ~* /\. {
    deny all;
  }
}
  1. 打开http://localhost/phpmyadmin应该可以进入phpmyadmin
  2. 在目录/usr/local/var/www下新建文件index.php, 内容如下

访问http://localhost/index.php应该可以看到php配置信息。

参考

  1. Homebrew国内如何自动安装(国内地址)
  2. Mac OS X 系统自带的 php-fpm 配置和 nginx、mysql 的安装
  3. mysqli_real_connect(): (HY000/2002): No such file or directory
  4. PHP with MySQL 8.0+ error: The server requested authentication method unknown to the client [duplicate]

你可能感兴趣的:(Mac下搭建PHP+MYSQL+NGINX+PHPMYADMIN开发环境)