在腾讯云轻量服务器上使用 CentOS 部署网站的详细步骤如下:
1. 准备工作
- 购买服务器:确保已购买腾讯云轻量服务器,选择 CentOS 系统镜像(如 CentOS 7/8/9)。
- 登录服务器:通过 SSH 连接服务器:
ssh root@你的服务器IP
2. 更新系统
yum update -y # 更新所有软件包
yum install -y epel-release # 安装EPEL扩展源(可选)
3. 安装必要服务
3.1 安装 Web 服务器(Nginx/Apache)
-
Nginx(推荐):
yum install -y nginx systemctl start nginx systemctl enable nginx访问
http://服务器IP测试是否显示 Nginx 欢迎页。 -
Apache:
yum install -y httpd systemctl start httpd systemctl enable httpd访问
http://服务器IP测试。
3.2 安装数据库(MySQL/MariaDB)
- MariaDB(MySQL 替代):
yum install -y mariadb-server mariadb systemctl start mariadb systemctl enable mariadb mysql_secure_installation # 运行安全配置脚本(设置密码等)
3.3 安装 PHP(可选)
yum install -y php php-mysql php-fpm
systemctl start php-fpm
systemctl enable php-fpm
测试 PHP:创建 /var/www/html/info.php 内容为 <?php phpinfo(); ?>,访问 http://服务器IP/info.php。
4. 配置网站
4.1 上传网站文件
- 使用
scp或 SFTP 工具上传代码到/var/www/html/(Apache)或/usr/share/nginx/html/(Nginx)。scp -r 本地代码目录 root@服务器IP:/var/www/html/
4.2 配置虚拟主机(以 Nginx 为例)
- 创建配置文件:
vim /etc/nginx/conf.d/yourdomain.conf -
示例配置:
server { listen 80; server_name yourdomain.com www.yourdomain.com; root /var/www/yourdomain; index index.html index.php; location / { try_files $uri $uri/ =404; } location ~ .php$ { fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; include fastcgi_params; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; } } - 测试并重载 Nginx:
nginx -t # 检查语法 systemctl reload nginx
5. 域名与安全
5.1 绑定域名
- 在域名控制台将域名解析到服务器 IP。
- 确保服务器安全组/防火墙放行
80(HTTP)和443(HTTPS)端口。
5.2 安装 SSL 证书(HTTPS)
- 使用 Let’s Encrypt 免费证书:
yum install -y certbot python3-certbot-nginx certbot --nginx -d yourdomain.com -d www.yourdomain.com certbot renew --dry-run # 测试自动续期
6. 常见问题排查
- 权限问题:确保网站目录权限正确:
chown -R nginx:nginx /var/www/yourdomain chmod -R 755 /var/www - 服务未启动:检查服务状态:
systemctl status nginx/mariadb/php-fpm - 防火墙:放行端口:
firewall-cmd --add-service=http --permanent firewall-cmd --add-service=https --permanent firewall-cmd --reload
7. 进阶优化
- 性能调优:调整 Nginx/Apache 和 MySQL 配置。
- 备份:定期备份网站文件和数据库。
- 监控:使用
htop、netdata等工具监控服务器状态。
按照以上步骤操作后,你的网站应该已成功部署。如果遇到具体问题,可结合日志(如 /var/log/nginx/error.log)进一步排查。
轻量云Cloud