How-Tos

Compiling PHP on Snow Leopard

Posted in How-Tos on August 29th, 2009 by andrew – Be the first to comment

I have been wanting to do a clean install on my laptop for the last couple of months and decided that I would just wait for 10.6 to avoid having too much downtime from work. I like to compile my dev stack from source, and as expected this turned out to be the only problematic area for my upgrade. As a whole the transition was very smooth - both MySQL and Apache compiled without a hitch. However, I had a couple of issues with PHP (5.2) that took a little more work (nothing some Google fu couldn’t solve).

Both of these problems are related to shared libraries that PHP relies on:

libresolv

Running make resulted in the following error:

Undefined symbols:
  "_res_9_dn_expand", referenced from:
      _zif_dns_get_mx in dns.o
  "_res_9_search", referenced from:
      _zif_dns_get_mx in dns.o
      _zif_dns_check_record in dns.o
  "_res_9_dn_skipname", referenced from:
      _zif_dns_get_mx in dns.o
      _zif_dns_get_mx in dns.o
ld: symbol(s) not found
collect2: ld returned 1 exit status
make: *** [libs/libphp5.bundle] Error 1

Thanks to a great StackOverflow thread I was able to get around this fairly easily by editing 2 lines in the Makefile after running the configure script:

EXTRA_LIBS = -lresolv [... all of the other flags that are already here]
EXTRA_LDFLAGS = -lresolv [... all of the other flags that are already here]

libiconv

An error was also thrown in reference to libiconv. Apparently there is something strange with Apple’s included libiconv libraries. I was pretty easily able to get around this by compiling a local copy of libiconv from the source at gnu.org:

# download source, extract, enter directory
./configure --prefix=/usr/local
make
sudo make install

Then, just add –with-iconv=/usr/local to your configure options so PHP will link to your copy instead of the Leopard one.

Final configure command:

./configure --prefix=/usr/local/php52 --with-apxs2=/usr/local/apache2/bin/apxs\
 --enable-shared=all --with-zlib --with-curl --with-freetype-dir=/usr/local\
 --with-jpeg-dir=/usr/local/lib --with-png-dir=/usr/local --with-gd\
 --with-mcrypt --with-mysql=/usr/local/mysql\
 --with-mysqli=/usr/local/mysql/bin/mysql_config\
 --with-pdo-mysql=/usr/local/mysql/bin/mysql_config --with-iconv=/usr/local

Note: I did find that these issues have been resolved in the PHP 5.3.1 branch and will most likely be backported to 5.2 at some point, so make sure to check future versions before taking this route.

Hopefully this helps someone else out until these issues are resolved by either the PHP team or Apple. Do let me know if I have missed something stupid here or if there is an easier way to resolve these issues.

Pipe text from terminal to clipboard

Posted in How-Tos on May 14th, 2009 by andrew – 1 Comment

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!

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.

Using a Linksys WMB54G as a wireless bridge for your Xbox

Posted in How-Tos on February 12th, 2009 by andrew – 5 Comments

Background

I have been meaning to post about this for a while, and since I have talked to a few interested people lately, I think now is a good time. One of the biggest downsides to getting an Xbox over some other modern systems is that it does not come with built-in wireless. This is fine, since I think most people have their router next to their television or, at the very least, are able to drop in an ethernet port. But, as you can imagine from the title of this post, I do not fall into either of these groups. I did a little research, and found good news: there is a wireless card for the Xbox. WIN! And then, I realized that the price tag on this little encapsulation of 1999 technology was $80! ULTRA FAIL!

Now I am not a notoriously cheap person, but I do have a small problem with this ridiculous price tag for what is undoubtably a cheap chip beneath the surface. So I set out to find a alternative; but not just any alternative, I wanted the cheapest alternative possible.

The obvious option is a wireless bridge. These tiny devices connect to your wireless router as a client and then bridge the connection to an ethernet port into which you can plug any device. And the good news is that these things only cost about $50. Getting warmer, but not quite there yet. After a little searching, I found yet another device. The Linksys WMB54G “music bridge.” This thing is great. It is cheap (about $30 from the Amazon vendor that I got it from), and it can function as a wireless bridge. However, when I got it I realized that setup is a little difficult, particularly if you aren’t interested in running their P.O.S. setup program that will only run on windows.

Instructions

All you’ll need to get this running is any computer and the contents of from the box. Don’t be afraid to try anything, if you end up screwing up your device so you cannot communicate with it, just hit the reset button and start over.

  1. First, unbox everything and plug in the WMB54G. Plug the ethernet cable into the adapter and directly into the ethernet port on your computer.
  2. While it boots (it is slower than hell), open up the wireless settings on your computer and change your ethernet connection settings to “Static” and assign the following IP addresses:
    IP Address: 192.168.1.120
    Subnet mask: 255.255.255.0
    Router (”gateway” on Windows): 192.168.1.100

    You don’t need to worry about any DNS settings since we aren’t going to need an external connection during setup.
  3. Open your favorite browser and navigate to 192.168.1.210. This will bring up the web configuration utility for the bridge.
  4. Don’t do anything with the LAN settings at this point. Use the “search” button to find your wireless network information, and enter your wireless key. I had trouble getting it to work with WEP for some reason (shoot be an email if it works for you and I will update this post). Once I changed my network over to WPA it worked great. Also, don’t get confused if you see “PSK” for security type - they mean WPA.
  5. Once your wireless information is all entered correctly, click “Apply” at the bottom. Go get yourself a cold one while it reboots - you are almost done.
  6. Next, you are going to want to set a cusom password for the admin utility. Do this in the “Password” tab (pat on the back if you guessed that), and then click “Apply” to save. You will need to re-authenticate once the device reboots if you change the password.
  7. Once it has rebooted, you can enter your LAN information. For 99% of you, leave it as “Automatic Configuration - DHCP.” If you have a manually configured network enter your settings. Click “Apply,” and wait again while it reboots.
  8. Once it is rebooted, you will just want to test that everything is working. Change your ethernet settings on your computer back to their original settings (and disable your wireless adapter if you have one) and test that the connection works. You should be able to access the internet just like usual. If your computer cannot connect, neither will your Xbox be able to connect. Note: you may need to refresh or “repair” your network connection on your computer so it can get a new IP address.

One last note: if your router supports Static DHCP, I would recommend making an entry for the MAC address of the WMB54G so you can easily access the admin in the future (it is printed on the bottom). If your router does not support this, you will need to log in to the router and try to figure out what address has been assigned to the WMB54G if you ever want to edit the settings again.

Hope that this helps some people! Let me know if there is anything on here that I can clarify!