🗣 Wd提问: 搬瓦工vps搭建网站
🤖 Ai回答: 在搬瓦工(BandwagonHost)VPS上搭建网站的完整流程如下:
一、准备工作
1、购买VPS
选择搬瓦工套餐(建议至少1核CPU/512MB内存)
记录IP地址、SSH端口、root密码(或SSH密钥)
2、选择操作系统
推荐:CentOS 7/8 或 Ubuntu 20.04/22.04(控制面板可重装)
二、连接VPS
1、SSH登录
bash
ssh root@your_server_ip -p 22 替换IP和端口
2、更新系统
bash
CentOS
yum update -y
Ubuntu
apt update && apt upgrade -y
三、安装环境(LNMP为例)
1、安装Nginx
bash
Ubuntu
apt install nginx -y
systemctl enable nginx
CentOS
yum install epel-release -y
yum install nginx -y
systemctl enable nginx
2、安装MySQL/MariaDB
bash
Ubuntu
apt install mariadb-server -y
mysql_secure_installation 设置root密码并加固
CentOS
yum install mariadb-server -y
systemctl enable mariadb
mysql_secure_installation
3、安装PHP
bash
Ubuntu
apt install php-fpm php-mysql -y
CentOS
yum install php-fpm php-mysql -y
systemctl enable php-fpm
四、配置网站
1、创建网站目录
bash
mkdir /var/www/your_domain
chown -R www-data:www-data /var/www/your_domain Ubuntu
chown -R apache:apache /var/www/your_domain CentOS
2、配置Nginx站点
编辑 `/etc/nginx/conf.d/your_domain.conf`:
nginx
server {
listen 80;
server_name your_domain.com www.your_domain.com;
root /var/www/your_domain;
index index.php index.html;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
include fastcgi_params;
fastcgi_pass unix:/var/run/php/php-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
重启Nginx:
bash
systemctl restart nginx
五、部署网站文件
1、上传代码
使用SFTP工具(如FileZilla)上传文件到 `/var/www/your_domain`
或通过Git克隆代码:
bash
apt install git -y
git clone https://github.com/your-repo.git /var/www/your_domain
2、测试PHP
创建 `info.php` 文件:
php
<?php phpinfo(); ?
访问 `http://your_server_ip/info.php` 检查PHP配置。
六、域名与SSL证书
1、绑定域名
在域名注册商处将域名A记录指向VPS的IP地址。
2、安装SSL证书(Let's Encrypt)
bash
安装Certbot
apt install certbot python3-certbot-nginx -y Ubuntu
yum install certbot python3-certbot-nginx -y CentOS
获取证书
certbot nginx -d your_domain.com -d www.your_domain.com
证书自动续期:
bash
certbot renew dry-run
七、防火墙配置
bash
Ubuntu
ufw allow 80/tcp
ufw allow 443/tcp
ufw enable
CentOS
firewall-cmd permanent add-service=http
firewall-cmd permanent add-service=https
firewall-cmd reload
八、常见问题排查
403 Forbidden:检查目录权限和Nginx配置的 `root` 路径。
502 Bad Gateway:确保PHP-FPM运行 (`systemctl status php-fpm`)。
数据库连接失败:确认MySQL用户权限及防火墙放行3306端口。
九、进阶优化
1、启用OPcache:加速PHP执行。
2、配置Redis缓存:减少数据库压力。
3、设置Cron任务:定时执行脚本(如WordPress定时发布)。
通过以上步骤,你的网站已部署完成!如需搭建特定程序(如WordPress),只需下载源码并配置数据库即可。
📣 商家广告:





0
IP地址: 137.70.200.22
搜索次数: 6
提问时间: 2025-12-16 18:56:03
本站所有
❓
问答
均由Ai自动分析整理,内容仅供参考,若有误差请用“联系”里面信息通知我们人工修改或删除。
本站由
🟢
豌豆Ai
提供技术支持,使用的最新版:
豌豆Ai站群搜索引擎系统 V.25.10.25
搭建本站。