Tutorial

How To Set Up Apache Virtual Hosts on Ubuntu 16.04

Updated on November 30, 2021
English
How To Set Up Apache Virtual Hosts on Ubuntu 16.04
Not using Ubuntu 16.04?Choose a different version or distribution.
Ubuntu 16.04

Introduction

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 server.

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 16.04 server. During this process, you’ll learn how to serve different content to different visitors depending on which domains they are requesting.

Prerequisites

Before you begin this tutorial, you should create a sudo-enabled 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:

  1. sudo apt-get update
  2. sudo apt-get install apache2

After these steps are complete, we can get started.

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

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 1 — Creating 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 folder 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:

  1. sudo mkdir -p /var/www/example.com/public_html
  2. sudo mkdir -p /var/www/test.com/public_html

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

Step 2 — Granting 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:

  1. sudo chown -R $USER:$USER /var/www/example.com/public_html
  2. sudo chown -R $USER:$USER /var/www/test.com/public_html

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:

  1. sudo chmod -R 755 /var/www

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 3 — Creating 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 an index.html page for each site.

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

  1. nano /var/www/example.com/public_html/index.html

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

/var/www/example.com/public_html/index.html
<html>
  <head>
    <title>Welcome to Example.com!</title>
  </head>
  <body>
    <h1>Success!  The example.com virtual host is working!</h1>
  </body>
</html>

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:

  1. cp /var/www/example.com/public_html/index.html /var/www/test.com/public_html/index.html

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

  1. nano /var/www/test.com/public_html/index.html
/var/www/test.com/public_html/index.html
<html>
  <head>
    <title>Welcome to Test.com!</title>
  </head>
  <body> <h1>Success!  The test.com virtual host is working!</h1>
  </body>
</html>

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

Step 4 — Creating 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.

Creating the First Virtual Host File

Start by copying the file for the first domain:

  1. sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/example.com.conf

Open the new file in your editor with root privileges:

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

The file will look something like this (Comments have been removed here to make the file more approachable):

/etc/apache2/sites-available/example.com.conf
<VirtualHost *:80>
    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/html
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

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.

ServerAdmin admin@example.com

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, called ServerAlias, defines further names that should match as if they were the base name. This is useful for matching hosts you defined, like www:

ServerName example.com
ServerAlias www.example.com

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 DocumentRoot directive to reflect the directory we created:

DocumentRoot /var/www/example.com/public_html

In total, our virtualhost file should look like this:

/etc/apache2/sites-available/example.com.conf
<VirtualHost *:80>
    ServerAdmin admin@example.com
    ServerName example.com
    ServerAlias www.example.com
    DocumentRoot /var/www/example.com/public_html
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Save and close the file.

Copying the First Virtual Host and Customizing 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:

  1. sudo cp /etc/apache2/sites-available/example.com.conf /etc/apache2/sites-available/test.com.conf

Open the new file with root privileges in your editor:

  1. sudo nano /etc/apache2/sites-available/test.com.conf

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:

/etc/apache2/sites-available/test.com.conf
<VirtualHost *:80>
    ServerAdmin admin@test.com
    ServerName test.com
    ServerAlias www.test.com
    DocumentRoot /var/www/test.com/public_html
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Save and close the file when you are finished.

Step 5 — Enabling 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:

  1. sudo a2ensite example.com.conf
  2. sudo a2ensite test.com.conf

Next, disable the default site defined in 000-default.conf:

  1. sudo a2dissite 000-default.conf

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

  1. sudo systemctl restart apache2

Step 6 — Setting 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 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 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:

  1. sudo nano /etc/hosts

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 203.0.113.10, I could add the following lines to the bottom of my hosts file:

/etc/hosts
127.0.0.1   localhost
127.0.1.1   guest-desktop
203.0.113.10 example.com
203.0.113.10 test.com

This will direct any requests for example.com and test.com on our computer and send them to our server at 203.0.113.10. 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 7 — Testing 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:

http://example.com

You should see a page that looks like this:

