How to Manage and Use Apache virtual hosts in Ubuntu

How to Manage and Use Apache virtual hosts in Ubuntu

In this post we will give you information about How to Manage and Use Apache virtual hosts in Ubuntu. Hear we will give you detail about How to Manage and Use Apache virtual hosts in Ubuntu And how to use it also give you demo for it if it is necessary.

What Is Apache Virtual Hosts?

Virtual Host term refers to the method of running over one website, such as yourdomain.com, test.yourdomain.com, or www.yourdomain.com, www.yourdomain2.com on a single system. There are two types of Virtual Hosting in Apache, namely IP-based virtual hosting and name-based virtual hosting. With IP-based virtual hosting, you can host multiple websites or domains on the same system, but each website/domain has a unique IP address. With name-based virtual hosting, you can host multiple websites/domains on the same IP address. Virtual hosting can be helpful if you want to host various websites and domains from a single physical server or VPS. I hope you got the basic idea of Apache virtual hosts. Today, we are going to see how to configure Apache virtual hosts in Ubuntu.

1. Prerequisites

  • The operating system running Ubuntu Linux
  • A root or non-root user with Sudo privileges
  • Has stable internet connection
  • Terminal window / Command line

2. Update Local Repositories

Update all system packages to the latest. Run the following command:

#! /bin/bash
sudo apt-get update
sudo apt-get upgrade -y

3. Installing Apache On VPS

Apache Virtual Host allows you to maximize your resources when setting up a website. This powerful software can use a single server and a single IP address to host many domains.

Before you can configure Apache Virtual Hosts, you need to install the Apache webserver. Run the following command:

#! /bin/bash
sudo apt-get install apache2

 

Type Y for Yes and press Enter.

If you have any problems setting up the webserver, refer to our in-depth tutorial on installing Apache on Ubuntu.

Apache should already be running. To check the status, run the following command:

Verify the installation by visiting the IP address of your server in your browser. You should see Apache2 Ubuntu Default Page will be displayed.

If this is not the case, some problems may have occurred while installing Apache. In this case, uninstalling Apache and proceeding with a new installation are advisable.

To remove Apache, run the following command:

#! /bin/bash
sudo apt-get purge apache2

4. Apache Virtual Host Directory Structure

Go to the directory where all the Apache configuration files are stored, run the following command:

#! /bin/bash
cd /etc/apache2

Once you are in, run the ls command, and you will see a few files and directories. Here are some critical directories and files you will see in the /etc/apache2 directory.

  • apache2.conf is Apache’s main configuration file.
  • conf-available holds all the extra configuration files.
  • conf-enabled contains symlinks of the configuration files that are enabled.
  • The envvars file contains environment variables.
  • mods-available contains all the available Apache modules.
  • mods-enabled contains symlinks of the modules that are allowed.
  • ports.conf include the port configuration (On which port do you want to listen for requests?).
  • sites-available contains all the virtual host files we create.
  • sites-enabled contains symlinks of all the virtual hosts that we want to enable.

We have to focus on sites-available and sites-enabled directories from all these files and directories because these directories will hold all our Virtual Host configuration files.

5. Configuring Virtual Host on Apache

The example of the Virtual Host configuration shown in this tutorial is name-based, since it relies on the website’s domain name to distinguish requests. There is another IP-based mode when multiple domains have to be each associated with a unique IP address.
In the example of this tutorial, mypersonaldomain.com will be used as a domain, but you will have to use an existing domain you own.
By accessing the server’s IP address where you have just installed Apache, you can generally find the content of your website inside a public folder at var/www/html.
If you want to host multiple websites, it’s easy to see how to create as many folders as there are websites provided that, for each website, the virtual host’s configuration file, which will make it accessible via the web, is correctly set up.
So, move inside /var/www and create the folder for your site. You may assign the corresponding domain name to each folder:

#! /bin/bash
cd /var/www
sudo mkdir -p yourdomain.com/{public_html,logs}

Add the following sample HTML:

#! /bin/bash
sudo nano /var/www/yourdomain.com/public_html/index.html
<html>
  <head>
    <title>Welcome to YourDomain.com!</title>
  </head>
  <body>
    <h1>Success! The yourdomain.com virtual host is working!</h1>
  </body>
</html>

Save and close this file as well.

Change file ownership of the directory, run the following command:

#! /bin/bash
sudo chown -R $USER:$USER /var/www/yourdomain.com/public_html

In the above code, we use $USER to grant permission to regular non-root users to access this directory.
We also need to change the file permissions of the Document Root folder to ensure that all files and pages are appropriately served.

#! /bin/bash
sudo chmod -R 755 /var/www

Now, create the first virtual host file in the sites-available directory, run the following command:

#! /bin/bash
sudo nano /etc/apache2/sites-available/yourdomain.conf

Now, Paste the following contents into the virtual host file. I will explain these directives in detail later in this guide.

<VirtualHost *:80>
    ServerAdmin [email protected]
    ServerName yourdomain.com
    ServerAlias www.yourdomain.com

    DocumentRoot /var/www/yourdomain.com/public_html

    ErrorLog /var/www/yourdomain.com/logs/error.log
    CustomLog /var/www/yourdomain.com/logs/access.log combined
</VirtualHost>

Save and close using the CTRL + X combination, then press Y and confirm by pressing ENTER.

Don’t forget to replace yourdomain.com with your domain name. Let’s first understand the directives used in this Virtual Host file.

  • ServerAdmin: The E-mail you provide in this directive will be displayed if the user encounters an Internal server error. So that they can contact you directly via E-mail.
  • ServerName: It’s the primary domain of your application. Here, I am using domain1.com. You can replace it with whatever domain name you want to host on your server.
  • ServerAlias: This directive holds all the different domains/subdomains you want to use to access the site. You can add multiple hostnames here, separated by space.
  • DocumentRoot: In this directive, define the directory in which you want to route the requests coming for this virtual host file if they enable it. It means that the requests coming on domain1.com will be routed to the /var/www/domain1.com/public_html directory.
  • ErrorLog: In this directive, define the path of the error log file. All the errors encountered in runtime will be logged in the file mentioned here.
  • CustomLog: This log file path you mention here will hold all the access logs.

And the <VirtualHost *:80> ... </VirtualHost> is the Virtual Host block. You can create multiple such blocks in a single yourdomain.conf file for multiple domains, and it will still work!

Then, enable the new site and disable the default Apache configuration:

#! /bin/bash
sudo a2ensite yourdomain

Once done, test the configuration for any syntax errors with:

#! /bin/bash
sudo apachectl configtest

The answer Syntax OK should be returned. Then, restart Apache to apply the changes and have the web server use your configuration file.

#! /bin/bash
sudo systemctl restart apache2

Now that you have your virtual hosts configured, you can test your setup by going to the domains that you configured in your web browser:

http://yourdomain.com

Thank you for reading this blog.

Also see: How To Install Vue 3 in Laravel 10 From Scratch

  .       .

If you have any queries or doubts about this topic please feel free to contact us. We will try to reach you.

Hope this code and post will helped you for implement How to Manage and Use Apache virtual hosts in Ubuntu. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US