在 CentOS 上通过 Apache 设置域名,一般分为以下几个步骤:
(以 CentOS 7/8 + Apache(httpd) 为例,假设你的域名是 example.com)
CentOS 已安装 Apache
yum install httpd -y # CentOS 7
dnf install httpd -y # CentOS 8
域名已解析到服务器公网 IP
example.com → 服务器IP
www.example.com → 服务器IP
服务器防火墙放行 80 / 443 端口
firewall-cmd --permanent --add-service=http
firewall-cmd --permanent --add-service=https
firewall-cmd --reload
mkdir -p /var/www/example.com
创建测试页面:
echo "Welcome to example.com" > /var/www/example.com/index.html
vi /etc/httpd/conf.d/example.com.conf
<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>
httpd -t
✅ 显示 Syntax OK 表示配置正确
systemctl restart httpd
systemctl enable httpd
浏览器访问:
http://example.com
✅ 如果看到页面内容,说明域名已成功绑定 Apache
welcome.conf 冲突:rm -f /etc/httpd/conf.d/welcome.conf
systemctl restart httpd
ping example.com
yum install certbot python3-certbot-apache -y
certbot --apache -d example.com -d www.example.com
确保 Apache 支持:
httpd -M | grep rewrite
并开启:
AllowOverride All
如果你能提供:
我可以给你更精确的配置方式。