1. Enable Apache mod_rewrite:
    sudo a2enmod rewrite
  2. Set up some site folders in the Document root, for Ubuntu it is located at /var/www:
    Create directory: mysite.com
    mkdir /var/www/mysite.comNOTE: Create an index.html file in each of these folders just for testing purposes.
    If  we open http://localhost/mysite.com from browser, we should be able to see the index.html pages.
    Our goal here is to be able to load sites by just typing mysite.com in browser and for this we need to set up virtual hosts.
  3. Add your sites to the hosts file:
    In Ubuntu open your hosts file.
    It should be located in the /etc folder.
    To make changes to this file you will need the proper admin permissions,
    so you may need to open it as root with the editor of your choice
    or open from command line.
    sudo vi /etc/hostsYou will have at least one line in your hosts file, maybe more.       localhost       chipmunk-thinkpadT500
    # 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

    Here our interest is on first line because it points local web server (localhost) to local IP address.

    We need to replicate that so that other sites do the same.
    So the hosts file will now look like this:       localhost       chipmunk-thinkpadT500       mysite.com       mysite1.com       mysite2.com
    # 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

    What this basically means is that when we type mysite.com in browser, it’s going to resolve back to local webserver. Same with mysite1.com etc.
    You can do as many of these as you want for as many local sites as you’re developing locally.

  4. Configure Apache:
    We now need to make changes to our Apache configuration so that Apache knows what to do to our new sites.
    If you navigate to /etc/apache2/sites-available you will see two files in there: default, and default-ssl.
    We need to add files to correspond to our sites in this folder.
    We create new files for editing, one for each site.
    Remember you need to do this with the correct permissions so you can save the files.
    From the terminal type: sudo vi /etc/apache2/sites-available/mysite.comIn this new file, type the following code.
    DocumentRoot /var/www/mysite.com
    ServerName www.mysite.com
    ServerAlias mysite.com


    We open the virtual host tag and tell Apache to listen to port 80.
    The DocumentRoot directive tells Apache where our site files are stored.
    ServerName is the server name of our site, the URL that we will use to navigate to the site.
    ServerAlias is any other name or URL that we may use to access the site, the most common being excluding the WWW.
    So basically we’re saying mysite.com is equivalent to www.mysite.com.
    You can add other aliases as you see fit, but this one is the most common.
    There are other directives that you can add to this file, such as error log information, admin email etc.
    Go ahead and create one of these files for each site.
  5. The next step is to enable these sites.
    From terminal run: sudo a2ensite mylocalsite1.com
    You should get a message that the site is being enabled, and a prompt to restart Apache.
    If you have more than one site to enable, you can go ahead and enable them all before reloading Apache.
    Once you’re all done, you can then restart apache to allow the configuration changes to take.
    sudo /etc/init.d/apache2 reloadOnce Apache successfully restarts, our site is successfully set up.
    Now we are able to open the site from browser by just typing mylsite.com.

If you navigate to /etc/apache2/sites-enabled, you will see a symlink to each of your sites (in terminal they just look like files.
But if you browse to the folder using your GUI file manager you will notice that they are links).
They are placed there by the a2ensite command we execute in the last step.

If you need to remove any sites, the cleanest way in my opinion is to reverse through the steps.

  1. First disable them using the a2dissite command:
    sudo a2dissite mysite.com
  2. Then delete the mysite.com file from /etc/apache2/sites-available folder.
  3. Then remove the reference to mysite.com from /etc/hosts
  4. Then finally restart the apache server

Leave a Reply

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