Tutoriel sur l’installation de virtual hosts sur un serveur apache

  • Source : https://www.digitalocean.com/community/tutorials/how-to-set-up-apache-virtual-hosts-on-ubuntu-14-04-lts
  • Langue : Anglais
  • Etat : Terminé 

Exemple de Vhosts : 80

[pastacode lang= »apacheconf » manual= »%3CVirtualHost%20*%3A80%3E%0A%0A%20%20%23The%20ServerName%20specifies%20what%20hostname%20must%20appear%20in%20the%20request’s%20Host%0A%20%20ServerName%20reseauxeasyandfree.simonlena.com%0A%20%20ServerAlias%20reseauxeasyandfree.simonlena.ovh%0A%0A%20%20ServerAdmin%20webmaster%40localhost%0A%0A%20%20DocumentRoot%20%2Fvar%2Fwww%2Fsite%0A%20%20%3CDirectory%20%2Fvar%2Fwww%2Fsite%3E%0A%20%20%20%20Options%20-Indexes%0A%20%20%20%20AllowOverride%20all%0A%20%20%20%20Order%20allow%2Cdeny%0A%20%20%20%20allow%20from%20all%0A%20%20%3C%2FDirectory%3E%0A%0A%20%20%23Available%20loglevels%3A%20trace8%20-%3E%20trace1%2C%20debug%2C%20info%2C%20notice%2C%20warn%2C%20error%2C%20crit%2C%20alert%2C%20emerg%0A%20%20%23LogLevel%20info%20ssl%3Awarn%0A%0A%20%20%23Destination%20des%20fichiers%20de%20Log%0A%20%20ErrorLog%20%24%7BAPACHE_LOG_DIR%7D%2Ferror_blog_reseauxeasyandfree.log%0A%20%20CustomLog%20%24%7BAPACHE_LOG_DIR%7D%2Faccess_blog_reseauxeasyandfree.log%20combined%0A%0A%20%20%23Redirection%20HTTPS%20(%20Let’s%20Encrypt%20)%0A%20%20RewriteEngine%20on%0A%20%20RewriteCond%20%25%7BSERVER_NAME%7D%20%3Dreseauxeasyandfree.simonlena.com%0A%20%20RewriteRule%20%5E%20https%3A%2F%2F%25%7BSERVER_NAME%7D%25%7BREQUEST_URI%7D%20%5BEND%2CQSA%2CR%3Dpermanent%5D%0A%0A%3C%2FVirtualHost%3E%0A » message= » » highlight= » » provider= »manual »/]


Exemple de Vhosts 443 : SSL

[pastacode lang= »apacheconf » manual= »%3CIfModule%20mod_ssl.c%3E%0A%3CVirtualHost%20*%3A443%3E%0A%0A%20%20ServerName%20XXXXXXXXXXX.com%0A%20%20ServerAlias%20XXXXXXXXXXX.ovh%0A%0A%20%20ServerAdmin%20webmaster%40localhost%0A%20%20DocumentRoot%20%2Fvar%2Fwww%2FXXXXXXXXXXX%0A%0A%20%20%3CDirectory%20%2Fvar%2Fwww%2FXXXXXXXXXXX%3E%0A%20%20%20%20Options%20-Indexes%0A%20%20%20%20AllowOverride%20all%0A%20%20%20%20Order%20allow%2Cdeny%0A%20%20%20%20allow%20from%20all%0A%20%20%3C%2FDirectory%3E%0A%0A%20%20%23Activation%20du%20HTTP2%0A%20%20Protocols%20h2%20http%2F1.1%0A%0A%20%20%23Available%20loglevels%3A%20trace8%20-%3E%20trace1%2C%20debug%2C%20info%2C%20notice%2C%20warn%2C%20error%2C%20crit%2C%20alert%2C%20emerg%0A%20%20%23LogLevel%20info%20ssl%3Awarn%0A%0A%20%20%23Destination%20des%20fichiers%20de%20Log%0A%20%20ErrorLog%20%24%7BAPACHE_LOG_DIR%7D%2Ferror_XXXXXXXXXXX.log%0A%20%20CustomLog%20%24%7BAPACHE_LOG_DIR%7D%2Faccess_XXXXXXXXXXX.log%20combined%0A%0A%20%20%23Certificat%20pour%20HTTPS%0A%20%20SSLCertificateFile%20%2Fetc%2Fletsencrypt%2Flive%2FXXXXXXX.com%2Ffullchain.pem%0A%20%20SSLCertificateKeyFile%20%2Fetc%2Fletsencrypt%2Flive%2FXXXXXXXXXXXX.com%2Fprivkey.pem%0A%20%20Include%20%2Fetc%2Fletsencrypt%2Foptions-ssl-apache.conf%0A%0A%20%20%23%23%20Security%20%23%23%0A%0A%20%20%23X-XSS-Protection%0A%20%20%23Header%20set%20X-XSS-Protection%20%20%221%3B%20mode%3Dblock%22%0A%20%20%23Oblige%20toute%20requetes%20par%20le%20HTTPS%20(%20Ne%20pas%20activer%20si%20certaines%20requetes%20en%20HTTP)%0A%20%20%23Header%20set%20Strict-Transport-Security%20%22max-age%3D31536000%3B%20includeSubDomains%3B%20preload%22%0A%20%20%23Xframe%20Option%0A%20%20Header%20always%20append%20X-Frame-Options%20DENY%0A%20%20%23Prevent%20MIME%0A%20%20Header%20set%20X-Content-Type-Options%20nosniff%0A%0A%3C%2FVirtualHost%3E%0A%3C%2FIfModule%3E%0A » message= » » highlight= » » provider= »manual »/]

