Apache Web Server Virtual Host Configuration



With Apache2 VirtualHost feature, one can run multiple websites on a single server easily, and this brief tutorial is going to show you how to accomplish that. Each virtualhost runs independently of the others. So if you’re going to be running a WordPress website on each VirtualHost, it will run as if it’s the only website on the server. VirtualHost matching Once the server is configured to listen to a specific address or port, Apache must decide what VirtualHost should be used to fulfill the client request. Before seeing the steps involved in this decision, let’s briefly see how a virtual host is defined. Learn to host your own website with Apache, a solid, well-known, and easy-to. To set up Name based virtual hosting you must need to tell Apache to which IP you will be using to receive the Apache requests for all the websites or domain names. We can do this with NameVirtualHost directive. Open Apache main configuration file with VI editor. root@tecmint # vi /etc/httpd/conf/httpd.conf. Fast Apache Hosting with FREE Installation and Configuration. We offer multiple Apache hosting plans tailored to suit even your most demanding needs. Virtual servers are the most popular platform for running an Apache web server. This is due to their performance, flexibility and reliability.

Apache Server is one of the most famous web servers. This server is open-source and works on various platforms on the Internet, powering many HTTP servers. Apache is a flexible tool and contains within it various other tools that extend its features and usage.

Install Apache Web Server

In this article, I will show you how to install the Apache web server on Ubuntu 20.04. This requires root or administrative privileges, so log in to the system via root.

Step 1: Upgrade Your APT

As always, first, update and upgrade your APT.

Step 2: Download and Install Apache

Next, download and install the Apache web server from the Ubuntu software repository by executing the following terminal command.

Step 3: Verify Apache Installation

To verify whether Apache has installed, check the server status. When the installation is complete, the apache2 server will start automatically.

Step 4: Enable Firewall Settings

Now, you should enable the firewall settings for the Apache webserver. To do this, use the UFW command to allow Apache traffic on Port 443 and Port 80 via the following terminal command.

Step 5: Verify Changes

You can verify this change by checking the firewall status with the following terminal command.

Step 6: Check If Apache Is Working

Check to see whether Apache is working correctly on your system. Open a web browser on your Ubuntu machine, open a new tab, and type the following URL into the URL bar. Be sure to replace the IP we have used with your own machine’s IP address.

Figure: Apache service running in a browser window.

Step 7: Set Up Virtual Host

Now, you are ready to set up a virtual host using the installed Apache web server. Apache contains a test virtual host that is enabled by default when it is installed. Hosting a single website is easy; you just have to upload the website content in its configuration file under the “/var/www/html.” The path to this configuration file is defined as follows.

Figure: Default configuration file opened in gedit editor.

Step 8: Create Domain Name

If you are hosting multiple websites, use the following command to create a new virtual host configuration file for every new website. The domain name created in this test example is www.example.com; this can be replaced with your desired domain name.

Step 9: Create New Directory File

Create a new file in the directory named “index.html” and paste the following content into this file.

<!DOCTYPE html>
<htmllang='en'dir='ltr'>
<head>
<metacharset='utf-8'>
<title>Welcome to example.com</title>
</head>
<body>
<h1>Success! example.com home page!</h1>
</body>
</html>

Figure: New index.html file with site content inside.

Save the file and close it. Change the file permission options with the following terminal command.

Step 10: Create Document in Text Editor

Open your favorite text editor and create this file in “/etc/apache2/sites-available” location. I am using the gedit text editor.

<VirtualHost *:80>
ServerName example.com
ServerAlias www.example.com
ServerAdmin [email protected]
DocumentRoot /var/www/example.com/public_html
<Directory /var/www/example.com/public_html>
Options -Indexes +FollowSymLinks
AllowOverride All
</Directory>
ErrorLog ${APACHE_LOG_DIR}/example.com-error.log
CustomLog ${APACHE_LOG_DIR}/example.com-access.log combined
</VirtualHost>

Figure: example.conf file opened with gedit editor.

Step 11: Link Configuration File

Link this configuration file to the a2ensite utility by executing the following terminal command.

To check for a syntax error in the configuration file, execute the following command.

Step 12: Restart Apache

Now restart Apache service, open your web browser, and type the following URL into the URL bar.

Figure: example.com opened in a browser window.

Uninstalling Apache Web Server

You can completely uninstall the Apache web server via the following terminal commands.

Conclusion

This article covered how to install the Apache web server, configure firewall settings for Apache, set up virtual hosts on Apache web server, and uninstall Apache.

So, you want to host multiple WordPress blogs on a single server? With Apache2 VirtualHost feature, one can run multiple websites on a single server easily, and this brief tutorial is going to show you how to accomplish that.

