DEV Community

hub
hub

Posted on

basic tutorial for setting up multiple domains and websites with Apache virtual hosts

note: if anybody is able to add some more ideas - this text can e considered as a preliminary text - i am not sure if this whole process works.
so any and all additional ideas were greatly appreciated. i look forward to hear from you

a basic tutorial for setting up multiple domains and websites with Apache virtual hosts on one

Give Ubuntu 20.04 server. Note that this is just a basic guide and you may want additional instructions. Take security and optimization steps depending on your specific needs.

Step 1: Prepare servers
Make sure your Ubuntu 20.04 server is up to date:

sudo apt update
sudo aptupgrade

Step 2: Install Apache

Install the Apache web server:

sudo apt install apache2

Step 3: Configure virtual hosts

Create configuration files for each virtual host. For example for domain1.com and domain2.com:

sudo nano /etc/apache2/sites-available/domain1.com.conf

Paste the following content:

<VirtualHost *:80>
ServerAdmin webmaster@domain1.com
ServerName domain1.com
DocumentRoot /var/www/domain1.com/html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Repeat the process for domain2.com:

sudo nano /etc/apache2/sites-available/domain2.com.conf

Paste the following content:

<VirtualHost *:80>
ServerAdmin webmaster@domain2.com
ServerName domain2.com
DocumentRoot /var/www/domain2.com/html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Step 4: Create directory structure

First and at the very beginning of all further steps
let's first create a basic directory structure, in which the site data that we provide to visitors is stored in our top-level Apache directory. We use examples of domain names (domain1.com and domain2.com), which are also used below.
You must replace these with your actual domain names.
So now create the directories for the websites:


sudo mkdir -p /var/www/domain1.com/html
sudo mkdir -p /var/www/domain2.com/html

Step 5: Set permissions:
Now we need to change the permissions to our current non-root user to be able to modify these files. So now we set the permissions for the directories:


sudo chown -R www-data:www-data /var/www/domain1.com/html
sudo chown -R www-data:www-data /var/www/domain2.com/html

In addition, we now also ensure that read-only access to the general Web directory and all files and folders within it are allowed so that pages can be served properly.


sudo chmod -R 755 /var/www

Step 6: Create demo pages for each virtual host:

in this step we will create a simple HTML demo page for each domain: So here in this step we will now create content that we can deploy. To do this, we create an index.html demo page for each site. We can open an index.html file in a text editor for our first site, for example in the nano editor.


echo "<html><head><title>Welcome to domain1.com</title></head><body><h1>Hello from domain1.com</h1></body></html>" | sudo tee /var/www/domain1.com/html/index.html

Repeat the process for domain2.com:


echo "<html><head><title>Welcome to domain2.com</title></head><body><h1>Hello from domain2.com</h1></body></html>" | sudo tee /var/www/domain2.com/html/index.html

This creates simple demo pages for each domain. You can later customize the content of these pages to suit your needs.

After you create the demo pages, continue with the remaining steps to enable the virtual hosts and restart the Apache server.
Note that this is just temporary demo content and you should provide your actual website files instead of these simple HTML pages, once you have prepared them.

....in this file we then create a domain-specific HTML document like the following:


/var/www/domain1.com/htmlgg
<html>
<head>
<title>Welcome to the Example site the domain1.com!</title>
</head>
<body>
<h1>Success! The domain1.com virtual host is working!</h1>
</body>
</html>

well - in this next step we save and close the file and then copy this file as the basis for our second site:


cp /var/www/example.com/public_html/index.html /var/www/domain1.com/index.html

Now let's open the file and change the relevant information:


nano /var/www/test.com//index.html
/var/www/test.com/domain1.com/index.html
<html>
<head>
<title>Welcome to Test.com!</title>
</head>
<body> <h1>Success! The test.com virtual host is working!</h1>
</body>
</html>

Step 7: Enable virtual hosts

Enable the virtual hosts:

sudo a2ensite domain1.com.conf
sudo a2ensite domain2.com.conf

Step 8: Restart Apache
Restart the Apache server to apply the changes:

sudo systemctl restart apache2

Step 9: Update DNS records
Update the DNS records for domain1.com and domain2.com to point to your server's IP address.

After these steps, the websites should be accessible under the respective domains.
** Note** that you need to make sure that the domains point to your server's IP address and that port 80 (HTTP) is open.
This tutorial provides a foundation for configuring virtual hosts on an Apache server on Ubuntu 20.04. Depending on your needs, additional configuration and security measures may be required.

note: if anybody is able to add some more ideas - this text can e considered as a preliminary text - i am not sure if this whole process works.

so any and all additional ideas were greatly appreciated.
i look forward to hear from you

Top comments (0)