[lnmp环境]mac-Mojave系统最新安装lnmp环境,亲测快速便捷

mac 10.14.76 Mojave系统自带的php为7.1.3如果只是简单调试php代码的环境已经足以.几分钟内即可搭建完整的lnmp环境

背景

新入手一台mbp,所以需要装一个简单的lnmp跑一些简单的php脚本,Mojave系统自带php是7.1.3已经完全符合使用要求,只需要简单的改下配置即可,nginxmysql更是直接使用brew 直接安装就行.但是brew install 不同版本的软件的时候已经更改为 php@71 而不是 php71 这种,所以不确定的时候一定要先 brew search php|mysql查看一下.

nginx

# 用mac 的包管理 brew 安装nginx
$ brew install nginx

# 启动nginx
$ sudo nginx

# 编辑nginx配置文件, 先改一下和项目路径 安装完nginx后配置文件默认就在 /usr/local/etc/nginx 下
$ sudo vi /usr/local/etc/nginx/nginx.conf
http {
    ...

    #gzip  on;
    autoindex on; # 开启文件浏览
    autoindex_exact_size off; # 显示kb大小
    autoindex_localtime on;  # 显示修改时间

    ...

    server{
        listen       80;
        server_name  localhost;
        ...
        location / {
                    root   /www; #php脚本位置
                    index  index.html index.htm;
                }
        ...
        location ~ \.php$ {
                    root           /www;
                    fastcgi_pass   127.0.0.1:9000;
                    fastcgi_index  index.php;
                   # fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name; #自带的注释掉 改成下面的
                    fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name; # 这个是解析php的
                    include        fastcgi_params;
                }
    }
wq!

# 重新加载配置
$ sudo nginx -s reload

# nginx的几个命令 重新加载配置|重启|停止|退出
$ sudo nginx -s reload|reopen|stop|quit

php

# 直接用系统自带的php,配置文件在 /private/etc下 先更改下log的位置
$ sudo vi /private/etc/php-fpm.conf
error_log = /usr/local/var/log/php-fpm.log #换成这个地址,不然启动会报一个日志的错

# 复制一个 php-fpm的配置文件 不然启动报错找不到conf
$ cd /private/etc/php-fpm.d/
$ sudo cp www.conf.default www.conf

#启动php-fpm
$ sudo php-fpm  

mysql

# brew安装mysql, 成功后注意一下末尾有很重要的信息,我就是没看仔细 耽误了一会功夫
$ brew install [email protected]
...

We've installed your MySQL database without a root password. To secure it run:
    mysql_secure_installation  #初始化mysql的root 密码

MySQL is configured to only allow connections from localhost by default

To connect run:
    mysql -uroot

[email protected] is keg-only, which means it was not symlinked into /usr/local,
because this is an alternate version of another formula.

If you need to have [email protected] first in your PATH run:
  echo 'export PATH="/usr/local/opt/[email protected]/bin:$PATH"' >> ~/.zshrc  # 我用的zsh 所以需要把环境变量更新到配置文件

For compilers to find [email protected] you may need to set:
  export LDFLAGS="-L/usr/local/opt/[email protected]/lib"
  export CPPFLAGS="-I/usr/local/opt/[email protected]/include"

For pkg-config to find [email protected] you may need to set:
  export PKG_CONFIG_PATH="/usr/local/opt/[email protected]/lib/pkgconfig"


To have launchd start [email protected] now and restart at login:
  brew services start [email protected] #brew services 管理 启动
Or, if you don't want/need a background service you can just run:
  /usr/local/opt/[email protected]/bin/mysql.server start # 这个是用mysql直接启动

#zsh下需要先增加环境变量
$ echo 'export PATH="/usr/local/opt/[email protected]/bin:$PATH"' >> ~/.zshrc
$ source ~/.zshrc

#启动mysql
$ /usr/local/opt/[email protected]/bin/mysql.server start

#初始化密码
$ mysql_secure_installation
按照提示设置即可

#登录mysql 输入上一步设置的 密码
$ mysql -uroot -p

各个步骤容易出现的问题

  1. 苹果系统自带php-fpm启动报错No pool defined failed to post process the configuration FPM initialization failed

    #具体错误
    $  /etc  sudo php-fpm        
    No log handling enabled - using stderr logging
    Created directory: /var/db/net-snmp
    Created directory: /var/db/net-snmp/mib_indexes
    [16-Aug-2019 20:36:21] WARNING: Nothing matches the include pattern '/private/etc/php-fpm.d/*.conf' from /private/etc/php-fpm.conf at line 125.
    [16-Aug-2019 20:36:21] ERROR: No pool defined. at least one pool section must be specified in config file
    [16-Aug-2019 20:36:21] ERROR: failed to post process the configuration
    [16-Aug-2019 20:36:21] ERROR: FPM initialization failed
    
    #解决方案:
    $ cd /private/etc/php-fpm.d/
    $ sudo cp www.conf.default www.conf
    # 重新启动php-fpm即可
    $ sudo php-fpm 
    
  2. mysql 启动报错 ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)

    这是由于brew install [email protected]后没有启动mysql服务导致的,所以解决方案是直接启动mysql服务就好:

    $ /usr/local/opt/[email protected]/bin/mysql.server start
    

本文作者: Bowen
本文链接: https://dangbowen.com/php/84c1c2fb.html
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明出处!

你可能感兴趣的:(mac工具)