Apache

How to Configure Apache Virtual Host in Apache

In my previous article I have explained how to install Apache Web Server in Linux and how to configure Apache HTTPD Sever in Linux.

In this article I will show you how to configure Apache Virtual Host.

Apache virtual hosts allow a single httpd web server to serve content for multiple domains or websites. Virtual host is defined in a Virtual Host container. The multiple virtual containers for multiple domains is specify in the VirtualHost directive.

Where to specify VirtualHost directive ?

Virtual hosts are configured using the block inside the main configuration.

For ease of administration, i would recommend this virtual host blocks are not defined inside /etc/httpd/conf/httpd.conf. Instead, please create new separate configuration files in /etc/httpd/conf.d/.

Configure Apache Virtual Host

Create separate configuration file to specify block.

# vim /etc/httpd/conf.d/vhost.conf
<VirtualHost 192.168.0.5:80>
DocumentRoot /var/www/html/website1
ServerName website1.looklinux.local
ServerAdmin [email protected]
ErrorLog "logs/website1.looklinux.local_error_log"
CustomLog "logs/website1.looklinux.local_access_log" combined
</VirtualHost>

<VirtualHost 192.168.0.5:80>
DocumentRoot /var/www/html/website2
ServerName website2.looklinux.local
ServerAdmin [email protected]
ErrorLog "logs/website2.looklinux.local_error_log"
CustomLog "logs/website2.looklinux.local_access_log" combined
</VirtualHost>

The document root specifies in each containers or virtualhost blocks is applicable to this virtual host and overrides any DocumentRoot directive elsewhere in the configuration.

Upload sample index.html or index.php into both document root (/var/www/html/website1 and /var/www/html/website2)

Restart or reload the Apache web service. To apply the new configuration.

# systemctl restart httpd 
OR 
# systemctl reload httpd

Allow access to Apache server configuring firewall.

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

Now you can access both virtual URL will return you different contain.

apache-virtual-hosts

Thank you! for visiting LookLinux.

If you find this tutorial helpful please share with your friends to keep it alive. For more helpful topic browse my website www.looklinux.com. To become an author at LookLinux Submit Article. Stay connected to Facebook.

About the author

mm

Santosh Prasad

Hi! I'm Santosh and I'm here to post some cool article for you. If you have any query and suggestion please comment in comment section.

Leave a Comment