Cómo cambiar el nombre de host (ServerName) en servidor Apache

El ServerName en Apache define el nombre de host principal (y puerto) con el que el servidor se identifica. Cambiarlo altera el dominio visible en redirecciones, errores, logs y virtual hosts. Requiere reinicio de Apache.

Ubicación del archivo principal

SistemaRuta típica httpd.conf / apache2.conf
RHEL/CentOS/Fedora/etc/httpd/conf/httpd.conf
Debian/Ubuntu/etc/apache2/apache2.conf (include /etc/apache2/sites-enabled/)
Windows (ApacheHaus, XAMPP, WampServer)C:\Apache24\conf\httpd.conf / C:\xampp\apache\conf\httpd.conf
macOS (Homebrew)/usr/local/etc/httpd/httpd.conf / /opt/homebrew/etc/httpd/httpd.conf
macOS (nativo)/etc/apache2/httpd.conf

Paso 1: Editar ServerName principal

  1. sudo nano /ruta/a/httpd.conf (o tu editor).
  2. Busca ServerName (puede estar comentado #ServerName www.example.com:80).
  3. Descomenta y cambia: ServerName nuevo.dominio.com:80 (puerto 80 HTTP, 443 HTTPS).
  4. Si no existe, añádelo al final del archivo.

Paso 2: VirtualHosts (Sitios múltiples)

Cada <VirtualHost *:80> (o *:443) debe tener su ServerName y opcional ServerAlias.

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

En Debian/Ubuntu: cada sitio en /etc/apache2/sites-available/ → a2ensite sitio → systemctl reload apache2.

Paso 3: Verificar sintaxis y recargar

  1. sudo apachectl configtest (o httpd -t, apache2ctl configtest). Debe salir Syntax OK.
  2. Recarga sin cortar conexiones: sudo systemctl reload apache2 / httpd -k graceful / sudo /etc/init.d/apache2 reload.
  3. O reinicio completo: sudo systemctl restart apache2 / httpd -k restart.

Paso 4: DNS y hosts local (Pruebas)

  • Público: Registra A/AAAA/CNAME en tu DNS apuntando a IP del servidor.
  • Local (tests): Edita /etc/hosts (Linux/Mac) o C:\Windows\System32\drivers\etc\hosts (Win):
    127.0.0.1 nuevo.dominio.com

SSL/TLS (HTTPS)

  • Certificado debe incluir el nuevo nombre (CN o SAN).
  • Let's Encrypt: sudo certbot --apache -d nuevo.dominio.com -d www.nuevo.dominio.com.
  • VirtualHost *:443 con SSLEngine on, SSLCertificateFile, SSLCertificateKeyFile.

Errores comunes

  • AH00558: Could not reliably determine the server's fully qualified domain name → Falta ServerName global.
  • Redirección infinita: ServerName en VirtualHost coincide con otro o RewriteRule mal hecha.
  • Nombre no resuelve: DNS no propagado, hosts local mal, firewall puerto 80/443.