在腾讯云轻量应用服务器上使用CentOS系统搭建网站的详细步骤如下:
一、准备工作
-
购买服务器
- 进入腾讯云官网,购买轻量应用服务器,选择CentOS系统(推荐CentOS 7或8)。
- 记录服务器的公网IP、用户名(默认
root)和密码(或SSH密钥)。
-
连接服务器
- Windows用户:使用PuTTY或Xshell。
- Mac/Linux用户:直接使用终端:
ssh root@你的服务器IP - 输入密码或加载密钥登录。
二、安装必要环境
1. 更新系统
yum update -y
2. 安装Web服务器(Nginx/Apache)
-
Nginx(推荐):
yum install nginx -y systemctl start nginx systemctl enable nginx访问
http://服务器IP应看到Nginx欢迎页。 -
Apache:
yum install httpd -y systemctl start httpd systemctl enable httpd访问
http://服务器IP测试。
3. 安装数据库(MySQL/MariaDB)
- MySQL 5.7:
yum install mysql-server -y systemctl start mysqld systemctl enable mysqld mysql_secure_installation # 运行安全配置脚本,设置root密码 - MariaDB:
yum install mariadb-server -y systemctl start mariadb systemctl enable mariadb mysql_secure_installation
4. 安装PHP(可选)
若需PHP支持(如WordPress):
yum install php php-mysql php-fpm -y
systemctl start php-fpm
systemctl enable php-fpm
修改Nginx/Apache配置以支持PHP(见下文)。
三、配置网站
1. 上传网站文件
- 方法1:使用SFTP工具(如FileZilla)连接服务器,上传文件到默认目录:
- Nginx:
/usr/share/nginx/html/ - Apache:
/var/www/html/
- Nginx:
- 方法2:通过Git克隆代码:
yum install git -y git clone 你的仓库地址 /var/www/html/
2. 配置域名(需已备案)
- 在腾讯云控制台将域名解析到服务器IP。
- 修改Nginx/Apache配置:
- Nginx:编辑
/etc/nginx/conf.d/yourdomain.conf:server { listen 80; server_name yourdomain.com; root /var/www/html; index index.html index.php; location / { try_files $uri $uri/ =404; } location ~ .php$ { fastcgi_pass 127.0.0.1:9000; include fastcgi_params; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; } } - Apache:编辑
/etc/httpd/conf.d/yourdomain.conf:<VirtualHost *:80> ServerName yourdomain.com DocumentRoot /var/www/html <Directory /var/www/html> AllowOverride All </Directory> </VirtualHost>
- Nginx:编辑
-
重启服务:
# Nginx nginx -t # 测试配置 systemctl restart nginx # Apache apachectl configtest systemctl restart httpd
3. 配置HTTPS(可选)
使用腾讯云SSL证书:
yum install certbot -y
certbot --nginx -d yourdomain.com
自动续签:
echo "0 0,12 * * * root certbot renew -q" | tee -a /etc/crontab
四、防火墙与安全
-
放行端口
firewall-cmd --permanent --add-service=http firewall-cmd --permanent --add-service=https firewall-cmd --reload -
禁用root远程登录(可选)
useradd yourusername passwd yourusername usermod -aG wheel yourusername sed -i 's/PermitRootLogin yes/PermitRootLogin no/g' /etc/ssh/sshd_config systemctl restart sshd
五、测试网站
- 访问
http://你的域名或http://服务器IP,检查是否正常显示。 - 若使用PHP,创建
info.php测试:echo "<?php phpinfo(); ?>" > /var/www/html/info.php访问
http://服务器IP/info.php。
常见问题
- 403 Forbidden:检查文件权限:
chown -R nginx:nginx /var/www/html/ # Nginx chown -R apache:apache /var/www/html/ # Apache - 数据库连接失败:确保MySQL允许远程访问(需配置
bind-address)。
按照以上步骤,即可在腾讯云CentOS服务器上成功部署网站。如需更复杂功能(如Node.js、Python等),可进一步安装对应环境。
云服务器