Tools: How to Set Up Apache Virtual Hosts on an Ubuntu VM (2026)

Tools: How to Set Up Apache Virtual Hosts on an Ubuntu VM (2026)

🛠 Prerequisites

Step 1: Create Project Directory

Step 2: Create Virtual Host Configuration

Step 3: Enable the Site

Step 4: Test Access

Step 5: Enable SSL (Recommended)

Step 6: Manage Multiple Virtual Hosts

✅ Conclusion Apache Virtual Hosts allow you to host multiple websites or applications on a single server. Each domain or subdomain can point to its own directory, making it possible to run separate projects side by side. Each site should have its own directory under /var/www/html. Example for api.iquipe.cloud: Apache stores site configs in /etc/apache2/sites-available/. Enable your new Virtual Host: Disable the default site if you don’t need it: You should see the PHP info page. Request a certificate: Choose the redirect option so HTTP traffic is automatically redirected to HTTPS. Repeat the process for other domains (e.g., test.domainame.com). Each domain gets: With Virtual Hosts, you can host multiple independent sites on one Ubuntu VM. Each domain points to its own directory, and Apache handles routing. Adding SSL ensures secure connections for all your hosted applications. Templates let you quickly answer FAQs or store snippets for re-use. replace the api-your-domainname-com with your domain name e.g. coremax.com or ai.coremax.com Hide child comments as well For further actions, you may consider blocking this person and/or reporting abuse

Command

Copy

$ -weight: 600;">sudo mkdir -p /var/www/html/api -weight: 600;">sudo chown -R www-data:www-data /var/www/html/api -weight: 600;">sudo chmod -R 755 /var/www/html/api -weight: 600;">sudo mkdir -p /var/www/html/api -weight: 600;">sudo chown -R www-data:www-data /var/www/html/api -weight: 600;">sudo chmod -R 755 /var/www/html/api -weight: 600;">sudo mkdir -p /var/www/html/api -weight: 600;">sudo chown -R www-data:www-data /var/www/html/api -weight: 600;">sudo chmod -R 755 /var/www/html/api echo "<?php phpinfo(); ?>" | -weight: 600;">sudo tee /var/www/html/api/index.php echo "<?php phpinfo(); ?>" | -weight: 600;">sudo tee /var/www/html/api/index.php echo "<?php phpinfo(); ?>" | -weight: 600;">sudo tee /var/www/html/api/index.php -weight: 600;">sudo nano /etc/apache2/sites-available/api-your-domainname-com.conf -weight: 600;">sudo nano /etc/apache2/sites-available/api-your-domainname-com.conf -weight: 600;">sudo nano /etc/apache2/sites-available/api-your-domainname-com.conf <VirtualHost *:80> ServerName api-your-domainname.com DocumentRoot /var/www/html/api <Directory /var/www/html/api> Options FollowSymLinks AllowOverride All Require all granted </Directory> ErrorLog ${APACHE_LOG_DIR}/api_error.log CustomLog ${APACHE_LOG_DIR}/api_access.log combined </VirtualHost> <VirtualHost *:80> ServerName api-your-domainname.com DocumentRoot /var/www/html/api <Directory /var/www/html/api> Options FollowSymLinks AllowOverride All Require all granted </Directory> ErrorLog ${APACHE_LOG_DIR}/api_error.log CustomLog ${APACHE_LOG_DIR}/api_access.log combined </VirtualHost> <VirtualHost *:80> ServerName api-your-domainname.com DocumentRoot /var/www/html/api <Directory /var/www/html/api> Options FollowSymLinks AllowOverride All Require all granted </Directory> ErrorLog ${APACHE_LOG_DIR}/api_error.log CustomLog ${APACHE_LOG_DIR}/api_access.log combined </VirtualHost> -weight: 600;">sudo a2ensite api-your-domainname-com.conf -weight: 600;">sudo -weight: 500;">systemctl reload apache2 -weight: 600;">sudo a2ensite api-your-domainname-com.conf -weight: 600;">sudo -weight: 500;">systemctl reload apache2 -weight: 600;">sudo a2ensite api-your-domainname-com.conf -weight: 600;">sudo -weight: 500;">systemctl reload apache2 -weight: 600;">sudo a2dissite 000-default.conf -weight: 600;">sudo -weight: 500;">systemctl reload apache2 -weight: 600;">sudo a2dissite 000-default.conf -weight: 600;">sudo -weight: 500;">systemctl reload apache2 -weight: 600;">sudo a2dissite 000-default.conf -weight: 600;">sudo -weight: 500;">systemctl reload apache2 http://api-your-domainname-com http://api-your-domainname-com http://api-your-domainname-com -weight: 600;">sudo -weight: 500;">apt -weight: 500;">install certbot python3-certbot-apache -y -weight: 600;">sudo -weight: 500;">apt -weight: 500;">install certbot python3-certbot-apache -y -weight: 600;">sudo -weight: 500;">apt -weight: 500;">install certbot python3-certbot-apache -y -weight: 600;">sudo certbot --apache -d api-your-domainname-com -weight: 600;">sudo certbot --apache -d api-your-domainname-com -weight: 600;">sudo certbot --apache -d api-your-domainname-com - Ubuntu VM with -weight: 600;">sudo access - Apache2 installed (-weight: 600;">sudo -weight: 500;">apt -weight: 500;">install apache2 -y) - Domain names pointing to your server’s public IP (configured via DNS) - Its own directory under /var/www/html/ - Its own config file under /etc/apache2/sites-available/ - Its own SSL certificate (or a shared multi‑domain certificate) - Joined Jul 16, 2024