Saturday, August 24, 2013

Setting Up Name Based Virtual Hosting in Apache on Ubuntu

Apache is by far the most popluar web server out there. Installing  apache on a Ubuntu machine is straightfoward. However, whats the use of it if we cant make it run multiple sites.

Virtual Hosts are used to run more than one site off of a single IP address. If you want to be able to handle more than one domain with one web server, you'll need to set up a virtual host for each. Here I will show you how to work with Virtual Hosts by setting up 2 different sites on a single apache installation using virtual hosts.

As an example, let us assume we are setting up 2 sites www.testwadt.com and www.testwadt.net

Create 2 separate folders where we will place the files which will serve the 2 sites. Let do this in the /var/www folder

mkdir /var/www/testwadt_com
mkdir /var/www/testwadt_net

Now for each of the 2 domains, put the files in the respective folders created for the same

Also create a log folder in each of the 2 folder created above
mkdir /var/www/testwadt_com/log
mkdir /var/www/testwadt_net/log


Now we need to define to Apache that we’re using name based virtual hosting instead of IP based. You can append the following line to your /etc/apache2/apache2.conf to define this:
NameVirtualHost ip.address:port

typically this would be
NameVirtualHost :80

The next step is to set up Virtual Hosts. Each virtual host needs its own file in the /etc/apache2/sites-available/ directory.
This allows for a clear and specific per-site configuration.

So lets create files for the two sites we wish to host: testwadt.com and testwadt.net

First lets create a file testwadt.com in /etc/apache2/sites-available

vi /etc/apache2/sites-available/testwadt.com

In the file place the following:

# domain: testwadt.com
# public: /var/www/testwadt_com

<VirtualHost *:80>
  # Admin email, Server Name (domain name), and any aliases
  ServerAdmin youremail@youremailprovider
  ServerName  www.testwadt.com
  ServerAlias testwadt.com

  # Index file and Document Root (where the public files are located)
  DirectoryIndex index.html index.php
  DocumentRoot /var/www/testwadt_com

  # Log file locations
  LogLevel warn
  ErrorLog  /var/www/testwadt_com/log/error.log
  CustomLog /var/www/testwadt_com/log/access.log combined
</VirtualHost>


Now lets create the 2nd file testwadt.net in /etc/apache2/sites-available

vi /etc/apache2/sites-available/testwadt.net

In the file place the following:

# domain: testwadt.net
# public: /var/www/testwadt_net

<VirtualHost *:80>
  # Admin email, Server Name (domain name), and any aliases
  ServerAdmin youremail@youremailprovider
  ServerName  www.testwadt.net
  ServerAlias testwadt.com

  # Index file and Document Root (where the public files are located)
  DirectoryIndex index.html index.php
  DocumentRoot /var/www/testwadt_net

  # Log file locations
  LogLevel warn
  ErrorLog  /var/www/testwadt_net/log/error.log
  CustomLog /var/www/testwadt_net/log/access.log combined
</VirtualHost>

What these settings do is as follows:

ServerName listens for requests asking for a certain domain
ServerAlias defines any additional domains that should match
ServerAdmin is the contact for the site
DocumentRoot is the path to the content for that site


Now that this file is created in the /etc/apache2/sites-available/ folder we’re just about ready to start, but we need to enable it. We can do that by creating a symbolic link in /etc/apache2/sites-enabled/

This is done by calling the a2ensite command

a2ensite testwadt.com
a2ensite testwadt.net

Finally, restart the Apache server to initialize all the changes

/etc/init.d/apache2 restart


Files that configure virtual hosts should be located in the /etc/apache2/sites-available/ directory.

The files should be symbolically linked to sites-enabled/ with the a2ensite tool.

Tuesday, May 14, 2013

Create a Git Repository from an existing non-empty directory and Push Files to a Remote Repository

From the terminal (or command line prompt in Windows) goto the non empty directory where you'd like to create a Git Repository

Run the following commands

git init

git add .

git commit -m 'Initialized my Git Repo'



Get the url of your remote repository.


For E.g in case the url is https://xyz@bitbucket.org/xyz/sample_repo.git

Run the following commands

git remote add origin https://xyz@bitbucket.org/xyz/sample_repo.git

git push -u origin master

Tuesday, February 12, 2013

Bookmarklets

Regular bookmarks (aka favorites) are just locations to visit, like “http://gmail.com”. Bookmarklets are javascript code that the browser runs on the current page, and they’re marked by “javascript:” instead of “http://”.

Technically, a bookmarklet is unobtrusive script written in JavaScript and stored as the URL of a bookmark in a web browser or as a hyperlink on a web page.

Bookmarklets are saved and used as normal bookmarks. As such, they are simple "one-click" tools which add functionality to the browser.

For example, they can:

Modify the appearance of a web page within the browser (e.g., change font size, background color, etc.)

Extract data from a web page (e.g., hyperlinks, images, text, etc.)

Submit the current page to a curation service such as minus, scoop.it, link-shortening service such as bit.ly, or bookmarking service such as Delicious

Query a search engine or online encyclopedia with highlighted text or by a dialog box

Submit the current page to a link validation service or translation service

Set commonly chosen configuration options when the page itself provides no way to do this

"Installation" of a bookmarklet is performed by creating a new bookmark, and pasting the code into the URL destination field. Alternatively, if the bookmarklet is presented as a link, under some browsers it can be dragged and dropped onto the bookmark bar. The bookmarklet can then be run by loading the bookmark normally.

When clicking a bookmarklet, imagine the page author wrote <script>bookmarklet code here</script> — it can do almost anything. There are a few restrictions:

  • Restricted length: Most URLs have a limit around 2000 characters, which limits the amount of code you can run. 
  • No spaces allowed: Some browsers choke on spaces in a URL, so yourcodelookslikethis.