apache tutorial - Name-based virtual host configuration - apache - apache web server - apache server - apache2




Name-based virtual host configuration in Apache

Name-based virtual hosting on Apache is described on the Apache website as such:

  • With name-based virtual hosting, the server relies on the client to report the hostname as part of the HTTP headers. Using this technique, many different hosts can share the same IP address.

Therefore, more than one website can be hosted on one server through this method. On ubuntu, the configuration files are in /etc/apache2/sites-available. In that directory, you will find 000-default.conf. That is the default configuration, all requests will be sent to this configuration file until others have been set up.

To set up a virtual host, here example.com will be used, but you should replace it with your domain.com. Copy the default file:

 cp 000-default.conf example.com.conf
click below button to copy the code. By Apache tutorial team

The configuration file can have the following directives:

 <VirtualHost *:80>
        ServerAdmin [email protected]
        ServerName example.com
        ServerAlias www.example.com

        DocumentRoot /var/www/example.com/html
        
        ErrorLog /var/log/apache/logs/error.log
        # Possible values include: debug, info, notice, warn, error, crit,
        # alert, emerg.
        LogLevel warn

        CustomLog /var/log/apache/logs/access.log combined    
</VirtualHost>
click below button to copy the code. By Apache tutorial team
  • The first line, indicates that all requests on port 80 (default http port) should be matched. You can also have a IP address instead of * which is the IP of the server.
  • ServerAdmin is the contact details of website admin used for displaying with http error messages.
  • ServerName is the domain name of website.
  • ServerAlias is a secondary name of the website, usually will be www.domain.com
  • DocumentRoot is the root folder loaded when we browse a website.
  • ErrorLog is the file in where errors are directed
  • LogLevel. is the level of errors to be sent to the log
  • CustomLog is the file where access information is directed

Edit the file replacing example.com with your website domain name and appropriate directory for the website files.

Save the file and enable the site with the following Apache command:

sudo a2ensite example.com.conf
click below button to copy the code. By Apache tutorial team

Reload apache

sudo service apache2 reload
click below button to copy the code. By Apache tutorial team

A few more things that must be checked:

  • Ensure your DNS for your domain is set up for the correct IP (this may take time to propogate)
  • Ensure your port 80 is open on the firewall
  • Ensure your file permissions are setup correctly on the server files - ownership should be www-data:www-data and directory permissions should be 750 and file permissions should be 640.

Your virtual host should be up and running! You can repeat this for other websites on the same server, with a different configuration file (using the same naming convention) and different directories under /var/www.


Related Searches to Name-based virtual host configuration in Apache