YouTube Icon

Code Playground.

How To Set Up Apache Virtual Hosts on Ubuntu 20.04

CFG

How To Set Up Apache Virtual Hosts on Ubuntu 20.04

A Virtual Host is an Apache arrangement mandate that permits you to run more than one site on a solitary worker. With Virtual Hosts, you can indicate the webpage report root (the registry containing the site records), make a different security strategy for each website, utilize diverse SSL declarations, and substantially more. 

This article portrays how to set up Apache Virtual Hosts on Ubuntu 20.04. 

Prerequisites

Guarantee that you have met the accompanying necessities before proceeding with the guide: 

  • At least one space names highlighting your public worker IP. 
  • Apache introduced on your Ubuntu framework. 
  • You are signed in as root or client with sudo benefits . 

Creating the Directory Structure

The archive root is where the site records for an area name are put away and served in light of solicitations. You can set the record root to any area you need, in this model, we will utilize the accompanying index structure: 

/var/www/
??? domain1.com
?   ??? public_html
??? domain2.com
?   ??? public_html

Every space facilitated on the worker will have its archive root set to/var/www/<domain_name>/public_html. 

Start by making the root catalog for the area: 

sudo mkdir -p /var/www/domain1.com/public_html

We'll likewise make an index.html record inside the area report root catalog that will be indicated when you visit the space in your program: 

/var/www/domain1.com/public_html/index.html
<!DOCTYPE html>
<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">
    <title>Welcome to domain1.com</title>
  </head>
  <body>
    <h1>Success! domain1.com home page!</h1>
  </body>
</html>

Since the orders above are executed as a sudo client, the recently made records and registries are claimed by root. To evade any consent issues change the responsibility for space record root catalog and all documents inside the registry to the apache client (www-information) : 

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

Creating a Virtual Hosts

On Ubuntu frameworks, Apache Virtual Hosts setup documents are situated in/and so on/apache2/destinations accessible catalog. They can be empowered by making emblematic connects to the/and so on/apache2/locales empowered index, which Apache read during the startup. 

Open your word processor of decision and make the accompanying fundamental Virtual Host design record: 

/etc/apache2/sites-available/domain1.com.conf
<VirtualHost *:80>
    ServerName domain1.com
    ServerAlias www.domain1.com
    ServerAdmin webmaster@domain1.com
    DocumentRoot /var/www/domain1.com/public_html

    <Directory /var/www/domain1.com/public_html>
        Options -Indexes +FollowSymLinks
        AllowOverride All
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/domain1.com-error.log
    CustomLog ${APACHE_LOG_DIR}/domain1.com-access.log combined
</VirtualHost>
  • ServerName: The space that should coordinate for this virtual host setup. This ought to be your area name. 
  • ServerAlias: All different areas or subdomains that should coordinate for this virtual host, for example, the www subdomain. 
  • DocumentRoot: The index from which Apache will serve the space records. 
  • Alternatives: This mandate controls which worker highlights are accessible in a particular index. 
    • - Indexes: Prevents registry postings. 
    • FollowSymLinks: When this choice is empowered, Apache will follow the emblematic connections. 
    • AllowOverride: Specifies which orders pronounced in the .htaccess document can abrogate the setup orders. 
  • ErrorLog, CustomLog: Specifies the area for log records. 

You can name the setup record as you like, yet the best practice is to utilize the space name as the name of the virtual host design document. 

To empower the new virtual host document, utilize the a2ensite aide content which makes a representative connection from the virtual host record to the locales empowered catalog: 

sudo a2ensite domain1.com

The other choice is to physically make a symlink as demonstrated as follows: 

sudo ln -s /etc/apache2/sites-available/domain1.com.conf /etc/apache2/sites-enabled/

When done, test the setup for any punctuation blunders with: 

sudo apachectl configtest

In the event that there are no mistakes, you will see the accompanying yield: 

Language structure OK 

Restart the Apache administration for the progressions to produce results: 

sudo systemctl restart apache2

At long last to confirm that everything is functioning true to form, open http://domain1.com in your program, and you will see the substance of the index.html page: 

Conclusion

You have figured out how to make an apache virtual host design to have various areas on a solitary Ubuntu worker. 

Rehash the means we sketched out above to make extra virtual hosts for every one of your spaces. 

In the event that you are confronting any issue, don't hesitate to leave a remark.




CFG