How To Set Up Apache Virtual Hosts on Ubuntu 14.04 LTS


The Apache web server is the most popular way of serving web content on the internet. It accounts for more than half of all active websites on the internet and is extremely powerful and flexible.

Apache breaks its functionality and components into individual units that can be customized and configured independently. The basic unit that describes an individual site or domain is called a virtual host.

These designations allow the administrator to use one server to host multiple domains or sites off of a single interface or IP by using a matching mechanism. This is relevant to anyone looking to host more than one site off of a single VPS.

Each domain that is configured will direct the visitor to a specific directory holding that site’s information, never indicating that the same server is also responsible for other sites. This scheme is expandable without any software limit as long as your server can handle the load.

In this guide, we will walk you through how to set up Apache virtual hosts on an Ubuntu 14.04 VPS. During this process, you’ll learn how to serve different content to different visitors depending on which domains they are requesting.


Before you begin this tutorial, you should create a non-root user as described in steps 1-4 here.

You will also need to have Apache installed in order to work through these steps. If you haven’t already done so, you can get Apache installed on your server through apt-get:

After these steps are complete, we can get started.

For the purposes of this guide, my configuration will make a virtual host for example.com and another fortest.com. These will be referenced throughout the guide, but you should substitute your own domains or values while following along.

To learn how to set up your domain names with DigitalOcean, follow this link. If you do not have domains available to play with, you can use dummy values.

We will show how to edit your local hosts file later on to test the configuration if you are using dummy values. This will allow you to test your configuration from your home computer, even though your content won’t be available through the domain name to other visitors.

Step One — Create the Directory Structure

The first step that we are going to take is to make a directory structure that will hold the site data that we will be serving to visitors.

Our document root (the top-level directory that Apache looks at to find content to serve) will be set to individual directories under the /var/www directory. We will create a directory here for both of the virtual hosts we plan on making.

Within each of these directories, we will create a public_html file that will hold our actual files. This gives us some flexibility in our hosting.

For instance, for our sites, we’re going to make our directories like this:

The portions in red represent the domain names that we are wanting to serve from our VPS.

Step Two — Grant Permissions

Now we have the directory structure for our files, but they are owned by our root user. If we want our regular user to be able to modify files in our web directories, we can change the ownership by doing this:

The $USER variable will take the value of the user you are currently logged in as when you press « ENTER ». By doing this, our regular user now owns the public_html subdirectories where we will be storing our content.

We should also modify our permissions a little bit to ensure that read access is permitted to the general web directory and all of the files and folders it contains so that pages can be served correctly:

Your web server should now have the permissions it needs to serve content, and your user should be able to create content within the necessary folders.

Step Three — Create Demo Pages for Each Virtual Host

We have our directory structure in place. Let’s create some content to serve.

We’re just going for a demonstration, so our pages will be very simple. We’re just going to make anindex.html page for each site.

