The Apache net server is a famous method for serving websites on the internet. As of 2019, it’s far expected to serve 29% of all active websites and it offers robustness and flexibility for developers. Using Apache, an administrator can installation one server to host a couple of domains or websites off of a unmarried interface or IP with the aid of the use of an identical system.
Each area or individual web site — called a “virtual host” — this is configured the usage of Apache will direct the tourist to a selected listing keeping that web site’s records. This is executed with out indicating that the same server is likewise responsible for other websites. This scheme is expandable without any software program limit as long as your server can manage the load. The fundamental unit that describes an man or woman site or area is called a virtual host.
In this guide, we will walk you via how to installation Apache virtual hosts on an Ubuntu 18.04 server. During this process, you’ll learn how to serve specific content material to exceptional visitors relying on which domain names they’re soliciting for.
Before you start this educational, you have to create a non-root user.
You will also need to have Apache installed as a way to paintings via those steps.If you haven’t already executed so, you may get Apache installed to your server thru the apt package manner:
- sudo apt update
- sudo apt install apache2
If you would like more designated instructions as well as firewall setup, please talk to our manual How To Install the Apache Web Server on Ubuntu 18.04.
For the functions of this manual, our configuration will make a virtual host for example.com and another for test.com. These may be referenced for the duration of the manual, but you have to alternative your personal domains or values even as following along.
If you are using DigitalOcean, you could learn how to installation domain names through following the product documentation, How to Add Domains. For different providers, discuss with their applicable product documentation If you do now not have domains to be had at this time, you may use test values.
We will display a way to edit your local hosts file in a while to check the configuration if you are using check values. This will assist you to validate your configuration from your house pc, even though your content material gained’t be to be had through the area call to different site visitors.
Step One — Create the Directory Structure
The first step that we are going to take is to make a directory shape so one can preserve the website online information that we are able to be serving to site visitors.
Our record root (the pinnacle-degree listing that Apache appears at to locate content material to serve) will be set to individual directories underneath the /var/www listing.We will create a directory here for each of the digital hosts we plan on making.
Within every of those directories, we are able to create a public_html folder on the way to keep our real files.This offers us some flexibility in our web hosting.
For instance, for our websites, we’re going to make our directories as follows. If you’re the use of real domains or change values, change out the highlighted text for these.
- sudo mkdir -p /var/www/example.com/public_html
- sudo mkdir -p /var/www/check.com/public_html
The quantities in red constitute the domains that we need to serve from our VPS.
Step Two — Grant Permissions
Now we’ve got the directory shape for our files, but they are owned by way of our root consumer.If we need our normal consumer in order to regulate documents in our internet directories, we will alternate the possession through doing this:
- sudo chown -R $USER:$USER /var/www/example.com/public_html
- sudo chown -R $USER:$USER /var/www/take a look at.com/public_html
The $USER variable will take the value of the consumer you are presently logged in as whilst you press ENTER. By doing this, our normal consumer now owns the public_html subdirectories wherein we will be storing our content.
We must additionally adjust our permissions to make certain that study get admission to is authorized to the general internet listing and all the files and folders it consists of so that pages may be served efficiently:
- sudo chmod -R 755 /var/www
Your net server need to now have the permissions it needs to serve content material, and your consumer must be able to create content inside the essential folders.
Step Three — Create Demo Pages for Each Virtual Host
We now have our listing shape in place. Let’s create some content to serve.
For demonstration functions, we’ll make an index.html web page for each web site.
Let’s start with example.com. We can open up an index.html document in a textual content editor, in this example we’ll use nano:
- nano /var/www/example.com/public_html/index.html
Within this report, create an HTML report that indicates the website it’s miles related to, just like the following:
<html><head><title>Welcome to Example.com!</title></head><frame><h1>Success! The example.com digital host is operating!</h1></frame>
Save and close the report (in nano, press CTRL + X then Y then ENTER) when you are finished.
We can copy this file to apply as the idea for our 2d website by typing:
- cp /var/www/example.com/public_html/index.html /var/www/take a look at.com/public_html/index.html
We can then open the report and adjust the relevant portions of facts:
- nano /var/www/check.com/public_html/index.html
/var/www/take a look at.com/public_html/index.html
<html><head><identify>Welcome to Test.com!</title></head><frame> <h1>Success! The test.com virtual host is operating!</h1></frame>
Save and near this document as well. You now have the pages vital to test the virtual host configuration.
Step Four — Create New Virtual Host Files
Virtual host files are the files that designate the real configuration of our virtual hosts and dictate how the Apache web server will respond to diverse domain requests.
Apache comes with a default digital host document referred to as 000-default.conf that we are able to use as a jumping off factor.We are going to copy it over to create a virtual host document for every of our domains.
We will start with one domain, configure it, replica it for our second area, after which make the few in addition modifications needed.The default Ubuntu configuration requires that each virtual host record end in .conf.
Create the First Virtual Host File
Start through copying the document for the primary area:
- sudo cp /and so on/apache2/web sites-available/000-default.conf /etc/apache2/websites-to be had/example.com.conf
Open the brand new report for your editor with root privileges:
- sudo nano /etc/apache2/sites-available/instance.com.conf
With remarks eliminated, the record will look much like this:
/and many others/apache2/websites-available/instance.com.conf
<VirtualHost *:80>ServerAdmin webmaster@localhostDocumentRoot /var/www/htmlErrorLog $APACHE_LOG_DIR/errors.logCustomLog $APACHE_LOG_DIR/get admission to.log blended
Within this document, we are able to customize the items for our first domain and add a few additional directives. This virtual host phase matches any requests that are made on port 80, the default HTTP port.
First, we want to change the ServerAdmin directive to an e-mail that the website online administrator can acquire emails thru.
After this, we need to add two directives.The first, known as ServerName, establishes the base area that must in shape for this virtual host definition.This will most probable be your area.The 2nd, called ServerAlias, defines further names that need to suit as if they had been the base name.This is useful for matching hosts you defined, like www:
The most effective other factor we need to alternate for our virtual host file is the vicinity of the file root for this area.We already created the directory we want, so we simply need to modify the DocumentRoot directive to reflect the directory we created:
When complete, our digital host report have to look like this:
/and so forth/apache2/web sites-available/instance.com.conf
<VirtualHost *:80>ServerAdmin firstname.lastname@example.orgServerName instance.comServerAlias www.example.comDocumentRoot /var/www/example.com/public_htmlErrorLog $APACHE_LOG_DIR/blunders.logCustomLog $APACHE_LOG_DIR/get right of entry to.log blended
At this point, store and near the file.
Copy First Virtual Host and Customize for Second Domain
Now that we have our first digital host document installed, we can create our 2d one through copying that file and adjusting it as wished.
Start by copying it:
- sudo cp /and so forth/apache2/sites-available/instance.com.conf /etc/apache2/web sites-to be had/check.com.conf
Open the brand new record with root privileges on your editor:
- sudo nano /etc/apache2/sites-available/take a look at.com.conf
You now need to alter all the pieces of information to reference your 2nd domain.When you’re completed, it must appear like this:
/etc/apache2/websites-to be had/check.com.conf
<VirtualHost *:eighty>ServerAdmin email@example.comServerName test.comServerAlias www.take a look at.comDocumentRoot /var/www/check.com/public_htmlErrorLog $APACHE_LOG_DIR/error.logCustomLog $APACHE_LOG_DIR/get admission to.log combined
Save and close the file while you are finished.
Step Five — Enable the New Virtual Host Files
Now that we’ve created our digital host documents, we ought to permit them. Apache includes some equipment that allow us to do that.
We’ll be using the a2ensite tool to allow every of our websites. If you would love to examine greater approximately this script, you may talk to the a2ensite documentation.
- sudo a2ensite example.com.conf
- sudo a2ensite take a look at.com.conf
Next, disable the default web page defined in 000-default.conf:
- sudo a2dissite 000-default.conf
When you are finished, you need to restart Apache to make those modifications take impact and use systemctl reputation to verify the achievement of the restart.
- sudo systemctl restart apache2
- sudo systemctl reputation apache2
Your server must now be installation to serve web sites.
Step Six — Set Up Local Hosts File (Optional)
If you haven’t been the use of real domain names which you very own to check this method and were the usage of some instance domains as a substitute, you could at the least take a look at the capability of this manner by briefly enhancing the hosts file for your neighborhood pc.
This will intercept any requests for the domains that you configured and factor them on your VPS server, just as the DNS system would do if you had been the use of registered domains. This will best paintings from your nearby laptop though, and most effective for testing functions.
Make certain you are operating for your nearby laptop for these steps and now not your VPS server.You will need to recognise the pc’s administrative password or in any other case be a member of the administrative organization.
If you’re on a Mac or Linux pc, edit your local document with administrative privileges by typing:
- sudo nano /and so on/hosts
If you are on a Windows machine, you can discover commands on changing your hosts file right here.
The information which you want to add are the public IP address of your server accompanied by way of the area you want to use to reach that server.
Using the domains used in this guide, and changing your server IP for the your_server_IP text, your report should seem like this:
/and so forth/hosts
Save and close the report.
This will direct any requests as an example.com and take a look at.com on our laptop and send them to our server. This is what we need if we aren’t surely the proprietors of these domains so as to test our virtual hosts.
Step Seven — Test your Results
Now that you have your digital hosts configured, you could take a look at your setup with the aid of going to the domain names that you configured for your net browser:
You need to see a page that looks as if this:
You can also go to your 2d web page and notice the record you created in your second web page.
http://take a look at.com
If both of these sites work as predicted, you’ve effectively configured two digital hosts at the same server.
If you adjusted your property pc’s hosts document, you can need to delete the traces you introduced now which you established that your configuration works. This will save you your hosts document from being packed with entries that are no longer important.
If you want to access this long time, don’t forget adding a website call for each website you want and placing it up to factor for your server.
If you accompanied alongside, you must now have a unmarried server dealing with two separate domain names. You can increase this method by way of following the steps we outlined above to make extra digital hosts.
There is not any software program restriction on the wide variety of domains Apache can handle, so experience loose to make as many as your server is capable of managing.