When setting up Apache with both IPv4 and IPv6 addresses, you might encounter issues where domain names resolve correctly to IPv6 addresses but the VirtualHost configuration doesn't work as expected. The key is proper syntax and configuration for IPv6 in Apache.
IPv6 addresses in Apache configurations must be enclosed in square brackets:
Listen [2001:db8::1]:80
NameVirtualHost [2001:db8::1]:80
Here's a working example for an IPv6 VirtualHost:
<VirtualHost [2001:db8::1]:80>
ServerName example.com
ServerAlias www.example.com
DocumentRoot /var/www/example
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
Ensure your DNS has proper AAAA records:
example.com. IN AAAA 2001:db8::1
www.example.com. IN AAAA 2001:db8::1
Verify IPv6 connectivity with these commands:
ping6 example.com
curl -6 http://example.com
Make sure these modules are enabled:
a2enmod ssl
a2enmod rewrite
Check your firewall allows IPv6 traffic on port 80:
ip6tables -L -n -v | grep 80
For multiple IPv6 VirtualHosts:
<VirtualHost [2001:db8::1]:80>
# First site
</VirtualHost>
<VirtualHost [2001:db8::2]:80>
# Second site
</VirtualHost>
- Missing square brackets around IPv6 addresses
- DNS propagation delays
- Firewall blocking IPv6 traffic
- Apache not listening on IPv6 interface
After making changes, always:
apache2ctl configtest
systemctl restart apache2
When working with IPv6 addresses in Apache, you need to follow different syntax rules compared to IPv4. The key differences are:
# IPv4 syntax
Listen 192.0.2.1:80
NameVirtualHost 192.0.2.1:80
# IPv6 syntax (must be enclosed in square brackets)
Listen [2001:db8::1]:80
NameVirtualHost [2001:db8::1]:80
Here's a full configuration example that works with both IPv4 and IPv6:
# File: /etc/apache2/ports.conf
Listen 192.0.2.1:80
Listen [2001:db8::1]:80
NameVirtualHost 192.0.2.1:80
NameVirtualHost [2001:db8::1]:80
# File: /etc/apache2/sites-available/ipv6-example.conf
ServerName example.com
ServerAlias www.example.com
DocumentRoot /var/www/example
ErrorLog ${APACHE_LOG_DIR}/example-error.log
CustomLog ${APACHE_LOG_DIR}/example-access.log combined
If your IPv6 virtual host isn't working, check these aspects:
- DNS Configuration: Verify your domain has proper AAAA records
- Firewall Rules: Ensure IPv6 traffic is allowed on port 80
- Apache Syntax: Double-check square brackets around IPv6 addresses
- Listen Directive: Confirm Apache is listening on the correct IPv6 address
To test your configuration:
# Check Apache syntax
apachectl configtest
# Verify listening ports
ss -tuln | grep 80
# Test DNS resolution
dig AAAA example.com
For servers handling both IPv4 and IPv6, consider this approach:
ServerName example.com
DocumentRoot /var/www/example
# Other directives...
This configuration allows the same virtual host to respond to both IP versions, simplifying management while maintaining compatibility.