0%

Mac服务器环境配置

服务器搭建记录

基础环境

Command Line Tools

Mac 的命令行开发者基础工具合集,包括 clang、make、git、lldb等等

1
xcode-select --install

该命令会触发图形化安装程序弹窗(需点击确认)

Homebrew

官网:https://brew.sh/zh-cn/

依赖 git,请先安装 Command Line Tools

建议使用国内镜像安装

1
/bin/zsh -c "$(curl -fsSL https://gitee.com/cunkai/HomebrewCN/raw/master/Homebrew.sh)"

安装结束后,更新环境变量

1
source /Users/XXXX/.zprofile

工具

htop

增强版 top,用于查看内存

1
brew install htop

使用 htop 命令查看动态内存使用情况

ncdu

磁盘占用查看工具

1
brew install ncdu
  • ncdu 查看当前目录
  • ncdu <PATH> 查看指定目录

orbstack

1
brew install orbstack

镜像配置

  1. 配置
  2. 重启 orbstack

Docker

~/navidrome/docker-compose.yml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
services:
navidrome:
image: deluan/navidrome:latest
platform: linux/amd64
user: 1000:1000 # should be owner of volumes
ports:
- "4533:4533"
restart: unless-stopped
environment:
# Optional: put your config options customization here. Examples:
ND_BASEURL: /navidrome
ND_LOGLEVEL: debug
volumes:
- "./data:/data"
- "./music:/music:ro"

docker-compose up -d

immich

App 位置:~/Apps/immich

安装

  1. 新建 App 文件夹,如 ~/Apps/immich

    1
    mkdir ~/Apps/immich
  2. 下载 docker-compose.yml and example.env(重命名为 .env)放到 App 文件夹

    1
    wget -O docker-compose.yml https://github.com/immich-app/immich/releases/latest/download/docker-compose.yml
    1
    wget -O .env https://github.com/immich-app/immich/releases/latest/download/example.env
  3. 环境配置,编辑 .env 文件,设置如照片上传位置

    1
    UPLOAD_LOCATION=/Volumes/Drive-WD/immich/library
  4. 启动容器

    1
    docker compose up -d

安装完成,则可从默认 2283 端口访问

网络

Nginx 反代

Nginx 安装

  1. 安装 nginx

    1
    brew install nginx
  2. 启动 nginx

    1
    brew services start nginx
  3. 测试访问

    1
    curl -I http://localhost:8080

Nginx 配置

/opt/homebrew/etc/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
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#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;
}

location ^~ /navidrome/ {
proxy_pass http://127.0.0.1:4533/navidrome/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-Protocol $scheme;
proxy_set_header X-Forwarded-Host $http_host;
proxy_buffering off;
}

location ^~ /immich/ {
proxy_pass http://127.0.0.1:2283/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-Protocol $scheme;
proxy_set_header X-Forwarded-Host $http_host;
proxy_buffering off;
}

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

Caddy 反代

安装 Caddy

1
brew install caddy

安装目录为下文的 /opt/homebrew/Cellar/caddy/2.10.0

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
guoyi@mac-mini ~ % brew install caddy
==> Auto-updating Homebrew...
Adjust how often this is run with `$HOMEBREW_AUTO_UPDATE_SECS` or disable with
`$HOMEBREW_NO_AUTO_UPDATE=1`. Hide these hints with `$HOMEBREW_NO_ENV_HINTS=1` (see `man brew`).
==> Fetching downloads for: caddy
==> Fetching caddy
==> Downloading https://mirrors.aliyun.com/homebrew/homebrew-bottles/caddy-2.10.0.arm64_se
################################################################################### 100.0%
==> Pouring caddy-2.10.0.arm64_sequoia.bottle.1.tar.gz
==> Caveats
When running the provided service, caddy's data dir will be set as
`/opt/homebrew/var/lib`
instead of the default location found at https://caddyserver.com/docs/conventions#data-directory

To start caddy now and restart at login:
brew services start caddy
Or, if you don't want/need a background service you can just run:
XDG_DATA_HOME="/opt/homebrew/var/lib" HOME="/opt/homebrew/var/lib" /opt/homebrew/opt/caddy/bin/caddy run --config /opt/homebrew/etc/Caddyfile
==> Summary
🍺 /opt/homebrew/Cellar/caddy/2.10.0: 39 files, 39.5MB
==> Running `brew cleanup caddy`...
Disable this behaviour by setting `HOMEBREW_NO_INSTALL_CLEANUP=1`.
Hide these hints with `HOMEBREW_NO_ENV_HINTS=1` (see `man brew`).
==> No outdated dependents to upgrade!
==> Caveats
zsh completions have been installed to:
/opt/homebrew/share/zsh/site-functions

在安装目录下,即可找到相应的服务配置 homebrew.mxcl.caddy.plist

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
<?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>EnvironmentVariables</key>
<dict>
<key>HOME</key>
<string>/opt/homebrew/var/lib</string>
<key>XDG_DATA_HOME</key>
<string>/opt/homebrew/var/lib</string>
</dict>
<key>KeepAlive</key>
<true/>
<key>Label</key>
<string>homebrew.mxcl.caddy</string>
<key>LimitLoadToSessionType</key>
<array>
<string>Aqua</string>
<string>Background</string>
<string>LoginWindow</string>
<string>StandardIO</string>
<string>System</string>
</array>
<key>ProgramArguments</key>
<array>
<string>/opt/homebrew/opt/caddy/bin/caddy</string>
<string>run</string>
<string>--config</string>
<string>/opt/homebrew/etc/Caddyfile</string>
</array>
<key>RunAtLoad</key>
<true/>
<key>StandardErrorPath</key>
<string>/opt/homebrew/var/log/caddy.log</string>
<key>StandardOutPath</key>
<string>/opt/homebrew/var/log/caddy.log</string>
</dict>
</plist>

找到其中的默认配置路径:/opt/homebrew/etc/Caddyfile,当前还没有该文件,也没有启动服务,需要编写完配置文件后,再启动服务

Caddyfile 配置文件

/opt/homebrew/Cellar/caddy/2.10.0

brew services start caddy

cpolar 内网穿透

macos cpolar 使用说明

cpolar 安装

  1. 安装 cpolar

    1
    brew tap probezy/core && brew install cpolar
  2. Token 认证

    1
    cpolar authtoken xxxxxxx
  3. 安装 cpolar 服务

    1
    sudo cpolar service install
  4. 启动 cpolar 服务

    1
    sudo cpolar service start
  5. 本地登入 http://127.0.0.1:9200/

ssh

设置 22 端口 tcp 隧道后

1
ssh <UserName>@3.tcp.cpolar.top -p <Port>