ML
    • Recent
    • Categories
    • Tags
    • Popular
    • Users
    • Groups
    • Register
    • Login

    Configure a Basic Apache HTTP Virtual Host for Most Linux

    IT Discussion
    apache httpd centos fedora korora rhel linux webserver virtualhost
    3
    5
    2.0k
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • scottalanmillerS
      scottalanmiller
      last edited by scottalanmiller

      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.

      1 Reply Last reply Reply Quote 2
      • thwrT
        thwr
        last edited by thwr

        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":

        • https://w3techs.com/technologies/details/os-linux/all/all (webservers, and "Unix" has a wide definition there)
        • https://brashear.me/blog/2015/08/24/results-of-the-2015-slash-r-slash-linux-distribution-survey/ (community focused, but take a look at "Which Linux distribution do you primarily use on your server computers?")
        • https://distrowatch.com/dwres.php?resource=popularity (not server related)
        • https://www.linux.com/news/best-linux-distros-2016 ("best" is debatable)
        1 Reply Last reply Reply Quote 1
        • thwrT
          thwr
          last edited by thwr

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

          scottalanmillerS 1 Reply Last reply Reply Quote 0
          • scottalanmillerS
            scottalanmiller @thwr
            last edited by

            @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.

            1 Reply Last reply Reply Quote 1
            • JaredBuschJ
              JaredBusch
              last edited by JaredBusch

              @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.

              1 Reply Last reply Reply Quote 1
              • 1 / 1
              • First post
                Last post