目录

选择服务器系统:为什么我转向了 Debian 12?

转变背景

在过去的使用经验中,我曾深刻体验了 Ubuntu 操作系统的稳定性和便利性,尤其是在获取各类软件的最新版本方面表现出色。然而,考虑到其相对较高的资源占用,我最终决定将服务器系统的选择转向 Debian。

Debian 操作系统在我眼中具有独特的优势,尤其是在资源管理方面。其稳定性和性能表现使其成为我未来所有服务器的首选。随着 Debian 12 的发布,我迎来了重新配置服务器系统的机会。

Debian 12 系统安装

首先,确保系统环境得到更新:

1
apt update && apt -y upgrade && apt install -y xz-utils openssl gawk file

然后,我选择使用了 veip007 大佬的脚本进行 Debian 12 的安装,他的仓库地址是 veip007/dd

1
wget -N --no-check-certificate https://raw.githubusercontent.com/veip007/dd/master/InstallNET.sh &&  chmod +x InstallNET.sh && ./InstallNET.sh -d 12 -v 64 -p "ssh-password" -port "22" --mirror 'https://ftp.debian.org/debian/'

请注意,需要替换 “ssh-password” 为您自己设置的 SSH 密码。安装过程中,您可以通过服务器厂商提供的 noVNC 查看安装进度。

配置服务器的基本环境

我之前也配置过,这是老版本,现在我的基本操作不变,就是设置了一些新的东西,而且更加快速的设置。

首先配置 zsh

信息
这行命令的作用:更新 Debian 的软件源,下载 zshgitvimcurlsudoohmyzsh
1
apt update && apt -y upgrade && apt install -y zsh && apt install -y git && apt install -y vim && apt install -y curl && apt install -y sudo && sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"
信息
这行命令的作用:下载 zsh-syntax-highlighting 插件、zsh-autosuggestions 插件,设置主题为 ys,然后添加这两个插件到 ~/.zshrc 中,最后 source ~/.zshrc
1
git clone https://github.com/zsh-users/zsh-syntax-highlighting.git ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-syntax-highlighting && git clone https://github.com/zsh-users/zsh-autosuggestions ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-autosuggestions && sed -i 's/ZSH_THEME=".*/ZSH_THEME="ys"/' ~/.zshrc && sed -i '/^plugins=/ s/plugins=.*/plugins=(git zsh-autosuggestions zsh-syntax-highlighting)/' ~/.zshrc && source ~/.zshrc

我一般会根据主机厂商的名字来设置服务器的名字,比如:

1
hostnamectl set-hostname Google_cloud_HK
警告
如果出现了 sudo: unable to resolve host US: No address associated with hostname 的异常,请按照以下步骤进行修复。
  1. 检查 /etc/hosts 文件:使用以下命令打开 /etc/hosts 文件进行编辑:

    1
    
    sudo vim /etc/hosts
    

    确保文件中包含正确的主机名和 IP 地址映射。例如,您可以添加以下行:

    1
    2
    
    127.0.0.1   localhost
    127.0.1.1   Google_cloud_HK
    

    将 “Google_cloud_HK” 替换为您的实际主机名。保存文件并退出。

  2. 检查 /etc/hostname 文件:使用以下命令打开 /etc/hostname 文件进行编辑:

    1
    
    sudo vim /etc/hostname
    

    确保文件中包含正确的主机名。保存文件并退出。

配置 alias

基础 alias

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
cat <<EOF >> ~/.zshrc
# easy to clear
alias c="clear"
# ping -c 5 -s 1000 $1
alias p5='p5(){ping -c 5 -s 1000 \$1;};p5'
# update and upagrade
alias upg="apt update && apt -y upgrade"
# install apps
alias ins="apt install -y "
# easy to alias
alias a="alias" 
# acme
alias acme.sh="/root/.acme.sh/acme.sh"
# nginx
alias ng="sudo /usr/sbin/nginx"
# glances
alias g="glances"
# systemctl
alias sy="systemctl"
EOF

域名配置相关 alias

1
2
3
4
5
cat <<EOF >> ~/.zshrc
# nginx配置
alias nginx-config='python3 /usr/local/bin/generate_nginx_config.py'
alias nginx-delete='python3 /usr/local/bin/nginx-delete.py'
EOF

使用的时候直接输入 nginx-config ,然后输入域名,接着输入反代的端口,这样就可以生成对应的 nginx 配置文件,同时 ssl 证书也使用了 acme.sh 进行了配置。当然,前提得是你先安装 acme.sh,这是我的安装教程: 安装 acme.sh

