Free tool · Developer tools
Apache VirtualHost generator
Type a domain and a document root and the Apache VirtualHost generator writes the block for Apache 2.4. The default example gives 2 VirtualHost blocks with 13 directives for example.com and www.example.com, one that redirects port 80 and one that serves HTTPS.
- Free
- No sign-up
- Runs in your browser
2 VirtualHost blocksfor example.com and www.example.com
- 13 directives
- Ports 80 and 443
- HTTP redirects to HTTPS (301)
- HTTPS needs mod_ssl loaded and a Listen 443 line in the main configuration.
- ServerName
- example.com
- ServerAlias
- www.example.com
- DocumentRoot
- /var/www/example.com/public_html
- Folder access
- AllowOverride None, Require all granted
How to use the Apache VirtualHost Generator
- Type the Domain name and the Document root, the folder that holds the site files.
- Tick the options you need: a www alias, HTTPS with certificate paths, a redirect from HTTP, .htaccess files and log files.
- Read the result and the notes under it.
- Press Copy or Download .conf and add the block to your Apache configuration.
How do I write an Apache VirtualHost for a domain?
A name-based VirtualHost lets one Apache server host many sites on the same address. Each block starts with VirtualHost and a port, such as *:443, and holds a ServerName for the site, a DocumentRoot for its files and any other settings for that site. Apache picks the block whose ServerName or ServerAlias matches the host name in the request. If no block matches, the first block in the file answers, and DNS must already point the name at the server because the block does not create any DNS record.
For example.com with the default options the generator writes 2 VirtualHost blocks. The first handles port 80 and holds only a redirect, so it has three directives. The second handles port 443 and holds ten. The table lists the lines of the HTTPS block and what each does.
| Directive | Value | What it does |
|---|---|---|
| ServerName | example.com | The host name this block answers to |
| ServerAlias | www.example.com | Another name that reaches the same block |
| DocumentRoot | "/var/www/example.com/public_html" | The folder the files are served from |
How do I redirect HTTP to HTTPS in a VirtualHost?
Keep a small block on port 80 that holds the same ServerName and ServerAlias and one line, Redirect permanent "/" "https://example.com/". The word permanent makes Apache answer with status 301. The rest of the path and any query string are added to the new address, so a request for /page?x=1 goes to the same path on HTTPS. Without the keyword the redirect is a temporary 302.
If you tick HTTPS but turn the redirect off, the generator writes a full block for both ports instead, 17 directives in all, so the site answers on HTTP and on HTTPS. The certificate and key lines point at PEM files, and each key line needs a matching certificate line. SSLEngine is off unless a block turns it on, and the mod_ssl module must be loaded and Apache must listen on port 443.
What do Directory, AllowOverride and Require all granted do?
The Directory block sets the rules for the document root. Require all granted allows every request to files in that folder. AllowOverride decides whether .htaccess files may change settings. Its default is None, which makes Apache ignore those files, and the generator writes None unless you tick the .htaccess option. With the option on, the line becomes AllowOverride All, which lets any setting in a .htaccess file apply. A plain block has 7 directives either way, 7 with the option on, because only the value changes.
ErrorLog and CustomLog write per-site log files. The log folder defaults to /var/log/apache2, so the files for example.com are example.com-error.log and example.com-access.log in that folder. The access log uses an explicit format string, so no named format has to exist first. A relative folder such as logs is read from the server root, and on Debian and Ubuntu the server root is /etc/apache2, which has no logs folder, so the default is a full path. On Red Hat based systems use /var/log/httpd instead.
How do I check and load the new VirtualHost?
Add the block to a file your main configuration reads. Run apachectl configtest, which parses the files and prints Syntax Ok or a description of the error. Then run apachectl graceful. It restarts Apache without cutting open connections and, like a restart, checks the configuration first so Apache does not stop on a mistake.
This generator writes text only. It cannot see your server, your certificate files or your DNS, so test the site in a browser after the reload.
Use it with
- JSON Formatter: format and validate JSON.
- Cron Expression Generator: build and explain cron schedules.
- cURL to Python Converter: turn a curl command into Python requests code.
Questions
What is a VirtualHost in Apache?
A block that gives one site its own name, folder and settings on a server that hosts several. Apache chooses the block from the host name in the request.
How do I add a www alias?
Tick Also answer to www. The generator adds a ServerAlias line, which for example.com is www.example.com.
How do I redirect HTTP to HTTPS in Apache?
Put Redirect permanent "/" "https://example.com/" in a VirtualHost on port 80. It sends a 301 and keeps the path and query string.
Why do I get 403 Forbidden on a new site?
Check that the folder has an access rule and that the server may read the files. The Directory block written here includes Require all granted, which allows every request to that folder.
What does AllowOverride None mean?
Apache ignores .htaccess files in that folder. It is the default. Choose All only when a site needs to change settings through those files.
Does this page send my domain or paths anywhere?
No. The block is built in your browser from what you type, and nothing is uploaded.
Sources
- Apache HTTP Server 2.4: name-based virtual host examples, read
- Apache HTTP Server 2.4: mod_authz_core, read
- Apache HTTP Server 2.4: core directives, read
- Apache HTTP Server 2.4: mod_ssl, read
- Apache HTTP Server 2.4: mod_alias, read
- Apache HTTP Server 2.4: mod_log_config, read
- Apache HTTP Server 2.4: apachectl, read
The tool's results are computed in your browser by the code on this page, using the sources listed above; the explanations are checked against the tool's own output.