MacOSX 开发环境搭建踩坑记

最近公司有同事离职,正好空出一部Mac,由于之前接触比较少,正好熟悉一下,于是就申请使用这台机子。

首先得装下环境吧,nginx、php、mysql、node 等,一个不能少。

这一装,把我折腾得,各种操作习惯可以培养,但是搭建环境的时候,踩了不少坑,把整个人都弄得不好了。

应该是同事离职没把他安装的环境清理干净,却把自己的账户删掉了,我在安装各种环境的时候各种冲突,好吧,有冲突也正常,那我把你安装的环境清理干净行再从新安装得了吧。结果,当我卸载一些软件的时候,刷刷刷,一大堆错误...

权限问题

首先是权限不够

Mac-mini:locks DYT$ brew uninstall nginx
xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools), missing xcrun at: /Library/Developer/CommandLineTools/usr/bin/xcrun
Uninstalling /usr/local/Cellar/nginx/1.10.3... (8 files, 980.9KB)
Error: Permission denied @ unlink_internal - /usr/local/var/homebrew/linked/nginx
1
2
3
4

那就先赋予权限:

sudo chown -R $(whoami) /usr/local/var/homebrew/
// or
sudo chown -R $USER:admin /usr/local/var/homebrew/
1
2
3

参考:

安装homebrew 失败的解决方案

Mac OS - Failed to install mongodb with Homebrew due to Permission Issue

mac brew install nginx遇到的坑

卸载重装

再次卸载,终于成功了

Mac-mini:locks DYT$ brew uninstall nginx
Uninstalling /usr/local/Cellar/nginx/1.10.3... (8 files, 980.9KB)
1
2

接下来是安装,继续报错:

Mac-mini:locks DYT$ brew install nginx
Updating Homebrew...
==> Installing dependencies for nginx: openssl, pcre
==> Installing nginx dependency: openssl
xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools), missing xcrun at: /Library/Developer/CommandLineTools/usr/bin/xcrun
Error: Failure while executing: git config --local --replace-all homebrew.private true
1
2
3
4
5
6

查了下资料,解决方案为:

打开终端输入

xcode-select --install
1

回车后,系统弹出下载xcode,点击确认,下载完成后即可。(实际上不是下载xcode,而是下载xcode有关插件,下载时长约1分钟)

出现这个错误原因猜想可能是因为之前安装过xcode卸载后出现的。

参考:

[问题] mac执行git命令出现xcrun: error: invalid active developer path解决方法

【Homebrew】Error: Failure while executing: git config --local --replace-all homebrew.private true

这下好了,终于装上了

Mac-mini:locks DYT$ brew install nginx
Updating Homebrew...
==> Installing dependencies for nginx: pcre
==> Installing nginx dependency: pcre
==> Downloading https://homebrew.bintray.com/bottles/pcre-8.42.high_sierra.bottl
######################################################################## 100.0%
==> Pouring pcre-8.42.high_sierra.bottle.tar.gz
🍺  /usr/local/Cellar/pcre/8.42: 204 files, 5.3MB
==> Installing nginx
==> Downloading https://homebrew.bintray.com/bottles/nginx-1.15.0.high_sierra.bo
######################################################################## 100.0%
==> Pouring nginx-1.15.0.high_sierra.bottle.tar.gz
==> Caveats
Docroot is: /usr/local/var/www

The default port has been set in /usr/local/etc/nginx/nginx.conf to 8080 so that
nginx can run without sudo.

nginx will load all files in /usr/local/etc/nginx/servers/.

To have launchd start nginx now and restart at login:
  brew services start nginx
Or, if you don't want/need a background service you can just run:
  nginx
==> Summary
🍺  /usr/local/Cellar/nginx/1.15.0: 23 files, 1.4MB
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

看到安装的路径为:/usr/local/Cellar/nginx/1.15.0

开启 nginx

开启nginx:

Mac-mini:Cellar DYT$ brew services start nginx
==> Successfully started `nginx` (label: homebrew.mxcl.nginx)
1
2

显示成功了,结果我一打开:http://localhost,打脸了,报个 ERR_CONNECTION_REFUSED,此时我的内心是奔溃的。

重启也无果、关闭

