Virtual Host settings in Apache
Virtual Host settings in Apache
Apache, along with php and mysql, can be installed on all operating systems and provides a platform for hosting web pages. A server that has a LAMP installed and has a network card can normally host a website. But if you want your server to respond to multiple sites, you have to use Virtual Hosts
Virtual Hosts, hereinafter referred to as VH, are configured in two ways, address-based or name-based
Virtual Host based on IP address
Suppose you want to host two websites on one server. In IP-Based Virtual Host mode, each of these sites requires a network card, which requires a total of two network cards. In this case, to access the content of each, we type the address of the network card that hosts that webpage in the browser
For example, FarazNetwork.ir site is available at 192.168.214.136 and FarazLinux.ir site is available at 192.168.214.138 from the local network. The necessary changes are made to the httpd.conf file
Name-based Virtual Host
Name-Based Virtual Hosts use a single network card to access multiple websites. In this case, when the Apache web server receives a request, it looks at the hostname in its HTTP Header and, depending on the hostname, displays a different website
Configuring this type of VH requires having a network card as well as a DNS Server to define and map the site name (same as hostname in the HTTP Header) to the address of the same network card. Suppose you want to host two sites, FarazNetwork.ir and FarazLinux.ir, on a network card at 192.168.214.136. So you need to make changes to the httpd.conf file and also enter two type A records that have the same IP address but a different DNS name (ie FarazNetwork.ir and FarazLinux.ir) in the zone file
Set up and configure name-based VHs
All settings are made in the httpd.conf file. Modifying this file is based on specifying the address and port on which the VHs (s) listen and respond. Also the definition of VHs that each VH has characteristics that are defined under a Directive
Parameter : NameVirtualHost Suppose you want all sites to be hosted on network card 51.255.106.16 and port number 80. This address and its port are specified via the NameVirtualHost parameter as follows
NameVirtualHost IP_ADDR: PORT_NUMBER
For example, to listen to all VHs on Port 80 and address 51.255.106.16, the parameter will be set as follows
NameVirtualHost 51.255.106.16:80
VirtualHost Directive : All VM attributes are defined between <VirtualHost IP_ADDR: PORT> and </VirtualHost>. IP_ADDR: The PORT in the Directive is for specifying the address and port on which VH is accessible. Suppose you have two network cards and you want to host multiple sites on each. So you need to first specify these URLs with the NameVirtualHost parameter. You can then specify which address and port this VH will use in the PORT: ADDR_IP line from the VirtualHost directory
For example, 51.255.106.16:80 means that out of all the addresses, this VH is hosted on the above address and port. Or 80: * means that this VH is hosted on all addresses and Port number 80
The features that are required to define a VH between the VirtualHost directives are as follows
(ServerAdmin : Specifies the email address of the site administrator (site administrator
DocumentRoot : The path and directory in which the content of the website is located. For example, if the site name is FarazNetwork.ir, we must create a directory with the same path as above
ServerName : The same name as the site name and the same name as the dns name that is inserted in the zone file. Type the same name in the browser and apache detects which site you have requested from the HTTPD Header
ErrorLog : Specifies the path to the file where site events are recorded
Note that you must create the directories specified in DocumentRoot and ErrorLog before restarting the Apache web server to apply the changes. Suppose you want to create a directory for FarazNetwork.ir
mkdir -p /var/www/laitec.ir/public_html
mkdir /var/www/laitec.ir/logs
: Now restart the httpd service with the following command
service httpd restart
دیدگاهتان را بنویسید
برای نوشتن دیدگاه باید وارد بشوید.