Posts Tagged ‘apache’

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.

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.