Logo

How to Set Up Apache virtual host on Ubuntu 24.04

Mar 15, 2024

To install and configure Apache virtual host on Ubuntu 24.04, follow these steps:

Step 1 : First, make sure your package lists are up-to-date by running:

sudo apt update

Step 2 : If Apache is not already installed, you can install it by running:

sudo apt install apache2

Step 3 : Create a directory to store your website files. For example:

sudo mkdir /var/www/example.com

Step 4 : Create a simple HTML file as the main page of your website:

sudo nano /var/www/example.com/index.html

Add some content to this file, such as:

<!DOCTYPE html>
<html>
<head>
    <title>Welcome to Example.com</title>
</head>
<body>
    <h1>Hello, World!</h1>
    <p>This is the default page of example.com.</p>
</body>
</html>

Step 5 : Make sure the directory and files are accessible by Apache:

sudo chown -R www-data:www-data /var/www/example.com
sudo chmod -R 755 /var/www/example.com

Step 6 : Create a new virtual host configuration file. For example:

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

Add the following content, replacing example.com with your domain name:

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

Step 7 : Enable the virtual host configuration you just created:

sudo a2ensite example.com.conf

Step 8 : Test the Apache configuration for syntax errors:

sudo apache2ctl configtest

If the test is successful, restart Apache for the changes to take effect:

sudo systemctl restart apache2

Step 9 : Open a web browser and navigate to your domain (e.g., http://example.com) to see your website.

Congratulations! You have successfully set up and configured an Apache virtual host on Ubuntu 24.04.

Recommended