上一篇
购买VPS后,通过SSH连接服务器,安装必要的网站运行环境(如LAMP/LEMP栈),配置域名解析指向VPS的IP地址,上传网站文件到指定目录,设置数据库(如需),最后进行安全配置和测试即可访问网站。
如何用VPS虚拟主机搭建网站(详细指南)
为什么选择VPS搭建网站?
VPS(Virtual Private Server)相比共享主机拥有独立资源、更高安全性和完全控制权,当您的网站流量增长或需要特殊环境配置时,VPS能提供更稳定的运行环境,根据W3Techs统计,全球超过35%的网站运行在VPS或独立服务器上。
搭建前的准备工作
-
选购VPS服务器
- 推荐服务商:阿里云、酷盾、DigitalOcean(国内访问选国内服务商)
- 配置建议:
- 小型博客:1核CPU/1GB内存/25GB SSD
- 电商网站:2核CPU/4GB内存/50GB SSD
- 系统选择:Ubuntu 22.04 LTS(新手友好)或CentOS 7
-
注册域名
- 在阿里云/Godaddy购买域名(如
yourdomain.com) - 确保完成实名认证(国内法规要求)
- 在阿里云/Godaddy购买域名(如
-
必备工具

- SSH客户端:PuTTY(Windows)或终端(Mac/Linux)
- FTP工具:FileZilla(文件传输)
- 文本编辑器:VS Code(配置修改)
详细搭建步骤(以Ubuntu+Nginx为例)
步骤1:连接VPS服务器
ssh root@your_server_ip # 替换为你的VPS实际IP 输入购买时设置的root密码
步骤2:更新系统并创建安全账户
apt update && apt upgrade -y adduser yourusername # 创建新用户 usermod -aG sudo yourusername # 赋予管理员权限
步骤3:安装网站运行环境(LEMP栈)
# 安装Nginx apt install nginx -y systemctl start nginx # 安装MySQL数据库 apt install mysql-server -y mysql_secure_installation # 按提示设置root密码 # 安装PHP apt install php-fpm php-mysql -y
步骤4:配置网站目录
mkdir -p /var/www/yourdomain.com/html chown -R yourusername:yourusername /var/www/yourdomain.com chmod -R 755 /var/www
步骤5:创建Nginx配置文件
nano /etc/nginx/sites-available/yourdomain.com
server {
listen 80;
root /var/www/yourdomain.com/html;
index index.php index.html;
server_name yourdomain.com www.yourdomain.com;
location / {
try_files $uri $uri/ =404;
}
location ~ .php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php8.1-fpm.sock;
}
}
ln -s /etc/nginx/sites-available/yourdomain.com /etc/nginx/sites-enabled/ nginx -t # 测试配置 systemctl reload nginx
步骤6:部署网站程序
- 通过FileZilla上传WordPress等程序到
/var/www/yourdomain.com/html - 或使用命令行:
cd /var/www/yourdomain.com/html wget https://wordpress.org/latest.tar.gz tar -xzvf latest.tar.gz mv wordpress/* .
步骤7:绑定域名解析
- 在域名服务商控制台添加A记录:
@ → 你的VPS IP
www → 你的VPS IP - 等待DNS生效(通常10分钟-24小时)
步骤8:完成网站安装
访问 http://yourdomain.com 根据提示完成:
- 创建数据库:
mysql -u root -p→CREATE DATABASE wpdb; - 填写数据库名、用户名、密码
- 设置管理员账户
关键安全加固措施
-
防火墙配置
ufw allow OpenSSH ufw allow 'Nginx Full' ufw enable
-
SSL证书安装(强制HTTPS)

apt install certbot python3-certbot-nginx -y certbot --nginx -d yourdomain.com -d www.yourdomain.com
-
定期自动更新
apt install unattended-upgrades dpkg-reconfigure unattended-upgrades # 选择Yes
运维与优化建议
-
性能监控工具
- htop(实时资源查看):
apt install htop - Netdata(可视化监控):
bash <(curl -Ss https://my-netdata.io/kickstart.sh)
- htop(实时资源查看):
-
备份策略

# 每日自动备份数据库 crontab -e * 3 * * * mysqldump -u root -p密码 数据库名 > /backups/db_$(date +%F).sql
-
缓存加速方案
- 安装Redis:
apt install redis-server php-redis - WordPress推荐插件:W3 Total Cache或WP Super Cache
- 安装Redis:
常见问题排查
- 502 Bad Gateway:检查
php-fpm状态systemctl status php8.1-fpm - 数据库连接失败:确认MySQL用户权限
GRANT ALL ON wpdb.* TO 'user'@'localhost'; - 文件权限错误:
chown -R www-data:www-data /var/www/yourdomain.com
重要提示:操作前务必创建系统快照!国内网站需完成ICP备案(阿里云/酷盾提供备案入口),否则域名无法解析。
引用说明
- Nginx官方配置指南:nginx.org/en/docs/
- Let’s Encrypt证书文档:certbot.eff.org
- Ubuntu服务器手册:ubuntu.com/server/docs
- MySQL安全配置参考:dev.mysql.com/doc/refman/8.0/en
基于Linux基金会认证工程师(LFCE)操作规范编写,定期更新验证,实际部署请以服务商最新文档为准,技术问题建议查阅官方知识库或社区论坛。
