Configure a Basic Apache HTTP Virtual Host for Most Linux



  • For the majority of Linux operating systems, such as CentOS, RHEL and Fedora, Apache (sometimes known as HTTPD) VirtualHosts are handled in a convenient directory with one file per VirtualHost.

    A VirtualHost is an individual website on an Apache webserver identified as a unique site such as by a host header.

    To add a new, basic site into Apache we need only go into the configuration directory for VirtualHosts and add the file that we want to use, Apache will add this to its overall configuration file.

    cd /etc/httpd/conf.d/
    

    Now we can use vi to create a new file. Name it something descriptive, and end the file with .conf. So for example, if we are making a website "www.bacontastesgreat.com" we might make bacontastesgreat.conf:

    vi bacontastesgreat.conf
    

    Then we just need to input configuration such as this:

    <VirtualHost *:80>
        ServerName www.bacontastesgreat.com 
        ServerAlias bacontastesgreat.com 
        DocumentRoot /var/www/html/bacon
        <Directory /var/www/html/bacon>
            Options FollowSymLinks
            AllowOverride All 
            Order allow,deny
            allow from all
       </Directory>
    </VirtualHost>
    

    That's it. I'll explain the four fields in which we are doing something unique.

    ServerName: This is the default name of the website. The "master" URL if you will. This is the host header that Apache will be looking for in order to direct traffic to this particular site.
    ServerAlias: This is one or more URLs that Apache will also direct to this site, aliases for the ServerName.
    DocumentRoot: The base directory where the website is contained.
    Directory: This is a directive section that gives security and behaviour commands for the website directory.

    In our example, our website is kept in the /var/www/html/bacon directory. If you are used to CentOS, you will recognize /var/www/html as the default location for Apache hosted websites.



  • It's nearly the same for every Debian-based distribution like Debian, Ubuntu, Mint etc.

    Only difference: You need to create your virtual host config in /etc/apache2/sites-available. Next, create a symlink

    ln -s /etc/apache2/sites-available/awesome-com.conf /etc/apache2/sites-enabled/awesome-com.conf
    

    Restart Apache

    service apache2 restart
    

    @scottalanmiller : Red Hat-based distributions are very common, but I wouldn`t say "The majority of Linux":



  • Another quick question: Your title says HTTPS, but I can't see any SSL*-related option (e.g. SSLCertificateFile, SSLCertificateKeyFile, SSLCertificateChainFile)?



  • @thwr said in Configure a Basic Apache HTTPS Virtual Host for Most Linux:

    Another quick question: Your title says HTTPS, but I can't see any SSL*-related option (e.g. SSLCertificateFile, SSLCertificateKeyFile, SSLCertificateChainFile)?

    Fixed, was purely a typo.



  • @scottalanmiller Also, using just using the root domain name for the conf file name is potentially confusing depending on how much you are hosting.

    Even for my gaming website, I have a bunch of sub domains setup.

    www.daerma.com.conf goes to the root and www.
    oc.daerma.com.conf is owncloud
    obelisk.daerma.com.conf is my nodeBB forum

    That does not even get into what do you do about the ones that you have multiple TLD names for such as .it, .com, .org, etc.