Setting up Apache virtual hosts on Linux Ubuntu server

0
(0)

Apache virtual hosts will allow you to host multiple websites from a single Linux Ubuntu web server

Apache Virtual Hosts Setup

It’s common practice to setup web servers to host multiple websites instead of just one website per server and in this quick tutorial we will take a look at setting up the Apache virtual hosts module to do just that.

Before we begin you will need a Apache Web server up and running on Ubuntu, you can follow our guide here on Setting up an Apache Web server on Linux Ubuntu

What are Apache Virtual Hosts

Setting up Apache virtual hosts is a feature of the Apache HTTP Server, which is one of the most widely used web servers on the internet. Virtual hosts allow you to run multiple websites or domains on a single physical server, each with its own unique domain name and content. This is particularly useful in shared hosting environments or situations where you have limited resources but need to host multiple websites.

In the context of Apache, a virtual host is a method of hosting multiple domains (or websites) on a single server. Each virtual host has its own configuration settings, including directives for how requests to that domain should be handled.

to read more on Apache Virtual hosts you can see the Official Apache Documentation Here

Types of Virtual Hosts

  • Name-based virtual hosts: These are the most common type of virtual hosts. With name-based virtual hosting, Apache selects the appropriate virtual host configuration based on the hostname provided by the client in the HTTP request headers. This means that multiple domain names can resolve to the same IP address, and Apache decides which website to serve based on the requested domain name.
  • IP-based virtual hosts: With IP-based virtual hosting, each domain is assigned a unique IP address, and Apache serves the appropriate content based on the IP address that the request is received on. This method is less common than name-based virtual hosting and requires additional IP addresses.

Configuring Virtual Hosts

Virtual hosts are configured in Apache’s main configuration file (httpd.conf) or in separate configuration files located in the “sites-available” or “conf.d” directories, depending on the server’s configuration. Each virtual host configuration typically includes:

  • The IP address and port to listen on (for IP-based virtual hosts).
  • The domain name(s) associated with the virtual host (for name-based virtual hosts).
  • The document root, which is the directory where the website’s files are stored.
  • Any additional configuration directives specific to that virtual host, such as access control rules, error handling, logging settings, etc.

Lets setup a Virtual Host on our Linux Web Server

For this Tutorial we will be setting up a Name based Virtual host. This way you don’t need multiple IP addresses. The Apache Web server will map each request to a DNS name and then serve up that page using a single IP address. This is the most common type for individuals, small businesses and web developers.

It’s a good idea to use multiple system users for each virtual host server or website. for instance on Linux we can add a new user then setup a web directory for that user on their home folder and then point the virtual host to the users path where he stores the webpages. This will also benefit security as well as help manage the server better.

Setting up a new user for the virtual host

Adding a new user in Linux is fairly simple, In your terminal execute the following command: Replace <username> with your user…

By default you will need to provide a strong password and you will be asked for additional information for this account. If you don’t have this information you can leave the fields blanks and just press enter to go through them all followed by accepting at the end.

If you need to change the password for this users then you can do so by typing in. Again replace <username> with your actual user’s name.

Now we will need to setup the directory that we will be using to store the webpages for this users. We ill call it html-public

Lastly, we need to set the correct permissions for this folder so that both the user and apache can access the files in this directory. This is crucial to get right.

For this example we will use the username: johndoe Make sure you replace this username with your own user which you created earlier.

In the code snippet above we create a new folder for user johndoe then, we give Apache access to this folder and to allow johndoe to write to this folder as well we add the user, in this case johndoe to the apache group.

There are other ways to do this as well, just with everything in the Linux space. for instance you could use the /var/www/html folder followed by the username to store the website files in for the user and then give johndoe user write access to that folder but for simplicity we will do it the way outlined above.

Setting up the first virtual host on the Web Server running Apache

Navigate to the following directory. In Ubuntu and Ubuntu type servers and even desktops running Apache its usually found in /etc/apache2/sites-available/

The sites-available Directory:

This directory typically contains configuration files for all available virtual hosts on the server. Each file represents a separate virtual host configuration:

  • Location:
    The sites-available directory is usually found within the Apache configuration directory. The exact location can vary depending on the operating system and how Apache was installed. Common paths include /etc/apache2/sites-available on Debian-based systems and /etc/httpd/sites-available on Red Hat-based systems.
  • File Format:
    Each file in the sites-available directory is a configuration file for a specific virtual host. These files often have names that correspond to the domain name or purpose of the virtual host, making it easy to identify them.
  • Content:
    The content of each configuration file includes directives specific to the virtual host being configured. This typically includes settings such as the ServerName, ServerAlias, DocumentRoot, ErrorLog, CustomLog, and any other directives necessary to define how requests to that virtual host should be handled.
  • Status:
    Virtual host configurations in the sites-available directory are considered “available” but not necessarily active. They are simply potential configurations that can be enabled or disabled as needed.

Create a new text file that we will be using to configure a new virtual host on the Apache Web server. Its good practice to use the domain name for the filename such as:

Next, we need to edit this file to configure the virtual host. I will be using nano as my editor but feel free to use which ever one you prefer.

Once you have the file open its time to setup the required lines for the virtual host.

Beaking down the Configuration file

