Apache virtual host example: host a website from a folder
Host a website from a folder with Apache, with or without .htaccess.
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.
<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
Step-by-step setup
- Save the config as /etc/apache2/sites-available/example.com.conf.
- Enable URL rewriting (needed for .htaccess routing): sudo a2enmod rewrite.
- Enable the site: sudo a2ensite example.com.conf.
- Test it: sudo apache2ctl configtest.
- Apply it: sudo systemctl reload apache2.
- Add HTTPS: sudo certbot --apache -d example.com.
What the important lines mean
- DocumentRoot is the folder Apache serves: /var/www/example.com/public.
- AllowOverride All lets .htaccess files work. Without it, rewrite rules are ignored.
- Options -Indexes hides folder listings.
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.
Free tools that help
- Common ports listSearch a list of common TCP and UDP port numbers: SSH 22, HTTP 80, HTTPS 443, MySQL 3306, PostgreSQL 5432 and more, with dev server ports.
- chmod calculatorTick read, write and execute for owner, group and others to get the chmod number, symbolic form and command. Or type 755 and see what it means.
Fix a problem with this setup
- XAMPP Apache not startingWhy Apache stops right after starting in XAMPP, and how to change its port or free port 80.
- Localhost redirected too many timesFix ERR_TOO_MANY_REDIRECTS on localhost: clear cookies, fix HTTP and HTTPS rules, and WordPress URL settings.
- Refused to connectWork through this checklist in order. Most cases are a server that is not running or the wrong port.
- Hosts fileWhat localhost means, why 127.0.0.1 sometimes works when localhost does not, and how to make names like myapp.test.
More server configs
- logrotate configKeep log files from filling your disk: rotate, compress and delete old logs on a schedule.
- fail2ban jail.localBan addresses that keep failing to log in. Choose ban time, retries and which services to protect.
- PM2 ecosystem.config.jsRun a Node.js app with PM2: restarts on crash, memory limit, cluster mode and start at boot.
- WireGuard server config (wg0.conf)Set up a WireGuard VPN server on Linux with one peer, NAT and the right commands.
- WireGuard client configCreate the client file that connects to your WireGuard server, with DNS, allowed IPs and keepalive.
- nginx HTTPS server blockServe your site over HTTPS with nginx: certificate paths, HTTP to HTTPS redirect, HSTS and a proxy or static site.