Apache virt host example

Likewise, if you can visit your second page:

http://test.com

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 and setting it up to point to your VPS server.

Conclusion

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.

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about our products

About the authors

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
72 Comments
Leave a comment...

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

I have configured the virtual host for example.com but still getting apache default page when i load the page.when i ping example.com i get the same IP that I configure in /etc/hosts.Have already disabled default page with a2dissite 000-default.conf. What could be the possible issue?

I have the same issue. It only works for me over SSL. I’ve posted on other sites asking for help fixing the issue.

If you are using SSL, you will have to keep the 000-default.conf enabled if you are letting your users visit your site with either http:// or https://. The 000-default.conf file acts as a catch all, and – as long as you have the proper redirect added in your virtual host file – all traffic will default to the https://.

As @mark3236216557d states, I just left the 000-default.conf enabled. If installing letsencrypt certs, you have to do some additional stuff. First you need to modify the 000-default-le-ssl.conf file (after configuring letsencrypt) to point your document root to the first domain you set up (at least in my case).

sudo nano /etc/apache2/sites-available/000-default-le-ssl.conf

Then modify your that to

DocumentRoot /var/www/example.com/public_html

Maybe the order of operations would be to set up all you virtual domains, and then do a mass letsencrypt configuration of all your domains like

sudo letsencrypt --apache -d example.com -d www.example.com -d test.com -d www.test.com

Your config must have the “.conf” extension eg /etc/apache2/sites-available/example.com.conf

to get the result, you need Allow incoming traffic for this profile. Please follow this tutorial.

Example link

The tutorial works for me. Thanks

Question 1

[1] DocumentRoot /var/www/test.com/public_html [2] DocumentRoot /var/www/test_com/public_html

which version is more better to use? Is it common practice to use dotted version?

Question 2 Once file /etc/apache2/sites-available/test.com.conf is changed, do I have re run $ sudo a2ensite test.com.conf or not?

Thanks!

Hi and thank you for the tutorials. I follow this instructions but when I try ping in console i receive the following:

64 bytes from sub-08.example.com (xxx.xx.xxx.xx): icmp_seq=8 ttl=64 time=0.059 ms

where do you thing I forget the example.com?

Could not get the new virtual server to see it’s DocumentRoot, it kept using the servers default yet it appeared to work: We put html docs in the users directories /home/bob/html adding DocumentRoot to each VirtualHost: <VirtualHost *> ServerName bob.fg DocumentRoot /home/bob/html <Directory /home/bob/html> allow from all Options Indexes FollowSymLinks AllowOverride None Require all granted
</Directory> </VirtualHost> Apache was not getting me to my DocumentRoot. After staring at the conf files I discovered webmin only put <VirtualHost *> instead of <VirtualHost *:80> made the change and it works now.

Thank you Brennen, this tutorial was perfect!

When I login with SFTP I can’t create folders inside the public_html folder but I can upload files or mofify them.

example.com” folder and the “public_html” folder both have permission 755 and user is my user where I logon.

Any idea why I can’t create folders?

Step 5 I got this error, when I try to use “sudo a2ensite mydomain.com.conf”

perl: warning: Setting locale failed. perl: warning: Please check that your locale settings: LANGUAGE = (unset), LC_ALL = (unset), LC_CTYPE = “UTF-8”, LANG = “en_US.UTF-8” are supported and installed on your system. perl: warning: Falling back to a fallback locale (“en_US.UTF-8”). Enabling site mydomain.com. To activate the new configuration, you need to run: service apache2 reload