This Apache virtual host configuration is responsible for serving the content located in the directory /home/johndoe/www-public when the domain myawesomedomain.co.za or www.myawesomedomain.co.za is accessed over HTTP (port 80). Let’s break it down:

  1. <VirtualHost *:80>: This line defines the start of the virtual host configuration block and specifies that this virtual host will respond to requests on port 80 for any available IP address (*).
  2. DocumentRoot /home/johndoe/www-public: This line sets the document root directory, which is the location where the files for the website are stored.
  3. ServerAdmin johndoe@myawesomedomain.co.za: This line specifies the email address of the server administrator.
  4. ServerName myawesomedomain.co.za: This line defines the primary domain name associated with this virtual host.
  5. ServerAlias www.myawesomedomain.co.za: This line defines an alias for the server, so requests to www.myawesomedomain.co.za will also be served by this virtual host.
  6. Options ExecCGI FollowSymLinks Includes IncludesNOEXEC: This line specifies the options that are enabled for this virtual host. These options control various behaviors such as CGI execution, symbolic link following, and inclusion of files.
  7. AccessFileName .htaccess: This line specifies the name of the per-directory configuration file.
  8. <Directory "/home/johndoe/www-public">: This block defines directives that apply to the specified directory (/home/johndoe/www-public). These directives include permissions, overrides, and options for that directory.
  9. AllowOverride All: This line allows the use of .htaccess files to override server configuration settings.
  10. Order allow,deny, allow from all: These lines control access permissions, allowing all requests to this directory.
  11. Options +SymLinksIfOwnerMatch: This line adds the SymLinksIfOwnerMatch option to the existing options for this directory. This option allows symbolic links to be followed only if the target file or directory is owned by the same user as the link.
  12. Require all granted: This line specifies that access to this directory is granted to all users.
  13. DirectoryIndex index.php home.html Index.html index.html index.htm login.php: This line specifies the order in which index files are used when a directory is requested. Apache will look for these files in the specified order and serve the first one it finds.
  14. LogLevel warn: This line sets the logging level for this virtual host to ‘warn’, meaning that only warnings and errors will be logged.
  15. TransferLog /home/johndoe/www-public/logs/access.log: This line specifies the location of the access log file for this virtual host, where HTTP request details will be logged.
  16. </VirtualHost>: This line marks the end of the virtual host configuration block.

For more information on additional configurations you can always visit the offical apache documentation

Enabeling the virtual host in Apache

After you are finished edithing this virtual host file save it and exit out. We are not ready to tell Apalche to load this new site and allow traffic to it. However you will need a DNS that points to the servers IP address. If you are testing this on a local area network you can simulate this by adding an entry to your windows hosts file. More on that here

To load the new configuration we need to let Apache know there is a new site that needs to me loaded. in your terminal on the web server run the following command

Where <username>.conf is the name of the file that you created for the virtual host server. In our demo we used the name www.myawesomedomain.co.za.conf, yours should be different. this command will essencially copy the configuratiuon file to the sites-enabled folder. Apache then loads this up when it starts.

sites-enabled Directory:

This directory contains symbolic links to configuration files from the sites-available directory that represent active virtual hosts. Here’s a closer look:

  • Location:
    Like sites-available, the sites-enabled directory is typically located within the Apache configuration directory.
  • File Format:
    Instead of actual configuration files, sites-enabled contains symbolic links (or shortcuts) to the files in sites-available. These symbolic links point to the virtual host configurations that are currently active.
  • Content:
    Since sites-enabled contains symbolic links, its content mirrors the content of sites-available. Each symbolic link in sites-enabled points to a specific virtual host configuration file in sites-available. This allows Apache to easily determine which virtual hosts should be active and serve requests.
  • Status:
    Virtual host configurations that have symbolic links in the sites-enabled directory are considered “enabled” and will be actively used by Apache to serve content.

After that we need to restart Apache to load the new configuration. Easily restart Apache using the following command inb terminal.

If all goes well your new virtual server should be loaded and available now.

Thus you can now use multiple websites served from a single server and IP address.

Additional Tips:

Secure Your Server: Consider adding a firewall rule to allow inbound traffic only on port 80 (or another port you’re using) and from authorized IP addresses.

Optimize Virtual Host Performance: For multiple virtual hosts, use server-name-based virtual hosts (recommended) for efficiency.

Grant Web Server Permissions: If the user doesn’t belong to the www-data group, set correct file permissions:

If you encounter errors, check Apache logs (/var/log/apache2/*) for clues on possible issues. Your virtual host will keep access logs seperatly in it the same directory under the logs folder as we configures the virtual host settings.

Concider the following:

You will still need DNS records to be setup for each of your virtual hosts that link to your Apache server either with a hosting provider or on your local area network. The Apache virtual hosts will each require having an unique name and configuration file.

Additionally you will need to add each virtual host to the linux hosts file as well. this was convered here Setting up a simple Apache web server on Linux Ubuntu

Hosting your own webserver that is internet facing can be a real challange, requires good knowledge of networking and server administration. Its useful and fun to play with but to run your own production server i would highly recommend going with a hosting provider, its lest of a security risk and way less maintance needed on your part.

How useful was this post?

Click on a star to rate it!

1 thought on “Setting up Apache virtual hosts on Linux Ubuntu server”

  1. Pingback: Setting up a simple Apache web server on Linux Ubuntu - Micro Fusion Insight

Comments are closed.

Scroll to Top