Apache uses the first virtual host found in the configuration also for requests that do not match any domain set in the ServerName and ServerAlias parameters. This also includes requests sent to the IP address of the server. Because you might run multiple sites on one Apache server, you need to tell Apache which directory contains the web files (the 'web root' or 'document root') per website. A Primer on Virtual Hosts. Virtual hosts are the bread and butter of Apache. They allow you to run multiple websites off of one web server as well as customize settings for.
The primary Apache configuration file is /etc/httpd/conf/httpd.conf. It contains a lot of configuration statements that don't need to be changed for a basic installation. In fact, only a few changes must be made to this file to get a basic website up and running. Creating virtual host configurations on your Apache server does not magically cause DNS entries to be created for those host names. You must have the names in DNS, resolving to your IP address, or nobody else will be able to see your web site.
Virtual hosting is a method for hosting multiple websites on a single machine. There are two types of virtual hosting: Name-based virtual hosting and IP-based virtual hosting.IP-based virtual hosting is a technique to apply different directives based on the IP address and port a request is received on. You can assign a separate IP for each website on a single server using IP-based virtual hosting. This is mainly used to host different websites on different ports or IP addresses.
In this article we will be creating:
To set up IP-based virtual hosting, you need to have more than one IP address assigned to your server. Setting up multiple IP addresses on a single network interface is called 'IP aliasing.' It is very useful, particularly if your server only has one network interface card (NIC).
To set up multiple IPs, you need to edit the ifcfg-eth0
file:
Add/edit the following lines:
Save and close the file when you are finished. Then restart the network service to make these changes take effect.
By default Apache listens for incoming connections on port 80
. For port-based virtual hosting, you need to tell Apache to listen for IP address 192.168.1.42 on port 80
and for IP address 192.168.1.43 on port 8080
.
To set up multiple ports, you need to edit the httpd.conf
file:
Add/edit the following lines:
Save and close the file, then restart Apache to make these changes take effect.
First, you need to make a directory structure which will hold the web pages. This directory is known as 'document root' for the domain.
In CentOS 7 the default Apache document root directory is /var/www/html/
.
Now, create two directory for websites www.ip-vhost.com
and www.port-vhost.com
in the default Apache document root directory:
Now, you need to create an index.html
file for each website which will identify that specific domain.
Let's create an index.html
file for the www.ip-vhost.com
ip virtual host.
Add the following content.
Save and close the file when you are finished.
Similarly, create an index.html
file for the www.port-vhost.com
virtual host.
Add the following content.
Save and close this file as well. Now, you have the pages to test the virtual host configuration.
In CentOS 7 by default the Apache service runs as the user apache
. You must change the ownership of these two virtual directories to apache
, so that Apache can read and write data.
You can change the ownership with chown
command.
You should also make the Apache document root /var/www/html
directory world readable, so that everyone can read files from that directory.
Now your web server has the permissions it needs to serve content. Download silicon laboratories usb devices driver.
The next step is to create a virtual host configuration file for each website. The name of each configuration file must end with .conf
.
Let's create a virtual host file for website www.ip-vhost.com
.
Add the following content.
Save and close the file when you are finished.
Similarly, create a virtual host file for website www.port-vhost.com
.
Add the following content.
When you are finished, it is a good idea to check the syntax of the configuration. You can check the syntax of files with the following command:
After the syntax check is done, restart Apache to make these changes take effect.
Now, you need to allow the Apache port 80
and 8080
using FirewallD.
You can do this by running following commands:
Now, reload the firewall service for the changes to take effect.
Now on the desktop Linux computer, open your web browser and go to the URLs http://192.168.1.42:80
and http://192.168.1.43:8080
.You should see sample pages that look like this:
www.ip-vhost.com
sample page:
www.port-vhost.com
demo page: Sindoh driver download for windows.
Apache virtual host configuration allows you to run multiple websites on the same server, that means you can run more than one website on the same Apache web server. You simply create a new virtual host configuration for each of your websites and restart the Apache configuration to start serving the website.
On Debian/Ubuntu, the recent version of Apache configuration files for all virtual hosts are stored in the /etc/apache2/sites-available/ directory. So, it makes really difficult to go through all of these virtual host configuration files to fix any configuration errors.
To make things easier, in this article we will show you how to list all enabled apache virtual hosts on a web server using a single command on the terminal. This method will also help you to see a few other useful apache configurations.
This is practically helpful in a scenario where you are assisting a company to fix their web server issues remotely, yet you do not know their current apache web server configurations, in regards to virtual hosts.
Read Also: How to Check Which Apache Modules are Enabled/Loaded in Linux
It will help ease searching for the virtual host of a specific website in the apache config files and assist in troubleshooting any apache issues, where you’ll, in most cases start with checking of the currently enabled virtual hosts before looking into the logs.
To list all enabled virtual hosts on the web server, run the following command in a terminal.
You will get a list of all configured virtual hosts as well as another important apache/httpd server configurations.
From the above output, we can clearly see which ports and IP addresses are configured for each website. We will also see each website virtual host configuration file and their location.
This comes very helpful, when you are troubleshooting or fixing any apache virtual host configuration errors or you simply want to see a list of all enabled virtual host summary on a web server.
That’s all! You might also find these following related articles on Apache web server.
If you have any questions relating to Apache HTTP server, use the comment form below to reach us.