如何配置服务器(作为个人服务器使用)

如果你的服务器是 Ubuntu,推荐按这条路线做:

登录服务器
→ 创建普通用户
→ 更新系统
→ 配置防火墙
→ 安装 Node.js / Git
→ 上传项目
→ 安装依赖并测试
→ 用 PM2 常驻运行
→ 安装 Nginx 做反向代理
→ 域名解析到服务器
→ 配置 HTTPS
→ 做基础安全加固

下面我按实际操作顺序给你。

1. 先 SSH 登录服务器

你的云厂商应该会给你一个公网 IP,例如:

203.0.113.10

Windows PowerShell 里:

ssh root@203.0.113.10

如果你创建服务器时使用 SSH Key,第一次可能会看到:

Are you sure you want to continue connecting?

输入:

yes

成功以后,你会看到类似:

root@myserver:~#

这说明你已经进入服务器。


2. 更新服务器

先做:

apt update
apt upgrade -y

然后安装一些常用工具:

apt install -y git curl ufw nginx

现在服务器就有:

Git
curl
UFW 防火墙
Nginx

3. 不要长期直接用 root

创建一个自己的管理账户,比如:

adduser deploy

给它 sudo 权限:

usermod -aG sudo deploy

以后尽量使用:

ssh deploy@你的服务器IP

而不是一直用:

root

这是非常值得养成的习惯。


4. 设置防火墙

因为你的网站最终只需要:

22    SSH
80    HTTP
443   HTTPS

所以:

ufw allow OpenSSH
ufw allow 'Nginx Full'
ufw enable

检查:

ufw status

应该类似:

22/tcp        ALLOW
80/tcp        ALLOW
443/tcp       ALLOW

特别注意:一定要先允许 SSH,再启用 UFW。

否则有可能把自己挡在服务器外面。


5. 安装 Node.js

不要直接:

apt install nodejs

因为 Ubuntu 自带仓库里的 Node.js 版本有时比较旧。

个人服务器比较适合使用 nvm 管理 Node.js。

先切换到你的普通用户:

su - deploy

安装 nvm:

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/master/install.sh | bash

重新打开终端,或者:

source ~/.bashrc

然后:

nvm install --lts

检查:

node -v
npm -v

看到版本号就成功了。


6. 把你的 Node.js 项目放到服务器

最推荐:

电脑
↓
GitHub
↓
服务器 git clone

例如你的项目已经在:

github.com/username/my-website

服务器上执行:

cd ~
git clone https://github.com/username/my-website.git

然后:

cd my-website

你的项目可能类似:

my-website/
├── package.json
├── package-lock.json
├── public/
├── src/
└── server.js

安装依赖:

npm ci

如果没有 package-lock.json

npm install

7. 先直接测试 Node.js

假设你的入口是:

server.js

运行:

node server.js

你的程序里最好写:

const PORT = process.env.PORT || 3000;

app.listen(PORT, '127.0.0.1', () => {
    console.log(`Server running on port ${PORT}`);
});

也就是说:

Node.js
↓
127.0.0.1:3000

不直接向互联网公开 3000 端口。

如果看到:

Server running on port 3000

说明 Node 项目本身没有问题。

按:

Ctrl + C

停止。


8. 用 PM2 让 Node.js 一直运行

问题是:

node server.js

只要你退出 SSH,程序通常就没了。

所以安装 PM2:

npm install -g pm2

运行:

pm2 start server.js --name my-website

查看:

pm2 status

应该出现类似:

┌────┬────────────┬────────┐
│ id │ name       │ status │
├────┼────────────┼────────┤
│ 0  │ my-website │ online │
└────┴────────────┴────────┘

然后:

pm2 save
pm2 startup

pm2 startup 会输出一条命令,再把它复制执行一次。

这样服务器重启以后:

Node.js

也会自动启动。


9. 配置 Nginx

你的结构应该是:

用户
 ↓
Nginx :80 / :443
 ↓
Node.js :3000

创建配置:

sudo nano /etc/nginx/sites-available/my-website

写:

server {
    listen 80;
    listen [::]:80;

    server_name example.com www.example.com;

    location / {
        proxy_pass http://127.0.0.1:3000;

        proxy_http_version 1.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;

        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
    }
}

把:

example.com

换成你的域名。

如果你现在还没有域名,可以暂时:

server_name _;

启用配置:

sudo ln -s /etc/nginx/sites-available/my-website /etc/nginx/sites-enabled/

可以删除默认页面:

sudo rm /etc/nginx/sites-enabled/default

然后测试:

sudo nginx -t

如果看到:

syntax is ok
test is successful

执行:

sudo systemctl reload nginx

10. 现在可以用 IP 访问了

浏览器输入:

http://你的服务器IP

例如:

http://203.0.113.10

如果一切正确:

浏览器
 ↓
203.0.113.10:80
 ↓
Nginx
 ↓
localhost:3000
 ↓
Node.js

你就应该能看到自己的网页。

这一步非常重要。

建议先保证 IP 能正常打开网站,再配置域名。


11. 买一个域名

例如你买:

yourname.com

在 Cloudflare、Porkbun 或其他域名注册商里管理 DNS。

添加:

Type    Name    Content

A       @       你的服务器IPv4
A       www     你的服务器IPv4

例如:

A   @      203.0.113.10
A   www    203.0.113.10

于是:

yourname.com
       ↓
DNS
       ↓
203.0.113.10
       ↓
Nginx
       ↓
Node.js

等 DNS 生效以后,你就可以:

http://yourname.com

打开网站。


12. 配置 HTTPS

现在一般不应该让正式网站停留在:

http://yourname.com

应该变成:

https://yourname.com

最简单是使用 Let's Encrypt + Certbot。

Ubuntu 可以安装:

sudo apt install certbot python3-certbot-nginx

然后:

sudo certbot --nginx -d yourname.com -d www.yourname.com

按照提示操作。

成功以后 Certbot 会自动修改 Nginx。

最终:

https://yourname.com

就有 HTTPS 了。

而且证书可以自动续期。

检查:

sudo systemctl status certbot.timer

13. 环境变量不要放进 GitHub

例如数据库密码:

DATABASE_URL=...
JWT_SECRET=...
API_KEY=...

不要写在:

const password = "123456";

更不要提交到 GitHub。

服务器可以建立:

nano .env

例如:

NODE_ENV=production
PORT=3000

DATABASE_URL=...
SESSION_SECRET=...

项目里加入:

.gitignore

并确保包含:

.env
node_modules/

如果使用 PM2,你也可以通过 ecosystem 文件管理生产环境配置。


14. 以后更新网站很简单

例如你在自己电脑上改代码:

VS Code
↓
git commit
↓
git push
↓
GitHub

服务器:

cd ~/my-website

git pull

npm ci

pm2 restart my-website

网站就更新了。

所以最基本的发布流程其实只有:

git pull
npm ci
pm2 restart my-website

如果项目需要构建:

git pull
npm ci
npm run build
pm2 restart my-website

以后还可以进一步用 GitHub Actions 自动完成。