Home / Configs / Servers / Apache virtual host

Apache virtual host example: host a website from a folder

Host a website from a folder with Apache, with or without .htaccess.

For Ubuntu and Debian with Apache 2.4. On Fedora or CentOS there is no a2ensite: put the file in /etc/httpd/conf.d/ and replace ${APACHE_LOG_DIR} with logs.

An Apache virtual host lets one server host a website from a chosen folder under your domain name. Fill in the form below, save the file in sites-available, and enable it.

Create your config file

Type your own values. The file below updates as you type, and nothing you type leaves your browser.

/etc/apache2/sites-available/example.com.conf
<VirtualHost *:80>
  ServerName example.com
  ServerAlias www.example.com
  ServerAdmin admin@example.com
  DocumentRoot /var/www/example.com/public

  <Directory /var/www/example.com/public>
    Options -Indexes +FollowSymLinks
    AllowOverride All
    Require all granted
  </Directory>

  ErrorLog ${APACHE_LOG_DIR}/example.com-error.log
  CustomLog ${APACHE_LOG_DIR}/example.com-access.log combined
</VirtualHost>

How this setup works

Diagram: Browser, then Apache, then Website folder
Browser (your domain) → Apache (matches ServerName) → Website folder (serves your files)

Step-by-step setup

  1. Save the config as /etc/apache2/sites-available/example.com.conf.
  2. Enable URL rewriting (needed for .htaccess routing): sudo a2enmod rewrite.
  3. Enable the site: sudo a2ensite example.com.conf.
  4. Test it: sudo apache2ctl configtest.
  5. Apply it: sudo systemctl reload apache2.
  6. Add HTTPS: sudo certbot --apache -d example.com.

What the important lines mean

Common errors and how to fix them

403 Forbidden
Apache cannot read /var/www/example.com/public. Check the folder path and that the www-data user can read it.
.htaccess rules are ignored
Turn on "Allow .htaccess rules" above and run sudo a2enmod rewrite.
AH00558: Could not reliably determine the server's fully qualified domain name
A harmless warning. Add ServerName example.com to /etc/apache2/apache2.conf to silence it.

Frequently asked questions

How do I enable an Apache virtual host?
Save the file in /etc/apache2/sites-available/, run sudo a2ensite yourdomain.conf, test with sudo apache2ctl configtest, then reload Apache.
Why are my .htaccess rules ignored?
The folder needs AllowOverride All, and for rewrite rules you must run sudo a2enmod rewrite.
How do I fix 403 Forbidden in Apache?
Make sure the folder has Require all granted, the path is correct, and the web server user (www-data) can read the folder and its parent folders.

Written for: Apache 2.4, Ubuntu 24.04 · Updated 2026-10-02

Free tools that help

Fix a problem with this setup

More server configs

See all 41 config generators