The same problem to me :(

This comment has been deleted

    I have configured all the step from above.but still when i press “example.com”,Not Found

    The requested URL / was not found on this server.

    Be careful, people! If you get a “Not Found” / “The requested URL / was not found on this server.” response, then make sure your DocumentRoot directory is correct in your example.com.conf file.

    I accidentally left the /html/ part in the url that was there by default:

    DocumentRoot /var/www/html/example.com/public_html
    DocumentRoot /var/www/example.com/public_html         <--- fixed
    

    If that doesn’t fix your “Not Found” problem, try this commant to check your apache logs:

    sudo cat /var/log/apache2/error.log
    

    hai, i have a quick question. why I unable to ping my virtualhost inside the server and also I unable to curl

    Hi. I’m having some issues while creating the virtual host. The file located in sites-available is called repo.local.conf, and it looks like this:

    <VirtualHost *:80>
        ServerAdmin webmaster@localhost
        ServerName repo.local
        ServerAlias www.repo.local
        DocumentRoot /media/syn/Almacen/Tools/RepoUbuntu/mirror/ubuntu.uci.cu
    
        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined
    </VirtualHost>
    

    This file was enabled using the procedure shown in this tutorial. The hosts file looks like this:

    127.0.0.1	localhost
    127.0.1.1	syn-hp
    127.0.0.1	repo.local
    
    # The following lines are desirable for IPv6 capable hosts
    ::1     ip6-localhost ip6-loopback
    fe00::0 ip6-localnet
    ff00::0 ip6-mcastprefix
    ff02::1 ip6-allnodes
    ff02::2 ip6-allrouters
    

    I did the “sudo chown -R $USER:$USER RepoUbuntu/” thing from Tools folder, and also gave it 777 permissions.

    After restarting apache2 service, I’m getting this message:

    Forbidden
    You don't have permission to access / on this server.
    Apache/2.4.18 (Ubuntu) Server at repo.local Port 80
    

    Does anyone knows where did i go wrong? Please help :).

    Nice article.

    I have one question. Why when changing ownership of the folders, do we set the ownership to;

    $USER:$USER
    

    I thought it should be set to

    $USER:www-data
    

    I have seen that somewhere else, and wasn’t too sure what the “www-data” stands for.

    Also as I only need to use these virtual machines locally when I open up my hosts file;

    /etc/hosts
    

    I point the domains to my localhost IP address like so;

    127.0.0.1  example.com
    127.0.0.1  test.com
    

    This worked well for me.

    it should be neither. it should all be www-data:www-data! Certain apps would not work if you change the file and folders to your user directly. You should add your user to the www-data group instead to have read/write access.

    How to use my username and my group instead of www-data user and group? When I going to change these values:

    export APACHE_RUN_USER=www-data
    export APACHE_RUN_GROUP=www-data
    
    

    to

    export APACHE_RUN_USER=my_user
    export APACHE_RUN_GROUP=my_group
    
    

    in the ‘envvars’ file, doesn’t works. Apparently php7 doesn’t work correctly if these changes are made.

    Better yet: How to use multiple users and groups with permissions differents to www-data using virtualhost?

    Thanks for help.

    Hi, I have a quick question. I have a domain running under the default /www/html and would like to add another domain, how do you accomplish this without moving the default site or do I have to move the default page when I add a second domain?

    Hi, this doesn’t work for me. When i opened the http://test.com, it directd me to https://www.test.com with a notification: 462 Forbidden Region: Your request for this resource had been blocked. This resource is not available in your region.

    DOSarrest Internet Security is a cloud based fully managed DDoS protection service. This request has been blocked by DOSarrest due to the above violation. If you believe you are getting blocked in error please contact the administrator of www.test.com to resolve this issue.

    In case of http://example.com, it showed: Example Domain

    This domain is established to be used for illustrative examples in documents. You may use this domain in examples without prior coordination or asking for permission.

    More information…

    Any ideas?

    For me www doesn’t work only http:// does

    I followed these instructions with two domains that I own. The first domain serves fine when I browse to http://www.domain1.org. The second one always returns the default page from /var/www/html even when I browse to http://www.domain2.com. I ran a2dissite 000-default.conf and restarted Apache, but get the same behavior.

    apache2ctl -S returns this:

    AH00558: apache2: Could not reliably determine the server’s fully qualified domain name, using 127.0.1.1. Set the ‘ServerName’ directive globally to suppress this message VirtualHost configuration: *:80 is a NameVirtualHost default server domain1.org (/etc/apache2/sites-enabled/domain1.org.conf:1) port 80 namevhost domain1.org (/etc/apache2/sites-enabled/domain1.org.conf:1) alias www.domain1.org port 80 namevhost domain2.com (/etc/apache2/sites-enabled/domain2.com.conf:1) alias www.domain2.com ServerRoot: “/etc/apache2” Main DocumentRoot: “/var/www/html” Main ErrorLog: “/var/log/apache2/error.log” Mutex default: dir=“/var/run/apache2/” mechanism=default Mutex watchdog-callback: using_defaults PidFile: “/var/run/apache2/apache2.pid” Define: DUMP_VHOSTS Define: DUMP_RUN_CFG User: name=“www-data” id=33 Group: name=“www-data” id=33

    Any idea why this is happening?

    I’ve follow everything to the letter sudo a2ensite olmacdoanldsfarm.com /var/www/html already enabled sudo a2ensite peapatchrvpark.com /var/www/peapatch/ already enabled sudo a2ensite redpoppyrentalhall.com /var/www/redpoppy/ already enabled sudo dissite default already disabled etc/hosts 127.0.0.1 olmacds localhost 192.168.1.29 olmacdonaldsfarm.com 192.168.1.29 peapatchrvpark.com 192.168.1.29 redpoppyrentalhall.com

    192.168.1.29 is the address of my server on my private network 65.245.187.152 is the address of my public ip it serves olmacdonaldsfarm.com just fine not the other two

    ive tried everything I have given up

    Be careful, people! If you get a “Not Found” / “The requested URL / was not found on this server.” response, then make sure your DocumentRoot directory is correct in your example.com.conf file.

    I accidentally left the /html/ part in the url that was there by default:

    DocumentRoot /var/www/html/example.com/public_html
    DocumentRoot /var/www/example.com/public_html         <--- fixed
    

    If that doesn’t fix your “Not Found” problem, try this commant to check your apache logs:

    sudo cat /var/log/apache2/error.log
    

    hai, i have a quick question. why I unable to ping my virtualhost inside the server and also I unable to curl

    Hi. I’m having some issues while creating the virtual host. The file located in sites-available is called repo.local.conf, and it looks like this:

    <VirtualHost *:80>
        ServerAdmin webmaster@localhost
        ServerName repo.local
        ServerAlias www.repo.local
        DocumentRoot /media/syn/Almacen/Tools/RepoUbuntu/mirror/ubuntu.uci.cu
    
        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined
    </VirtualHost>
    

    This file was enabled using the procedure shown in this tutorial. The hosts file looks like this:

    127.0.0.1	localhost
    127.0.1.1	syn-hp
    127.0.0.1	repo.local
    
    # The following lines are desirable for IPv6 capable hosts
    ::1     ip6-localhost ip6-loopback
    fe00::0 ip6-localnet
    ff00::0 ip6-mcastprefix
    ff02::1 ip6-allnodes
    ff02::2 ip6-allrouters
    

    I did the “sudo chown -R $USER:$USER RepoUbuntu/” thing from Tools folder, and also gave it 777 permissions.

    After restarting apache2 service, I’m getting this message:

    Forbidden
    You don't have permission to access / on this server.
    Apache/2.4.18 (Ubuntu) Server at repo.local Port 80
    

    Does anyone knows where did i go wrong? Please help :).

    Nice article.

    I have one question. Why when changing ownership of the folders, do we set the ownership to;

    $USER:$USER
    

    I thought it should be set to

    $USER:www-data
    

    I have seen that somewhere else, and wasn’t too sure what the “www-data” stands for.

    Also as I only need to use these virtual machines locally when I open up my hosts file;

    /etc/hosts
    

    I point the domains to my localhost IP address like so;

    127.0.0.1  example.com
    127.0.0.1  test.com
    

    This worked well for me.

    it should be neither. it should all be www-data:www-data! Certain apps would not work if you change the file and folders to your user directly. You should add your user to the www-data group instead to have read/write access.

    How to use my username and my group instead of www-data user and group? When I going to change these values:

    export APACHE_RUN_USER=www-data
    export APACHE_RUN_GROUP=www-data
    
    

    to

    export APACHE_RUN_USER=my_user
    export APACHE_RUN_GROUP=my_group
    
    

    in the ‘envvars’ file, doesn’t works. Apparently php7 doesn’t work correctly if these changes are made.

    Better yet: How to use multiple users and groups with permissions differents to www-data using virtualhost?

    Thanks for help.

    Hi, I have a quick question. I have a domain running under the default /www/html and would like to add another domain, how do you accomplish this without moving the default site or do I have to move the default page when I add a second domain?

    Hi, this doesn’t work for me. When i opened the http://test.com, it directd me to https://www.test.com with a notification: 462 Forbidden Region: Your request for this resource had been blocked. This resource is not available in your region.

    DOSarrest Internet Security is a cloud based fully managed DDoS protection service. This request has been blocked by DOSarrest due to the above violation. If you believe you are getting blocked in error please contact the administrator of www.test.com to resolve this issue.

    In case of http://example.com, it showed: Example Domain

    This domain is established to be used for illustrative examples in documents. You may use this domain in examples without prior coordination or asking for permission.

    More information…

    Any ideas?

    For me www doesn’t work only http:// does

    I followed these instructions with two domains that I own. The first domain serves fine when I browse to http://www.domain1.org. The second one always returns the default page from /var/www/html even when I browse to http://www.domain2.com. I ran a2dissite 000-default.conf and restarted Apache, but get the same behavior.

    apache2ctl -S returns this:

    AH00558: apache2: Could not reliably determine the server’s fully qualified domain name, using 127.0.1.1. Set the ‘ServerName’ directive globally to suppress this message VirtualHost configuration: *:80 is a NameVirtualHost default server domain1.org (/etc/apache2/sites-enabled/domain1.org.conf:1) port 80 namevhost domain1.org (/etc/apache2/sites-enabled/domain1.org.conf:1) alias www.domain1.org port 80 namevhost domain2.com (/etc/apache2/sites-enabled/domain2.com.conf:1) alias www.domain2.com ServerRoot: “/etc/apache2” Main DocumentRoot: “/var/www/html” Main ErrorLog: “/var/log/apache2/error.log” Mutex default: dir=“/var/run/apache2/” mechanism=default Mutex watchdog-callback: using_defaults PidFile: “/var/run/apache2/apache2.pid” Define: DUMP_VHOSTS Define: DUMP_RUN_CFG User: name=“www-data” id=33 Group: name=“www-data” id=33

    Any idea why this is happening?

    I’ve follow everything to the letter sudo a2ensite olmacdoanldsfarm.com /var/www/html already enabled sudo a2ensite peapatchrvpark.com /var/www/peapatch/ already enabled sudo a2ensite redpoppyrentalhall.com /var/www/redpoppy/ already enabled sudo dissite default already disabled etc/hosts 127.0.0.1 olmacds localhost 192.168.1.29 olmacdonaldsfarm.com 192.168.1.29 peapatchrvpark.com 192.168.1.29 redpoppyrentalhall.com

    192.168.1.29 is the address of my server on my private network 65.245.187.152 is the address of my public ip it serves olmacdonaldsfarm.com just fine not the other two

    ive tried everything I have given up

    I followed this on ubuntu 16.04. At the end, when I try to access http://example.com in the browser, all in see is ‘ERROR’ Also, I used to access the default apache2 homepage by typing localhost but after the configuration, I can only see it by typing 127.0.0.1. When I type in localhost, I see ‘ERROR’

    PS: I used, DocumentRoot /var/www/fintecg/public

    because I want to use this for an actual Phalcon project whose index.php is in the public, not public_html folder

    Please help, I followed this tutorial. amazing tutorial. thank you. I HAVE PROBLEM. the files not working. when I add anything to the public htmls file not working even php.info. I followed this tutorial step by step.

    Very helpful!

    anybody get this error " To activate the new configuration, you need to run: service apache2 reload " ?

    A note for anyone using this tutorial: There are three errors:

    1. Create The first Virtual Host File The tutorial instructions are: sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/example.com.conf

    This should be: sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/example.conf —The name of the file “example” should not have the “.com” extension.

    1. Open the new file in your editor with root privileges: The tutorial instructions are: $sudo nano /etc/apache2/sites-available/example.com.conf

    This should be: sudo nano /etc/apache2/sites-available/example.conf – Again the file name should not have the “.com” extension.

    1. /etc/apache2/sites-available/example.com.conf There is an error in the conf file: The ServerName and the ServerAlias are reversed, the inputs should be:

    <VirtualHost *:80> ServerAdmin admin@example.com ServerName www.example.com or Your domain ServerAlias example.com or Your domain DocumentRoot /var/www/example.com/public_html ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined </VirtualHost> 4. You may see this error: To activate the new configuration, you need to run: service apache2 reload run this $sudo service apache2 restart.

    Go to the domain and you should see the page that you configured.

    That really saved my day! no ‘.com’ in the middle of .conf file.

    Hi I follow your guide and everything works fine. I have a commercial web site and a blog. I try to install in a third (virtual) folder the Flarum forum software. The installation after some problems completed correct. But when I try to open the forum I receive 404 error, there Is no forum. Any ideas where is the problem. Maybe is in forum.conf?

    Hello, Can somebody help me with pointing a domain to an address like this:

    http://javaapp:8001 or http://xxx.xxx.xxx.xxx:8001

    How can I reverse point an apache virtual host to an IP address with a port please?

    I see .conf.save files generated when i make a site.; mysite.conf.save. what are these files and can i delete them?

    I am in step 3 , I have typed in Html code for trail and now I want to save it. How to save it and keyboard short cuts don’t work. Can any one help how to save file to /var/www/test.com/public_html/index.html . Thanks

    Great tutorial and steps. Thx!

    Hi,

    I am using this with Ubuntu 16.04.3 LTS and the first site seems directing correctly but the second site is showing the message below:

    Not Found
    
    The requested URL / was not found on this server.
    
    Apache/2.4.18 (Ubuntu) Server at heatpumpchecker.com Port 80
    

    When running “sudo apache2ctl configtest” I am receiving the following error:

    AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1. Set the 'ServerName' directive globally to suppress this message
    Syntax OK
    

    I have disabled 000-default.conf and checked the configuration of the other sites.

    Any guidance would be appreciated.

    Thanks.

    I got this working. I had set the document root wrong on one of the sites.

    You should add extra firewall permission for this,if it is not working just try,

    sudo ufw allow in “Apache Full”

    did you remember this from the previous tutorial :)

    hey what if your server has ssl ? and what about ssl for multiple domain on single ip ? also when i visit my one website(site2) it is redirecting me to my other website (site1) location : site1 /var/www/html site2 : /var/www/example.com/html anythng wrong here ??? i followed exact tutorial, i think it is about ssl and also i dont want my site2 visitors to visit site1 just keep them separate

    I have the same problem as you, and I feel it’s to do with SSL as well.

    In step four create new Virtual host files, I had to add example.com:80 in <VirtualHost *: 80> and then it was like this <VirtualHost example.com:80>, this way virtualhost finds the directory of the site correctly, I repeated this to <VirtualHost test.com:80> and so it worked perfectly.

    WOW! Thank you!!

    Perfect installation. I finally have my http://parentpositivekids.com up and running, thanks to the wonderful tutorials here on DO.

    I tried over and over again to do the multi-server, multi-WordPress on Nginx, but could never get it to work. I can now finally see the light at the end of the tunnel… thank you for the glimmer of hope! I was truly beginning to think this non-techy person was never going to get it!

    Apache is easier for me.

    Very Thankful article. it save my lots of time…

    Very clear instructions. Thanks.

    One addition: www.mydomain.com was not working, mydomain.com was

    I found this article via searchengine:

    Why isn’t my site available via the www prefix?

    goto : DNS -> SELECT DOMAIN -> ADD RECORD -> Select record type “A” hostname : www IP Adress : your droplets IP

    After applying this, it works. Thanks

    great tutorial. Thanks a lot!

    Very good tutorial, thanks for sharing your expertise!

    Just a note to those who follow the link at the beginning to set up a Ubuntu server: if you follow those (also good) instructions exactly, you will set up the firewall, ufw, to not allow connections on port 80, thus making this tutorial fail at the last step.

    To avoid this, after you ‘enable’ ufw in that tutorial, do this:

    $ sudo ufw allow 80/tcp
    

    Hi, I’ve been using this guide for some time but I have a problem: I’m hosting about different sites on my apache server, some are static html, some are drupal and some are wordpress. With drupal and Wordpress sites I’m not allowed to update plugins directly from the sites’ administrations and I have to do it manually through FTP. I guess this is due to permissions configurations… Also I’ve noticed other sites suggest to give www-data (which is apache user) ownership and permissions to access the sites directories, instead this guide says permissions and ownership of the directories should be set for the regular user…

    Which practice is best? And may give permissions to www-data solve my problem?

    thanks

    Great article… did not even struggle to have my domain up and running… Great stuff!

    This comment has been deleted

      [URL=http://happythanksgivingimages.info/]What Day Is Thanksgiving[/URL] [URL=http://happythanksgivingimages.info/thanksgiving-turkey-pictures.html]Happy Thanksgiving Turkey Pictures 2018[/URL] [URL=http://happythanksgivingimages.info/thanksgiving-photos.html]Thanksgiving Photos 2018[/URL] [URL=http://happythanksgivingimages.info/thanksgiving-poems.html]Thanksgiving Poems[/URL] [URL=http://happythanksgivingimages.info/thanksgiving-greetings.html]Thanksgiving day greetings 2018[/URL] [URL=http://happythanksgivingimages.info/thanksgiving-prayer.html]Thanksgiving prayer for family[/URL]

      Hey, I made this Virtual Host setup much easier by creating a script: https://github.com/karimzouine1/apache2-virtual-hosts-automation-script Hope you find it useful it’s an open source project, I did to make V-Hosts easier :)

      Also, Digital Ocean Team if you want you can use my script in your tutorials. I just want to make it easier for people to setup v-hosts in seconds and hope you guys might use it :)

      This comment has been deleted

        Hello Author, Thank you very much for writing us the article. I have a question, by disabling the default site: 000-default.conf haven’t we defeated the whole purpose of the apache virtual host? I would like my default page be available all the time and I can reach to the newly created web host page both at the same time: if someone wants to access it they just require to type http://mynewpage.mylabserver.com could anybody help to achieve this target ?

        tutorial worked for me but i have an issue

        https://domain1.com https://domain2.com both are working fine but when i open https://www.domain2.com it was showing contents of https://domain1.com

        Hi, I did these steps as described, in attempt to create a subdomain.

        In the folder

        /etc/apache2/sites-available
        

        I create the file ci.lojainfo.com.br.conf with the content

        <VirtualHost *:80>
            ServerName ci.lojainfo.com.br
            ServerAdmin tiago.ferreira@lojainfo.com.br
            DocumentRoot /var/www/lojainfo/apps/ci
            ErrorLog ${APACHE_LOG_DIR}/error.log
            CustomLog ${APACHE_LOG_DIR}/access.log combined
        </VirtualHost>
        

        And runned the command

        sudo a2ensite ci.lojainfo.com.br
        

        Reloaded the apache with the command

        sudo service apache2 reload
        

        But not working, I’m missing some steps

        Thanks for your help

        I have configured my three websites, using VHost, all are working fine. However, just one of WP website downloads a file by name “download” and has some HTML code inside. I am not sure how to fix it, when other WP sites are working purely fine.

        Please suggest any help. I have not applied any permissions to the folders (of all sites), and don’t know which permission I need to apply if required.

        I have tried this solution for my couple of websites. I am using PHP5.6 with Apache2.4. However, one of my website does not load. If I try to access it, this simply downloads a file (with HTML code in it).

        I have same WP application running fine on my local machine. I tested by adding test PHP and HTML files, the server renders them (also including PHP_shorttags), quite strange situation for me to understand and debug, whats going wrong with my application.

        Will appreciate any help or expert opinion in this regard. May be other people also face similar situation.

        I want to set up 2 different sites with the main domain and sub-domain.

        Here is an example of it,

        Let me know what will be good changes with the above tutorial steps.

        My droplet is up and running in LAMP environment. The domain has SSL and it’s installed at the server here. The Application is running good as well.

        Now I need to add another domain at this droplet, means I need to create virtual host.

        I have mainly 3 question : #1. How to bring the source code which is in now /var/www/html. According this tutorial I have to bring it here : /var/www/example.com/public_html

        #2. How to configure / update the SSL for the existing domain and add SSL for new domain

        #3. For each domain I do add at the virtual host, can I allocate different IP for each ?

        I have created virtual host for example.com, everything works fine for me but, the error log always goes to the default /var/log/apache/error.log file I need it to be placed inside /var/www/example.com/error.log I have tried changing in /etc/apache2/sites-available/example.com.conf =>

        <VirtualHost *:80> ServerAdmin admin@test.com ServerName test.com ServerAlias www.test.com DocumentRoot /var/www/test.com/public_html ErrorLog /var/www/example.com/error.log CustomLog /var/www/example.com/access.log combined </VirtualHost>

        still errors are logging inside default apache log file Iam using ubuntu 18.04, please help me out in this

        Tested in a brand new Droplet for production: all is right!

        A little detail: in the /etc/apache2/sites-available/example.com.conf be careful with

        DocumentRoot /var/www/example.com/public_html
        

        Just check the change /html for /public_html

        Another detail: edit sudo nano /etc/apache2/apache2.conf and add this line

        ServerName your_name_server
        

        and after check with sudo apache2ctl configtest for get Syntax OK

        Thanks for the tutorial, good job!

        If you have setup a firewall in ubuntu before doing the above steps, you will not see the test sites because the firewall will block incoming HTTP requests.

        You can enable them with the following command:

        sudo ufw allow http
        

        You can crosscheck whether they are allowed or not with

        sudo ufw status
        

        You made my day

        Thank you so much for this very useful tutorial !

        Hi, my first domain works perfectly with the tutorial but when I tried for a second domain, the site can’t be reached

        Hi, I’ve been working through this and have been struggling to get the final piece to work. I’ve discovered there is another file in there.

        000-default-le-ssl.conf
        

        along with the file

        000-default.conf
        

        Should I delete one of these? Hope you can help.

        Thanks for the post, It’s pretty much the same as local env.

        I can’t remember which part of the tutorial you see this, (I’m guessing it’s the first command under Prerequisites) but you might notice a GPG key is missing(on line 5 or so) after you run:

        sudo apt-get update
        

        I found a solution to this here: https://askubuntu.com/a/1389929

        I’ll copy and paste the solution below:

        You will want to import the missing GPG keys for the repository like this:

        sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 467B942D3A79BD29
        

        Then update:

        sudo apt update
        

        Try DigitalOcean for free

        Click below to sign up and get $200 of credit to try our products over 60 days!

        Sign up

        Join the Tech Talk
        Success! Thank you! Please check your email for further details.

        Please complete your information!

        Become a contributor for community

        Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

        DigitalOcean Documentation

        Full documentation for every DigitalOcean product.

        Resources for startups and SMBs

        The Wave has everything you need to know about building a business, from raising funding to marketing your product.

        Get our newsletter

        Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

        New accounts only. By submitting your email you agree to our Privacy Policy

        The developer cloud

        Scale up as you grow — whether you're running one virtual machine or ten thousand.

        Get started for free

        Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

        *This promotional offer applies to new accounts only.