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.
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:
yum update -y
yum install httpd -y
systemctl enable httpd
systemctl start httpd
systemctl status httpd
firewall-cmd --permanent --add-service=http
firewall-cmd --permanent --add-service=https
firewall-cmd --reload
mkdir -p /var/www/example.com/public_html
chown -R apache:apache /var/www/example.com
chmod -R 755 /var/www/example.com
cat > /var/www/example.com/public_html/index.html
cat > /etc/httpd/conf.d/example.com.conf
httpd -t
systemctl restart httpd
getenforce
httpd -S
curl http://example.com
systemctl start httpd
systemctl stop httpd
systemctl restart httpd
systemctl reload httpd
systemctl enable httpd
systemctl status httpd
httpd -t
httpd -S
tail -f /var/log/httpd/access_log
tail -f /var/log/httpd/error_log
ss -tulnp | grep httpd
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.