Apache web server can host different websites to the same server. There is no need for certain server machines and soft for each of websites. All this is possible due to the Virtual Hosting (VHost) concept.
During website hosting, each of them receives a certain record in the Apache configuration. There are several types of virtual hosting which differ with system and functional peculiarities.
What Are the Types of Apache Virtual Hostings?
- Name-based
- IP-based
Name-based virtual hostings are used for hosting various virtual websites to one IP address. For configuring such type virtual hostings, it is required to install IP address through which the Apache requests will be received from all necessary websites. One may add as many websites to the name-based virtual hostings as it will be required.
In order to launch IP-based virtual hosting, it will need more than one address with the configuration on the required server. Thus, the number of hostings will depend on the IP address numbers. For example, if the server has 5 IP addresses, then one can create 5 virtual hostings on their IP base.
Virtual hosting is the best solution for small projects with the limited financing. The point is, that such site resources as traffic, processing time, core memory will be divided between all site users on the one server. If the vizitorization is high, then the website functionality will decrease.
Crate Apache Virtual Host
Lest assume we want to create virtual host for our website mydomain.com . All virtual host files are place on directory /etc/apache2/sites-available/ . So lets create virtual host file or our domain mydomain.com :
sudo nano /etc/apache2/sites-available/mydomain.com.conf
Now put below content on mydomain.com.conf file :
<VirtualHost *:80> # Admin email, Server Name (domain name) and any aliases ServerAdmin [email protected] ServerName mydomain.com ServerAlias www.mydomain.com # Index file and Document Root (where the public files are located) DirectoryIndex index.html index.php DocumentRoot /var/www/html/mydomian.com # Custom log file locations LogLevel warn ErrorLog /var/www/html/mydomian.com/error.log CustomLog /var/www/html/mydomian.com/access.log combined </VirtualHost>
You need to change DocumentRoot,vDirectoryIndex, LogLevel, ErrorLog & CustomLog according to your directory structure.
Press ctrl+o to save file and then ctrl+x to exit the editor
Now execute below command to enable this site:
sudo a2ensite mydomain.com
Setup Local Hosts File
if we are not using actual domain names or we are doing this setup in our localhost, then we need to setup localhost files. Use below command to open local hosts file in edit mode:
sudo nano /etc/hosts
You will see entries like this :
127.0.0.1 localhost
For localhost, add below command on this file:
127.0.0.1 mydomain.com 127.0.0.1 www.mydomain.com
For hosting server, add below command on this file:
server_ip_address mydomain.com server_ip_address www.mydomain.com
Please replace ‘server_ip_address with your real server ip address.
Now access http://mydomain.com in browser.
LogLevel Values
- Trace – Only when I would be “tracing” the code and trying to find one part of a function specifically.
- Debug – Information that is diagnostically helpful to people more than just developers (IT, sysadmins, etc.).
- Info – Generally useful information to log (service start/stop, configuration assumptions, etc). Info I want to always have available but usually don’t care about under normal circumstances. This is my out-of-the-box config level.
- Warn – Anything that can potentially cause application oddities, but for which I am automatically recovering. (Such as switching from a primary to backup server, retrying an operation, missing secondary data, etc.)
- Error – Any error which is fatal to the operation, but not the service or application (can’t open a required file, missing data, etc.). These errors will force user (administrator, or direct user) intervention. These are usually reserved (in my apps) for incorrect connection strings, missing services, etc.
- Fatal – Any error that is forcing a shutdown of the service or application to prevent data loss (or further data loss). I reserve these only for the most heinous errors and situations where there is guaranteed to have been data corruption or loss.