nginx-delete 会删除 /root/.acme.sh/domain_ecc/etc/nginx/sites-available/domain.conf/etc/nginx/ssl/domain/etc/nginx/sites-enabled/domian.conf。避免删除域名的时候的复杂操作,这样借用脚本就可以快速的配置了。

两个 python 文件的内容:

 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
sudo cat <<EOF > /usr/local/bin/generate_nginx_config.py
import os

def generate_nginx_config(domain, proxy_port):
    config = f"""server {{
    listen 443 ssl;
    listen [::]:443 ssl;
    listen 443 quic;
    listen [::]:443 quic;
    # To support HTTP/2
    http2 on;
    
    # Quic or HTTP/3 response header
    add_header Alt-Svc 'h3=":443"; ma=86400';
    server_name {domain};

    # SSL
    ssl_certificate         /etc/nginx/ssl/{domain}/fullchain.cer;
    ssl_certificate_key     /etc/nginx/ssl/{domain}/{domain}.key;
    ssl_trusted_certificate /etc/nginx/ssl/{domain}/fullchain.cer;

    # security
    include                 nginxconfig.io/security.conf;

    # logging
    access_log              /var/log/nginx/{domain}_access.log combined buffer=512k flush=1m;
    error_log               /var/log/nginx/{domain}_error.log warn;

    # reverse proxy
    location / {{
        proxy_pass            http://127.0.0.1:{proxy_port};
        proxy_set_header Host \$host;
        include               nginxconfig.io/proxy.conf;
    }}

    # additional config
    include nginxconfig.io/general.conf;
}}

# HTTP redirect
server {{
    listen      80;
    listen      [::]:80;
    server_name {domain};

    location / {{
        return 301 https://{domain}\$request_uri;
    }}
}}
"""

    # Write config to file
    config_path = f'/etc/nginx/sites-available/{domain}.conf'
    with open(config_path, 'w') as config_file:
        config_file.write(config)

    # Create symbolic link
    symlink_path = f'/etc/nginx/sites-enabled/{domain}.conf'
    os.symlink(config_path, symlink_path)
    ac(domain)

    print(f'Nginx configuration for {domain} has been created and activated.')

def ac(domain):
    if not domain:
        print("Usage: ac <domain>")
        return 1

    ssl_dir = f"/etc/nginx/ssl/{domain}"
    if not os.path.exists(ssl_dir):
        os.makedirs(ssl_dir)

    os.system(f'acme.sh --issue -d "{domain}" --dns dns_cf')
    os.system(f'acme.sh --install-cert -d "{domain}" \
        --key-file "{ssl_dir}/{domain}.key" \
        --fullchain-file "{ssl_dir}/fullchain.cer" \
        --ca-file "{ssl_dir}/ca.cer" \
        --reloadcmd "systemctl restart nginx"')

    print(f"SSL certificate for {domain} has been generated and installed, and Nginx reloaded.")


if __name__ == "__main__":
    domain = input('Enter the domain: ')
    proxy_port = input('Enter the proxy port: ')

    generate_nginx_config(domain, proxy_port)
EOF
 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
sudo cat <<EOF > /usr/local/bin/nginx-delete.py
import os
import shutil

def delete_nginx_config(domain):
    # Delete SSL directory
    ssl_dir = f'/etc/nginx/ssl/{domain}'
    if os.path.exists(ssl_dir):
        shutil.rmtree(ssl_dir)
        print(f'SSL directory {ssl_dir} deleted.')

    # Delete sites-enabled config
    sites_enabled_path = f'/etc/nginx/sites-enabled/{domain}.conf'
    if os.path.exists(sites_enabled_path):
        os.remove(sites_enabled_path)
        print(f'Sites-enabled symlink {sites_enabled_path} deleted.')

    # Delete sites-available config
    sites_available_path = f'/etc/nginx/sites-available/{domain}.conf'
    if os.path.exists(sites_available_path):
        os.remove(sites_available_path)
        print(f'Sites-available config {sites_available_path} deleted.')

    # Delete acme.sh directory
    acme_sh_dir = f'/root/.acme.sh/{domain}_ecc'
    if os.path.exists(acme_sh_dir):
        shutil.rmtree(acme_sh_dir)
        print(f'acme.sh directory {acme_sh_dir} deleted.')

    print(f'Nginx configuration and SSL files for {domain} have been deleted.')

if __name__ == "__main__":
    domain = input('Enter the domain to delete: ')

    if not domain:
        print('Domain cannot be empty. Exiting...')
        exit(1)

    delete_nginx_config(domain)
EOF

对了,这里面的 nginxconfig.io 目录下的文件我是从 digitalocean 弄来的,这样配置更加简单,使用它的配置信息。

流媒体检测 alias

1
2
3
4
cat <<EOF >> ~/.zshrc
# Streaming unblock test
alias stream="bash <(curl -L -s https://raw.githubusercontent.com/lmc999/RegionRestrictionCheck/main/check.sh)"
EOF

docker app 相关 alias

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
cat <<EOF >> ~/.zshrc
# docker apps
# dc
alias dc='docker compose pull && docker compose up -d && docker rmi `docker images -q -f dangling=true`'

# portainer
alias portainer='docker stop portainer && docker rm portainer && docker pull portainer/portainer-ce:latest && docker run -d -p 9443:9443 --name portainer --restart=always -v /var/run/docker.sock:/var/run/docker.sock -v portainer_data:/data portainer/portainer-ce:latest && docker rmi `docker images -q -f dangling=true`'

# docker clean images
alias dci='docker rmi `docker images -q -f dangling=true`'
EOF

按需配置

开启 BBR 加速

1
2
3
4
echo "net.core.default_qdisc=fq" >> /etc/sysctl.conf
echo "net.ipv4.tcp_congestion_control=bbr" >> /etc/sysctl.conf
sysctl -p
lsmod | grep bbr

设置 Ipv4 优先于 Ipv6

1
echo "precedence ::ffff:0:0/96 100" >>/etc/gai.conf

安装 glances 监控服务器状态

1
sudo apt install -y glances

或者:

1
sudo apt install python3 python3-dev python3-jinja2 python3-psutil python3-setuptools hddtemp python3-pip lm-sensors -y && pip install bottle && sudo pip3 install glances

安装 docker 和 docker compose

  • 配置环境
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
sudo apt update
sudo apt install ca-certificates curl gnupg -y
sudo install -m 0755 -d /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/debian/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
sudo chmod a+r /etc/apt/keyrings/docker.gpg

echo \
  "deb [arch="$(dpkg --print-architecture)" signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/debian \
  "$(. /etc/os-release && echo "$VERSION_CODENAME")" stable" | \
  sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt update
  • 安装
1
sudo apt install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin -y
  • 卸载
1
sudo apt purge docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin docker-ce-rootless-extras -y
  • 检测版本
1
docker --version && docker compose version

使用 portainer 来进行 docker 管理

1
docker volume create portainer_data && docker run -d -p 127.0.0.1:9443:9443 --name portainer --restart=always -v /var/run/docker.sock:/var/run/docker.sock -v portainer_data:/data portainer/portainer-ce:latest

通过:https://IP:9443 进行访问,或者配置 nginx https 访问,使用上面的 nginx-config 快速生成对应的配置文件。

设置时区

1
sudo dpkg-reconfigure tzdata

设置系统日志文件大小

1
sudo journalctl --vacuum-size=100M

设置字符集

问题
如果系统的字符集不能正确显示中文或者其他信息,将其设置为 en_US.UTF-8,方便其显示。
1
sudo localedef -v -c -i en_US -f UTF-8 en_US.UTF-8
  1. 确认系统是否已经安装所需的语言包。在终端中输入以下命令查看:
1
locale -a

确保"en_US.UTF-8"在输出的列表中。

  1. 更改系统语言环境。可以通过编辑 /etc/default/locale 文件来更改系统的默认语言环境。使用以下命令打开该文件:
1
sudo vim /etc/default/locale

在打开的文件中,你可以看到类似于以下的内容:

1
LANG=en_US

将其更改为:

1
LANG=en_US.UTF-8

保存文件并退出编辑器。

  1. 重新启动系统,以使更改生效:
1
sudo reboot

完成这些步骤后,系统的语言环境应该会更改为"en_US.UTF-8"。请注意,更改语言环境可能会影响一些程序和界面的显示语言,所以请谨慎进行更改。

自动删除 7 天之前的系统日志

1
0 0 * * * journalctl --vacuum-time=7d

每七天自动删除 log 目录下的所有压缩文件

1
2
0 0 * * 0 rm /var/log/*.gz
0 0 * * 0 rm /var/log/nginx/*.gz

通过这些配置和 alias,我希望为我的服务器系统带来更高效、更稳定的运行环境。这些步骤和设置也为其他对 Debian 12 感兴趣的用户提供了一份详尽的参考。