Mac 安装 LNMP 环境

Mac 安装 Lnmp环境

1、安装Homebrew

  • Homebrew是一款Mac系统下的软件包管理工具,brew命令类似ubuntu上的apt-get,能十分方便的在Mac上安装或卸载软件
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
  • 参考资料:Homebrew

2、安装php

  • Mac系统上默认安装了php,版本比较过时,版本稍微有点低并且有些扩展是没有安装。

更新brew

$ brew update

添加三方仓库

$ brew tap homebrew/dupes  
$ brew tap homebrew/versions  
$ brew tap homebrew/homebrew-php

如果没有该目录,则创建

$ sudo mkdir /usr/local/var
$ sudo chmod 777 /usr/local/var 
$ sudo mkdir /usr/local/sbin/
$ sudo chown -R : /usr/local/sbin/

我们可以通过brew options命令来查看安装选项

$ brew options php72

开始安装PHP72

$ brew install php (--with-apache 若需要生成apache的libphp7.so,则添加该安装选项)

查看php版本

$ php -v

3、配置php及php-fpm

可执行文件及配置文件路径

  • php, phpize, php-config
  • /usr/local/opt/php7.2/bin
  • php-fpm /usr/local/opt/php7.2/sbin/php-fpm
  • php.ini /usr/local/etc/php/7.2/php.ini
  • php-fpm.conf /usr/local/etc/php/7.2/php-fpm.conf

修改php-fpm配置

sudo vim /usr/local/etc/php/7.2/php-fpm.conf

为了防止访问无权限,所以修改user及group

  • user = abel
  • group = staff

为了区别不同版本的php-fpm,修改php70-fpm的端口

  • isten = 127.0.0.1:9001

添加php-fpm为开机启动项

$ mkdir -p ~/Library/LaunchAgents
$ cp /usr/local/opt/php72/homebrew.mxcl.php72.plist ~/Library/LaunchAgents/
$ launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.php72.plist

查看php-fpm是否启动成功

$ ps aux|grep php-fpm

4、安装Nginx

$ brew install nginx

添加nginx为开机启动项

$ mkdir -p ~/Library/LaunchAgents
$ cp /usr/local/Cellar/nginx/1.10.2_1/homebrew.mxcl.nginx.plist ~/Library/LaunchAgents/
$ launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.nginx.plist

设置权限
为了监听1024以下端口,需修改nginx所属者

$ sudo chown root:wheel /usr/local/Cellar/nginx/1.10.2_1/bin/nginx
$ sudo chmod u+s /usr/local/Cellar/nginx/1.10.2_1/bin/nginx

nginx的操作命令

  • 启动nginx
sudo nginx
  • 重新加载配置|重启|停止|退出 nginx
nginx -s reload|reopen|stop|quit
  • 测试配置是否有语法错误
nginx -t

5、配置nginx

$ sudo vim /usr/local/etc/nginx/nginx.conf

配置nginx虚拟主机

server {
      listen       80;
      server_name  localhost;
      # 设定网站根目录
      root /Users/wanghuiyu/PhpstormProjects/stock/public;
      # 网站默认首页
      index index.php index.html index.htm;
      #charset koi8-r;
      #access_log  logs/host.access.log  main;
      location / {
          # 修改为 Laravel 转发规则,否则PHP无法获取$_GET信息,提示404错误
          try_files $uri $uri/ /index.php?$query_string;
      }
      #error_page  404              /404.html;
      # redirect server error pages to the static page /50x.html
      #
      error_page   500 502 503 504  /50x.html;
      location = /50x.html {
          root   html;
      }
      # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
      #
      location ~ \.php$ {
          fastcgi_split_path_info ^(.+\.php)(/.+)$;
          fastcgi_intercept_errors on;
          fastcgi_pass   127.0.0.1:9001;
          fastcgi_index  index.php;
          fastcgi_param  SCRIPT_FILENAME  /Users/wanghuiyu/PhpstormProjects/stock/public$fastcgi_script_name;
          include        /usr/local/etc/nginx/fastcgi_params;
      }
  }

重启nginx

$ nginx -s reload

6、安装mysql

brew install mysql

mysql_secure_installation设置密码

  • 设置mysql密码安全校验限制
  • set global validate_password_policy=0;
  • set global validate_password_length=4;

首先启动MySQL服务

brew services start mysql

这种是后台启动方式, 方便你下次使用MySQL服务的时候, 直接使用
然后登录MySQL

mysql -u root
  • MySQL5.7以后会出现输入update mysql.user set password=password('root') where user='root'时提示ERROR 1054 (42S22): Unknown column 'password' in 'field list',原来是mysql数据库下已经没有password这个字段了,password字段改成了authentication_string.

接下来更新root密码

update mysql.user set authentication_string=password('root') where user='root' ;

最会别忘了刷新权限

flush privileges;

现在退出MySQL, 测试一下你设置的密码吧!

你可能感兴趣的:(Mac 安装 LNMP 环境)