Linux 服务器环境初始化
记录在新装的 Linux 服务器(CentOS / Debian / Ubuntu)上从零搭建开发与部署环境的过程。
1. 安装 Node.js 和 npm
请参考 Node.js 官网。
通过 nodesource 安装
CentOS / RHEL:
bash
curl -fsSL https://rpm.nodesource.com/setup_16.x | sudo -E bash -
sudo yum install -y nodejsDebian / Ubuntu:
bash
curl -fsSL https://deb.nodesource.com/setup_16.x | bash - && \
apt-get install -y nodejsTIP
setup_16 中的 16 是 Node.js 主版本号,按需替换。
卸载
CentOS / RHEL:
bash
yum remove nodejs && \
rm -r /etc/yum.repos.d/nodesource*.repo && \
yum clean allDebian / Ubuntu:
bash
apt-get purge nodejs && \
rm -r /etc/apt/sources.list.d/nodesource.list && \
rm -r /etc/apt/keyrings/nodesource.gpg2. 安装 Visual Studio Code
配合 SSH 远程开发可参考 SSH 跨平台配置指南。
3. 安装 Git
请参考 Git 官网。
4. 安装 pnpm
请参考 pnpm 官网。
bash
npm install -g pnpm5. 申请免费 HTTPS 证书(certbot)
用 certbot 申请免费的域名证书,比 acme.sh 更易上手。
certbot 是 EFF 赞助的开源工具,用于自动化管理和部署 SSL/TLS 证书。它能自动与 Let's Encrypt 通信获取免费证书,让网站管理员轻松获取、部署和更新证书,确保网站安全性。
certbot 支持 Apache、Nginx 等常见 Web 服务器,并提供通用插件系统。
SSL 证书用于加密网站与用户之间的传输数据;目前 TLS 1.3 是最新版本。HTTPS 默认走 443 端口。
安装
Debian / Ubuntu:
bash
apt update -y && apt install -y certbotCentOS:
bash
yum -y update && yum -y install certbotAlpine:
bash
apk update && apk add certbot申请证书
确认 80 与 443 端口未被占用:
bash
curl -s ipv4.ip.sbbash
certbot certonly --standalone -d your-domain.com --email your@email.com --agree-tos --no-eff-email --force-renewal证书存放目录:
bash
ls /etc/letsencrypt/live/自动续签
下载续签脚本:
bash
curl -O https://raw.githubusercontent.com/kejilion/sh/main/auto_cert_renewal-1.sh
chmod +x auto_cert_renewal-1.sh加入 crontab 每天执行:
bash
echo "0 0 * * * cd ~ && ./auto_cert_renewal-1.sh" | crontab -
crontab -eDNS 方式申请(国内可用)
bash
certbot certonly --manual --preferred-challenges dns -d your-domain.com到域名后台添加一条 TXT 记录,解析 5 分钟后回 SSH 终端回车继续。