Httpd Host



In Apache (httpd) virtual hosts are used to host web content for multiple domains off of the same server depending on the IP address or domain name that is being used. Depending on the request received different virtual host configuration can apply, resulting in different settings and web content being served from a single web server. For example a web server with one IP address can host multiple domain names such as example.com and example.org and many more.

The Number One HTTP Server On The Internet ¶ The Apache HTTP Server Project is an effort to develop and maintain an open-source HTTP server for modern operating systems including UNIX and Windows. The goal of this project is to provide a secure, efficient and extensible server that provides HTTP services in sync with the current HTTP standards. CentOS / RHEL: How to create and host yum repository over httpd. YUM (Yellowdog Updater, Modified) provide more services and functionality than is available with the rpm command and other RPM-based tools. With Yum tools and plug-ins, you can. I'm using Apache Httpd 2.4 as a web server,and I fail to allow access only from a specific host to a URL-path '/x' on my web server. Httpd.conf: Require host myhost.com. As my previous article mentioned, all of the configuration files for Apache are located in /etc/httpd/conf and /etc/httpd/conf.d. The data for the websites is located in /var/www by default. With multiple websites, you will need to provide multiple locations, one for each site you host. Name-based virtual hosting.

Here we are going to cover how to configure virtual hosts for Apache 2.4 so that we can have multiple domains serving different websites based on what is requested.


Studying for your RHCE certification? Checkout our RHCE video course over at Udemy which is 20% off when you use the code ROOTUSER.

Example Apache Virtual Host Configuration

Virtual host configuration is typically placed within the /etc/httpd/conf/httpd.conf file, and also in unique .conf files within the /etc/httpd/conf.d directory. It is good practice to create a new .conf file within /etc/httpd/conf.d if you are adding multiple websites to be hosted from the same web server, as this keeps the configuration clean and is easier to manage. In our example we will be working with /etc/httpd/conf.d/example1.conf which will be for the website www.example.com and /etc/httpd/conf.d/example2.conf which will be for the website www.example.org.

Tar zip mac. First we’ll start with some example virtual host configuration and then discuss what each line is actually doing. For additional examples, see the Apache documentation.

The below example virtual host configuration has been saved within the /etc/httpd/conf.d/example1.conf file.

The below example virtual host configuration has been saved within the /etc/httpd/conf.d/example2.conf file.

In the above examples we have two virtual host configuration blocks. The first is for www.example.com while the second is for www.example.org. Below we will explain each line for the example1/example.com virtual host as the configuration is mostly the same between the two.

  • <Directory /var/www/html/example1> – This opens the directory tag and is used to enclose a group of directives that apply to the directory specified.
  • Require all granted – This is required to grant access, without it the Apache logs will show “authz_core:error” as the default configuration within the /etc/http/conf/httpd.conf file defines the directory of “/” with “Require all denied”.
  • </Directory> – This closes the directory tag.
  • <VirtualHost *:80> – This virtual host tag indicates that the configuration after it will apply to any IP address as per “*” on port 80, “*” can instead be modified to a particular IP address that is available on the server. The port can also be changed if the Listen directive for that port is defined within the main httpd.conf file.
  • DocumentRoot “/var/www/example1” – The document root is the directory where the content exists that Apache should serve when we visit the domain name, in this case going to www.example.com will direct us to files within the /var/www/example1 directory on the web server. The directory specified should exist and ideally contain content.
  • ServerName www.example.com – This is the unique name that the virtual host is for, in this case the virtual host configuration block is for the www.example.com website.
  • ServerAlias example.com – Alternate names can be used when matching a request and are specified with ServerAlias, these allow us to provide other name based aliases as only one ServerName is allowed per virtual host.
  • ServerAdmin [email protected] – This is an email address that is provided in error messages, allowing users to contact the web master of the web server.
  • ErrorLog “/var/log/httpd/error_log_example1” – This is the file where error logs are stored that are related to this virtualhost, which are useful when troubleshooting problems.
  • CustomLog “/var/log/httpd/access_log_example1” combined – This is where access logs are stored, for instance when a client views a web page the access requests will be logged here.
  • </VirtualHost> – This is the closing tag for the virtual host block, indicating the end of the configuration for the particular virtual host.

If any configuration within a virtual host is missing, the defaults specified in the main /etc/httpd/conf/httpd.conf file will be used instead.

The second virtual host block in the example2.conf file is mostly the same, except that it takes care of requests for www.example.org and example.org serving the content within /var/www/html/example2. Errors are logged to /var/log/httpd/error_log_example2 and access requests are logged to /var/log/httpd/access_log_example2.

The syntax of our two .conf files can be tested with the ‘apachectl configtest’ command as shown below. In this case the document root directories have not yet been created so we are given a warning and should create these.

The directories can be created with mkdir as shown below.

Now that our directories exist the warning should no longer come up. In this example I have created two index.html files with a text editor in both directories, the contents are shown below.

Before testing our virtual host configuration, any changes to Apache configuration files such as modification to virtual hosts will require the httpd service to either be restarted or reloaded to pick up the configuration changes. Apache can be reloaded to make use of configuration changes with ‘systemctl reload httpd’, for further information see our service management guide.

Testing the Virtual Hosts

Host

Once virtual host configuration has been put in place and Apache reloaded, the relevant DNS records will need to be created so that the domains will resolve to the web server. Alternatively you can test by modifying your hosts file. In this example we can modify the /etc/hosts file and add the following entry.

This will make these domains resolve to localhost, now we can browse the content and confirm our virtual hosts are working correctly. In this instance we are going to use the curl command to view the contents of each website.

This confirms that the correct index.html pages within /var/www/html/example1 and /var/www/html/example2 are being retrieved successfully for each domain, as defined within the virtual host configuration.

Httpd Hosting

Further Information

If you get stuck or have trouble remembering any of this, remember the httpd-manual package which can be installed and viewed at http://localhost/manual.

From the main page, simply select Virtual Hosts for help on this topic.

Summary

With a few lines of virtual host configuration we can enable Apache to serve multiple websites from the same web server, allowing us to host multiple websites within the same shared hosting environment.

This post is part of our Red Hat Certified Engineer (RHCE) exam study guide series. For more RHCE related posts and information check out our full RHCE study guide.

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.

Https Hosts File

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.

List Apache Virtual Host 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.

Https Hosting Dmsi

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.

Http Hostname

If you have any questions relating to Apache HTTP server, use the comment form below to reach us.