MAC开发必备的3个工具

223 阅读1分钟

前言

会介绍的3个工具:

  • Homebrew
  • nginx
  • SwitchHosts

软件介绍

Homebrew

Homebrew 是 macOS(或 Linux)缺失的软件包的管理器。 通过brew可以很方便的在Mac中安装软件或者是卸载软件

安装

打开终端,在命令行中输入如下代码

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

运行

7011fb36d3373338fcfa92e7e961ce38.png

更新了系统,安装 Homebrew 老失败,报错:fatal: unable to access 'https://github.com/Homebrew/brew/':

解决方法:

执行这两行命令

git config --global --unset http.proxy 
git config --global --unset https.proxy

再重新执行安装命令。安装成功后,我们就可以使用brew命令了。

查看版本号

brew --version
// Homebrew 4.0.6

若正确输出版本号,就表示我们已经安装成功了。

常用指令

  • brew search nginx 搜索软件
  • brew install nginx 安装软件
  • brew uninstall nginx 卸载软件
  • sudo brew info nginx 查看安装信息(比如查看安装目录等)
  • brew list 查看已安装的软件

Nginx

Nginx是一款轻量级的Web服务器、反向代理服务器,由于它的内存占用少,启动极快,高并发能力强,在互联网项目中广泛应用。

安装

使用brew安装nginx

brew install nginx

配置信息

1、查看nginx的配置信息

brew info nginx

截屏2023-03-20 19.19.41.png

  • nginx安装目录:/usr/local/Cellar/nginx/1.23.3

  • nginx安装来源:https://github.com/Homebrew/homebrew-core/blob/HEAD/Formula/nginx.rb

  • nginx文件根目录:/usr/local/var/www

  • nginx在这个目录中加载所有文件:/usr/local/etc/nginx/servers/

  • 默认端口号是8080

2、打开nginx目录:

open /usr/local/etc/nginx/

3、打开nginx安装目录:

open /usr/local/Cellar/nginx

就可看到1.23.3文件夹,打开它 截屏2023-03-20 19.30.46.png

html文件夹中有50.html和index.html文件

截屏2023-03-20 19.32.34.png

其实它是指向的就是 /usr/local/var/wwww目录的

20230320193731.jpg

启动服务

brew services start nginx

在终端输入 ps -ef|grep nginx 命令看是否有启动

启动服务后,打开http://localhost:8080 即可访问页面。

截屏2023-03-20 19.51.33.png

成功的话,就是欢迎页面(index.html)

重启服务

brew services restart nginx

停止服务

终端输入ps -ef|grep nginx获取到nginx的进程号, 注意是找到“nginx:master”的那个进程号

kill -QUIT 进程号 (从容的停止,即不会立刻停止)
Kill -TERM 进程号 (和上面一样,也是立刻停止)
Kill -INT 进程号 (和上面一样,也是立刻停止)

nginx.conf配置

cat /usr/local/etc/nginx/nginx.conf

#user  nobody;
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  65;

    #gzip  on;

    server {
        listen       8080;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  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;
        location = /50x.html {
            root   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$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$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
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # 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;
    #    }
    #}
    include servers/*;
}

这样就可以搭建本地服务了。

以上只是简单介绍,想要了解的,可以看下这个文章

SwitchHosts

SwitchHosts是一款管理切换编辑Mac系统hosts的工具。

使用 brew 安装

brew install switchhosts --cask

特点:

  • 语法高亮
  • 支持多hosts组
  • 点击行号切换启用/注释当前行host
  • 在状态栏快速切换
  • 支持本地/远端hosts
  • 支持导入/导出
  • 支持Alfred搜索 (仅适用于Mac)

安装成功后,电脑上会出现这个图标

截屏2023-03-20 20.22.14.png

打开软件

截屏2023-03-20 20.23.13.png

可以在这里配置IP和域名的映射关系