How to set up Apache virtual host on Ubuntu 22.04

Setting up an Apache virtual host on Ubuntu 22.04 involves creating separate configurations for different websites or applications that are hosted on the same server. Here's a step-by-step guide:

Step 1 : Install Apache:

If you haven't already, you can install Apache on your Ubuntu 22.04 system using the following command:

Step 2 : Create a Directory Structure:

Create directories to house your virtual host's files and then create an index.html file inside the public_html directory. Replace example.com with your domain or app name:

In the index.html file, add some basic HTML content like this:

Step 3 : Set Permissions:

Set the appropriate permissions for the directory so that Apache can access it:

Step 4 : Create a Virtual Host Configuration:

Create a new virtual host configuration file in the /etc/apache2/sites-available/ directory. Replace example.com with your domain or application name:

Inside the file, add the following content as a starting point for your virtual host configuration:

Adjust the values to match your specific configuration.

Step 5 : Enable the Virtual Host:

Create a symbolic link from the configuration file in the sites-available directory to the sites-enabled directory to enable the virtual host:

Step 6 : Disable the Default Host (if needed):

If the default Apache virtual host is enabled, you might want to disable it to avoid conflicts:

Step 7 : Test Configuration and Restart Apache:

Check if the Apache configuration is valid:

If the test is successful, restart Apache to apply the changes:

Step 8 : Update Hosts File (Optional):

If you're setting up a local development environment, you might want to update your system's hosts file to map your domain to the local IP address (127.0.0.1):

Add a line like this:

Step 9 : Upload Your Files:

Upload your website or application files to the directory you specified in the virtual host configuration (/var/www/example.com/public_html in this example).

Step 10 : Access Your Virtual Host:

Open a web browser and enter the virtual host's URL. For instance, if it's example.com, enter http://example.com

Your Apache virtual host should now be set up and accessible. Remember to customize the configurations according to your specific requirements.