Q. Can you give or specify an example for Apache name based VirtualHost feature under CentOS/ RHEL / Fedora Linux?
A. The term Virtual Host refers to the practice of maintaining more than one web site on one Apache machine or server.
The NameVirtualHost directive is a required directive if you want to configure name-based virtual hosts.
With the NameVirtualHost directive you specify the IP address on which the server will receive requests for the name-based virtual hosts. This will usually be the address to which your name-based virtual host names resolve.
Apache 2 Name based virtualhost Configuration
For example if your IP address is 203.54.2.5. Set it as follows in httpd.conf file:
# vi /etc/httpd/conf/httpd.conf
Set NameVirtualHost as follows:
NameVirtualHost 203.54.2.5:80
Let us see how your virtual host entry looks for two domains called theos.in and nixcraft.com . Code for theos.in domain:
<VirtualHost theos.in/>
ServerAdmin webmaster@theos.in
DocumentRoot /var/www/theos.in
ServerName theos.in
ServerAlias www.theos.in
ErrorLog /var/logs/httpd/theos.in/error_log
CustomLog /var/logs/httpd/theos.in/access_log common
</VirtualHost>
Code for nixcraft.com domain:
<VirtualHost nixcraft.com/>
ServerAdmin webmaster@nixcraft.com
DocumentRoot /var/www/nixcraft.com
ServerName nixcraft.com
ServerAlias www.nixcraft.com
ErrorLog /var/logs/httpd/nixcraft.com/error_log
CustomLog /var/logs/httpd/nixcraft.com/access_log common
</VirtualHost>
Save and close the file. Restart Apache:
# /etc/init.d/httpd restart
Make sure both domains theos.in and nixcraft.com points to (A address) 203.54.2.5. Also make sure you create /var/logs/httpd/domain-name directories and point ftp user directory to appropriate /var/www/domain-name directory.
Virtual Hosts and IP addresses
IPv4 has limited set of IP address. If you want to maintain multiple domains/hostnames on your
# machine you can setup VirtualHost containers for them. Most configurations use only name-based virtual hosts so the server doesn’t need to worry about IP addresses using the asterisks in the config file as follows:
NameVirtualHost *:80 <VirtualHost *:80> ServerAdmin webmaster@dummy-host.example.com DocumentRoot "/usr/local/docs/dummy-host.example.com" ServerName dummy-host.example.com ServerAlias www.dummy-host.example.com ErrorLog "/var/log/dummy-host.example.com-error_log" CustomLog "/var/log/dummy-host.example.com-access_log" common </VirtualHost> <VirtualHost *:80> ServerAdmin webmaster@dummy-host2.example.com DocumentRoot "/usr/local/docs/dummy-host2.example.com" ServerName dummy-host2.example.com ErrorLog "/var/log/dummy-host2.example.com-error_log" CustomLog "/var/log/dummy-host2.example.com-access_log" common </VirtualHost>
🐧 Get the latest tutorials on Linux, Open Source & DevOps via:
- RSS feed or Weekly email newsletter
- Share on Twitter • Facebook • 10 comments... add one ↓
Category | List of Unix and Linux commands |
---|---|
File Management | cat |
Firewall | Alpine Awall • CentOS 8 • OpenSUSE • RHEL 8 • Ubuntu 16.04 • Ubuntu 18.04 • Ubuntu 20.04 |
Network Utilities | dig • host • ip • nmap |
OpenVPN | CentOS 7 • CentOS 8 • Debian 10 • Debian 8/9 • Ubuntu 18.04 • Ubuntu 20.04 |
Package Manager | apk • apt |
Processes Management | bg • chroot • cron • disown • fg • jobs • killall • kill • pidof • pstree • pwdx • time |
Searching | grep • whereis • which |
User Information | groups • id • lastcomm • last • lid/libuser-lid • logname • members • users • whoami • who • w |
WireGuard VPN | Alpine • CentOS 8 • Debian 10 • Firewall • Ubuntu 20.04 |
A very useful guide. Im in a differnt situation though where I want to use my self signed SSL certificate on 2 apaches. I’ve succefully installed it on one server and works as a charm. I want to use te same certificate on another apache server(fail over). Can this be really done? If so please help out. If someone can point me to a good tutorial It’ll be highly appreciated. I need to test this out at this stage is all. Many thanks in advance.
Perfect guide thanks.
I have added the following to mine to include any sub directory, you then over ride this if you actually need to sub directory.
ServerName example.co.uk
ServerAlias http://www.example.co.uk *.example.co.uk
please i have problem with my university of London account shows ”500 internal server error”
please i have problem with my university of London account shows ”500 internal server error”
This tutorial doesn’t work.
hi
i have a query
If and only if we have our own Domain (Registered) only we can able to configure Name Virtual Host
?? or we dont need of Registered domain name to host
Thanks in advance
Regards
Bharath
Job for httpd.service failed. See ‘systemctl status httpd.service’ and ‘journalctl -n’ for details.
[root@localhost atps]# systemctl status httpd.service
httpd.service – The Apache HTTP Server
Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled)
Active: failed (Result: exit-code) since Thu, 2013-02-07 16:21:22 IST; 11s ago
Process: 9548 ExecStop=/usr/sbin/httpd $OPTIONS -k graceful-stop (code=exited, status=1/FAILURE)
Process: 9546 ExecStart=/usr/sbin/httpd $OPTIONS -DFOREGROUND (code=exited, status=1/FAILURE)
Status: “Total requests: 0; Current requests/sec: 0; Current traffic: 0 B/sec”
CGroup: name=systemd:/system/httpd.service
Feb 07 16:21:22 localhost.localdomain httpd[9546]: httpd: Syntax error on line 61 of /etc/httpd/conf/httpd.conf: Canno…ctory
Feb 07 16:21:22 localhost.localdomain systemd[1]: httpd.service: main process exited, code=exited, status=1/FAILURE
Feb 07 16:21:22 localhost.localdomain httpd[9548]: httpd: Syntax error on line 61 of /etc/httpd/conf/httpd.conf: Canno…ctory
Feb 07 16:21:22 localhost.localdomain systemd[1]: Failed to start The Apache HTTP Server.
Feb 07 16:21:22 localhost.localdomain systemd[1]: Unit httpd.service entered failed state
I’m using fedora 18 with latest httpd.
i couln’t set up the virtual host yet. please help me
In several tutorials one scenario is not clearly explained.
If we need to point virtual host to /home/user/public_html directory there are few more things:
1. In httpd.conf put comment on
# UserDir disabled
# remove comment:
UserDir public_html
#point your virtual host:
ServerName some-dummy.host.com
DocumentRoot /home/user/public_html
ErrorLog logs-some-dummy.host.com
CustomLog logs-some-dummy.host.com
2. From a shell:
# chmod 711 /home/user/
# chmod 755 /home/user/public_html/
3. Restart apache:
# service httpd restart
It works for me.
What about when you’re looking to use a VirtualHost to be a catchall to allow all domains that don’t exist in any other VirtualHost. Can you use ? Will it accept http://www.example.com and example.com too?