Virtual Hosts – Beginners guide to Digital Ocean

Ernest Marcinko Hosting, Tutorials 3 Comments

By now we have a working private server with very basic features and a demo user. By default your website is data is accessed from the following directory:

/var/www/

So if you open up your server address in your browser the files in this directory are parsed. This is not bad, however if you are planning to use 2 or more domains on the same server, then you need to tell the apache which directories should it use for each domain. This is what the virtual hosts files are used for. In this case please check out the digital oceans official virtual hosts tutorial.

Creating the new default directory

Even if you are not planning to use another domain with your droplet I still recommend changing the directory to the following structure:

/var/www/example.com/public_html/

It's much nicer and you are probably used to see the public_html folder at your previous host a lot.

Create the new folders

It's time to log in to your terminal. By running the following command you can create the new public_html directory. Change the example.com to your domain name of course.

sudo mkdir -p /var/www/example.com/public_html

Now we have the directory, but we still need to tell the apache server that we want to use this instead of var/www

Granting permissions

After creating the folder we need to make it accessible to the current user as well as the www-data process. If the ownerships are incorrect, your application won't be able to access the file system correctly. Run this command on the newly created directory:

sudo chown -R demo:www-data /var/www/example.com/public_html

The file system permisions needs to be set accordingly:

sudo chmod g+w /var/www/example.com/public_html -R

You might need to do these operations again, once the files are copied to the server.

Changing the Virtual Hosts files

Now that the directories are created the virtual hosts file is ready to be changed. There is a default hosts file in use, which you can find in the following location:

/etc/apache2/sites-available/000-default.conf

Instead of changing this file, let's make a copy of it's contents to a new file (don't forget to change the name):

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

Let's make changes to that file. Open it up in the nano (or vi, or any) editor:

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

You can disregard everything in that file if you want. Put this into that file:

Hit CTRL + X to exit, it will ask you if you want to save the changes. Of course don't forget to change the domain name and the admin contact information.

You may have noticed that there is a directory tag. That will ensure that the symbolic links will work correctly. (.htaccess files)

After finishing we need to tell the apache server to use this virtual hosts configuration file instead of the default one.
The following commands will do the job:

sudo a2ensite example.com.conf
sudo a2dissite 000-default.conf

After everything is done we still need to restart the webserver:

sudo service apache2 restart

Creating an index file

This step is optional, but it will make sure that your configuration is right. Since there is nothing in your public_html directory you should see an error message. Let's quickly make an index file to make sure, that our configuration is indeed right:

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

Copy&paste this html code there:

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

Save the file (CTRL + X then y). If you know open your server in your browser, you should be seeing the success message.

Chapters

<< Chapter #3      Chapter #4 >>

Ernest Marcinko

WordPress developer, javascript enthusiast, photoshop clicker and the founder of wp-dreams.com

Stay connected

Related Posts
Filter by
Post Page
Tutorials Wordpress Hosting Blog Testing
Sort by

Saving space automatically – Beginners guide to Digital Ocean

After a while, you will notice an exponential growth of used space, without even copying anything to it. How
2016-05-06 09:24:05
admin

18

Swap file – Beginners guide to Digital Ocean

If you reached this step, your server is up and running flawlessly. However after gaining some traffic it will run out
2015-01-21 15:48:02
admin

18

Mailing – Beginners guide to Digital Ocean

Sending mails properly is probably the hardest task to achieve with Digital Ocean. I will present two ways of doing
2015-01-20 21:38:53
admin

18

FTP & SFTP – Beginners guide to Digital Ocean

So far we have a running web server with a working database, but we don't have FTP access. SFTP However
2015-01-16 17:08:19
admin

18

From XAMPP to Vagrant WordPress

2016-01-05 19:58:17
admin

8

Comments 3

  1. Chris

    I think you got this part wrong:

    sudo chown -R www-data:demo /var/www/example.com/public_html

    I think it should be demo:www-data since it’s user:group.

    1. Post
      Author
  2. Merrick

    For some reason when I try to load the test “index.html”, I get a 403 Forbidden Error. Any ideas? Thanks for this by the way, it’s an awesome guide!

Leave a Reply

Your email address will not be published. Required fields are marked *