mac 10.14.76 Mojave系统安装nginx,php,mysql

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# 用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

1
2
3
4
5
6
7
8
9
10
# 直接用系统自带的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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# brew安装mysql, 成功后注意一下末尾有很重要的信息,我就是没看仔细 耽误了一会功夫
$ brew install mysql@5.7
...

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

mysql@5.7 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 mysql@5.7 first in your PATH run:
echo 'export PATH="/usr/local/opt/mysql@5.7/bin:$PATH"' >> ~/.zshrc # 我用的zsh 所以需要把环境变量更新到配置文件

For compilers to find mysql@5.7 you may need to set:
export LDFLAGS="-L/usr/local/opt/mysql@5.7/lib"
export CPPFLAGS="-I/usr/local/opt/mysql@5.7/include"

For pkg-config to find mysql@5.7 you may need to set:
export PKG_CONFIG_PATH="/usr/local/opt/mysql@5.7/lib/pkgconfig"


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

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

#启动mysql
$ /usr/local/opt/mysql@5.7/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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#具体错误
$ /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
  1. mysql 启动报错 ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)

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

    1
    $ /usr/local/opt/mysql@5.7/bin/mysql.server start
Bowen wechat
博客文章会同步更新在公众号[后端浪里个浪]
一毛也是爱~