Configure Web Server with Virtual Hosts - Basic

Centos 7.6

Most of the World Wide Web runs on Apache web servers. This guide will show you how to install and configure an Apache web server using virtual hosts. Virtual hosts allow you to host multiple websites on a single web server (using a single IP address).

Play Video

Install Apache (HTTPD) web server.

sudo yum install httpd -y

Enable HTTPD service.

systemctl enable httpd.service

Create website content directories.

sudo mkdir -p /var/www/website1.com/public_html
sudo mkdir -p /var/www/website2.com/public_html

Create website content.

nano /var/www/website1.com/public_html/index.html

<!DOCTYPE html>
  <body>
   <h1>website1.com</h1>
  <body>
</html>

nano /var/www/website2.com/public_html/index.html

<!DOCTYPE html>
  <body>
   <h1>website2.com</h1>
  <body>
</html>

Change ownership of website content directories.

sudo chown -R $USER:$USER /var/www/website1.com/public_html
sudo chown -R $USER:$USER /var/www/website2.com/public_html

Change permissions of the web content directories.

sudo chmod -R 755 /var/www

Configure Apache to look for sites-enabled in specified directory.

echo "IncludeOptional sites-enabled/*.conf" >> /etc/httpd/conf/httpd.conf

Create virtual hosts and sites enabled directories.

sudo mkdir /etc/httpd/sites-available
sudo mkdir /etc/httpd/sites-enabled

Configure virtual hosts files.

sudo nano /etc/httpd/sites-available/website1.com.conf

<VirtualHost *:80>
  ServerName www.website1.com
  ServerAlias website1.com
  DocumentRoot /var/www/website1.com/public_html
</VirtualHost>

sudo nano /etc/httpd/sites-available/website2.com.conf

<VirtualHost *:80>
  ServerName www.website2.com
  ServerAlias website2.com
  DocumentRoot /var/www/website2.com/public_html
</VirtualHost>

Enable websites.

sudo ln -s /etc/httpd/sites-available/website1.com.conf /etc/httpd/sites-enabled/website1.com.conf
sudo ln -s /etc/httpd/sites-available/website2.com.conf /etc/httpd/sites-enabled/website2.com.conf

Restart Apache.

sudo apachectl restart

Advertisements