🗣 Wd提问: linux云服务器搭建
🤖 Ai回答: 在 Linux 云服务器上搭建服务是一个常见需求,具体步骤取决于你要搭建的服务类型(如 Web 服务器、数据库、应用后端等)。下面我将为你提供一个通用的 Linux 云服务器初始配置和常见服务(以 Nginx + Node.js 应用为例)的搭建流程。
一、准备工作
1、购买云服务器
选择主流云厂商(阿里云、腾讯云、华为云、AWS、Google Cloud、DigitalOcean 等)
操作系统建议:Ubuntu 22.04 LTS 或 CentOS Stream 9(本文以 Ubuntu 22.04 为例)
2、登录服务器
bash
ssh root@你的服务器公网IP
或使用密钥登录(更安全)
ssh -i ~/.ssh/your-key.pem ubuntu@公网IP
二、基础安全设置
1、更新系统
bash
sudo apt update && sudo apt upgrade -y
2、创建普通用户(避免直接使用 root)
bash
adduser yourname
usermod -aG sudo yourname 赋予 sudo 权限
3、配置 SSH 安全(可选但推荐)
编辑 `/etc/ssh/sshd_config`:
bash
sudo nano /etc/ssh/sshd_config
修改以下内容:
conf
PermitRootLogin no 禁止 root 登录
PasswordAuthentication no 禁用密码登录(使用密钥)
重启 SSH:
bash
sudo systemctl restart sshd
4、配置防火墙(UFW)
bash
sudo ufw allow OpenSSH
sudo ufw allow 80/tcp HTTP
sudo ufw allow 443/tcp HTTPS
sudo ufw enable
三、安装常用软件
1、安装 Git、curl、vim 等工具
bash
sudo apt install -y git curl vim htop unzip
2、安装 Node.js(以 Node.js 18 为例)
bash
curl -fsSL https://deb.nodesource.com/setup_18.x | sudo -E bash -
sudo apt install -y nodejs
node -v
npm -v
3、安装 Nginx(反向代理/Web 服务器)
bash
sudo apt install -y nginx
sudo systemctl start nginx
sudo systemctl enable nginx
访问 `http://你的服务器IP`,看到 Welcome to nginx! 表示成功。
四、部署一个简单的 Node.js 应用
1、创建项目目录
bash
mkdir -p /var/www/myapp
cd /var/www/myapp
2、创建简单应用(app.js)
js
// app.js
const http = require('http');
const server = http.createServer((req, res) = {
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end('Hello from Node.js on Linux Cloud Server!\n');
});
server.listen(3000, '127.0.0.1', () = {
console.log('Server running at http://127.0.0.1:3000/');
});
3、使用 PM2 守护进程运行(避免手动启动)
bash
sudo npm install -g pm2
pm2 start app.js name myapp
pm2 startup 设置开机自启
pm2 save
五、配置 Nginx 反向代理
编辑 Nginx 配置文件:
bash
sudo nano /etc/nginx/sites-available/myapp
内容如下:
nginx
server {
listen 80;
server_name your-domain.com; 或直接写服务器IP
location / {
proxy_pass http://127.0.0.1:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
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_cache_bypass $http_upgrade;
}
}
启用站点:
bash
sudo ln -s /etc/nginx/sites-available/myapp /etc/nginx/sites-enabled/
sudo nginx -t 测试配置
sudo systemctl reload nginx
现在访问 `http://你的服务器IP` 就能看到你的 Node.js 应用内容。
六、(可选)配置 HTTPS(使用 Let's Encrypt)
bash
sudo apt install -y certbot python3-certbot-nginx
sudo certbot nginx -d your-domain.com
按照提示操作,自动配置 HTTPS 并设置自动续期。
七、其他常见服务
| 服务类型 | 安装命令示例 |
|
| MySQL | `sudo apt install mysql-server` |
| PostgreSQL | `sudo apt install postgresql postgresql-contrib` |
| Redis | `sudo apt install redis-server` |
| Docker | `curl -fsSL https://get.docker.com | sh` |
总结
1、安全第一:禁用 root 登录、使用密钥、开防火墙。
2、使用非 root 用户 运行应用。
3、用 PM2/Nginx/systemd 管理服务。
4、定期更新系统:`sudo apt update && sudo apt upgrade`。
5、备份重要数据。
如果你有具体要搭建的服务(如 WordPress、Docker 容器、Python Flask、Java Spring Boot 等),可以告诉我,我会提供针对性教程。
📣 商家广告:





0
IP地址: 33.90.102.241
搜索次数: 0
提问时间: 2025-11-10 00:48:02
🛒 域名购买:
wd.chat
wangai.net
63.gold
il.gs
yw.gs
2.js.cn
836.net.cn
te.biz
lv.gs
zhang.la
❓️ 热门提问:
云计算的三种服务模式是
编程儿童网站
站群系统cdn加速
vps中心
单窗口独立ip
香港赛马场在哪
服服务器
香港优惠vps
电子商城的网站开发
查询域名属于谁
豌豆Ai站群搜索引擎系统
🌐 域名评估:
cc.jx.cn
mdrm.cn
vtol.plus
v.jl.cn
catl.hb.cn
oxvc.cn
b.nx.cn
tudouyun.cn
su7.zone
wz.chat
⛏ 最新挖掘:
凯里酸汤鱼
贵阳烤脑花
都匀毛尖茶
花溪牛肉粉
贵阳烤生蚝
贵阳烤豆腐
贵阳纸包鱼
贵阳特色菜
🖌 热门作画:
🤝 关于我们:
豌豆Ai
域名
建站
站群
留痕
推广
评估
源码
开发Ai
工具
日记
价格
加盟
广告
流量
留言
联系
🗨 加入群聊:
🔗 友情链接:
搜全网
ai作画
ai提问
🧰 站长工具:
Ai工具 whois查询 搜索
📢 温馨提示:本站所有问答由Ai自动创作,内容仅供参考,若有误差请用“联系”里面信息通知我们人工修改或删除。
👉 技术支持:本站由豌豆Ai提供技术支持,使用的最新版:《豌豆Ai站群搜索引擎系统 V.25.10.25》搭建本站。