Each virtualhost runs independently of the others. So if you’re going to be running a WordPress website on each VirtualHost, it will run as if it’s the only website on the server. This allows you to save cost on additional servers and resources.

This tutorial is going to be short.. and won’t waste your time with other unnecessary stuff.

Step 1: Setup Ubuntu Server

I find Ubuntu easy to manage and maintain so this tutorial is going to be based on Ubuntu Linux. You can get Apache2 VirtualHost to work on other Linux distributions, but Ubuntu is a great distribution for new users and beginners.

So, install Ubuntu with root access and run the commands below to update it.

sudo apt-get update && sudo apt-get dist-upgrade && sudo apt-get autoremove

Step 2: Install Apache2 Web Server

After updating Ubuntu, run the commands below to install Apache2 web server.

sudo apt-get install apache2

Step 3: Install MySQL Database Server

After installing Apache2, run the commands below to install MySQL database server.

sudo apt-get install mysql-server mysql-client

During the installation, you’ll be prompted to create a new password for MySQL root user. Do it! This password will be used to logon to MySQL server.

Step 4: Install PHP and other Modules

After installing MySQL server, run the commands below to install PHP and other PHP modules.
sudo apt-get install php libapache2-mod-php php-mysql php-curl php-gd php-pear php-imagick php-imap php-mcrypt php-recode php-tidy php-xmlrpc

At this point, all the servers and packages WordPress requires to function are installed. You next steps will be to create WordPress databases, configure Apache2 VirtualHosts and download WordPress content from online.

Step 5: Create WordPress Databases and Users

Run the commands below to logon to MySQL server

sudo mysql -u root -p

WordPress requires a database. Run the commands below to create new databases as well as a database users. Just duplicate the commands for additional databases and users for as many as you want. Make sure they all have unique names.

The commands below create a new database called wpdatabase

CREATE DATABASE wpdatabase;

The commands below create a new database user called wpuser and grant the user access to the wpdatabase.

GRANT ALL ON wpdatabase.* TO 'wpuser'@'localhost' IDENTIFIED BY 'type_new_password_here';

[alert-note]Repeat the steps above to create additional databases for additional WordPress sites. For example, create database wpdatabase1, wpdatabase2 as well as the users for the additional WordPress websites.[/alert-note]

When you’re done, run the commands below to save your changes and exit.

Step 6: Create Multiple VirtualHost for Multiple Sites

After creating multiple databases for the multiple WordPress sites you want to run, follow the guide below to create multiple VirtualHosts for the sites. Copy the default Apache2 site configuration file to create additional VirtualHosts (sites).

You see we create two virtualhosts for two different websites.. (example.com.conf and example.net.conf)

Each of the above site config file will host a unique domain.

Now open each file by running the commands below for each..

sudo nano /etc/apache2/sites-available/example.com.conf

Then for example.com.conf file, configure the server name, server alias, DocumentRoot and others unique to example.com domain. Repeat the same for other virtualhosts.

Apache Web Server Virtual Host Configuration

Do the above for all the sites that you want to host.. each with unique identity, including the DocumentRoot, ServerName, ServerAlias and Domain

When you’re done. save your changes and close out.

Next, enable the sites you’ve just created by running the commands below for each of the virtualhosts.

Enable other modules as well

Now move the the next step.

Step 7: Create Each Directory for the sites

Now that you’ve configured the multiple sites in Apache2, go and create the DocumentRoot folders for each of the sites you defined in the configuration above.. The line the reads DocumentRoot should match each location.

Configure appropriate folder permissions for all the sites

Restart Apache2 web server by running the commands below

sudo systemctl restart apache2

At this point, all your sites should be already for WordPress content. Next, go and download WordPress content and extract into the DocumentRoot folder for each site.

Step 8: Download WordPress content

Now download WordPress content and extract into the root directory for each site.

cd /tmp/ && wget http://wordpress.org/latest.tar.gz

Then extract the downloaded file.

And copy to each root folder for each site.

Step 9: Configure WordPress database settings

Run the commands below to create WordPress wp-config.php settings file from its sample.

Then open wp-config.php file and make the following highlighted changes to reference the database and user you created above.

Apache Web Server Virtual Host Configuration Tutorial

sudo nano /var/www/html/example.com/wp-config.php

When the file opens, make the changes and save.

Apache Web Server Virtual Host Configuration Software

Do the above for each of the site you create, making sure the database connection info is correct for each site.

sudo systemctl reload apache2

Now open your web browser and browse to the server domains and you should see WordPress default setup page for each site.

Apache Multiple Virtual Hosts

Enjoy!

Httpd Virtual Host Configuration

You may also like the post below: