DEV Community

Oyolola toni
Oyolola toni

Posted on • Edited on

How to setup an Apache server on Ubuntu 22.04.4 LTS with Virtual hosting

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
Enter fullscreen mode Exit fullscreen mode

Next, install the Apache server

sudo apt install apache2
Enter fullscreen mode Exit fullscreen mode

Run this command to start the Apache server

systemctl start apache2.service
Enter fullscreen mode Exit fullscreen mode

Run this command to verify that Apache is running

systemctl status apache2.service
Enter fullscreen mode Exit fullscreen mode

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

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Insert the following code in the index.html **file by pressing **i **to write the file. And to save it press **ESQ **to 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>
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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>
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Then disable the default configuration file

sudo a2dissite 000-default.conf
Enter fullscreen mode Exit fullscreen mode

Check for configuration errors using the following command:

sudo apache2ctl configtest
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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

Top comments (0)