Skip to main content

Featured

Create a Virtual Host in Apache 2 on Ubuntu 24.04

Virtual Hosts in Apache2 allow you to host multiple websites on a single server. This is extremely useful if you want to separate projects, run multiple domains, or simply organize your server more efficiently. Each Virtual Host can have its own configuration, document root, and domain name. In this tutorial, you’ll learn how to create and enable a Virtual Host in Apache2 on Ubuntu 24.04.

Install Apache 2 on Ubuntu 24.04

Apache2 is one of the most popular and reliable open-source web servers. If you’re running Ubuntu 24.04 and want to host websites or applications, Apache2 is an excellent choice. In this guide, you’ll learn how to install and configure Apache2 step by step.

Update Your Package Index

Before installing new software, update the package list to ensure you’re getting the latest versions:

sudo apt update

Install Apache2

Run the following command to install Apache2:

sudo apt install apache2 -y

This will install Apache2 along with all its necessary dependencies.

Configure the Apache Environment Variables

By default, Apache runs under the www-data user. However, you may want Apache to run under your Linux username (for example, gerson).

Open the file:

sudo nano /etc/apache2/envvars

Find these lines:

export APACHE_RUN_USER=www-data
export APACHE_RUN_GROUP=www-data

Change www-data to your Linux username. Example:

export APACHE_RUN_USER=gerson
export APACHE_RUN_GROUP=gerson

💡 Note: If you’re not sure what your Linux username is, run this command in your terminal:

echo $USER

It will print your current user.

Adjust Apache’s Main Configuration

Next, allow .htaccess overrides and enable directory options.

Open the main configuration file:

sudo nano /etc/apache2/apache2.conf

Find this section:

<Directory /var/www/>
        Options Indexes FollowSymLinks
        AllowOverride None
        Require all granted
</Directory>

Change AllowOverride None to:

AllowOverride All

This enables the use of .htaccess files for flexible per-directory configurations.

Enable Important Apache Modules

Apache comes with powerful modules, but they are not all enabled by default. Let’s enable headers and rewrite, which are essential for modern websites.

sudo a2enmod headers
sudo a2enmod rewrite

Restart Apache2

After making configuration changes, restart Apache to apply them:

sudo systemctl restart apache2

Set Correct Permissions

Make sure you own the /var/www/html directory so you can easily add or edit website files:

sudo chown -R $USER:$USER /var/www/html

Verify Apache Installation

Open a web browser and visit:

http://localhost

If Apache is running correctly, you’ll see the Apache2 Ubuntu Default Page.

Conclusion

You’ve successfully installed and configured Apache2 on Ubuntu 24.04. Your server is now ready to host static websites, dynamic applications, or serve as the foundation for frameworks like WordPress, Laravel, or Django.

By enabling .htaccess overrides, activating key modules, and setting proper permissions, you’ve ensured that Apache is flexible and secure for your projects.

Comments

Popular Posts