在腾讯云 CentOS 服务器上新建站点(如 Nginx 或 Apache)的步骤如下:
一、准备工作
-
登录服务器
ssh root@你的服务器IP -
更新系统
yum update -y
二、安装 Web 服务器
选项1:Nginx
-
安装 Nginx:
yum install nginx -y -
启动并设置开机自启:
systemctl start nginx systemctl enable nginx -
验证安装:
浏览器访问http://你的服务器IP,看到 Nginx 欢迎页即成功。
选项2:Apache
-
安装 Apache:
yum install httpd -y -
启动并设置开机自启:
systemctl start httpd systemctl enable httpd -
验证安装:
访问http://你的服务器IP,看到 Apache 测试页即成功。
三、配置站点
1. 创建站点目录
mkdir -p /var/www/你的域名/public_html
chown -R apache:apache /var/www/你的域名/public_html # Apache 用户
# 或
chown -R nginx:nginx /var/www/你的域名/public_html # Nginx 用户
2. 添加测试页面
echo "<h1>Welcome to 你的域名</h1>" > /var/www/你的域名/public_html/index.html
3. 配置虚拟主机
Nginx 配置
-
创建配置文件:
vi /etc/nginx/conf.d/你的域名.conf -
写入以下内容(替换
你的域名和服务器IP):server { listen 80; server_name 你的域名 www.你的域名; root /var/www/你的域名/public_html; index index.html; access_log /var/log/nginx/你的域名.access.log; error_log /var/log/nginx/你的域名.error.log; location / { try_files $uri $uri/ =404; } } -
测试配置并重启:
nginx -t # 检查语法 systemctl restart nginx
Apache 配置
-
创建配置文件:
vi /etc/httpd/conf.d/你的域名.conf -
写入以下内容:
<VirtualHost *:80> ServerName 你的域名 ServerAlias www.你的域名 DocumentRoot /var/www/你的域名/public_html ErrorLog /var/log/httpd/你的域名.error.log CustomLog /var/log/httpd/你的域名.access.log combined </VirtualHost> -
重启 Apache:
systemctl restart httpd
四、域名解析(可选)
- 在域名服务商(如腾讯云 DNS)添加 A 记录,将域名指向服务器 IP。
- 测试域名解析是否生效:
ping 你的域名
五、防火墙设置
- 放行 HTTP/HTTPS 流量:
firewall-cmd --permanent --add-service=http firewall-cmd --permanent --add-service=https firewall-cmd --reload
六、HTTPS 配置(推荐)
使用 Let’s Encrypt 免费证书:
-
安装 Certbot:
# Nginx yum install certbot python3-certbot-nginx -y certbot --nginx -d 你的域名 -d www.你的域名 # Apache yum install certbot python3-certbot-apache -y certbot --apache -d 你的域名 -d www.你的域名 - 证书会自动续期。
七、验证站点
浏览器访问 http://你的域名 或 https://你的域名,看到测试页面即成功。
常见问题
-
403 Forbidden:检查目录权限和
SELinux状态:chmod 755 /var/www setenforce 0 # 临时关闭 SELinux -
Nginx/Apache 无法启动:检查日志:
journalctl -xe
按需调整配置后,你的站点即可正常运行!
云服务器