Apache is a popular web server used by many websites all over the world. It allows you to host multiple domains and websites from a single server, with the help of Virtual Hosts. If you have a powerful server with good capacity, then it is advisable to host multiple websites on it using virtual hosts. In this article, we will look at how to host multiple website on one server.
Host Multiple Websites on One Apache Server in Ubuntu
Here are the steps to host multiple domains on one Apache server in Ubuntu. For our example, we will host domains example1.com and example2.com.
1. Install Apache
Open terminal and run the following commands to install Apache web server. Skip this step if you have already installed it.
$ sudo apt update $ sudo apt install apache2
Also read : How to block visitors by country in NGINX
2. Create Directories
We will create 2 separate folders for the 2 websites (example1.com and example2.com) that we will host on our Apache server.
$ sudo mkdir /var/www/html/example1.com $ sudo mkdir /var/www/html/example2.com
Also read : How to Set Environment Variable in Ubuntu
3. Create index.html files
Next we will create index.html files for each of the websites.
Run the following command to create index.html file for example1.com website.
$ sudo vi /var/www/html/example1.com/index.html
Add the following lines to it.
<html> <title>www.example1.com</title> <h1>Welcome to www.example1.com Website</h1> <p>Index page for example1.com</p> </html>
Save and close the file.
Similarly, run the following command to create index.html file for example2.com website
$ sudo vi /var/www/html/example2.com/index.html
Add the following lines to it.
<html> <title>www.example2.com</title> <h1>Welcome to www.example2.com Website</h1> <p>Index page for example2.com</p> </html>
Save and close the file.
Also read : How to Install Webmin in Ubuntu
4. Change file ownership
Run the following command to change ownership of the folders created in step 2, to www-data. This is the default user used by Apache to work with website files & folders.
$ sudo chown -R www-data:www-data /var/www/html/example1.com $ sudo chown -R www-data:www-data /var/www/html/example2.com
Also read : How to Configure Iptables in Ubuntu
5. Create Virtual Host Files
Next, we create virtual host files for each website at /etc/apache2/sites-available. Run the following command to create virtual host file for example1.com.
$ sudo vi /etc/apache2/sites-available/example1.com.conf
Add the following lines to it.
<VirtualHost *:80> ServerAdmin admin@example1.com ServerName example1.com DocumentRoot /var/www/html/example1.com DirectoryIndex index.html ErrorLog ${APACHE_LOG_DIR}/example1.com_error.log CustomLog ${APACHE_LOG_DIR}/example1.com_access.log combined </VirtualHost>
Save and close the file.
In the above configuration, we tell Apache that this virtual host configuration is meant for server name (example.com) whose files are located at DocumentRoot /var/www/html/example1.com.
Similarly, we create virtual host file for example2.com website.
$ sudo vi /etc/apache2/sites-available/example2.com.conf
Add the following lines to it.
<VirtualHost *:80> ServerAdmin admin@example2.com ServerName example2.com DocumentRoot /var/www/html/example2.com DirectoryIndex index.html ErrorLog ${APACHE_LOG_DIR}/example2.com_error.log CustomLog ${APACHE_LOG_DIR}/example2.com_access.log combined </VirtualHost>
Also read : How to Use NGINX Reverse Proxy with NodeJS
6. Enable virtual hosts
Run the following commands to enable the above 2 virtual hosts that we have created.
$ sudo a2ensite example1.com $ sudo a2ensite example2.com
Also read : How to Convert Image to PDF in Ubuntu
7. Restart Apache web server
Restart Apache web server to apply changes
$ sudo systemctl restart apache2
Also read : How to Search in VI Editor
8. Test the websites
Open browser and go to http://example1.com. You will see the index page for example1.com
Similarly, open browser and go to http://example2.com. You will see the index page for example2.com
That’s it. Now you will be able to access both your websites hosted on one server.
Related posts:
How to Change Apache Log Level
How to Force HTTPS in .htaccess in Apache
How to Escape Percent (%) in Apache using .htaccess
How to Set Content-Disposition Header to Attachment in Apache
How to Fix Mixed Content/Insecure Content Issue in Apache/PHP
Limit Bandwidth & Connections in Apache
Apache Http Server vs Apache Tomcat Server
How to Filter Logs Between Date Range in Linux
Sreeram has more than 10 years of experience in web development, Python, Linux, SQL and database programming.