
Apache Virtual Hosts (vHost) allow you to host multiple websites on a single CentOS, Red Hat, Rocky Linux, AlmaLinux servers, making it a cost-effective and efficient solution for web hosting. Whether you’re managing multiple domains, subdomains, or different applications, configure vHost Apache in Centos ensures that each site operates independently with its own directory, logs, and settings.
In this beginner-friendly guide, we will walk you through the step-by-step process to configure vHost Apache in CentOS, Red Hat, Rocky Linux, AlmaLinux Linux and all the rpm based OS. By the end of this tutorial, you will have a fully functional virtual host setup, ready to serve multiple websites on your Apache server. 🚀
Let’s get started! 🔥
Table of Contents
- Types of Virtual Hosts in Apache
- Prerequisites to configure vHost Apache in CentOS
- Step 1: Install Apache on CentOS (If Not Already Installed)
- Step 2: Create Directories for Virtual Hosts
- Step 3: Configure vHost Apache in CentOS
- Step 4: Test and Apply the Configuration
- Step 5: Configure Firewall (If Enabled)
- Step 6: Update Your Domain DNS Settings
- Step 7: Enable HTTPS with Let’s Encrypt SSL (Optional but Recommended)
- Step 8: Manage Apache Virtual Hosts
- Conclusion
Types of Virtual Hosts in Apache
- Name-Based Virtual Hosts (Most Common)
- Multiple websites share the same IP address but are distinguished by their domain names.
- Example:
example.com
andtest.com
can both run on the same server IP.
- IP-Based Virtual Hosts
- Each website has a unique IP address.
- Useful when dealing with SSL certificates for older browsers that don’t support SNI.
- Port-Based Virtual Hosts
- Different websites are hosted on the same server but on different ports.
- Example:
site1.com:8080
andsite2.com:9090
.
How vHost Apache Works
Apache uses virtual host configuration files to route requests based on domain names or IP addresses. When a request comes in, Apache checks its virtual host settings and directs the user to the corresponding website directory.
Why Use vHost Apache?
✔️ Efficient Hosting – Run multiple sites on one server.
✔️ Cost-Effective – Save money by using a single IP.
✔️ Custom Configuration – Each site can have different settings and security rules.
✔️ SSL Support – Secure individual sites with SSL certificates.
Prerequisites to configure vHost Apache in CentOS
Before starting, ensure you have the following:
- A CentOS 7 or CentOS 8, Red Hat, AlmaLinux, Rocky Linux server with Apache installed. If not installed already Please follow this tutorial to install the apache. | Installing Apache on CentOS 8 and Rocky Linux 9: A Beginner’s Guide
- Root or sudo user privileges
- A registered domain name (or a subdomain for testing)
- Access to the command line (SSH)
Step 1: Install Apache on CentOS (If Not Already Installed)
If you haven’t installed Apache yet, you can do so using the following command:
sudo yum install httpd -y
Once installed, start the Apache service and enable it to start on boot:
sudo systemctl start httpd
sudo systemctl enable httpd
To verify that Apache is running, use:
sudo systemctl status httpd
You should see output indicating that Apache is active and running.
Step 2: Create Directories for Virtual Hosts
Each website hosted on your server should have its own directory. Let’s create directories for two example websites:
sudo mkdir -p /var/www/domain1.com/public_html
sudo mkdir -p /var/www/domain2.com/public_html
Set the correct permissions:
sudo chown -R apache:apache /var/www/domain1.com/public_html
sudo chown -R apache:apache /var/www/domain2.com/public_html
sudo chmod -R 755 /var/www
Create an index file for each domain to test the setup:
echo "<h1>Welcome to Domain1</h1>" | sudo tee /var/www/domain1.com/public_html/index.html
echo "<h1>Welcome to Domain2</h1>" | sudo tee /var/www/domain2.com/public_html/index.html
Step 3: Configure vHost Apache in CentOS
Apache stores its virtual host configurations in /etc/httpd/conf.d/
. We will create separate configuration files for each website.
Create the first virtual host file:
sudo nano /etc/httpd/conf.d/domain1.com.conf
Add the following content:
<VirtualHost *:80>
ServerAdmin [email protected]
DocumentRoot /var/www/domain1.com/public_html
ServerName domain1.com
ServerAlias www.domain1.com
ErrorLog /var/log/httpd/domain1.com-error.log
CustomLog /var/log/httpd/domain1.com-access.log combined
</VirtualHost>
Create the second virtual host file:
sudo vi /etc/httpd/conf.d/domain2.com.conf
Press i
to go to insert mote and add the following content:
<VirtualHost *:80>
ServerAdmin [email protected]
DocumentRoot /var/www/domain2.com/public_html
ServerName domain2.com
ServerAlias www.domain2.com
ErrorLog /var/log/httpd/domain2.com-error.log
CustomLog /var/log/httpd/domain2.com-access.log combined
</VirtualHost>
Save and exit each file (Press Esc
key to exist from insert mode then tyle :wq!
and press enter to exit.
Step 4: Test and Apply the Configuration
Before restarting Apache, test the configuration for syntax errors:
sudo apachectl configtest
If the output says Syntax OK, restart Apache to apply the changes:
sudo systemctl restart httpd
Step 5: Configure Firewall (If Enabled)
If you have a firewall enabled, allow HTTP traffic:
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --reload
To allow HTTPS traffic (for SSL setup later):
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --reload
Step 6: Update Your Domain DNS Settings
To make your domains accessible, update your DNS A records to point to your server’s public IP address.
For example, in your domain registrar’s DNS settings, add:
- A record for
domain1.com
pointing toyour-server-ip
- A record for
www.domain1.com
pointing toyour-server-ip
Repeat the same for domain2.com
.
Once DNS propagation is complete, you should be able to access http://domain1.com
and http://domain2.com
in your web browser.
Step 7: Enable HTTPS with Let’s Encrypt SSL (Optional but Recommended)
To secure your websites with SSL, install Certbot:
sudo yum install epel-release -y
sudo yum install certbot python3-certbot-apache -y
Obtain an SSL certificate:
sudo certbot --apache
Follow the on-screen instructions to configure SSL for your domains.
To auto-renew SSL certificates, set up a cron job:
echo "0 0 * * * certbot renew --quiet" | sudo tee -a /etc/crontab
Step 8: Manage Apache Virtual Hosts
Here are some useful Apache management commands:
- Restart Apache:
sudo systemctl restart httpd
- Stop Apache:
sudo systemctl stop httpd
- Start Apache:
sudo systemctl start httpd
- Check Apache status:
sudo systemctl status httpd
- Disable a virtual host (temporarily):
sudo mv /etc/httpd/conf.d/domain1.com.conf /etc/httpd/conf.d/domain1.com.conf.bak sudo systemctl restart httpd
- Enable it back:
sudo mv /etc/httpd/conf.d/domain1.com.conf.bak /etc/httpd/conf.d/domain1.com.conf sudo systemctl restart httpd
Conclusion
Setting up Apache Virtual Hosts on CentOS Linux allows you to host multiple websites efficiently on a single server. By following this guide, you have learned how to configure vHost Apache in CentOS step by step, from directory creation and configuration setup to firewall settings and SSL integration.
Now, your server is optimized for hosting multiple domains with ease. For better performance, consider enabling caching and security enhancements.
Stay tuned to Magnetbyte for more detailed tutorials and expert tech solutions! 🚀