温馨提示×

centos apache怎样绑定域名

小樊
39
2026-09-06 02:30:29
栏目: 云计算

CentOS 上通过 Apache 绑定域名,通常需要完成以下几个步骤(以 CentOS 7/8 + Apache 2.4 为例):


一、前提条件

  1. 域名已解析到服务器 IP
    • 在你的域名服务商处,添加 A 记录:
      域名 → 服务器公网IP
      
  2. Apache 已安装
    httpd -v
    
    如未安装:
    yum install httpd -y
    systemctl start httpd
    systemctl enable httpd
    

二、创建网站目录

mkdir -p /var/www/example.com
echo "Hello example.com" > /var/www/example.com/index.html

三、创建 Apache 虚拟主机配置

1️⃣ 新建配置文件

vi /etc/httpd/conf.d/example.com.conf

2️⃣ 写入以下内容

<VirtualHost *:80>
    ServerName example.com
    ServerAlias www.example.com
    DocumentRoot /var/www/example.com

    <Directory /var/www/example.com>
        AllowOverride All
        Require all granted
    </Directory>

    ErrorLog /var/log/httpd/example.com-error.log
    CustomLog /var/log/httpd/example.com-access.log combined
</VirtualHost>

⚠️ 把 example.com 换成你的真实域名


四、检查并重启 Apache

httpd -t
systemctl restart httpd

五、防火墙放行 80 端口

firewall-cmd --permanent --add-service=http
firewall-cmd --reload

如果是云服务器,还要在 安全组 放行 80 端口。


六、测试访问

浏览器访问:

http://example.com

七、常见扩展

✅ 绑定多个域名

为每个域名新建一个 .conf 文件即可。

✅ 使用 HTTPS(推荐)

yum install mod_ssl certbot -y
certbot --apache -d example.com -d www.example.com

✅ 本地测试(无域名)

修改本地 hosts 文件:

服务器IP example.com

如果你愿意,可以告诉我:

  • CentOS 版本(7 / 8 / Stream)
  • Apache 是否已安装
  • 是否需要 HTTPS / 多域名 / 子目录

我可以给你更精确的配置。

0