Apache Virtual Hosting IP Based and Name Based Virtual Hosts in RHEL/CentOS/Fedora


If you're hosting multiple websites on a single server, you'll want to set up virtual hosts to keep each site separate. Virtual hosting can be IP-based or name-based. In this article, we'll discuss how to set up virtual hosting in RHEL/CentOS/Fedora using both IP-based and name-based virtual hosts.

What is Virtual Hosting?

Virtual hosting is a method of hosting multiple websites on a single server. It's an essential technique for saving resources and costs for businesses. When you host multiple websites on a single server, you want to ensure that each site has its own unique identity and is separated from other sites.

There are two types of virtual hosting, IP-based and name-based. IP-based virtual hosting involves assigning a unique IP address to each website. Name-based virtual hosting uses HTTP/1.1 Host header to identify website.

Setting Up IP-based Virtual Hosting in RHEL/CentOS/Fedora

To set up IP-based virtual hosting, you need to have multiple IP addresses assigned to your server. Follow these steps to configure IP-based virtual hosting −

Step 1: Edit /etc/httpd/conf/httpd.conf file and add following lines

NameVirtualHost *:80

The Listen directive tells Apache to listen on port 80, which is default HTTP port. NameVirtualHost directive tells Apache to use name-based virtual hosting.

Step 2: Add following lines for each website you want to host

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

Replace x.x.x.x with IP address you want to use for this website. ServerName directive specifies domain name of website. ServerAlias directive specifies any additional domain names that should be associated with website. DocumentRoot directive specifies location of website's files.

Step 3: Save and close file, then restart Apache using following command

systemctl restart httpd.service

Setting Up Name-based Virtual Hosting in RHEL/CentOS/Fedora

Name-based virtual hosting is a popular way to host multiple websites on a single server because it doesn't require multiple IP addresses. Follow these steps to configure name-based virtual hosting −

Step 1: Edit /etc/httpd/conf/httpd.conf file and add following lines

Listen 80
NameVirtualHost *:80

The Listen directive tells Apache to listen on port 80, which is default HTTP port. NameVirtualHost directive tells Apache to use name-based virtual hosting.

Step 2: Add following lines for each website you want to host

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

The ServerName directive specifies domain name of website. ServerAlias directive specifies any additional domain names that should be associated with website. DocumentRoot directive specifies location of website's files.

Step 3: Save and close file, then restart Apache using following command

systemctl restart httpd.service

Testing Your Virtual Hosts

To test your virtual hosts, you can add a temporary entry to your local hosts file. Follow these steps −

Step 1 − Edit /etc/hosts file on your local machine and add following line −

x.x.x.x example.com

Replace x.x.x.x with IP address of your server.

Step 2 − Open your web browser and enter http://example.com in address bar

Step 3 − If you've configured your virtual hosts correctly, you should see website for example.com. If not, check your configuration and try again.

Managing Virtual Hosts in RHEL/CentOS/Fedora

To manage virtual hosts in RHEL/CentOS/Fedora, you can use Apache VirtualHost directive. VirtualHost directive allows you to specify settings for each virtual host.

Here are some common settings you may want to include in your VirtualHost directive −

  • ServerName − domain name of website.

  • ServerAlias − Additional domain names that should be associated with website.

  • DocumentRoot − location of website's files.

  • ErrorLog − location of website's error log file.

  • CustomLog − location of website's access log file.

Here's an example of a VirtualHost directive with these settings −

<VirtualHost *:80>
   ServerName example.com
   ServerAlias www.example.com
   DocumentRoot /var/www/example.com/public_html/
   ErrorLog /var/log/httpd/example.com/error.log
   CustomLog /var/log/httpd/example.com/access.log combined
</VirtualHost>

