How to Create a Virtual Host in Apache 2.4

You are currently viewing How to Create a Virtual Host in Apache 2.4

Welcome to our comprehensive guide on how to create a virtual host in Apache 2.4. If you’re new to server management or looking to delve deeper into Apache configuration, you’ve come to the right place. In this tutorial, we will walk you through the process of creating a virtual host.

A virtual host is a feature of Apache that allows one server to host multiple websites. It is an essential tool for web administrators managing numerous domains, enabling them to control the configuration of individual sites independently. By the end of this post, you will understand how to set up and configure your virtual host in Apache 2.4.

What is Virtual Hosting

Before we start, let’s discuss what virtual hosting is. Virtual hosting is a method for hosting multiple domain names (with separate handling of each name) on a single server (or pool of servers). This allows one server to share its resources, such as memory and processor cycles, without requiring all services provided to use the same hostname. The term virtual hosting is usually used for web servers but the principles do carry over to other Internet services.

Prerequisites

Before diving into the guide, ensure you meet the following prerequisites:

  • Basic understanding of the Linux command line.
  • A system with Apache 2.4 installed or the ability to install it. This guide will also include steps to install Apache.
  • Sudo or root access to your server to execute commands and edit configuration files.
  • A registered domain name or a test domain for setting up and testing the virtual host.

If you need to brush up on your command line skills or set up a suitable environment, many resources are available online to assist you.

How to Create A Virtual Host in Apache 2.4

First, please log in to your server using Putty or a terminal. Then finish the installation of Apache 2.4 by Running this command.

sudo yum install httpd24 -y

This command will install Apache 2.4 into your Amazon Linux machine. After you have successfully installed Apache 2.4, then create a folder named “AWS” in the “/var/www” directory. To create a new folder please run this command.

sudo mkdir /var/www/aws

Set the folder permission to 0775 by running this command.

sudo chmod 0775 /var/www/aws 

After that, run the following command to open the Virtual Host configuration file.

sudo vi /etc/httpd/conf.d/vhost.conf

In my examples I always use VI as the editor, if you use a different editor then adjust the command accordingly.

After you have opened the file, please paste the following content over there. I assumed the domain name to be awswithatiq.com. Please replace it with your domain/subdomain.

<VirtualHost *:80>

  # REQUIRED. Set this to the host/domain/subdomain that
  # you want this VirtualHost record to handle.

  ServerName awswithatiq.com

  # Optional. You can specify additional host names that
  # serve up the same site. This can be top-level, domains,
  # sub-domains, and can even use wildcard subdomains such
  # as *.yourdomain.com - just separate each host name
  # with a single space.

  ServerAlias www.awswithatiq.com

  # REQUIRED. Set this to the directory you want to use for
  # this vhost site's files.

  DocumentRoot /var/www/aws

  # Optional. Uncomment this and set it to your admin email
  # address, if you have one. If there is a server error,
  # this is the address that Apache will show to users.

  ServerAdmin [email protected]

  # Optional. Uncomment this if you want to specify
  # a different error log file than the default. You will
  # need to create the error file first.

  #ErrorLog /var/www/vhosts/logs/error_log

  # REQUIRED. Let's make sure that .htaccess files work on 
  # this site. Don't forget to change the file path to
  # match your DocumentRoot setting above.
  
  <Directory /var/www/aws>
    AllowOverride All
  </Directory>

</VirtualHost>

Here’s what each directive does:

  • VirtualHost: This encloses the directives that apply to the virtual host. *:80 means that the virtual host is listening on any IP address on port 80.
  • ServerAdmin: This specifies the email address of the server administrator.
  • ServerName: This sets the base domain that should match this virtual host definition.
  • ServerAlias: This directive is optional and sets the alternate names for a host, like www.
  • DocumentRoot: This is the directory from which Apache will serve the site’s files.
  • ErrorLog and CustomLog: These specify where Apache should write errors and access logs for this site.

After pasting and adjusting the template to fit your domain, save and close the file. Now your virtual host is set up, but it’s not active yet. We’ll cover that in the next section.

Once you pasted the content, then restart the Apache server to take effect.

sudo service httpd restart 

Testing your Setup: Open your web browser and visit your domain (http://example.com). If you see your website or the placeholder page you created, your virtual host is working correctly.

Troubleshooting Common Issues

While setting up a virtual host, you may encounter a few common issues. Here are some tips on how to resolve them:

  • Apache isn’t serving the right content: Check the spelling and path in your configuration file. The DocumentRoot directive should point to the directory containing your website files.
  • Apache fails to start or restart: Use the apachectl configtest command to check your configuration files for syntax errors.
  • You can’t access your website: Make sure your domain is correctly pointing to your server’s IP address. You may also need to clear your browser cache.

Securing Your Virtual Host

After setting up your virtual host, you should also secure it by installing an SSL certificate. Here’s a brief overview of how to do it using Let’s Encrypt, a free and open certificate authority:

  1. Install Certbot: Certbot is the tool that will fetch SSL certificates from Let’s Encrypt and configure Apache to use them.
sudo yum install -y https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
sudo yum-config-manager --enable epel
sudo yum install certbot python2-certbot-apache -y
  1. Fetch and Install the SSL Certificate: Run the following command to fetch and install the SSL certificate
sudo certbot --apache
  1. Confirm SSL is Working: To confirm SSL is working, visit your site at https://example.com. You should see a padlock icon in the address bar.

Remember, securing your website with SSL not only protects your user’s data but also improves your site’s SEO ranking and builds trust with your users.

Conclusion

Congratulations! You’ve successfully created a virtual host in Apache 2.4. You’ve learned how to install Apache, understand its configuration files, set up a virtual host, troubleshoot common issues, and secure your virtual host with SSL.

Managing a web server can be complex, but with these skills, you’re well on your way to handling multiple websites efficiently. Remember, practice is key to mastering server management. So, feel free to create more virtual hosts and experiment with different configurations.

References and Further Reading

If you want to dive deeper into Apache and web server management, consider exploring the following resources:

Happy hosting!

Atiqur Rahman

I am MD. Atiqur Rahman graduated from BUET and is an AWS-certified solutions architect. I have successfully achieved 6 certifications from AWS including Cloud Practitioner, Solutions Architect, SysOps Administrator, and Developer Associate. I have more than 8 years of working experience as a DevOps engineer designing complex SAAS applications.

This Post Has 3 Comments

Leave a Reply