Net2Secure: A Leading Data Center Service Provider in India

Home Knowledge Base Tech Guide /

How to Configure Apache Virtual Hosts on CentOS7?


Apache Virtual hosts enable you to host several websites on a single CentOS 7 server using a single Apache installation. Each website can have its own document root, domain name, logs, and configuration, making Virtual Hosts the standard approach for hosting multiple web applications on a single server.

This post will guide you through how to install Apache, create a Virtual Host, configure the crucial directories and permissions, verify the configuration, and test your website on CentOS7.

What is Apache Virtual Host?

An Apache Virtual Host refers to a configuration feature in the Apache HTTP server that allows you to host several websites or web applications on a single server. Instead of needing a separate server for each website, VM Hosts enable Apache to serve different websites based on the requested domain name or IP address. Each virtual host can have its own document root, domain name, and log files, delivering isolation and flexibility for handling various websites.

Virtual hosts are mainly used by web hosting providers, businesses, and developers to effectively leverage server resources while keeping website configurations separate. For instance, a single Apache server can host example.com, website1.com, and website2.com at the same time, with each website serving content from its own directory.

Generally, there are two types of Apache virtual hosts:

  1. Name-Based Virtual Hosts: Multiple websites share the same IP address, and Apache decides which website to serve based on the domain name specified in the HTTP request. This is the most widely used and recommended approach.
  2. IP-Based Virtual Hosts: Each website is allocated to a unique IP address, and Apache serves the correct website based on the destination IP address. This method is less common nowadays due to the limited availability of IPv4 addresses.

    By configuring Apache virtual hosts, administrators can simplify website management, boost resource utilization, and host various domains on a single CentOS 7 server without deploying separate Apache instances.

Steps to Configure Apache Virtual Hosts on CentOS7

Step 1: Update the Server

yum update -y

Step 2: Install Apache

yum install httpd -y

Step 3: Enable and Start Apache

systemctl enable httpd
systemctl start httpd
systemctl status httpd

Step 4: Open HTTP/HTTPS Ports

firewall-cmd --permanent --add-service=http
firewall-cmd --permanent --add-service=https
firewall-cmd --reload

Step 5: Create Website Directory

mkdir -p /var/www/example.com/public_html

Step 6: Set Ownership and Permissions

chown -R apache:apache /var/www/example.com
chmod -R 755 /var/www/example.com

Step 7: Create Test Web Page

cat > /var/www/example.com/public_html/index.html

Step 8: Create Virtual Host Configuration

cat > /etc/httpd/conf.d/example.com.conf

Step 9: Check Apache Configuration

httpd -t

Step 10: Restart Apache

systemctl restart httpd

Step 11: Configure SELinux (If Enabled)

getenforce

Step 12: Verify Virtual Host

httpd -S

Step 13: Test Website

curl http://example.com

Start Apache

systemctl start httpd

Stop Apache

systemctl stop httpd

Restart Apache

systemctl restart httpd

Reload Apache

systemctl reload httpd

Enable Apache at Boot

systemctl enable httpd

Check Apache Status

systemctl status httpd

Check Apache Configuration

httpd -t

List Virtual Hosts

httpd -S

View Access Log

tail -f /var/log/httpd/access_log

View Error Log

tail -f /var/log/httpd/error_log

Check Listening Ports

ss -tulnp | grep httpd

Check Firewall Rules

firewall-cmd --list-all

Thus, configuring Apache virtual hosts on CentOS7 is a systematic way to host several websites on a single server. By implementing the steps outlined above, validate the configuration, and verify that your website is accessible. Properly configured virtual hosts not only facilitate website management but also make it easier to expand your hosting environment as new domains or applications are integrated.

Once your virtual host is up and operating, consider improving your server by allowing HTTPS with an SSL/TLS certificate, implementing regular security updates, configuring backups, and controllingApache logs for performance and troubleshooting. These best practices will help ensure your hosted websites remain secure, reliable, and optimized for long-term operation.


Anurag Soam

By Anurag Soam

A skilled Technical Content Writer with an unwavering dedication to covering complex concepts and specializing in writing explicit, concise, and SEO-friendly content for diverse audiences. With a solid background in technology, including SaaS, PaaS, IaaS, Documentation, Cloud, Cybersecurity, and DevOps, heโ€™s an expert in curating content that enhances user experience and facilitates product success. Heโ€™s constantly updated on the latest industry trends and tools to deliver qualitative technical content.

View all of Anurag Soam's posts.

Contact Us


Related Tech Guide Posts

We use cookies

By continuing to browse this site, you are agreeing to the use of cookies to enhance your experience.