linux apache virtual hosts example

Create the directory structure

mkdir -p /var/www/html/www.vhost1.com
mkdir -p /var/www/html/www.vhost2.com

Create test web pages for each virtual host
echo "vhost1" > /var/www/html/www.vhost1.com/index.html
echo "vhost1" > /var/www/html/www.vhost1.com/index.html
echo "vhost2" > /var/www/html/www.vhost2.com/index.html

Set up ownership and permissions
chown -R apache:apache /var/www/html/www.vhost1.com
chown -R apache:apache /var/www/html/www.vhost2.com
chmod -R 755 /var/www/html

Create virtual host configuration files
vi /etc/httpd/conf.d/vhost1.com.conf

<VirtualHost *:80>

ServerName www.vhost1.com
ServerAlias vhost1.com
DocumentRoot /var/www/html/www.vhost1.com
ErrorLog /var/www/html/www.vhost1.com/error.log
CustomLog /var/www/html/www.vhost1.com/access.log combined

</VirtualHost>

vi /etc/httpd/conf.d/vhost2.com.conf

<VirtualHost *:80>

ServerName www.vhost2.com
ServerAlias vhost2.com
DocumentRoot /var/www/html/www.vhost2.com
ErrorLog /var/www/html/www.vhost2.com/error.log
CustomLog /var/www/html/www.vhost2.com/access.log combined

</VirtualHost>

apachectl configtest
After the syntax check is done, restart Apache to make these changes take effect.
systemctl restart httpd
Test the virtual hosts

Add the following lines, using the static IP address of your server:

192.168.4.235 www.vhost1.com
192.168.4.235 www.vhost2.com
Save and exit the file.

This entry was posted in Uncategorized. Bookmark the permalink.

Comments are closed.