Let’s start with example.com. We can open up an index.html file in our editor by typing:

In this file, create a simple HTML document that indicates the site it is connected to. My file looks like this:

Save and close the file when you are finished.

We can copy this file to use as the basis for our second site by typing:

We can then open the file and modify the relevant pieces of information:

Save and close this file as well. You now have the pages necessary to test the virtual host configuration.

Step Four — Create New Virtual Host Files

Virtual host files are the files that specify the actual configuration of our virtual hosts and dictate how the Apache web server will respond to various domain requests.

Apache comes with a default virtual host file called 000-default.conf that we can use as a jumping off point. We are going to copy it over to create a virtual host file for each of our domains.

We will start with one domain, configure it, copy it for our second domain, and then make the few further adjustments needed. The default Ubuntu configuration requires that each virtual host file end in .conf.

Create the First Virtual Host File

Start by copying the file for the first domain:

Open the new file in your editor with root privileges:

The file will look something like this (I’ve removed the comments here to make the file more approachable):

As you can see, there’s not much here. We will customize the items here for our first domain and add some additional directives. This virtual host section matches any requests that are made on port 80, the default HTTP port.

First, we need to change the ServerAdmin directive to an email that the site administrator can receive emails through.

After this, we need to add two directives. The first, called ServerName, establishes the base domain that should match for this virtual host definition. This will most likely be your domain. The second, calledServerAlias, defines further names that should match as if they were the base name. This is useful for matching hosts you defined, like www:

The only other thing we need to change for a basic virtual host file is the location of the document root for this domain. We already created the directory we need, so we just need to alter the DocumentRootdirective to reflect the directory we created:

In total, our virtualhost file should look like this:

Save and close the file.

Copy First Virtual Host and Customize for Second Domain

Now that we have our first virtual host file established, we can create our second one by copying that file and adjusting it as needed.

Start by copying it:

Open the new file with root privileges in your editor:

You now need to modify all of the pieces of information to reference your second domain. When you are finished, it may look something like this:

Save and close the file when you are finished.

Step Five — Enable the New Virtual Host Files

Now that we have created our virtual host files, we must enable them. Apache includes some tools that allow us to do this.

We can use the a2ensite tool to enable each of our sites like this:

When you are finished, you need to restart Apache to make these changes take effect:

You will most likely receive a message saying something similar to:

This is a harmless message that does not affect our site.

Step Six — Set Up Local Hosts File (Optional)

If you haven’t been using actual domain names that you own to test this procedure and have been using some example domains instead, you can at least test the functionality of this process by temporarily modifying the hosts file on your local computer.

This will intercept any requests for the domains that you configured and point them to your VPS server, just as the DNS system would do if you were using registered domains. This will only work from your computer though, and is simply useful for testing purposes.

Make sure you are operating on your local computer for these steps and not your VPS server. You will need to know the computer’s administrative password or otherwise be a member of the administrative group.

If you are on a Mac or Linux computer, edit your local file with administrative privileges by typing:

If you are on a Windows machine, you can find instructions on altering your hosts file here.

The details that you need to add are the public IP address of your VPS server followed by the domain you want to use to reach that VPS.

For the domains that I used in this guide, assuming that my VPS IP address is, I could add the following lines to the bottom of my hosts file:

This will direct any requests for example.com and test.com on our computer and send them to our server at This is what we want if we are not actually the owners of these domains in order to test our virtual hosts.

Save and close the file.

Step Seven — Test your Results

Now that you have your virtual hosts configured, you can test your setup easily by going to the domains that you configured in your web browser:

You should see a page that looks like this:

Apache virt host example

Likewise, if you can visit your second page:

You will see the file you created for your second site:

Apache virt host test

If both of these sites work well, you’ve successfully configured two virtual hosts on the same server.

If you adjusted your home computer’s hosts file, you may want to delete the lines you added now that you verified that your configuration works. This will prevent your hosts file from being filled with entries that are not actually necessary.

If you need to access this long term, consider purchasing a domain name for each site you need andsetting it up to point to your VPS server.


If you followed along, you should now have a single server handling two separate domain names. You can expand this process by following the steps we outlined above to make additional virtual hosts.

There is no software limit on the number of domain names Apache can handle, so feel free to make as many as your server is capable of handling.