UbuntuUbuntu Based

How To Setup Virtual Host Apache on Ubuntu 24.04 LTS

Setup Virtual Host Apache on Ubuntu 24.04

Apache, the widely-used open-source web server, plays a crucial role in hosting websites across the internet. One of its powerful features is the ability to set up virtual hosts, allowing you to host multiple domains on a single server. In this comprehensive guide, we will walk you through the step-by-step process of setting up virtual hosts with Apache on Ubuntu 24.04 LTS, the latest long-term support release of the popular Linux distribution.

Prerequisites

Before diving into the virtual host setup, ensure that you have the following prerequisites in place:

  • A running instance of Ubuntu 24.04 LTS
  • SSH access to your server
  • A registered domain name pointing to your server’s IP address
  • Basic knowledge of the Linux command line

With these requirements met, let’s proceed to the first step of our virtual host configuration journey.

Step 1: Update and Upgrade the System

To ensure your Ubuntu server is up to date with the latest security patches and software updates, run the following commands:

sudo apt update
sudo apt upgrade

These commands will refresh the package list and upgrade any outdated packages to their latest versions, providing a stable and secure foundation for your virtual host setup.

Step 2: Install Apache Web Server

If Apache is not already installed on your Ubuntu server, you can easily install it using the APT package manager. Execute the following command:

sudo apt install apache2

Once the installation is complete, verify that Apache is running by checking its service status:

sudo systemctl status apache2

If Apache is active and running, you’ll see a green “active (running)” status, indicating that the web server is ready to serve your virtual hosts.

Step 3: Configure Firewall Settings

To ensure that your virtual hosts are accessible from the internet, you need to configure your firewall to allow HTTP and HTTPS traffic. Ubuntu 24.04 LTS comes with the UFW (Uncomplicated Firewall) utility, which simplifies firewall management. Run the following command to allow Apache traffic:

sudo ufw allow 'Apache Full'

This command will open the necessary ports for both HTTP (port 80) and HTTPS (port 443) traffic, enabling your virtual hosts to be accessed by visitors.

Step 4: Set Up the Directory Structure for Your Website

Create a directory to store your website’s files and content. For the purpose of this guide, let’s assume your domain name is “your_domain”. Execute the following command to create the directory:

sudo mkdir -p /var/www/html/your_domain

Next, set the appropriate permissions and ownership for the directory to ensure that your web server can access and serve the files:

sudo chown -R $USER:$USER /var/www/html/your_domain
sudo chmod -R 755 /var/www/html/your_domain

These commands will grant your user account ownership of the directory and set the necessary permissions for the webserver to read and execute the files.

Step 5: Create a Virtual Host Configuration File

Navigate to the Apache sites-available directory and create a new configuration file for your virtual host:

sudo nano /etc/apache2/sites-available/your_domain.conf

Inside the file, add the following configuration, replacing “your_domain” with your actual domain name:

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

This configuration block defines the basic settings for your virtual host, including the server name, document root, and log file locations. Save the file and exit the editor.

Step 6: Enable the New Virtual Host Configuration

To activate your newly created virtual host configuration, use the a2ensite command:

sudo a2ensite your_domain.conf

If you have the default Apache configuration enabled, it’s recommended to disable it to avoid conflicts:

sudo a2dissite 000-default.conf

Finally, reload Apache to apply the changes:

sudo systemctl reload apache2

Your virtual host is now enabled and ready to serve your website.

Step 7: Test Your Virtual Host Setup

Open a web browser and access your domain name (e.g., http://your_domain). If everything is set up correctly, you should see your website’s content displayed.

If you encounter any issues, you can troubleshoot by checking the Apache error logs:

less /var/log/apache2/error.log

The error logs will provide valuable information to help you identify and resolve any configuration or file permission issues.

Step 8: Secure Your Site with SSL Using Let’s Encrypt

To add an extra layer of security to your virtual host and enable HTTPS, you can obtain an SSL certificate from Let’s Encrypt, a free and automated certificate authority. Install the Certbot client and the Apache plugin:

sudo apt install certbot python3-certbot-apache

Next, run Certbot to obtain and install the SSL certificate for your domain:

sudo certbot --apache -d your_domain -d www.your_domain

Certbot will guide you through the process, automatically configure Apache to use the SSL certificate, and set up automatic renewal to keep your certificate valid.

To test the automatic renewal process, you can run the following command:

sudo certbot renew --dry-run

This command simulates the renewal process without actually obtaining a new certificate, ensuring that the renewal configuration is working as expected.

Congratulations! You have successfully installed Apache. Thanks for using this tutorial to set up virtual hosts Apache web server on Ubuntu 24.04 LTS Focal Fossa system. For additional help or useful information, we recommend you check the official Apache website.

VPS Manage Service Offer
If you don’t have time to do all of this stuff, or if this is not your area of expertise, we offer a service to do “VPS Manage Service Offer”, starting from $10 (Paypal payment). Please contact us to get the best deal!

r00t

r00t is a seasoned Linux system administrator with a wealth of experience in the field. Known for his contributions to idroot.us, r00t has authored numerous tutorials and guides, helping users navigate the complexities of Linux systems. His expertise spans across various Linux distributions, including Ubuntu, CentOS, and Debian. r00t's work is characterized by his ability to simplify complex concepts, making Linux more accessible to users of all skill levels. His dedication to the Linux community and his commitment to sharing knowledge makes him a respected figure in the field.
Back to top button