How to setup an Apache server on Ubuntu 22.04.4 LTS with Virtual hosting
Tags: apache, devops, ubuntu, selfhosting
Published: 2024-05-31 15:45:40


![](https://cdn-images-1.medium.com/max/2000/1*dXU1djueJapzeCDXKq2jzA.jpeg)

Apache is a popular open source web server that is widely used to host web pages.

In this tutorial, you’ll be creating a virtual host environment to run multiple websites with Apache on your Ubuntu 22.04.4 LTS server allowing you to add several domains hosted on just 1 IP address.

## Installing Apache

We’ll begin by updating the local repository with the following command

    sudo apt update

Next, install the Apache server

    sudo apt install apache2

Run this command to start the Apache server

    systemctl start apache2.service

Run this command to verify that Apache is running

    systemctl status apache2.service
    

You can test if your Apache server is live by typing your public IP address in your web server, you should get the following result

![apache home page](https://cdn-images-1.medium.com/max/2000/1*KSdvA3MKETakRXlOn4iElA.png)

## Configuring Your Own Website

Apache by default is setup to serve documents from **/var/www. **So you will need to create store your webpage directory in this folder. We will be using demo.com here as follows:

    sudo mkdir -p /var/www/demo.com

You will need to modify the ownership of this directory so as to be accessible and executable by your user account. This is done by executing the following:

    sudo chown -R $USER:$USER /var/www/demo.com

Next you will change the permission settings of the directory to allow everyone to have read and execute permissions while the $USER has read, write and execute permissions

    sudo chmod -R 755 /var/www/demo.com

Next create an **index.html** file that will be used to host your site’s html code

    sudo vim /var/www/demo.com/index.html

Insert the following code in the **index.html **file by pressing **i **to write the file. And to save it press **ESQ **t**o exit insert mode, **and **:wq **after to save and quit the the file editor

    <html>
        <head>
            <title>Welcome to Your_domain!</title>
        </head>
        <body>
            <h1>Success!  The your_domain virtual host is working!</h1>
        </body>
    </html>

## Setting up the VirtualHost Configuration File

The next step is to setup the configuration file for the webserver in the **/etc/apache2** folder — which is where the Apache configuration file is located. The file also uses the **.conf **extension. Create the configuration file using the following command

    sudo vim /etc/apache2/sites-available/demo.com.conf

Insert this code into the file

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

Save and close the file

## Activating VirtualHost file

After setting up the website, the next step is to activate the virtual host file. Do that by running the following commands

First enable the file with the **a2ensite **command

    sudo a2ensite demo.com.conf

Then disable the default configuration file

    sudo a2dissite 000-default.conf

Check for configuration errors using the following command:

    sudo apache2ctl configtest

The output of that command should be “ Syntax OK” to indicate that it is properly configured

Restart the Apache server to implement the changes

    sudo systemctl restart apache2

Congrats! Your website is now hosted on your machine. To view the website navigate to http://demo.com