Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Apache Virtual Hosting IP Based and Name Based Virtual Hosts in RHEL/CentOS/Fedora
Apache Virtual Hosting allows you to host multiple websites on a single server by creating separate virtual hosts. There are two main types: IP-based virtual hosting (each site gets a unique IP address) and name-based virtual hosting (sites share an IP address but are distinguished by domain name). This guide shows how to configure both types in RHEL/CentOS/Fedora.
What is Virtual Hosting?
Virtual hosting is a method of hosting multiple websites on a single server, saving resources and costs for businesses. When hosting multiple websites, each site needs its own unique identity and separation from other sites.
IP-based virtual hosting Assigns a unique IP address to each website
Name-based virtual hosting Uses HTTP/1.1 Host header to identify websites sharing the same IP
Setting Up IP-based Virtual Hosting
IP-based virtual hosting requires multiple IP addresses assigned to your server. Follow these steps to configure it:
Step 1 Configure Apache Main Settings
Edit /etc/httpd/conf/httpd.conf and add the Listen directives:
Listen 192.168.1.10:80 Listen 192.168.1.11:80
Step 2 Create Virtual Host Configurations
Add virtual host blocks for each IP address:
<VirtualHost 192.168.1.10:80> ServerName site1.com ServerAlias www.site1.com DocumentRoot /var/www/site1.com/public_html/ ErrorLog /var/log/httpd/site1.com/error.log CustomLog /var/log/httpd/site1.com/access.log combined </VirtualHost> <VirtualHost 192.168.1.11:80> ServerName site2.com ServerAlias www.site2.com DocumentRoot /var/www/site2.com/public_html/ ErrorLog /var/log/httpd/site2.com/error.log CustomLog /var/log/httpd/site2.com/access.log combined </VirtualHost>
Step 3 Restart Apache
systemctl restart httpd.service
Setting Up Name-based Virtual Hosting
Name-based virtual hosting is more popular as it doesn't require multiple IP addresses:
Step 1 Configure Apache Main Settings
Edit /etc/httpd/conf/httpd.conf and add:
Listen 80 NameVirtualHost *:80
Step 2 Create Virtual Host Configurations
<VirtualHost *:80> ServerName site1.com ServerAlias www.site1.com DocumentRoot /var/www/site1.com/public_html/ ErrorLog /var/log/httpd/site1.com/error.log CustomLog /var/log/httpd/site1.com/access.log combined </VirtualHost> <VirtualHost *:80> ServerName site2.com ServerAlias www.site2.com DocumentRoot /var/www/site2.com/public_html/ ErrorLog /var/log/httpd/site2.com/error.log CustomLog /var/log/httpd/site2.com/access.log combined </VirtualHost>
Step 3 Restart Apache
systemctl restart httpd.service
Testing Your Virtual Hosts
To test virtual hosts, modify your local /etc/hosts file temporarily:
# Add this line to /etc/hosts on your local machine 192.168.1.10 site1.com 192.168.1.10 site2.com
Then open your web browser and navigate to http://site1.com or http://site2.com to verify the configuration.
Key Virtual Host Directives
| Directive | Purpose | Example |
|---|---|---|
| ServerName | Primary domain name | ServerName example.com |
| ServerAlias | Additional domain names | ServerAlias www.example.com |
| DocumentRoot | Website files location | DocumentRoot /var/www/example.com/ |
| ErrorLog | Error log file location | ErrorLog /var/log/httpd/error.log |
| CustomLog | Access log file location | CustomLog /var/log/httpd/access.log |
Common Management Commands
Use these Apache commands to manage virtual hosts:
# Test Apache configuration apachectl configtest # Graceful restart (completes current requests) apachectl graceful # Immediate restart systemctl restart httpd # Start Apache service systemctl start httpd # Stop Apache service systemctl stop httpd
Best Practices
Enable Log Rotation
Prevent log files from consuming excessive disk space:
CustomLog "|/usr/sbin/rotatelogs /var/log/httpd/site1.com/access.log.%Y%m%d 86400" combined
HTTP to HTTPS Redirect
<VirtualHost *:80> ServerName example.com Redirect permanent / https://example.com/ </VirtualHost>
Enable Compression
<IfModule mod_deflate.c> AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css text/javascript application/javascript </IfModule>
Comparison
| Feature | IP-Based | Name-Based |
|---|---|---|
| IP Requirements | Multiple IPs needed | Single IP sufficient |
| SSL/TLS Support | Full support (legacy) | Requires SNI extension |
| Resource Usage | Higher IP consumption | More efficient |
| Configuration | More complex | Simpler setup |
Conclusion
Apache virtual hosting enables efficient multi-site hosting on a single server. Name-based virtual hosting is preferred for most scenarios due to IP address efficiency, while IP-based hosting suits specific requirements like legacy SSL configurations. Both methods provide robust website separation and management capabilities in RHEL/CentOS/Fedora environments.
