How to Create an Apache Virtual Host on Ubuntu (24.04 / 22.04)

Quick answer: To create an Apache virtual host on Ubuntu, make a folder for the site in /var/www/, create a config file in /etc/apache2/sites-available/ with a <VirtualHost> block (ServerName and DocumentRoot), enable it with sudo a2ensite, test with sudo apache2ctl configtest, and reload Apache with sudo systemctl reload apache2.

Apache virtual hosts let one Ubuntu server host many websites, each with its own domain and folder. This guide works on Ubuntu 24.04 LTS and 22.04 LTS (and the same steps apply to older releases such as 20.04 and 18.04). You’ll set up two sites, test the configuration, and add free HTTPS.

What you need

  • An Ubuntu server with a user that has sudo access
  • A domain name pointing to the server’s IP address (an A record in your DNS)
  • Port 80 (and 443 for HTTPS) open in your firewall

Step 1: Install Apache

sudo apt update
sudo apt install apache2
sudo ufw allow 'Apache Full'   # if you use the UFW firewall

Visit your server’s IP address in a browser. You should see the default Apache page.

Step 2: Create the site folders

sudo mkdir -p /var/www/example.com/public_html
sudo chown -R $USER:$USER /var/www/example.com/public_html
sudo chmod -R 755 /var/www

Add a test page:

echo '<h1>example.com works</h1>' > /var/www/example.com/public_html/index.html

Step 3: Create the virtual host file

sudo nano /etc/apache2/sites-available/example.com.conf

Paste:

<VirtualHost *:80>
    ServerName example.com
    ServerAlias www.example.com
    ServerAdmin webmaster@example.com
    DocumentRoot /var/www/example.com/public_html

    <Directory /var/www/example.com/public_html>
        AllowOverride All
        Require all granted
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/example.com_error.log
    CustomLog ${APACHE_LOG_DIR}/example.com_access.log combined
</VirtualHost>

AllowOverride All lets .htaccess files work, which WordPress and many PHP apps need.

Step 4: Enable the site and test

sudo a2ensite example.com.conf
sudo a2dissite 000-default.conf   # optional: disable the default site
sudo a2enmod rewrite              # needed for pretty URLs
sudo apache2ctl configtest        # should print "Syntax OK"
sudo systemctl reload apache2

Open http://example.com — you should see your test page. Repeat Steps 2–4 with a different domain and folder for each extra site.

Step 5: Add free HTTPS with Let’s Encrypt

sudo apt install certbot python3-certbot-apache
sudo certbot --apache -d example.com -d www.example.com

Certbot creates the HTTPS virtual host, can redirect HTTP to HTTPS, and sets up automatic renewal. Check renewal with sudo certbot renew --dry-run.

Troubleshooting

Problem Likely cause and fix
Default Apache page still shows The site isn’t enabled or the default site takes priority — run a2ensite, disable 000-default, reload.
403 Forbidden Folder permissions or a missing Require all granted in the Directory block.
Domain doesn’t load at all DNS not pointing to the server yet, or firewall blocking port 80/443. Check with ping example.com.
“Syntax error” on configtest A typo in the .conf file — the error message shows the file and line number.
WordPress permalinks give 404 Enable mod_rewrite and make sure AllowOverride All is set.

Related guides

Frequently asked questions

Where are Apache virtual host files stored on Ubuntu?

In /etc/apache2/sites-available/. Enabled sites are symlinked into /etc/apache2/sites-enabled/ by a2ensite.

How many virtual hosts can one Apache server run?

There’s no fixed limit — it depends on your server’s memory, CPU and traffic. Small VPS servers commonly host several low-traffic sites.

Do I need a separate IP address for each site?

No. Name-based virtual hosts let many domains share one IP address, including with HTTPS.

What’s the difference between sites-available and sites-enabled?

sites-available holds all your configs; sites-enabled contains only the ones Apache actually loads.

Sources and further reading

Leave a Reply

Your email address will not be published. Required fields are marked *