Tagged: LAMP Toggle Comment Threads | Keyboard Shortcuts

  • Kartik 12:00 AM on August 19, 2011 Permalink
    Tags: Apache, , , Hosting, , LAMP, , ,   

    Configuring Apache for Developing Multiple Websites under Ubuntu Linux 

    Are you a beginner in web development with LAMP environment, or a CS/IT student overwhelmed with setting up LAMP and configuring it for your web-related academic project? If yes, then you are at the right place.

    We have discussed earlier how easy it is to setup a web development environment on your Ubuntu desktop. In this article we will try to have a more customized setup of this environment for easily working with multiple websites. Also find below introduction to some useful Apache utilities and configuration files on Ubuntu/Debian operating system.

    One of the ways to work on multiple sites is to put different directories under /var/www and access them as http://localhost/site1, http://localhost/site2 etc. but there is another and more elegant way. We can access our sites simply by pointing our browsers to http://site1/ and http://site2 etc. To find out how, follow the tutorial.

    Let’s keep all our development websites under the home directory in public_html folder. Apart from being easy to manage your website from inside your home folder, this has the benefit that you no longer need to use sudo to put files in your document root (/var/www earlier).

    Open the Terminal and follow the steps for a test setup, you may customize it your preferences later:

    kartik@PlatiniumLight ~ $ mkdir public_html
    kartik@PlatiniumLight ~ $ cd public_html/
    kartik@PlatiniumLight ~/public_html $ mkdir site1
    kartik@PlatiniumLight ~/public_html $ mkdir site2
    kartik@PlatiniumLight ~/public_html $ 

    Create files called index.html with some content inside each of these directories to help you identify them when you walk through this tutorial:

    kartik@PlatiniumLight ~/public_html $ cd site1
    kartik@PlatiniumLight ~/public_html/site1 $ gedit index.html
    Screenshot - index.html (~/public_html/site1) - gedit

    Screenshot - index.html (~/public_html/site1) - gedit

    Enter some text such as “This is site 1” and save the file. Similarly put “This is site 2” in the index.html of site2 directory.

    kartik@PlatiniumLight ~/public_html/site1 $ cd ../site2
    kartik@PlatiniumLight ~/public_html/site2 $ gedit index.html
    Screenshot - index.html (~/public_html/site2) - gedit

    Screenshot - index.html (~/public_html/site2) - gedit

    Now begins the main step of the process. We have to create separate apache configuration file for each of the websites. This is fairly easy to do. We copy the default config file and modify it to our needs:

    kartik@PlatiniumLight ~/public_html/site2 $ cd /etc/apache2/sites-available/
    kartik@PlatiniumLight /etc/apache2/sites-available $ sudo cp default site1
    kartik@PlatiniumLight /etc/apache2/sites-available $ sudo cp default site2
    kartik@PlatiniumLight /etc/apache2/sites-available $ sudo vim site1

    Note that I am using vim editor for making the changes as I prefer it for its amazing syntax highlight support even for various linux configuration files, you may use gedit, or any other editor instead.

    We need to change the path of the DocumentRoot and Directory to the particular website’s directory, and add a ServerName directive:

    ServerName site1
    DocumentRoot /home/kartik/public_html/site1
    <Directory /home/kartik/public_html/site1/>

    Take notice of the ‘/’ near the end of the second line, it is important. Also don’t forget to add the line with ServerName directive just before the DocumentRoot directive. The lines which need to be edited are highlighted with red arrows in the following screenshot:

    Screenshot - Terminal - Editing site1 config file

    Screenshot - Terminal - Editing site1 config file

    Save the file, similar changes need to be done for other sites also, site2 in this example.

    Now we need to make entries in the hosts file so that Apache can recognize the new sites.

     kartik@PlatiniumLight ~ $ sudo gedit /etc/hosts

    Add the names of the new sites after localhost as shown:

    Screenshot - hosts (/etc) - gedit

    Screenshot - hosts (/etc) - gedit

    Now enable the new websites by using a2ensite command:

    kartik@PlatiniumLight ~ $ sudo a2ensite site1
    Enabling site site1.
    Run '/etc/init.d/apache2 reload' to activate new configuration!
    kartik@PlatiniumLight ~ $ 

    Do similarly for site2, and then run the following command to activate the new configuration:

    kartik@PlatiniumLight ~ $ sudo service apache2 reload
    * Reloading web server config apache2
    apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1 for ServerName
    [ OK ]
    kartik@PlatiniumLight ~ $ 

    You can ignore the warning which is shown in the above output. Now open your favorite browser and open http://site1 and http://site2 in different tabs, you will find the content of their respective index.html files.

    Screenshot - Chromium showing both the sites

    Screenshot - Chromium showing both the sites

    And now you are done with setting up multiple sites in Apache. You can create as many sites as desired by following the same procedure, creating a config file for each website.

    Here follows an introduction to some useful Apache utilities and configuration files which aid you in this kind of a setup:

    a2ensite – As we used this above, you already know what it does – enable a website among those available under /etc/apache2/sites-available directory.

    a2dissite – does exactly the opposite – disable a website so that it is no longer accessible from your web server. E.g.

    kartik@PlatiniumLight ~ $ sudo a2dissite site2
    [sudo] password for kartik:
    Site site2 disabled.
    Run '/etc/init.d/apache2 reload' to activate new configuration!
    kartik@PlatiniumLight ~ $ sudo service apache2 reload
    * Reloading web server config apache2
    apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1 for ServerName
    [ OK ]
    kartik@PlatiniumLight ~ $ 

    This will disable site2 from loading any more. You can use a2ensite to enable it again.

    Similar to sites, there are corresponding directories and commands for controlling apache modules also. Available modules are kept under /etc/apache2/mods-available directory and enabled ones are symlinked under mods-enabled directory. The corresponding commands are – a2enmod and a2dismod. E.g.

    kartik@PlatiniumLight /etc/apache2/conf.d $ sudo a2enmod rewrite
    Enabling module rewrite.
    Run '/etc/init.d/apache2 restart' to activate new configuration!
    kartik@PlatiniumLight /etc/apache2/conf.d $ sudo service apache2 reload
    * Reloading web server config apache2
    apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1 for ServerName
    [ OK ]
    kartik@PlatiniumLight /etc/apache2/conf.d $ 

    This will enable Apache rewrite module which is required my some CMS solutions like Drupal to enable pretty URLs.

    Hope this article was useful to you, feel free to ask your doubts, problems you face, or any similar tips you might have through comments.

    An edited version of this article first appeared at http://www.muktware.com/articles/1876

     
    • Ershad K 10:30 PM on August 19, 2011 Permalink | Reply

      Informative post, nicely written 🙂

    • Abhimanyu M A 11:34 AM on August 22, 2011 Permalink | Reply

      Hey think last time i tried it in Hostel LAN i was able to perform a MiM attack, people trying out http://site1/ got to my site if it resulted in no actual site being there , and [not sure about this] even when the other site took a bit long to respond

      • dhandeep 10:07 PM on August 22, 2011 Permalink | Reply

        exactly.. u need to work a lot for setting up the backend to fake sites and get the whole control of the lan data… 🙂 😉 .. well u can even setup a plugin that rediercts to actual site if that site is not on ur comp.. (this ll eliminate the chance of ppl suspecting u.)

      • Kartik 10:28 PM on August 22, 2011 Permalink | Reply

        It might have been a problem of incorrectly configured DNS entries in the hosts file. Please elaborate your scenario more, I am unable to understand the situation clearly.

    • sorbh_teke 5:28 PM on September 12, 2012 Permalink | Reply

      What configuration need to do in order to make all these sites available through internet ??

      • Kartik 8:07 PM on September 12, 2012 Permalink | Reply

        From Apache’s side everything is ready. Additionally, you need to configure your DNS servers to point these new ‘names’ to your server.

    • Miquel 12:52 AM on September 28, 2012 Permalink | Reply

      Hi Kartik. I know it’s an old post, but I’ve configured my sites the same way as you.
      But now I have a problem with mod_rewrite. One of my sites is a Joomla 2.5 site with SEF and URL rewriting enabled (site1).
      When I pint to http://site1 I get redirected to http://site1/en (its a multilingual site, it’s ok).
      But when I access any other site, for instance http://site2, I get redirected to http://site2/es and the contents of site1 are being shown.
      Whats happening? Can coexist more than one site, one with URL rewriting and another without it?
      Kind regards

      • Kartik 9:45 AM on September 28, 2012 Permalink | Reply

        Interesting.

        Looks like your rewrite rules are in the wrong place. If you put them in .htaccess of site1 (instead of putting in apache’s config file) and access site2, I doubt it will ever redirect again to site1’s contents.

        Let me know, if this solved your problem.

    • miquel 11:38 PM on October 3, 2012 Permalink | Reply

      Thanks Kartik. I reviewed all the process and I have some sites with ServerName and some others without it, and maybe this was the error. Now it works fine, great!

    • edo 8:16 AM on February 8, 2013 Permalink | Reply

      Nice post guy. It’s realy help me. Thanks

    • Bharat 11:32 AM on October 3, 2013 Permalink | Reply

      Hey there Kartik Buddy,

      This requires and addition of a tag

      “ServerName site1”

      in sites-enabled/site1

      To make it perfectly work !

      Rest is cool 😉

      • Kartik 12:35 PM on October 3, 2013 Permalink | Reply

        You are right. But is it absolutely necessary? I have tried this setup without ServerName directive and it works completely fine.

        • Bharat 6:23 PM on October 3, 2013 Permalink

          yeah but in my case (ubuntu 12.04) it keeps on redirecting me to index.html of /var/www withdout this !

        • Kartik 7:45 PM on October 3, 2013 Permalink

          Bharat, I just checked my post again, ServerName directive was always there (with extra warning about including it, see just above the screenshot), just that the screenshot doesn’t contain it. 🙂

  • Kartik 12:00 AM on July 9, 2011 Permalink
    Tags: , , , , , Joomla, LAMP, , MySQL, , ,   

    Easiest Way to Setup a Web Development Environment on Ubuntu-based Distros 

    Did you know how easy it is to get a basic web development environment on your Ubuntu-based Linux distribution? Guess what, it just takes 2 commands on the Terminal:

    sudo apt-get install tasksel

    This will install a small utility which lets you install a lot packages grouped together as software collections.

    sudo tasksel

    Launch tasksel and select ‘LAMP server‘ by pressing the SPACE key, press ENTER when you are done (see attached screenshot). It will take some time for the required packages to download and install. Near the end of setup, the installer will ask you to create a password for MySQL‘s root user.

    Select LAMP Server among the choices in Tasksel

    Select LAMP Server among the choices in Tasksel

    After the installer finishes, you have the environment ready. Head over to your favorite browser and open http://localhost If everything went fine, the page will say It works!

    It works!

    It works!

    Now you can start creating websites by putting your html, php, etc. files under /var/www directory or just choose to go with CMS solutions like Drupal, WordPress or Joomla.

    The author also recommends to install phpmyadmin package if you happen to work with MySQL databases.

    An edited version of this article first appeared at http://www.muktware.com/articles/08/2011/1348

     
c
Compose new post
j
Next post/Next comment
k
Previous post/Previous comment
r
Reply
e
Edit
o
Show/Hide comments
t
Go to top
l
Go to login
h
Show/Hide help
shift + esc
Cancel