Creating Virtual Hosts in Apache for Fedora
In the /etc/httpd/conf/httpd.conf file add following line at the end or in the virtual host section.
NameVirtualHost *
# for domain mydomain1.com
< VirtualHost * >
DocumentRoot /var/www/vhosts/mydomain1/htdocs
ServerName mydomain1.com
ErrorLog /var/www/vhosts/mydomain1/logs/error_log
CustomLog /var/www/vhosts/mydomain1/logs/access_log common
< /VirtualHost >
# for domain mydomain2.com
< VirtualHost * >
DocumentRoot /var/www/vhosts/mydomain2/htdocs
ServerName mydomain2.com
ErrorLog /var/www/vhosts/mydomain2/logs/error_log
CustomLog /var/www/vhosts/mydomain2/logs/access_log common
< /VirtualHost >
Save end Close the file.
Create directories/files according to the virtual hosts defined in the httpd.conf file.
$ cd /var/www
$ mkdir vhosts
$ cd /var/www/vhosts
$ mkdir -p mydomain1/htdocs
$ cd mydomain1
$ mkdir logs
$ cd logs
$ touch error_log
$ touch access_log
$ cd mydomain1
$ chown -R apache:apache logs
$ chown -R apache:apache htdocs
Restart the web server (apache).
/etc/init.d/httpd restart
To teat the Virtual Host syntax:
$ /usr/sbin/httpd -S
(Output will be like below)
VirtualHost configuration:
wildcard NameVirtualHosts and _default_ servers:
*:* is a NameVirtualHost
port * namevhost mydomain1.com (/etc/httpd/conf/httpd.conf:1049)
port * namevhost mydomain2.com (/etc/httpd/conf/httpd.conf:1057)
Syntax OK
Virtual Host can be easily set and tested on Local Fedora / Redhat machine
In local Fedora/RH machine:
NameVirtualHost 127.0.0.1
< VirtualHost 127.0.0.1 >
DocumentRoot "/var/www/html"
ServerName localhost
< /VirtualHost >
< VirtualHost 127.0.0.1 >
DocumentRoot "/var/www/mysite"
ServerName mysite.com
< /VirtualHost >
Add the mysite.com in the /etc/hosts file (at the end)
127.0.0.1 localhost.localdomain localhost localhost
::1 localhost6.localdomain6 localhost6
127.0.0.1 mysite.com mysite.com
Restart the Apache.
Point your browser at:
http://localhost
It will display the content under /var/www/html dir
http://mysite.com/
It will display the content under /var/www/mysite dir