This guide is designed for people who followed our previous Apache webserver guide or you currently have Apache webserver installed and would like to set up multiple websites (vhosts) on your webserver.
Step 1 — Setting up vHosts
Hosting a single website using your Apache server is easy, just upload your website's files into the /var/www/html
folder. Simple enough. But, what if you want to host multiple websites? The answer, vHosts. Each website is set up as a vHosts, which contains all the website’s information on the webserver.
For every new website, you will need to create a vHost configuration file. To do this use this command (replacing the link with your domain).
$ sudo mkdir -p /var/www/website.com
Let's create a folder to store log information:
$ sudo mkdir -p /var/www/website/log
Assign permissions:
$ sudo chown -R $USER:$USER /var/www/website/html
Ensure root permissions are set:
$ sudo chmod -R 755 /var/www
Step 2 — Create Directory File
Now that the vHosts folders are set up, we now need to create the directory files for the vHost.
Using a text editor of your choice, in this case, we will use vi
, create a sample index.html
file.
$ sudo vi /var/www/website/html/index.html
Once the file opens, press i
to enter INSERT
mode. Copy and paste this code into the file:
<html>
<head>
<title>Hello World!</title>
</head>
<body>
<h1>This vHost is successfully working! Sweet.</h1>
</body>
</html>
Save and exit by pressing the ESC
key and typing :wq
followed by pressing ENTER.
Change the file permissions with:
$ sudo chown -R www-data: /var/www/website.com
Step 3 — Create vHosts
Now, we can create the sites-available
and sites-enabled
directories. The sites-available
is where the vHosts are stored. The sites-enabled
directory is where the ‘active’ vHosts are stored.
We can create both directories using this command:
$ sudo mkdir /etc/httpd/sites-available /etc/httpd/sites-enabled
Next, we need to edit Apache’s configuration file to tell Apache to look for vHosts in the sites-enabled
directory. Open the configuration file:
$ sudo vi /etc/httpd/conf/httpd.conf
Add this string to the end of the file:
IncludeOptional sites-enabled/*.conf
Save and exit.
Now we need to create a vHost file for our website in the sites-available
directory. Using this command (remember to replace ‘website’ with your domain):
$ sudo vi /etc/httpd/sites-available/website.conf
Add this to the file (again replacing ‘website’ with your domain):
<VirtualHost *:80>
ServerName www.website
ServerAlias website
DocumentRoot /var/www/website/html
ErrorLog /var/www/website/log/error.log
CustomLog /var/www/website/log/requests.log combined
</VirtualHost>
Save and exit the file.
Now that we have created the vHost files, we need to enable them.
$ sudo ln -s /etc/httpd/sites-available/website.conf /etc/httpd/sites-enabled/website.conf
Your virtual hosts are configured and ready to serve. But, let's restart Apache using:
$ sudo systemctl restart httpd
After the restart, you should now be able to head on over to http://yourdomain
and see the file we created.