Configure Apache Virtual Hosts - Ubuntu

Difficulty: 2
Time: 15 minutes

Want to host websites on your server? Using Apache? Great. This article will show you how to do exactly that using Apache’s “virtual hosts.”

In Apache, you can use virtual hosts to direct http traffic for a given domain name to a particular directory (i.e. the root directory of the website for the domain in the request). This feature is commonly used to host multiple websites, but we recommend using it for every website on your server including the first.

Throughout this article, we'll use an example domain - coolexample.com - but you should replace it with the domain name or subdomain you want to host on your server.

Install the Apache web server

To get Apache on your server, you can either install it as part of a LAMP stack, or you can install Apache by itself:

  1. Update your packages using apt-get:
    sudo apt-get update
  2. Install Apache:
    sudo apt-get install apache2

Set up the virtual host

  1. Create the virtual directories for your domain:
    sudo mkdir -p /var/www/coolexample.com/public_html
  2. Change the ownership to the Apache group:
    sudo chown -R www-data:www-data /var/www/coolexample.com/public_html
    This lets Apache modify files in your web directories.
  3. Change the directory's permissions so its files can be read from the internet:
    sudo chmod -R 755 /var/www/coolexample.com/public_html

Create content for the website

If you have the content for the website prepped, you can upload it to the /public_html folder you created in the last section.

If you don't have content ready to upload, you can create a sample home page (also known as an index file, which is the first page that loads when visitors come to your domain).

  1. Create the index file:
    sudo vim /var/www/coolexample.com/public_html/index.html
  2. Add some content to the file:
    <html> 
    <head> 
    <title>Welcome to my site! 
    </head> 
    <body> 
    <h1>Hooray! Your virtual host is working!</h1> 
    </body> 
    </html>
  3. Save and close the file:
    :wq!

Create virtual host file

For the virtual host configuration file, we're going to copy Apache's default config file called 000-default.conf from the sites-available directory as a template.

  1. Copy the 000-default.conf file:
    sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/coolexample.com.conf
  2. Open your newly copied config file:
    sudo vim /etc/apache2/sites-available/coolexample.com.conf
  3. In the file, find DocumentRoot towards the beginning of the file, and update with your own domain:
    DocumentRoot /var/www/coolexample.com/public_html
  4. Below DocumentRoot, add these two lines:
    ServerName www.coolexample.com
    ServerAlias coolexample.com
  5. Save and close the file:
    :wq!

Note: Updating the ServerAdmin, ErrorLog, and CustomLog are not required to set up your virtual host, but you can go ahead and update ErrorLog and CustomLog to your domain and ServerAdmin to your email.

Enable your virtual host

  1. Enable your virtual host:
    sudo a2ensite coolexample.com.conf
  2. Restart Apache:
    sudo service apache2 restart

Point your domain name to your server

If your domain name isn't currently loading another website, you should point it to your server to test your new config.

How you do this depends on where your domain name is registered and whose server you're using:

Domain registered? Server hosted? Do this...
GoDaddy GoDaddy Point your domain name to a server
Another company GoDaddy Find a server's public IP address and then update your domain name's primary ("@") A record.
GoDaddy Another company Find your server's IP address, and then change your domain's IP address to use it.
Another company Another company Find your server's IP address, and then change your domain's IP address to use it.

Changes to your domain can take up to 48 hours to display across the internet. However, once they do, you can visit your domain name and view the test page you created earlier!

Adding additional virtual hosts

To host additional sites, repeat the steps above starting with Set up the virtual host.

In the Create virtual host file section, you can save some time by simply copying the configuration file you used for your first domain:

sudo cp /etc/apache2/sites-available/coolexample.com.conf /etc/apache2/sites-available/your second domain name.conf

Next steps

With Apache set up to work with your domain name, you can now do things like set up things like WordPress or Drupal.


Помогла ли вам эта статья?
Благодарим вас за отзыв. Чтобы связаться с сотрудником службы поддержки клиентов, позвоните по номеру этой службы или воспользуйтесь опцией чата выше.
Мы рады вам помочь! У вас остались вопросы?
Приносим извинения. Расскажите нам, какие затруднения вы испытываете или почему рекомендованное решение не помогло устранить проблему.