How To Setup Virtual Host In Apache (Ubuntu 14.x.x)

Let’s learn how to setup virtual host in apache (on Ubuntu) – most of what am going to show you will apply to other versions of Ubuntu below 14.*. and so if you need to configure your own local virtual host on Ubuntu, you can simply follow this tutorial. I will make this as simple as possible; after all, permissions and configurations can really screw your Ubuntu installation faster than you can blink – especially when you are not an Ubuntu expert. Let us do it.

how to setup virtual host in apache

How To Setup Virtual Host In Apache – Simple Steps

  • Step 1 : Create Your Folders
  • First, create a folder inside your /var/www/ folder and name it vhosts. Inside that folder, create a folder which I would name after the website am building. In my sample, I called it api because my site is named api. You can do this in your terminal or browse directly to where your /var/www/ folder is located.

  • Step 2: Create Two Folders Inside Your First Folder
  • The first folder I created in my demo is called htdocs – this folder will contain all the project/web page files (if you have used XAMPP before, this might sound familiar to you). The second folder, I called logs – this will save the logs from Apache. These are mostly error messages.

  • Step 3: Create a demo file – index.html
  • Using your favorite editor (Sublime Text is my favorite), create a simple html file and place anything you want in it. This is totally up to you. The purpose of this file is for testing and you can delete it later. Then finally place your html file inside the htdocs folder. Now move on…

  • Step 4: Create a Plain text file named after your site
  • Again, pop open your favorite text editor and create a text file named after your website (for example mine is api. Your text file should have an extension of .conf which means mine would look like this : api.conf. Next, you need some content for your text file. Read on – almost done.

  • Step 5: Copy the text below and paste it into your open file (from step 4) above
  • NOTE:Please replace all occurences of api in the above text with your own site name created in the beginning.

    Now save your file anywhere you want – could be on your desktop – just remember to give it an extension of .conf. Before closing it, make sure it looks exactly like the snapshot below (particularly (Directory) should start with uppercase D. Then finally close it. Move to the next step.

    how to setup virtual host in apache

  • Step 6: Copy the above file to /etc/apache2/sites-available/
  • Open your terminal CTRL+ALT+T, change to the location where you saved your file from step 5 and enter this command.

    Remember to change the name of your file to what you named yours.

  • Step 7: Open your /etc/hosts/ and add your site.
  • Using your terminal, issue the following command to open /etc/hosts in your favorite editor or use gedit which comes with ubuntu.

    Now add your newly created domain name – mine is api.local (yours is probably different depending on what you named your site). Now it will look something like this.

    After adding your site to the hosts file, click save and close it.

  • Step 8: Open your terminal and issue the following command:
  • The above command enables the needed module to make virtual hosts work in your Apache server.

    Now you also need to enable the site you just created in this whole process. Run the following command on your terminal.

    Replace api with your site.

    To activate the new configuration, you are required to run the following next command:

    And then finally, restart the Apache server by running the following command on your terminal:

    If you followed the above instructions exactly as they are, you should be able to visit your brand new website by opening your browser and going to api.local and it will display your index.html page that I told you to create earlier…remember that part?

    If you are working on multiple sites, all you need to do is repeat the above steps and you will have a virtual host in no time.

In my next tutorial, am going to show you how to setup the popular Laravel Framework and serve your applications using the above virtual host. We will just modify it a little bit for that purpose.

If for some reasons you cannot serve files directly, you might need to change some permissions for apache and so you should click here to fix that problem.

If you liked this post because it helped you solve your problem, please consider supporting this blog by sharing this post with your friends using the buttons below. If you have questions, please ask them through the comments or contact page. Otherwise, thank you for stopping by and I hope you found this post helpful and you have a great day.

Written By Elisha Chirchir

Elisha Chirchir is a software developer. He is also the founder of Simple Developer and co-founder of Instinctive Software Solutions. On any given day, he works on both Android and Web Development. During his 'free time', he offers training to those interested in learning how to code in php, java, python, javaScript etc. You can easily find him on StackOverflow Android chatroom or on Twitter @Eenvincible

4 Comments on “How To Setup Virtual Host In Apache (Ubuntu 14.x.x)

  1. […] Nguồn ảnh: […]

  2. budyk Reply

    September 1, 2014 at 9:21

    I use xampp (lampp) for ubuntu, it already has htdocs. is ok not to set up virtual host ?

    • Elisha Chirchir Reply

      September 1, 2014 at 3:59

      A virtual host enables you to create local domain names like project1.local,, etc. I don’t think you can just do it using a lamp installation without configuration.

  3. captainumbrella Reply

    August 8, 2017 at 4:30

    How to deploy a whole laravel project using this exacte vhost configuration, cause i puted my project inside the htdocs and copied the public file and puted it in the curret folder of te project which is htdocs and changed the lines require __DIR__.’/bootstrap/autoload.php’; and $app = require_once __DIR__.’../../../html/bootstrap/app.php’; but still the website doesn’t load 🙂

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.