You can also use Apache command-line tools to manage virtual hosts. Here are some common commands −

  • apachectl configtest − Tests your Apache configuration for errors.

  • apachectl graceful − Gracefully restarts Apache, which allows any currently running requests to complete before shutting down.

  • apachectl restart − Restarts Apache immediately, which may interrupt currently running requests.

  • apachectl start − Starts Apache if it's not already running.

  • apachectl stop − Stops Apache if it's currently running.

Here are a few more tips and best practices for setting up and managing virtual hosts in RHEL/CentOS/Fedora −

Use Access Controls

It's important to ensure that only authorized users can access your virtual hosts. Use Apache's access controls to restrict access to your virtual hosts based on IP address or username and password.

Use Directory Indexes

If you're hosting static files like HTML, CSS, and images, it's a good idea to enable directory indexes so that users can browse files in a directory. Use Options directive to enable directory indexes −

<Directory /var/www/example.com/public_html/>
   Options +Indexes
</Directory>

Use Redirects

If you need to redirect users from one virtual host to another or from HTTP to HTTPS, use Apache's Redirect directive. Here's an example of how to redirect users from HTTP to HTTPS −

<VirtualHost *:80>
   ServerName example.com
   Redirect permanent / https://example.com/
</VirtualHost>

<VirtualHost *:443>
   ServerName example.com
   # SSL/TLS configuration goes here
</VirtualHost>

Use Log Rotation

Apache writes access and error logs for each virtual host to disk. Over time, these logs can become very large and consume a lot of disk space. Use log rotation to keep your log files manageable. Here's an example of how to rotate access logs for a virtual host −

<VirtualHost *:80>
   ServerName example.com
   # ...
   CustomLog "|/usr/sbin/rotatelogs /var/log/httpd/example.com/access.log.%Y%m%d 86400" combined
   # ...
</VirtualHost>

This will rotate access log for example.com every day and keep logs for 30 days.

Use Caching Plugins

If you're hosting dynamic websites like WordPress, Joomla or Drupal, it's a good idea to use caching plugins to improve performance. These plugins can cache frequently accessed pages and reduce load on your server. Popular caching plugins for WordPress include WP Super Cache and W3 Total Cache.

Enable Compression

Enabling compression can significantly reduce amount of data that's sent between server and client, which can improve performance. Use Apache's mod_deflate module to enable compression −

<IfModule mod_deflate.c>
   AddOutputFilterByType DEFLATE text/html text/plain text/xml application/xml application/xhtml+xml text/javascript text/css application/x-javascript
</IfModule>

Use Virtual Hosts For Testing

Virtual hosts are a great way to test websites before you make them live. Set up a separate virtual host for each test site and use it to test new features, plugins, and updates.

Keep Your Virtual Hosts Updated

Just like your server software, it's important to keep your virtual hosts updated with latest security patches and updates. Make sure you regularly update your website's CMS, plugins, and themes to keep them secure and bug-free.

Use Backups

It's important to back up your virtual hosts regularly to ensure that you can recover from data loss or server failure. Use a backup tool like Duplicity or rsync to create backups of your virtual hosts and store them on an external drive or in cloud.

Use a Content Delivery Network (CDN)

If you're hosting large files like images, videos, or software downloads, it's a good idea to use a CDN to distribute those files. A CDN can improve performance by caching files on servers located around world, reducing load on your server and improving download speeds for users. Popular CDNs include Cloudflare and Amazon CloudFront.

Monitor for Security Issues

Virtual hosting can make your server more vulnerable to security threats like malware, hacking, and DDoS attacks. Use a security monitoring tool like fail2ban to monitor your server for suspicious activity and block malicious IP addresses.

Conclusion

Virtual hosting is an essential technique for hosting multiple websites on a single server. In RHEL/CentOS/Fedora, you can set up virtual hosting using either IP-based or name-based virtual hosts. IP-based virtual hosting requires multiple IP addresses, while name-based virtual hosting uses HTTP/1.1 Host header to identify website. You can manage virtual hosts using Apache VirtualHost directive or Apache command-line tools.

Updated on: 02-May-2023

191 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements