服务器配置(Ubuntu)
服务器环境配置记录(Ubuntu)
📋 概述
本文档记录了 Ubuntu 服务器环境的完整配置流程,包括系统初始化、软件安装、服务配置等步骤。
🌐 中文语言环境配置
# 安装中文语言包sudo apt install language-pack-zh-hans language-pack-zh-hans-base
# 设置系统语言环境sudo update-locale LANG=zh_CN.UTF-8
# 永久生效配置echo 'export LANG=zh_CN.UTF-8' >> ~/.bashrc⚙️ 基础系统配置
APT 软件包管理
# 安装常用工具sudo apt install neofetch net-tools p7zip-full
# 移除不必要的预装软件sudo apt remove cloud-guest-utils landscape-common pollinate \ ubuntu-pro-client tmux cloud-initramfs-copymods \ cloud-initramfs-dyn-netconf lxd-agent-loader lxd-installer fwupd
# 系统更新sudo apt update && sudo apt upgrade -ysudo apt autoremove -y🔐 自动登录配置
NOTE修改系统服务文件启用自动登录功能。
编辑 /usr/lib/systemd/system/getty@.service:
[Service]ExecStart=-/sbin/agetty --autologin jhll1124 --noclear %I $TERM🔑 SSH 密钥认证配置
TIP配置 SSH 公钥认证以提高安全性和便利性。
# 将公钥添加到授权密钥文件mkdir -p ~/.sshchmod 700 ~/.sshecho "你的公钥内容" >> ~/.ssh/authorized_keyschmod 600 ~/.ssh/authorized_keys
# 可选:禁用密码登录(仅密钥登录)sudo nano /etc/ssh/sshd_config# 修改:PasswordAuthentication nosudo systemctl restart ssh💾 存储配置
硬盘自动挂载
# 创建挂载点目录sudo mkdir /mnt/***sudo chmod 777 /mnt/*编辑 /etc/fstab 添加挂载配置:
# 数据硬盘挂载/dev/sda4 /mnt/jhll ntfs-3g defaults 0 0/dev/sda3 /mnt/data ntfs-3g defaults 0 0/dev/sda5 /mnt/ll ntfs-3g defaults 0 0/dev/sda6 /mnt/jhll/ext4 ext4 defaults 0 0
# 内存盘配置(27GB临时存储)tmpfs /mnt/tmpfs tmpfs defaults,size=27G 0 0🐚 Shell 环境配置
Fish Shell 安装配置
# 安装 Fish Shellsudo apt install fish
# 设置 Fish 为默认 Shellchsh -s /usr/bin/fish
# 安全配置sudo passwd -d jhll1124🚫 服务优化与禁用
禁用不必要的系统服务
sudo systemctl disable x11-common apache2 netplan-ovs-cleanup cryptdisks-early cryptdisks snapd.apparmor snapd.autoimport snapd.core-fixup snapd.failure snapd.recovery-chooser-trigger snapd.seeded snapd snapd.snap-repair snapd.system-shutdownsudo systemctl disable snapd.socket📊 日志系统优化
WARNING减少磁盘写入,延长 SSD 寿命。
# 禁用系统日志服务sudo systemctl disable rsyslog systemd-journald
# 配置 Journald 不存储日志sudo nano /etc/systemd/journald.conf# 修改为:Storage=none
# 清理现有日志sudo truncate -s 0 /var/log/*sudo rm -rf /var/log/journal📁 Server 目录准备
# 创建服务目录结构sudo cp -r /path2/server /serversudo chown -R $(whoami):$(whoami) /serversudo chmod -R 775 /server🖼️ Python 图片服务 (RandPic)
Systemd 服务配置
创建 /etc/systemd/system/randpic.service:
[Unit]Description=Random Image HTTP ServiceAfter=network.target
[Service]ExecStart=sudo /usr/bin/python3 /server/RandPic.pyRestart=alwaysRestartSec=2
[Install]WantedBy=multi-user.target启用服务:
sudo systemctl enable randpicsudo systemctl start randpic📝 博客服务 (Fuwari 框架)
项目初始化
cd /servergit clone https://github.com/CuteLeaf/Firefly.gitcd Fireflypnpm install使用指令
下列指令均需要在项目根目录执行:
| Command | Action |
|---|---|
pnpm install | 安装依赖 |
pnpm dev | 在 localhost:4321 启动本地开发服务器 |
pnpm build | 构建网站至 ./dist/ |
pnpm preview | 本地预览已构建的网站 |
pnpm new-post <filename> | 创建新文章 |
📁 文件列表服务 (Oplist)
安装配置
# 解压安装tar -zxvf openlist-linux-amd64.tar.gzsudo mv openlist /usr/local/bin/alist
# 初始化服务alist serverSSL 证书配置
编辑 ~/data/config.json:
{ "cert_file": "/server/cert/server.pem", "key_file": "/server/cert/server-key.pem"}Systemd 服务配置
创建 /etc/systemd/system/alist.service:
[Unit]Description=Alist File List ServiceAfter=network.target
[Service]Type=simpleExecStart=/usr/bin/sudo alist serverWorkingDirectory=~Restart=on-failureUser=jhll1124
[Install]WantedBy=multi-user.target启用服务:
sudo systemctl enable alistsudo systemctl start alist🌐 Caddy Web 服务器配置
安装
sudo apt install caddysudo systemctl enable caddy或者使用官方稳定最新版本:
sudo apt install -y debian-keyring debian-archive-keyring apt-transport-https curlcurl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/gpg.key' | sudo gpg --dearmor -o /usr/share/keyrings/caddy-stable-archive-keyring.gpgcurl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/debian.deb.txt' | sudo tee /etc/apt/sources.list.d/caddy-stable.listsudo apt updatesudo apt install caddy站点配置
Caddy 中文文档 | 原文
配置文件位置:/etc/caddy/Caddyfile,并确保其权限:
sudo chmod 777 /etc/caddy/Caddyfile临时启动 caddy:
sudo caddy run --environ --config /etc/caddy/Caddyfile如果要配置 ACME,将 tls 块修改为以下配置,通过 DNS 验证网站所有权,此处以 Cloudflare API 为例,请确保该 API 有更改 DNS 记录的权限:
tls { dns cloudflare {env.CLOUDFLARE_API_TOKEN} }注意,DNS 验证需要:
- 安装带 DNS 插件的 Caddy:
sudo caddy add-package github.com/caddy-dns/cloudflare- 如果不能安装插件,则需要开放 80 端口,直接删除
tls行,通过 HTTP 验证
CAUTION以下为完整 Caddy 配置,仅供参考,请勿全部照搬,否则后果自负。
(errors) { handle_errors { root /server/error rewrite /{http.error.status_code}.html file_server }}
cialo.site { tls /server/cert/cialo.site_bundle.crt /server/cert/cialo.site.key root /server/data import errors encode gzip handle /pic/* { reverse_proxy http://127.0.0.1:5737 } handle /img/* { file_server browse } handle /html/* { file_server browse } handle /file/* { file_server browse } handle /* { error 403 }}
https://cialo.site:52011 { tls /server/cert/cialo.site_bundle.crt /server/cert/cialo.site.key import errors reverse_proxy https://127.0.0.1:5201 { transport http { tls_insecure_skip_verify } }}
https://cialo.site:30721 { tls /server/cert/cialo.site_bundle.crt /server/cert/cialo.site.key import errors reverse_proxy http://172.17.0.1:80}🔄 动态 DNS 服务 (DDNS-GO)
安装部署
# 解压安装tar -xvf ddns-go_6.8.0_linux_x86_64.tar.gzsudo mv ddns-go /usr/local/bin/
# 安装为系统服务sudo ddns-go -s install🔒 网络安全配置
V2Ray 代理服务
sudo apt install v2raysudo cp /path2/config.json /etc/v2ray/config.jsonmkcert SSL 证书信任
复制证书到 /etc/ssl/certs 或 /usr/local/share/ca-certificates/ 并执行:
sudo update-ca-certificates --fresh🛠️ 系统维护
重建 grub-efi
# 重新安装 GRUB EFIsudo apt install grub-efi-amd64 grub-efi-amd64-signed grub-common grub2-commonsudo grub-install --target=x86_64-efi --efi-directory=/boot/efi --bootloader-id=ubuntusudo update-grub📦 系统备份与恢复
# 完整系统备份sudo tar -cvpzf /path2/server-citlali.tar.gz -C /mount .
# 系统恢复sudo tar -xvpzf /path2/server-citlali.tar.gz -C /mountCAUTION双系统环境下请确保关闭 Windows 快速启动功能,避免文件系统冲突。
⚠️ 暂不使用服务
🌐 Nginx Web 服务器配置
sudo apt install nginxsudo systemctl enable nginx配置文件位置:/etc/nginx/sites-available/default
CAUTION以下为完整 Nginx 配置,仅供参考,请勿全部照搬,否则后果自负。
配置较长,可点击展开查看。
点击展开 Nginx 完整配置
server { charset utf-8;
listen 443 ssl http2 default_server; listen [::]:443 ssl http2 default_server;
ssl_certificate /server/cert/cialo.site_bundle.crt; ssl_certificate_key /server/cert/cialo.site.key;
root /server/data;
index index.html index.htm index.php;
server_name _;
error_page 403 /403.html; location = /403.html { root /server/error; } error_page 404 /404.html; location = /404.html { root /server/error; }
location ~ \.php$ { fastcgi_pass unix:/run/php/php8.3-fpm.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; }
location / { deny all; }
location /img/ { autoindex on; autoindex_exact_size off; autoindex_localtime on; }
location /html/ { autoindex on; autoindex_exact_size off; autoindex_localtime on; }
location /file/ { autoindex on; autoindex_exact_size off; autoindex_localtime on; }}
server { listen 52011 ssl http2;
ssl_certificate /server/cert/cialo.site_bundle.crt; ssl_certificate_key /server/cert/cialo.site.key;
proxy_buffering off; proxy_request_buffering off;
client_max_body_size 0;
proxy_connect_timeout 300s; proxy_send_timeout 365d; proxy_read_timeout 365d; send_timeout 365d;
location / { proxy_pass https://127.0.0.1:5201;
proxy_ssl_verify off;
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; }}
server { listen 30721 ssl http2;
ssl_certificate /server/cert/cialo.site_bundle.crt; ssl_certificate_key /server/cert/cialo.site.key;
proxy_buffering off; proxy_request_buffering off;
location / { proxy_pass http://172.17.0.1; 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; }}点击展开 Nginx 备用注释配置
综述层
# You should look at the following URL's in order to grasp a solid understanding# of Nginx configuration files in order to fully unleash the power of Nginx.# https://www.nginx.com/resources/wiki/start/# https://www.nginx.com/resources/wiki/start/topics/tutorials/config_pitfalls/# https://wiki.debian.org/Nginx/DirectoryStructure## In most cases, administrators will remove this file from sites-enabled/ and# leave it as reference inside of sites-available where it will continue to be# updated by the nginx packaging team.## This file will automatically load configuration files provided by other# applications, such as Drupal or Wordpress. These applications will be made# available underneath a path with that package name, such as /drupal8.## Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples.
# Virtual Host configuration for example.com## You can move that to a different file under sites-available/ and symlink that# to sites-enabled/ to enable it.##server {# listen 80;# listen [::]:80;## server_name example.com;## root /var/www/example.com;# index index.html;## location / {# try_files $uri $uri/ =404;# }#}server 层
# Note: You should disable gzip for SSL traffic. # See: https://bugs.debian.org/773332 # # Read up on ssl_ciphers to ensure a secure configuration. # See: https://bugs.debian.org/765782 # # Self signed certs generated by the ssl-cert package # Don't use them in a production server! # # include snippets/snakeoil.conf;
location / { # First attempt to serve request as file, then # as directory, then fall back to displaying a 404. try_files $uri $uri/ =404; }
# pass PHP scripts to FastCGI server # #location ~ \.php$ { # include snippets/fastcgi-php.conf; # # # With php-fpm (or other unix sockets): # fastcgi_pass unix:/run/php/php7.4-fpm.sock; # # With php-cgi (or other tcp sockets): # fastcgi_pass 127.0.0.1:9000; #}
# deny access to .htaccess files, if Apache's document root # concurs with nginx's one # #location ~ /\.ht { # deny all; #}📁 SMB 文件共享服务
WARNING当前暂不使用 SMB 服务,使用 Windows 自带 SMB 功能
# 安装 Samba 服务sudo apt install samba
# 创建专用用户sudo useradd smbusersudo passwd smbusersudo systemctl enable smbdSMB 配置文件 /etc/samba/smb.conf 内容:
[global] min protocol = SMB2 max protocol = SMB3
[server] path = /mnt read only = no browsable = yes guest ok = yes force user = smbuser🤖 QQ 机器人服务 (QBot/NapCat)
WARNING当前暂不使用 QQ 机器人功能
# 切换到临时文件系统cd /mnt/tmpfs
# 复制安装包并安装sudo cp QQ.deb .sudo bash NapCat-Installer.sh
# 设置权限sudo chmod -R 775 /optNOTEWebUI 配置说明
添加 WebSocket 客户端: ws://localhost:2536/OneBotv11
🐘 PHP 环境支持
WARNING当前暂不使用 PHP 支持
# 添加 PHP PPA 仓库sudo add-apt-repository ppa:ondrej/php# 按回车确认
# 安装 PHP 和 FPMsudo apt install php php-fpm
# 配置 PHPsudo cp /path2/php.ini /etc/php/8.3/cli/php.inisudo cp /path2/php.ini /etc/php/8.3/fpm/php.ini
# PHP-FPM 会自动注册为系统服务systemctl status php8.3-fpm📊 服务状态检查
配置完成后,使用以下命令验证服务状态:
systemctl status randpic fuwari alist nginx ddns-go所有服务应显示为 active (running) 状态。
分享
如果这篇文章对你有帮助,欢迎分享给更多人!