Skip to content
Search

Apache:Httpd:VirtualHost

lastmod 2025-02-14created 2017-01-025 edits

Explains how to configure virtual hosts in HTTPD.

If you use name-based virtual hosts, you must give the IP address used for the name-based virtual hosts in the NameVirtualHost directive. That is, the NameVirtualHost directive in the configuration file must specify the IP address that the host aliases (CNAME) of the name-based virtual hosts resolve to.

If there is no NameVirtualHost directive for a given IP address, the first virtual host containing that address is treated as an IP-based virtual host. You can also use the wildcard * for the IP address. As long as only one NameVirtualHost directive is used for a given IP:PORT pair, you can mix multiple NameVirtualHost and VirtualHost directives.

The order of the NameVirtualHost and VirtualHost directives does not matter.

Syntax

Usage is as follows.

NameVirtualHost *:80
<VirtualHost *:80>
...
</VirtualHost>

Example

A sample example follows.

## yourid.conf
<Directory "/var/www/blog">
    AllowOverride None
    Options None
    Require all granted
</Directory>

<Directory "/var/www/wiki">
    AllowOverride None
    Options None
    Require all granted
</Directory>

# Virtual Hosts
# You may use the command line option '-S' to verify your virtual host
# configuration.

# Use name-based virtual hosting.
# NameVirtualHost has no effect and will be removed in the next release
#NameVirtualHost *:80
#NameVirtualHost *:443

<VirtualHost *:80>
    DocumentRoot "/var/www/blog"
    ServerName server-project.com
    ServerAlias *.server-project.com

    ErrorLog "/var/log/httpd/yourid_blog_error_log"
    CustomLog "/var/log/httpd/yourid_blog_access_log" common
</VirtualHost>

<VirtualHost *:443>
    DocumentRoot "/var/www/html"
    ServerName server-project.com

    SSLEngine on
    #SSLOptions +StrictRequire
    #SSLRequireSSL
    SSLProtocol all
    SSLCipherSuite HIGH:MEDIUM

    #SSLRandomSeed startup file:/dev/urandom 1024
    #SSLRandomSeed connect file:/dev/urandom 1024

    SSLCertificateFile    "/etc/httpd/ssl/server.crt"
    SSLCertificateKeyFile "/etc/httpd/ssl/server.key"
</VirtualHost>

<VirtualHost *:443>
    DocumentRoot "/var/www/html"
    ServerName www.server-project.com
</VirtualHost>

<VirtualHost *:443>
    DocumentRoot "/var/www/wiki"
    ServerName wiki.server-project.com

    ErrorLog "/var/log/httpd/yourid_wiki_error_log"
    CustomLog "/var/log/httpd/yourid_wiki_access_log" common
</VirtualHost>

Virtual host configuration by type

See also

Favorite site

author Zer0 sitemap