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.
🛠Prerequisites
- Ubuntu VM with sudo access
- Apache2 installed (
sudo apt install apache2 -y) - Domain names pointing to your server’s public IP (configured via DNS)
Step 1: Create Project Directory
Each site should have its own directory under /var/www/html.
Example for api.iquipe.cloud:
sudo mkdir -p /var/www/html/api
sudo chown -R www-data:www-data /var/www/html/api
sudo chmod -R 755 /var/www/html/api
Add a test file:
echo "<?php phpinfo(); ?>" | sudo tee /var/www/html/api/index.php
Step 2: Create Virtual Host Configuration
Apache stores site configs in /etc/apache2/sites-available/.
Create a new file:
sudo nano /etc/apache2/sites-available/api-your-domainname-com.conf
Add:
<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>
Step 3: Enable the Site
Enable your new Virtual Host:
sudo a2ensite api-your-domainname-com.conf
sudo systemctl reload apache2
Disable the default site if you don’t need it:
sudo a2dissite 000-default.conf
sudo systemctl reload apache2
Step 4: Test Access
Visit:
http://api-your-domainname-com
You should see the PHP info page.
Step 5: Enable SSL (Recommended)
Install Certbot:
sudo apt install certbot python3-certbot-apache -y
Request a certificate:
sudo certbot --apache -d api-your-domainname-com
Choose the redirect option so HTTP traffic is automatically redirected to HTTPS.
Step 6: Manage Multiple Virtual Hosts
Repeat the process for other domains (e.g., test.domainame.com). Each domain gets:
- 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)
✅ Conclusion
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.
Top comments (1)
replace the api-your-domainname-com with your domain name e.g. coremax.com or ai.coremax.com