奋斗
努力

腾讯云服务器 centos 新建站点?

云计算

在腾讯云 CentOS 服务器上新建站点(如 Nginx 或 Apache)的步骤如下:


一、准备工作

  1. 登录服务器

    ssh root@你的服务器IP
  2. 更新系统

    yum update -y

二、安装 Web 服务器

选项1:Nginx

  1. 安装 Nginx:

    yum install nginx -y
  2. 启动并设置开机自启:

    systemctl start nginx
    systemctl enable nginx
  3. 验证安装:
    浏览器访问 http://你的服务器IP,看到 Nginx 欢迎页即成功。

选项2:Apache

  1. 安装 Apache:

    yum install httpd -y
  2. 启动并设置开机自启:

    systemctl start httpd
    systemctl enable httpd
  3. 验证安装:
    访问 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 配置
  1. 创建配置文件:

    vi /etc/nginx/conf.d/你的域名.conf
  2. 写入以下内容(替换 你的域名服务器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;
       }
    }
  3. 测试配置并重启:

    nginx -t   # 检查语法
    systemctl restart nginx
Apache 配置
  1. 创建配置文件:

    vi /etc/httpd/conf.d/你的域名.conf
  2. 写入以下内容:

    <VirtualHost *:80>
       ServerName 你的域名
       ServerAlias www.你的域名
       DocumentRoot /var/www/你的域名/public_html
       ErrorLog /var/log/httpd/你的域名.error.log
       CustomLog /var/log/httpd/你的域名.access.log combined
    </VirtualHost>
  3. 重启 Apache:

    systemctl restart httpd

四、域名解析(可选)

  1. 在域名服务商(如腾讯云 DNS)添加 A 记录,将域名指向服务器 IP。
  2. 测试域名解析是否生效:
    ping 你的域名

五、防火墙设置

  1. 放行 HTTP/HTTPS 流量:
    firewall-cmd --permanent --add-service=http
    firewall-cmd --permanent --add-service=https
    firewall-cmd --reload

六、HTTPS 配置(推荐)

使用 Let’s Encrypt 免费证书:

  1. 安装 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.你的域名
  2. 证书会自动续期。

七、验证站点

浏览器访问 http://你的域名https://你的域名,看到测试页面即成功。


常见问题

  1. 403 Forbidden:检查目录权限和 SELinux 状态:

    chmod 755 /var/www
    setenforce 0   # 临时关闭 SELinux
  2. Nginx/Apache 无法启动:检查日志:

    journalctl -xe

按需调整配置后,你的站点即可正常运行!

未经允许不得转载:云服务器 » 腾讯云服务器 centos 新建站点?