Mac-mini:Cellar DYT$ brew services restart nginx
Stopping `nginx`... (might take a while)
==> Successfully stopped `nginx` (label: homebrew.mxcl.nginx)
==> Successfully started `nginx` (label: homebrew.mxcl.nginx)
Mac-mini:Cellar DYT$ brew services stop nginx
Stopping `nginx`... (might take a while)
==> Successfully stopped `nginx` (label: homebrew.mxcl.nginx)
1
2
3
4
5
6
7

查看一下nginx的具体信息

Mac-mini:Cellar DYT$ brew info nginx
nginx: stable 1.15.0 (bottled), HEAD
HTTP(S) server and reverse proxy, and IMAP/POP3 proxy server
https://nginx.org/
/usr/local/Cellar/nginx/1.15.0 (23 files, 1.4MB) *
  Poured from bottle on 2018-07-09 at 13:04:00
From: https://github.com/Homebrew/homebrew-core/blob/master/Formula/nginx.rb
==> Dependencies
Required: openssl ✔, pcre ✔
Optional: passenger ✘
==> Options
--with-passenger
	Compile with support for Phusion Passenger module
--HEAD
	Install HEAD version
==> Caveats
Docroot is: /usr/local/var/www

The default port has been set in /usr/local/etc/nginx/nginx.conf to 8080 so that
nginx can run without sudo.

nginx will load all files in /usr/local/etc/nginx/servers/.

To have launchd start nginx now and restart at login:
  brew services start nginx
Or, if you don't want/need a background service you can just run:
  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

好吧,试试直接启动

Mac-mini:Cellar DYT$ nginx
nginx: [warn] the "user" directive makes sense only if the master process runs with super-user privileges, ignored in /usr/local/etc/nginx/nginx.conf:2
nginx: [emerg] bind() to 0.0.0.0:80 failed (13: Permission denied)
1
2
3

又报个 Permission denied,nginx 不允许低于1000的端口直接启动,于是附加管理员权限启动:

再重新启动:

Mac-mini:Cellar DYT$ sudo nginx -c /usr/local/etc/nginx/nginx.conf
Password:
nginx: [emerg] getpwnam("jack") failed in /usr/local/etc/nginx/nginx.conf:2
1
2
3

又报 getpwnam("jack"),查看了文件 /usr/local/etc/nginx/nginx.conf,发现里面的配置还是之前同事的配置,注释了以下一行:

#user jack admin;
1

再次启动

