mac下php-fpm,nginx和mysql环境配置

1. 安装Brew
官网: http://brew.sh/
安装指令: $ /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
指令有可能会变动,推荐到官网使用最新指令

小tips:如果安装太慢或报错的话建议使用国内的镜像源,直接在终端粘下面这行代码就行,然后选择一个国内的源
/bin/zsh -c "$(curl -fsSL https://gitee.com/cunkai/HomebrewCN/raw/master/Homebrew.sh)"

使用国内镜像如果还有报错的话可以参考这篇文章
2. Nginx
安装指令: brew install nginx
启动nginx: sudo nginx
重新加载|重启|停止|退出 nginx: nginx -s reload|reopen|stop|quit
(注意:如果nginx目录是在/usr/local/Cellar目录下(brew的安装目录)的话,指令有可能会报错,这时候只能是通过杀死进程重启的方式了)
检查配置: nginx -t

3. 修改nginx的配置文件

  1. 打开nginx.config文件: vim /usr/local/etc/nginx/nginx.conf

  2. 找到server的location配置,给index加一个index.php(命令行模式下可使用/进行搜索)

    location / {
    root  html;
    index  index.html index.htm index.php;
    }
    
  3. 打开server下备注是的location ~.php$(即删除代码前面的 ‘#’)

    location ~ \.php$ {
    	root          html;
    	fastcgi_pass  127.0.0.1:9000;
    	fastcgi_index  index.php;
    	fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
    	include        fastcgi_params;
    	}
    
  4. 修改fastcgi_param参数
    fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
    改为:
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

  5. 创建index.php
    在 /usr/local/var/www 目录下,删除 index.html,创建 index.php

4. 安装php-fpm

  1. Mac OSX 10.9的系统自带了PHP、php-fpm,省去了安装php-fpm的麻烦。 这里需要简单地修改下php-fpm的配置,否则运行php-fpm会报错
    $ sudo cp /private/etc/php-fpm.conf.default /private/etc/php-fpm.conf
    $ vim /private/etc/php-fpm.conf

  2. 修改php-fpm.conf文件中的error_log项【大概在30行附近】,默认该项被注释掉,这里需要去注释并且修改为error_log = /usr/local/var/log/php-fpm.log。如果不修改该值,运行php-fpm的时候会提示log文件输出路径不存在的错误

  3. 测试nginx服务
    在之前nginx配置文件default.conf中设置的root项对应的文件夹下创建测试文件index.php

    <!-- lang: php -->
    <!-- ~/nginx_sites/index.php -->
    <?php phpinfo(); ?>
    
  4. 查看nginx服务是否启动:【启动成功、有master这个进程】
    ps -ef|grep nginx

  5. 如果有,通过下面指令杀死进程
    sudo kill -9 进程号

  6. 重启nginx
    sudo nginx

  7. 启动php服务
    sudo php-fpm

  8. 在浏览器地址栏输入localhost:8080,如果配置正确地话,应该能看到PHP相关信息的页面

  9. 如果出现403Forbidden的问题,可能有如下两个原因

    1. 当前用户没有对于/usr/local/var/www路径的读取权限
      测试:在该目录下新建一个hello.html的文件,地址栏输入localhost:8080/hello.html看能否正常访问,如果不能则修改权限即可

      <!DOCTYPE html>
      <html lang="en">
      <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Document</title>
      </head>
      <body>
        hello world
      </body>
      </html>
      
    2. 如果能正常访问,对于nginx的配置可能遗漏了什么东西,回去对照修改nginx文件步骤再走一遍流程

      要记得重新启动nginx和php-fpm

5. 安装mysql

  1. 官方下载dmg安装包
    官网地址: https://downloads.mysql.com/archives/community/
    点击安装(类似win的exe安装包,一直默认步骤即可)
    **重点注意: 安装到最后一步会有一个弹窗,告诉你初始密码,牢记(否则只能是重置root密码了)**
    或者通过brew命令安装
    brew install mysql

  2. 配置
    在终端运行mysql_secure_installation脚本,该脚本会一步步提示你设置一系列安全性相关的参数,包括:设置root密码,关闭匿名访问,不允许root用户远程访问,移除test数据库。当然运行该脚本前记得先启动mysql服务。

  3. 测试
    进入/usr/local/mysql/bin/,用ls命令查看是否有mysql
    (使用brew安装的方式有可能在/usr/local/Cellar/mysql/bin/)

  4. 连接数据库
    mysql -u root -p

6. 部署在nginx服务器上的php文件连接mysql数据库
按照上面步骤配置好环境后:在www目录下创建php文件输入下面连接代码

<?php
    $servername = "localhost";
    $username = "root";
		  $password = "root";
	   try {
    		  $conn = new PDO("mysql:host=$servername;", $username, $password);
  		    echo "连接成功";
    }
	   catch(PDOException $e)
    {
		      echo $e->getMessage();
	   }
?>
需要自己修改的是第四行的password选项,是你自己设置的root的密码
写完代码检可能会出现下面错误:【如果浏览器中显示连接成功,则就表示成功了】
mac上连接mysql数据库时报错:SQLSTATE[HY000] [2002] No such file or directory
  1. 第一种解决方式【在php.ini文件中找到extension=php_mysqli.dll一行不需要取消注释,否则启动php-fpm或报错: Unable to load dynamic library】
    参考链接: https://blog.csdn.net/J__Max/article/details/83008493
    重启nginx和php-fpm,在浏览器输入localhost:8080/文件名即可进行检验

  2. 第二种解决方式
    检查/var/mysql是否存在
    $ll /var/mysql

    如果/var/mysql不存在则创建
    $sudo mkdir /var/mysql

    将mysql.sock文件映射到/var/mysql路径中
    $sudo ln -s /private/tmp/mysql.sock /var/mysql/mysql.sock
    重新刷新错误页面…

mac上连接mysql数据库时报错:SQLSTATE[HY000] [2054] The server requested authentication method unknown to the client

是 MySQL 8默认使用了新的密码验证插件:caching_sha2_password ,而有些 PHP 版本不支持这个问题造成的
php连接mysql8.0 报错 修改mysql配置文件my.cnf
但是使用mac安装的mysql是没有这个配置文件的

  1. 首先我们先看一下MySQL默认指定的my.cnf路径,使用命令

    mysql --help|grep 'my.cnf'
    

    这是四个默认的指定路径,MySQL从第一个路径依次去找my.cnf配置的文件,我们直接将my.cnf文件放在第一个路径下就可以了
    在这里插入图片描述

  2. 创建配置文件
    sudo vi /etc/my.cnf
    写入如下代码

    # Example MySQL config file for medium systems.  
      #  
      # This is for a system with little memory (32M - 64M) where MySQL plays  
      # an important part, or systems up to 128M where MySQL is used together with  
      # other programs (such as a web server)  
      #  
      # MySQL programs look for option files in a set of  
      # locations which depend on the deployment platform.  
      # You can copy this option file to one of those  
      # locations. For information about these locations, see:  
      # http://dev.mysql.com/doc/mysql/en/option-files.html  
      #  
      # In this file, you can use all long options that a program supports.  
      # If you want to know which options a program supports, run the program  
      # with the "--help" option.  
      # The following options will be passed to all MySQL clients  
      [client]
      default-character-set=utf8
      #password   = your_password  
      port        = 3306  
      socket      = /tmp/mysql.sock   
      # Here follows entries for some specific programs  
      # The MySQL server  
      [mysqld]
      character-set-server=utf8
      init_connect='SET NAMES utf8
      port        = 3306  
      socket      = /tmp/mysql.sock  
      skip-external-locking  
      key_buffer_size = 16M  
      max_allowed_packet = 1M  
      table_open_cache = 64  
      sort_buffer_size = 512K  
      net_buffer_length = 8K  
      read_buffer_size = 256K  
      read_rnd_buffer_size = 512K  
      myisam_sort_buffer_size = 8M  
      character-set-server=utf8  
      init_connect='SET NAMES utf8' 
    # Don't listen on a TCP/IP port at all. This can be a security enhancement,  
    # if all processes that need to connect to mysqld run on the same host.  
    # All interaction with mysqld must be made via Unix sockets or named pipes.  
    # Note that using this option without enabling named pipes on Windows  
    # (via the "enable-named-pipe" option) will render mysqld useless!  
    #   
    #skip-networking  
     
      # Replication Master Server (default)  
      # binary logging is required for replication  
      log-bin=mysql-bin  
     
        # binary logging format - mixed recommended  
        binlog_format=mixed  
     
          # required unique id between 1 and 2^32 - 1  
          # defaults to 1 if master-host is not set  
          # but will not function as a master if omitted  
          server-id   = 1  
     
        # Replication Slave (comment out master section to use this)  
        #  
        # To configure this host as a replication slave, you can choose between  
        # two methods :  
        #  
        # 1) Use the CHANGE MASTER TO command (fully described in our manual) -  
        #    the syntax is:  
        #  
        #    CHANGE MASTER TO MASTER_HOST=<host>, MASTER_PORT=<port>,  
        #    MASTER_USER=<user>, MASTER_PASSWORD=<password> ;  
        #  
        #    where you replace <host>, <user>, <password> by quoted strings and  
        #    <port> by the master's port number (3306 by default).  
        #  
        #    Example:  
        #  
        #    CHANGE MASTER TO MASTER_HOST='125.564.12.1', MASTER_PORT=3306,  
        #    MASTER_USER='joe', MASTER_PASSWORD='secret';  
        #  
        # OR  
        #  
        # 2) Set the variables below. However, in case you choose this method, then  
        #    start replication for the first time (even unsuccessfully, for example  
        #    if you mistyped the password in master-password and the slave fails to  
        #    connect), the slave will create a master.info file, and any later  
        #    change in this file to the variables' values below will be ignored and  
        #    overridden by the content of the master.info file, unless you shutdown  
        #    the slave server, delete master.info and restart the slaver server.  
        #    For that reason, you may want to leave the lines below untouched  
        #    (commented) and instead use CHANGE MASTER TO (see above)  
        #  
        # required unique id between 2 and 2^32 - 1  
        # (and different from the master)  
        # defaults to 2 if master-host is set  
        # but will not function as a slave if omitted  
        #server-id       = 2  
        #  
        # The replication master for this slave - required  
        #master-host     =   <hostname>  
        #  
        # The username the slave will use for authentication when connecting  
        # to the master - required  
        #master-user     =   <username>  
        #  
        # The password the slave will authenticate with when connecting to  
        # the master - required  
        #master-password =   <password>  
        #  
        # The port the master is listening on.  
        # optional - defaults to 3306  
        #master-port     =  <port>  
        #  
        # binary logging - not required for slaves, but recommended  
        #log-bin=mysql-bin  
     
          # Uncomment the following if you are using InnoDB tables  
          #innodb_data_home_dir = /usr/local/mysql/data  
          #innodb_data_file_path = ibdata1:10M:autoextend  
          #innodb_log_group_home_dir = /usr/local/mysql/data  
          # You can set .._buffer_pool_size up to 50 - 80 %  
          # of RAM but beware of setting memory usage too high  
          #innodb_buffer_pool_size = 16M  
          #innodb_additional_mem_pool_size = 2M  
          # Set .._log_file_size to 25 % of buffer pool size  
          #innodb_log_file_size = 5M  
          #innodb_log_buffer_size = 8M  
          #innodb_flush_log_at_trx_commit = 1  
          #innodb_lock_wait_timeout = 50  
     
            [mysqldump]  
            quick  
            max_allowed_packet = 16M  
     
              [mysql]  
              no-auto-rehash  
              # Remove the next comment character if you are not familiar with SQL  
              #safe-updates  
              default-character-set=utf8   
     
            [myisamchk]  
            key_buffer_size = 20M  
            sort_buffer_size = 20M  
            read_buffer = 2M  
            write_buffer = 2M  
     
              [mysqlhotcopy]  
              interactive-timeout
    
  3. 修改文件读写权限
    sudo chmod 664 /etc/my.cnf

  4. 重启mysql服务器
    sudo /usr/local/mysql/support-files/mysql.server restart

    另附mysql启动和停止命令
    启动MySQL服务
    sudo /usr/local/mysql/support-files/mysql.server start

    停止MySQL服务
    sudo /usr/local/mysql/support-files/mysql.server stop

    如果还是失败,继续往下走

  5. 打开mysql配置文件
    sudo vi /etc/my.cnf
    在如图位置添加一行代码:default_authentication_plugin=mysql_native_password
    在这里插入图片描述

  6. 重启mysql
    测试是否成功,没有成功继续往下走

  7. 执行如下语句
    登录mysql客户端
    mysql -uroot -p
    切换到mysql数据库
    use mysql;
    修改登录用户的 plugin 为 mysql_native_password
    ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '密码';
    FLUSH PRIVILEGES;
    以上修改完成以后,本地链接没有问题,但是远程连接还是不行,需要修改 host 为 %
    update user set host = '%' where user ='root';
    假如上一步修改不成功,提示有重复数据 Duplicate entry ‘%-root’ for key ‘user.PRIMARY’,删除其他的信息
    delete user where user='root' and host ='%';

  8. 再次重启mysql

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值