Archive for May, 2009

Pipe text from terminal to clipboard

Posted in How-Tos on May 14th, 2009 by andrew – 2 Comments

Here is a cool little OS X tip that I stumbled upon a couple of weeks ago. I was writing documentation for the setup of one of our applications and I found myself copying the contents of configuration files into the wiki manually. If you have done this before, you know that it can be pretty annoying when you are working from the terminal and you end up cutting off the ends of lines because you don’t realize that they are chopped off in nano.

However, thanks to Stack Overflow, I was able to find a very elegant solution for this situation when you are working from a Mac client: pbcopy (and the yen to it’s yang, pbpaste). These 2 little programs allow you to pipe text to and from your clipboard into your terminal. For example, pretend I have a configuration file that I need to copy into a blog post or wiki:

$ cat localhost.conf | pbcopy

Now I can just use a standard paste command to insert the contents of the file into any input field. No need to open the file in an editor: easy. The inverse is also possible. Pretend you have copied a code snippet from an online tutorial to your clipboard and you need to create a file with the copied text as the content.

$ pbpaste > somefile.txt

I am sure that there are some other cool uses for these commands, so put your ideas into the comments!

A couple of site changes

Posted in Blogging on May 7th, 2009 by andrew – 2 Comments

There are many reasons to have a personal blog. The most obvious (and frequent) reason is to have a place to keep your friends up-to-date with what you are doing. However, blogs are also a great tool for allowing potential employers or clients to know what you are passionate about and how you think. While I am not currently looking for a change in either of those areas, I decided that it is important to lay the groundwork now.

To begin with, I have finally moved this blog to a permanent home at andrewbredow.com instead of a subdomain to a site that I have no intention of developing any time soon. Any old links to site will be redirected here with 301 (permanent) redirect for the time being, and I will probably get rid of the subdomain altogether in a couple of months.

Additionally, I have made the switch over to Feedburner (which is now owned by Google) for my RSS feed. This will provide me with some better metrics and additional exposure and it also allows the possibility of switching out blogging software while keeping the same feed address. If you are subscribed to the old feed it will continue to work as long as the redirect is in place. However, it would be great if you re-subscribed using the Feedburner feed!

That’s all for now; there are more exciting things coming soon!

Using multiple Apache virtual hosts on a development machine

Posted in How-Tos on May 4th, 2009 by andrew – 3 Comments

The problem

When I first developing web applications I was only responsible for one site. My typical workflow was to have a checkout of the application in my document root and to just use http://localhost to debug it before I checked in my changes. As I started working on other projects, I would just go into my Apache configuration and change my document root to another directory. This got rather annoying after a while since I had to edit files and restart Apache every time I switched gears.

Then the light bulb clicked on: I knew how to configure virtual hosts to run multiple sites on web servers, why didn’t I just do the same thing on my local machine? Once this is accomplished, it becomes very painless to switch contexts between multiple applications. As an added bonus it also makes it very easy to install open source web applications on your local machine, do some hacking, and learn how they work. While this same thing is technically possible just by adding new folders to your document root, this can really confuse some web applications that are expecting to handle all of their URLs as directly relative to your base path.

Prerequisites

The only prerequisite to this tutorial is that you have a relatively clean install of Apache on your local machine. You can use the default OS X install, or also also a vanilla copy installed via the Windows Apache installer. I am going to give instructions for a *NIX OS, but this setup also works fine in Windows.

Set up

  1. First, you will want to locate your Apache configuration directory (containing http.conf). On OS X or most other flavors of UNIX it is /etc/apache2/, or Windows: C:/apache2/conf. Next, open your terminal and cd into the directory:
    $ cd /usr/local/apache2/conf
  2. Add a “sites” subdirectory to your Apache configuration directory. This is the directory that will hold all of your virtual host configuration files.
    $ mkdir sites
  3. Locate your httpd.conf file, open it up for editing
    $ nano httpd.conf
  4. Now, we want to edit a couple of configuration settings. You may find variations of these already in the file that you can either delete or modify.
    NameVirtualHost *:80
    Include conf/sites

    The first line tells Apache to use name-based virtual hosts. This means that it will match virtual hosts based on the domain name of the request. The second line causes all files in the sites directory to be included when Apache reads in it’s configuration files. Note that this folder location is relative to your ServerRoot location which you will find elsewhere in httpd.conf if you search for it.

  5. Next, you are going to go into your “sites” folder and create your first virtual host configuration file. It is helpful to name the files based on the domain that they reference (I am going to create http://local.andrew).
    $ cd sites
    $ nano local.andrew.conf

    The basic file contents are

    <VirtualHost *:80>
      ServerName local.andrew
      DocumentRoot /Users/andrew/Sites/somecoolsite
     
      <Directory /Users/andrew/Sites/somecoolsite>
        Options +FollowSymLinks
        AllowOverride All
      </Directory>
    </VirtualHost>

    This is fairly self explanatory. In the first line you set the domain that you want to use with the ServerName directive. The document root of your files (they can be anywhere on your computer) goes after the DocumentRoot directive. Finally, the Directory block is where you can put any settings that are specific to this host (e.g. Rewrite rules).

  6. Next, you will need to create an entry in your host file to direct all traffic sent to your custom domain to your local machine. On any *NIX system the file is /etc/hosts, and on Windows it is C:\WINDOWS\system32\drivers\etc\hosts. The format is the same on all systems, just add a line like:
    127.0.0.1  local.andrew
  7. You are all done setting up your first virtual host. Restart Apache (you will probably need to use a hard restart - apachectl restart - for your vhost settings to be applied), and direct your browser to whatever hostname you created. You should see your site right away!

Now that you have 1 host set up it is super easy to add more. Just copy your existing host file to a new name and edit the settings accordingly! This should greatly improve your workflow and help you be a more productive ninja developer.