Mac-mini:~ DYT$ sudo nginx
Password:
nginx: [emerg] bind() to 0.0.0.0:80 failed (48: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (48: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (48: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (48: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (48: Address already in use)
1
2
3
4
5
6
7

我尼天,这么多,查看了下 /usr/local/etc/nginx/nginx.conf,发现最后有一句:

include servers/*.conf;
1

说明在 /usr/local/etc/nginx/servers/ 下的所有 conf 都被解析,配置为虚拟主机,

打开了此目录下其中一个文件 dytadmin.ynhdkc.com.conf,的确是配置了虚拟主机:

server {
  listen       80;
  server_name  www.dytAdmin.com dytadmin.com;
  root   "/usr/local/var/www/dytAdmin/public";

  location / {
      index  index.html index.htm index.php;
      #autoindex  on;
      if (!-e $request_filename) {
          rewrite  ^/(.*)$  /index.php?s=/$1  last;
          break;
      }

  }
  location ~ \.php(.*)$ {
    proxy_connect_timeout 300s;
    proxy_send_timeout 300s;
    proxy_read_timeout 300s;
    fastcgi_pass   127.0.0.1:9001;
    fastcgi_index  index.php;
    fastcgi_split_path_info  ^((?U).+\.php)(/?.+)$;
    fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
    fastcgi_param  PATH_INFO  $fastcgi_path_info;
    fastcgi_param  PATH_TRANSLATED  $document_root$fastcgi_path_info;
    include        fastcgi_params;
  }
}
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

回到 /usr/local/etc/nginx/nginx.conf,注释之:

#include servers/*.conf;
1

重新加载 nginx 配置:

Mac-mini:Cellar DYT$ sudo nginx -s reload
1

如果执行 nginx -s reload 重启服务器报以下错误信息

nginx: [error] open() "/usr/local/var/run/nginx.pid" failed (2: No such file or directory)
1

执行以下命令即可

nginx -c /usr/local/etc/nginx/nginx.conf
1

查看端口 netstat -anL (https://www.v2ex.com/t/177896)

可以看到端口已经启动成功,touch 了一个 index.php,输入了

<?php phpinfo(); ?>
1

貌似终于跑起来了,浏览器输入 http://localhost,终于出现了 403 Forbidden | nginx/1.15.0 ,总算进了一步。

/usr/local/var/www 中创建一个 test.html 随便输入点内容,地址栏输入: http://localhost/test.html 可以看到正常内容,suo'ming。

接下来就差配置 php 了。

参考:

错误代码: ERR_CONNECTION_REFUSED 解决方法

nginx: [emerg] bind() to 0.0.0.0:80 failed (13: Permission denied)

安装 php

在安装之前,先查询了下apache和nginx解析PHP的原理。以前,我一直使用的是apache做web服务器,配置过apache的都应该知道,php是挂在apache下的一个模块,当http请求一个php文件的话,apache会调用php模块儿进行解析,然后返回html给apache。

而nginx又点不一样,它必须通过一个FastCGI的进程管理器来启动php解析,也就是是php-fpm,当nginx接受的php请求时,它会向它监听的fpm发送一个请求,当fpm接受到请求以后,在创建一个子进程来调用php进行解析。当解析完成后,回收线程,返回html给nginx,这里也引出了一个问题,那就是php鸡肋一般的单例,由于php的生命周期是伴随着一个请求的开始和结束,当这个请求完毕后,该线程就会自动回收了,所以php的静态对象,只能存在于一个请求当中,不想Java等其他语言,静态变量会一直存在。

注意:MacOSX 自带 Apache 和 php,不要去删除系统自带的 Apache 和 php, 我开始就删除了系统自带的Apache,导致后面php一直无法安装成功,重装了Apache后才弄好,所以建议不要轻易删除系统自带的Apache和php,高手另说。

php 的安装相对 nginx 来说,要复杂点,因为brew 默认没有php的包

brew tap homebrew/dupes
1

执行完上面命令后,这个时候才可以安装php,不过php有很多的版本,可以用:brew search php 来查看具体的版本。

然后安装PHP

brew install php56 --with-imap --with-tidy --with-debug --with-mysql --with-fpm
brew install php70 --with-imap --with-tidy --with-debug --with-mysql --with-fpm
1
2

安装的时候报个错:

Mac-mini:servers DYT$ brew install php70 --with-imap --with-tidy --with-debug --with-mysql --with-fpm
Updating Homebrew...
Error: php@7.0 7.0.15_8 is already installed
To upgrade to 7.0.30, run `brew upgrade php@7.0`
1
2
3
4

说是PHP已经安装过了,可以升级。

查看 PHP 安装信息:

Mac-mini:servers DYT$ brew info php
php: stable 7.2.7 (bottled)
General-purpose scripting language
https://secure.php.net/
Not installed
From: https://github.com/Homebrew/homebrew-core/blob/master/Formula/php.rb
==> Dependencies
Build: httpd ✘, pkg-config ✘
Required: apr ✘, apr-util ✘, argon2 ✘, aspell ✘, autoconf ✔, freetds ✘, freetype ✘, gettext ✔, glib ✘, gmp ✘, icu4c ✘, jpeg ✘, libpng ✘, libpq ✘, libsodium ✘, libzip ✘, openssl ✔, pcre ✔, unixodbc ✘, webp ✘
==> Caveats
To enable PHP in Apache add the following to httpd.conf and restart Apache:
    LoadModule php7_module /usr/local/opt/php/lib/httpd/modules/libphp7.so

    <FilesMatch \.php$>
        SetHandler application/x-httpd-php
    </FilesMatch>

Finally, check DirectoryIndex includes index.php
    DirectoryIndex index.php index.html

The php.ini and php-fpm.ini file can be found in:
    /usr/local/etc/php/7.2/

To have launchd start php now and restart at login:
  brew services start php
Or, if you don't want/need a background service you can just run:
  php-fpm
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

看见上面的输出,缺少很多依赖包,于是果断执行升级以自动安装这些依赖:

Mac-mini:servers DYT$ brew upgrade php@7.0
Updating Homebrew...
==> Upgrading 1 outdated package, with result:
php@7.0 7.0.15_8 -> 7.0.30_1
==> Upgrading php@7.0 
==> Installing dependencies for php@7.0: apr, apr-util, aspell, freetds, libpng, freetype, libffi, glib, gmp, icu4c, jpeg, libpq, libtool, libzip, mhash, mcrypt, unixodbc, webp
==> Installing php@7.0 dependency: apr
==> Downloading https://homebrew.bintray.com/bottles/apr-1.6.3.high_sierra.bottl
...
######################################################################## 100.0%
==> Pouring php@7.0-7.0.30_1.high_sierra.bottle.tar.gz
...
==> Caveats
To enable PHP in Apache add the following to httpd.conf and restart Apache:
    LoadModule php7_module /usr/local/opt/php@7.0/lib/httpd/modules/libphp7.so
    <FilesMatch \.php$>
        SetHandler application/x-httpd-php
    </FilesMatch>
Finally, check DirectoryIndex includes index.php
    DirectoryIndex index.php index.html
The php.ini and php-fpm.ini file can be found in:
    /usr/local/etc/php/7.0/

This formula 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 this software first in your PATH run:
  echo 'export PATH="/usr/local/opt/php@7.0/bin:$PATH"' >> ~/.bash_profile
  echo 'export PATH="/usr/local/opt/php@7.0/sbin:$PATH"' >> ~/.bash_profile

For compilers to find this software you may need to set:
    LDFLAGS:  -L/usr/local/opt/php@7.0/lib
    CPPFLAGS: -I/usr/local/opt/php@7.0/include


To have launchd start php@7.0 now and restart at login:
  brew services start php@7.0
Or, if you don't want/need a background service you can just run:
  php-fpm
==> Summary
🍺  /usr/local/Cellar/php@7.0/7.0.30_1: 502 files, 65.6MB
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

启动 php

再次查看 php 配置

Mac-mini:servers DYT$ brew info php
php: stable 7.2.7 (bottled)
General-purpose scripting language
https://secure.php.net/
Not installed
From: https://github.com/Homebrew/homebrew-core/blob/master/Formula/php.rb
==> Dependencies
Build: httpd ✘, pkg-config ✘
Required: apr ✔, apr-util ✔, argon2 ✘, aspell ✔, autoconf ✔, freetds ✔, freetype ✔, gettext ✔, glib ✔, gmp ✔, icu4c ✔, jpeg ✔, libpng ✔, libpq ✔, libsodium ✘, libzip ✔, openssl ✔, pcre ✔, unixodbc ✔, webp ✔
==> Caveats
To enable PHP in Apache add the following to httpd.conf and restart Apache:
    LoadModule php7_module /usr/local/opt/php/lib/httpd/modules/libphp7.so

    <FilesMatch \.php$>
        SetHandler application/x-httpd-php
    </FilesMatch>

Finally, check DirectoryIndex includes index.php
    DirectoryIndex index.php index.html

The php.ini and php-fpm.ini file can be found in:
    /usr/local/etc/php/7.2/

To have launchd start php now and restart at login:
  brew services start php
Or, if you don't want/need a background service you can just run:
  php-fpm
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

使用以下命令运行 php-fpm

Mac-mini:php@7.0 DYT$ launchctl load -w /usr/local/opt/php70/homebrew.mxcl.php@7.0.plist
1

再次 netstat -anL,发现 php-fpm 已经监听到 127.0.0.1.9001

修改 /usr/local/etc/nginx/nginx.conf

# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
  fastcgi_pass   127.0.0.1:9001;
  fastcgi_index  index.php;
  fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
  include        fastcgi_params;
}
1
2
3
4
5
6
7
8

重新加载 nginx 配置: sudo nginx -s reload

再次输入 http://localhost/index.php,我亲爱的 php 信息界面终于出现了

附上最终的 nginx.conf 配置文件:

user DYT admin;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout   300;

    #gzip  on;

    server {
       listen       80;
       server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   /Users/DYT/Desktop/test;
            index  index.php index.html index.htm;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        location ~ \.php$ {
            fastcgi_pass   127.0.0.1:9001;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        location ~ /\.ht {
            deny  all;
        }
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #

    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}
############################ servers ##########################
    # include servers/*.conf;
}
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100

参考:

Mac 平台搭建PHP开发环境:Nginx、PHP、MySQL

设置 php 启动快捷指令

打开 ~/.bash_profile,添加

alias php70.start=launchctl\ load\ -w\ /usr/local/opt/php70/homebrew.mxcl.php@7.0.plist
alias php70.stop=launchctl\ unload\ -w\ /usr/local/opt/php70/homebrew.mxcl.php@7.0.plist
1
2

快捷指令设置之后,终端执行 source ~/.bash_profile,就可以用 php70.startphp70.stop 来启动和停止php-fpm了。

参考:

mac搭建nginx与php

设置 php 开机启动

如果想设开机启动的话

mkdir -p ~/Library/LaunchAgents
cp /usr/local/opt/php70/homebrew.mxcl.php@7.0.plist ~/Library/LaunchAgents/

launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.php@7.0.plist #开机启动
launchctl unload ~/Library/LaunchAgents/homebrew.mxcl.php@7.0.plist #取消开机启动
1
2
3
4
5

参考:

Mac下安装php7.0+nginx

Mac中的定时任务利器:launchctl

配置 nginx 虚拟主机

第一种方式:直接在 nginx.conf 中配置:

http {
    # ...
    server {
        listen       80;
        server_name  www.dytapp.com dytapp.com;
        root   "/Users/DYT/Desktop/dyt-app/public";

        location / {
            index  index.html index.htm index.php;
            #autoindex  on;
            if (!-e $request_filename) {
                rewrite  ^/(.*)$  /index.php?s=/$1  last;
                break;
            }

        }
        location ~ \.php(.*)$ {
            proxy_connect_timeout 300s;
            proxy_send_timeout 300s;
            proxy_read_timeout 300s;
            fastcgi_pass   127.0.0.1:9001;
            fastcgi_index  index.php;
            fastcgi_split_path_info  ^((?U).+\.php)(/?.+)$;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            fastcgi_param  PATH_INFO  $fastcgi_path_info;
            fastcgi_param  PATH_TRANSLATED  $document_root$fastcgi_path_info;
            include        fastcgi_params;
        }
    }
    # ...
}
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

第二种方式:include servers/*.conf

还记得之前注释掉过一行语句:

include servers/*.conf;
1

将之打开,即可加载 /usr/local/etc/nginx/servers 下的所有文件作为虚拟主机配置,比如将方式一中的 server 部分保存到名为 www.dytapp.com.conf 的文件中,重新加载配置文件也能生效。

不过无论哪一种方式,都记得在 /etc/hosts 文件中加入相关配置:

127.0.0.1   localhost
127.0.0.1   www.dytapp.com
127.0.0.1   dytapp.com
1
2
3

nginx 开机启动

使用 homebrew 安装时的配置

如果通过 Homebrew 安装的 nginx,可以通过以下步骤设置开机启动

cp /usr/local/Cellar/nginx/【nginx版本】/homebrew.mxcl.nginx.plist ~/Library/LaunchAgents/
launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.nginx.plist
sudo chown root:wheel /usr/local/Cellar/nginx/【nginx版本】/bin/nginx
sudo chmod u+s /usr/local/Cellar/nginx/【nginx版本】/bin/nginx
1
2
3
4

手动配置开机启动

如果不是通过 homebrew 安装的 nginx,可以通过手工创建 plist 加入定时任务的方式启动

  1. 新建文件
sudo vim /Library/LaunchDaemons/homebrew.mxcl.nginx.plist
1

输入

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
  <dict>
    <key>Label</key>
    <string>homebrew.mxcl.nginx</string>
    <key>RunAtLoad</key>
    <true/>
    <key>KeepAlive</key>
    <false/>
    <key>ProgramArguments</key>
    <array>
        <string>/usr/local/opt/nginx/bin/nginx</string>
        <string>-g</string>
        <string>daemon off;</string>
    </array>
    <key>WorkingDirectory</key>
    <string>/usr/local</string>
  </dict>
</plist>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
  1. 修改权限
sudo chmod 644 /Library/LaunchDaemons/homebrew.mxcl.nginx.plist
1

如果出现 Path had bad ownership/permissions 问题,表示权限不够,可以将之更改为 root 权限:

sudo chown root /Library/LaunchDaemons/homebrew.mxcl.nginx.plist
1
  1. 注册为系统服务
sudo launchctl load -w /Library/LaunchDaemons/homebrew.mxcl.nginx.plist 
1

卸载为

sudo launchctl unload -w /Library/LaunchDaemons/homebrew.mxcl.nginx.plist 
1

查看是否已经加入定时任务

launchctl list | grep nginx
1
  1. 重启后查看nginx是否启动
ps -ef | grep nginx
1

参考:

mac nginx设为开机启动

Mac 通过 Homebrew 安装 nginx 并设置开机启动配置

安装 mysql

发现系统中本身就有一个 homebrew 安装的 MySQL,准备先卸载,再使用官方安装包进行安装。

MySQL 官方镜像地址: https://dev.mysql.com/downloads/file/?id=475582

安装完成后,系统会自动生成一个管理员密码,之后可以修改:

可以在偏好设置中找到 MySQL 设置,可以在此开启 MySQL:

将命令添加到系统命令

  • (1). 进入 /usr/local/mysql/bin,查看此目录下是否有mysql。
  • (2). 执行 vim ~/.bash_profile,在该文件中添加 mysql 命令的目录,PATH=$PATH:/usr/local/mysql/bin,添加完成后,按esc,然后输入wq保存。
  • (3). 最后在命令行输入 source ~/.bash_profile

初始化用户密码

  • 通过 mysql -uroot -p 登录mysql, 输入之前保存的密码。
  • 重置 mysql 初始密码,使用查询语句 SET PASSWORD FOR 'root'@'localhost' = PASSWORD('newpassword'); 即可。

参考:

[Mac] 记录使用Homebrew安装Mysql全过程

在Mac上安装mysql数据库

mac安装mysql的两种方法(含配置)

安装 node

本来系统里面我是看到有 node 的,结果一执行:

Mac-mini:bin DYT$ npm -v
dyld: Library not loaded: /usr/local/opt/icu4c/lib/libicui18n.59.dylib
  Referenced from: /usr/local/bin/node
  Reason: image not found
Abort trap: 6
1
2
3
4
5

查阅资料,解决方案还不算太难:

brew uninstall --ignore-dependencies node
brew uninstall --ignore-dependencies icu4c
brew install icu4c
brew link icu4c --force
brew install node
1
2
3
4
5

再运行 node

Mac-mini:~ DYT$ node -v
v10.6.0
Mac-mini:~ DYT$ npm -v
6.1.0
1
2
3
4

参考:

【原】Mac下使用Brew安装node出错

附录:一些常用的路径

# nginx 相关
/usr/local/etc/nginx/nginx.conf # nginx 主配置文件
/usr/local/Cellar/nginx/1.15.0/ # nginx 安装目录
/usr/local/etc/nginx/servers/ # nginx 虚拟主机文件存放目录
/usr/local/var/www/ # 默认站点目录
ps -ef | grep nginx # 查看 nginx 是否启动

# php 相关
/usr/local/etc/php/7.0/ # php 配置文件路径
/usr/local/etc/php/7.0/php.ini # php 配置文件
/usr/local/Cellar/php@7.0/7.0.30_1/ # PHP 安装目录

# 其他
/usr/local/var/homebrew/ # homebrew 配置相关目录
~/.bash_profile # 别名文件
/etc/hosts # hosts 文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

附录:一些常用的命令

# nginx 相关
brew install nginx # 安装 nginx
brew uninstall nginx # 卸载 nginx
brew services restart nginx # 启动 nginx 服务
brew services stop nginx # 关闭 nginx 服务
brew info nginx # 查看 nginx 相关信息

sudo nginx # 使用缺省配置启动 nginx 服务
sudo nginx -c /usr/local/etc/nginx/nginx.conf # 使用制定配置文件启动 nginx 服务
sudo nginx -s reload # 重新加载 nginx 配置
sudo nginx -s reopen | quit | stop # 开启 退出 停止 
nginx -h # 查看 nginx 命令参数 
nginx -t # 测试配置文件是否正确

# php 相关
brew search php # 查询 PHP 相关的包
brew install php70 --with-imap --with-tidy --with-debug --with-mysql --with-fpm # 安装 PHP
brew upgrade php@7.0 # 升级 php
brew services start php # 启动 php-fpm 服务
brew info php # 查看 PHP 相关信息

launchctl load -w /usr/local/opt/php70/homebrew.mxcl.php@7.0.plist # 启动 php-fpm 服务
launchctl unload -w /usr/local/opt/php70/homebrew.mxcl.php@7.0.plist # 关闭 php-fpm 服务

# 其他
sudo chown -R $(whoami) # 更改所有者
sudo chown -R $USER:admin # 更改所有者
xcode-select --install # 下载并安装xcode有关插件
netstat -anL # 查看端口监听
source ~/.bash_profile # 使别名生效
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

写在后面

配置得差不多了,发现有一个叫做 MAMP 的集成包,没早一点发现,不过经过这样的折腾,自己又精进了不少。

MIT Licensed | Copyright © 2018-present 滇ICP备16006294号

Design by Quanzaiyu | Power by VuePress | Hosted by Coding Pages