How to Configure Apache Virtual Hosts on Ubuntu
Apache HTTP Server is an open-source web server created and maintained by the Apache Software Foundation.
Apache is one of the most used web content delivery methods on the Internet. Its versatility, dependability, and performance make Apache a popular web server worldwide.
Apache’s modular design allows authentication, URL rewriting, virtual host creation, and more.
Apache supports virtual hosting, which allows configuring Apache to host multiple websites or domains. Virtual Hosts in Apache let you connect several domains to one IP address.
Read this article to the end to learn how to configure Apache virtual hosts on an Ubuntu (20.04) server to host numerous sites or domains without restrictions.
As we previously targeted CentOS users and taught them how to set up virtual hosts on CentOS 7, in this article, Ubuntu users will learn to host several websites on one Ubuntu server, each with its own specific domain or subdomain. If you’re ready, let’s begin. Install Apache virtual hosts on Ubuntu using the step-by-step guide.
Prerequisites to Configure Apache Virtual Hosts on Ubuntu
Before proceeding with the instructions, consider the following:
- A server running Ubuntu VPS
- A non-root user with sudo privileges
- Adequate CPU, memory, and disk resources
- Ensure the correct configuration of domain names or subdomains pointing to the IP address of your server
- Create a backup before making any changes to Apache configuration files
10 Steps to Set up Apache Virtual Hosts on Ubuntu
By following the steps below, you can prepare an Ubuntu server for hosting multiple websites with the configuration of Apache virtual hosts on Ubuntu:
1. Install Apache on the Ubuntu server (Prerequisites)
Installing the Apache web server on the Ubuntu server is a prerequisite for configuring Apache virtual hosts on Ubuntu. Therefore, first, check whether Apache is installed on the Ubuntu server. If Apache is not installed yet, install it by executing the following commands:
sudo apt update
sudo apt install apache2
If the UFW firewall is enabled on the Ubuntu server, you should allow Apache traffic in the UFW firewall:
sudo ufw allow 'Apache Full'
Now Apache should be successfully installed and running on your Ubuntu server. Verify the status of Apache to ensure the successful installation on Ubuntu:
sudo systemctl status apache2
The expression “Active: active (running)” indicates that the Apache service is active and running. Now, you can connect to your server’s IP address in your browser (http://your_IP_address or http://localhost).
Displaying the Apache welcome page confirms the successful installation of the Apache web server.
2. Start Apache Service
After installing the Apache service, start it by running the following command:
sudo systemctl start apache2
You can also enable the Apache service to start on boot:
sudo systemctl enable apache2
3. Create the Directory Structure
Two types of virtual hosting are configurable in Apache: IP-based virtual hosting and name-based virtual hosting.
IP-based virtual hosting refers to associating each website with a distinct IP address, while name-based virtual hosting refers to associating multiple domains with a single IP address. In this guide, we will configure name-based virtual hosting and use a domain we own.
The content and files of a website associated with a domain name hosted on a Ubuntu server are stored in the document root, which is accessible and configurable in the /var/www directory. To configure virtual hosts, you should start by creating a directory structure in the /var/www directory. This directory is created to hold the data and web content that needs to be served in response to visitors’ requests.
To host multiple sites, you need to create separate directories based on the number of websites and for each virtual host, and set each virtual host configuration directory correctly. For this purpose, generate your site directory in the /var/www/ directory:
sudo mkdir -p /var/www/example.com/ public_html
sudo mkdir -p /var/www/anotherexample.com/ public_html
Replace example.com and anotherexample.com with your respective domain names.
4. Set Permissions
Since the previous commands were executed with sudo permissions, the ownership of the directories you have created is set to the root user. If you want the current user to have sufficient permissions to edit files, you need to change the domain document root directory ownership. Therefore, at this step, it’s time to set the ownership for the directories you have created using the chown command:
sudo chown -R $USER:$USER /var/www/example.com/ public_html
sudo chown -R $USER:$USER /var/www/anotherexample.com/ public_html
By executing this command, the public_html subdirectories will belong to the current user who is currently logged into the Apache server.
In addition to this, setting correct permissions is essential to ensure read and write permissions for the general web directory and all the files it contains, enabling the seamless presentation of pages without issues. To change File Permissions, get help from chmod command:
sudo chmod -R 755 /var/www
As a result, at this step, you have configured the necessary permissions to create content in the associated directories.
5. Create Sample HTML Pages for each virtual host
Now that you have created your directory structure, you can start your activities on each virtual hosting site. You can generate and present unique content on each different site. To provide visual feedback when visiting the domain and connecting to the site in your browser, create an index.html file for your domain in the domain document root. To create the index.html file for your site, use the following command:
nano /var/www/ example.com /public_html/index.html
Then, in the file, create an HTML file for each virtual host, which serves as a representation of information about the site that users connect to:
/var/www/ Example.com /public_html/index.html
<html>
<head>
<title>Welcome to Example.com!</title>
</head>
<body>
<h1>Success! The Example.com virtual host is working!</h1>
</body>
</html>
To save the changes and close the nano editor, press CTRL+X, then press Y when prompted, and finally, press Enter.
You can use this file as a base for your other sites. To do so, you need to copy this file:
cp /var/www/ Example.com /public_html/index.html /var/www/ anotherexample.com /public_html/index.html
Then open the new file using your favorite editor and repeat the previous steps for the new file:
nano /var/www/ anotherexample.com /public_html/index.html
<html>
<head>
<title>Welcome to anotherexample.com!</title>
</head>
<body> <h1>Success! The anotherexample.com virtual host is working!</h1>
</body>
</html>
Save the file to apply the changes.
An HTML file is now created to test each virtual host.
6. Create Virtual Host Configuration Files
The /etc/apache2/sites-available/ directory contains configuration files for Apache virtual hosts on Ubuntu systems. These virtual host configuration files define how the Apache web server responds and behaves regarding requests for different domains. Therefore, by executing the following command, create separate virtual host configuration files for each site in the /etc/apache2/sites-available/ directory by copying the default Apache virtual host file (000-default.conf):
sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/ example.com.conf
Pay attention that the virtual host file ends with .conf in the previous command.
7. Edit Virtual Host Configuration Files
Then open the new Virtual Host configuration file in your favorite text editor (e.g., nano or vim) for editing with sudo privileges:
sudo nano /etc/apache2/sites-available/ example.com.conf
Add the following lines to the content of the file and adjust values to match your setup:
<VirtualHost *:80>
ServerAdmin [email protected]
ServerName example.com
ServerAlias www. example.com
DocumentRoot /var/www/example.com/html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
In this configuration, each component should be set as follows:
- ServerAdmin: Configure the email address for the site admin so that emails are directed to this address.
- ServerName: Specify the domain for which the virtual host configuration is defined. (Usually, it is your domain name.)
- ServerAlias: Define other domains and subdomains that should match this virtual host and point to the IP address of a server.
- ErrorLog, CustomLog: Define the paths for log files.
As a result, save the file and exit.
In order to generate an additional configuration file, one may duplicate the contents from the initial virtual host site:
sudo cp /etc/apache2/sites-available/ example.com.conf /etc/apache2/sites-available/ anotherexample.com.conf
Then, open the new configuration file with the editor, copy the content you added to the first configuration file to the new file, and adjust the values based on your settings.
8. Enable the Virtual Hosts
After creating virtual host files, it’s time to enable them. In Apache, you can enable virtual hosts by creating symbolic links to the configuration files in the sites-enabled directory. To do this, use the a2ensite
tool as follows:
sudo a2ensite example.com.conf
sudo a2ensite anotherexample.com.conf
Sample Output:
Enabling site example.com.
To activate the new configuration, you need to run:
systemctl reload apache2
As you can see, the previous command’s output reminds you to reload the Apache server. Before reloading the Apache server, make sure that the configuration does not contain any syntax errors:
sudo apache2ctl configtest
Output:
. . .
Syntax OK
This message indicates that there is no error in the configuration, and the syntax provided by Apache has been correctly utilized in the configuration.
9. Restart Apache
To apply the changes, restart Apache by running the following command:
sudo systemctl restart apache2
Note: Before restarting your server, it is recommended to disable the default site defined in 000-default.conf:
sudo a2dissite 000-default.conf
If you are using real domains, make sure to configure DNS records pointing to the IP address of your server.
10. Test your Apache service configuration
To ensure that the configured virtual hosts are working correctly, open your web browser and visit your domains (e.g., http://example.com and http://anotherexample.com). Seeing the message below on the index.html page indicates that everything is working as expected, and you have successfully configured Apache virtual hosts on your Ubuntu server.
Conclusion
Congratulations! You have now equipped your Ubuntu server with the Apache service, and your server is ready to host multiple websites. By following the steps in this article, you should now be able to manage two websites on your Ubuntu server with different domains. You can even create and configure more virtual hosts, hosting as many websites as your server can handle.
Remember that for each website you intend to host on your Ubuntu server, you need to create a virtual host, and this guide helps you create and configure Apache virtual hosts on your Ubuntu server.
If you need further guidance, feel free to share your questions in the comments section.