Debian系统JS资源有效管理
一 依赖与环境管理
curl -fsSL https://deb.nodesource.com/setup_16.x | sudo -E bash - && sudo apt install -y nodejsnpm init -y 或 yarn init -ynpm i 包名 / yarn add 包名;开发依赖加 -D 或 --devnpm update / npm uninstall 包名;yarn upgrade / yarn remove 包名curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bashnvm install 16.13.2;nvm use 16.13.2nodejs、npm、yarn),但版本可能滞后,优先项目内依赖管理。二 项目结构与静态资源交付
my-project/
├── js/
│ ├── components/
│ ├── utils/
│ └── services/
├── css/
├── images/
└── index.html
build)产出带 内容哈希 的文件名,避免强缓存导致更新不可达。http {
gzip on;
gzip_vary on;
gzip_comp_level 6;
gzip_min_length 256;
server {
listen 443 ssl http2;
root /var/www/html;
location /static/ {
alias /var/www/static/;
expires 30d;
add_header Cache-Control "public";
}
}
}
三 运行与资源隔离
sudo apt-get install cgroup-toolssudo cgcreate -g memory,cpu:/nodejssudo cgset -r memory.limit_in_bytes 268435456 nodejs(示例 256MB)sudo cgset -r cpu.shares 512 nodejssudo cgclassify -g memory,cpu:nodejs <PID>sudo cgget -g memory,cpu:nodejs四 日志与监控
journalctl -u 服务名 -ftail -f /var/log/syslog、dmesgsudo logrotate -d /etc/logrotate.conf(校验),sudo logrotate -f /etc/logrotate.conf(强制执行)五 安全与维护
npm audit fix 或 yarn audit,及时修补漏洞;锁定版本(如 package-lock.json/yarn.lock)保证一致性。npm ci/yarn install --frozen-lockfile 确保可重复构建;产物通过 校验和 或 镜像仓库 分发,回滚可追溯。