温馨提示×

CentOS从零开始如何设置虚拟主机

小樊
42
2025-11-24 02:53:21
栏目: 云计算

从零开始设置 CentOS 虚拟主机

一 环境准备与安装

  • 更新系统并安装 Web 服务(以 Apache httpd 为例):
    • 安装:sudo yum install httpd -y
    • 启动并设置开机自启:sudo systemctl start httpd && sudo systemctl enable httpd
    • 检查状态:sudo systemctl status httpd(看到 active (running) 即正常)
  • 防火墙放行 HTTP/HTTPS
    • sudo firewall-cmd --permanent --add-service=http
    • sudo firewall-cmd --permanent --add-service=https
    • sudo firewall-cmd --reload
  • 说明:若你更偏好 Nginx,也可用 sudo yum install nginx 安装,后续按 Nginx 的 server 配置方式创建虚拟主机。

二 基于域名的虚拟主机 Apache 版

  • 创建站点目录与测试页:
    • sudo mkdir -p /var/www/example.com/html
    • echo "<h1>Welcome to example.com</h1>" | sudo tee /var/www/example.com/html/index.html
    • 权限:sudo chown -R apache:apache /var/www/example.com && sudo chmod -R 755 /var/www/example.com
  • 新建虚拟主机配置(文件后缀必须为 .conf,放在 /etc/httpd/conf.d/):
    • sudo vi /etc/httpd/conf.d/example.com.conf
    • 内容示例:
      <VirtualHost *:80>
          ServerAdmin webmaster@example.com
          DocumentRoot /var/www/example.com/html
          ServerName example.com
          ServerAlias www.example.com
          ErrorLog /var/log/httpd/example.com-error.log
          CustomLog /var/log/httpd/example.com-access.log combined
      
          <Directory /var/www/example.com/html>
              Options Indexes FollowSymLinks
              AllowOverride All
              Require all granted
          </Directory>
      </VirtualHost>
      
  • 语法检查与生效:
    • 检查:sudo httpd -t(出现 Syntax OK 再继续)
    • 重启:sudo systemctl restart httpd
  • 说明:如需基于 IP端口 的虚拟主机,可在主配置中增加 Listen 端口 并为不同 ServerName/端口 分别写 <VirtualHost> 段。

三 基于域名的虚拟主机 Nginx 版

  • 安装并启动 Nginx:
    • sudo yum install nginx -y
    • sudo systemctl start nginx && sudo systemctl enable nginx
  • 创建站点目录与测试页(同上,目录可自定义,示例用 /var/www/example.com/html
  • 新建站点配置(放在 /etc/nginx/conf.d/):
    • sudo vi /etc/nginx/conf.d/example.com.conf
    • 内容示例:
      server {
          listen 80;
          server_name example.com www.example.com;
      
          root /var/www/example.com/html;
          index index.html index.htm;
      
          location / {
              try_files $uri $uri/ =404;
          }
      
          error_log /var/log/nginx/example.com-error.log;
          access_log /var/log/nginx/example.com-access.log;
      }
      
  • 语法检查与生效:
    • 检查:sudo nginx -t
    • 重启:sudo systemctl reload nginx(或 restart

四 DNS 解析与本地测试

  • 生产环境:在域名注册商控制台添加 A 记录,将 example.comwww.example.com 指向服务器 公网 IP
  • 本地测试(无公网域名时):在本机或客户端编辑 /etc/hosts
    • 添加:服务器IP example.com www.example.com
    • 浏览器访问:http://example.com 应看到你的测试页。

五 常见问题与进阶

  • 403 Forbidden:检查目录权限与 <Directory> 配置,确保有 Require all granted,并且目录属主为 apache:apache
  • 配置不生效:执行 httpd -t(Apache)或 nginx -t(Nginx)查看语法;确认站点配置位于正确目录(Apache 为 /etc/httpd/conf.d/*.conf;Nginx 为 /etc/nginx/conf.d/*.conf),然后重启服务。
  • 端口未放行或被占用:确认 firewall-cmd 已放行 80/443,用 ss -tulpen | grep ':80\|:443' 检查端口占用。
  • 启用 HTTPS(推荐):使用 Let’s EncryptCertbot
    • 安装:sudo yum install certbot python2-certbot-apache(Apache)
    • 获取并自动配置证书:sudo certbot --apache -d example.com -d www.example.com
    • 按提示选择是否重定向 HTTP→HTTPS,证书将自动续期。

0