One of the huge benefits of using lighttpd is the ease in which you can set up a chrooted web environment. The reason this is such a great feature is that it allows you to separate your webroot from the rest of your server which means that if you were to be compromised through lighttpd there isn’t much the hacker can do. So, let’s say you just set up your shiny new lighttpd install, and you have now configured PHP to run in the chrooted environment too, great! You would probably do exactly what I did – start building your site! Well, if you’re like me and decided to use WordPress and you thought “Hey, I want to see what other themes there are!” then this tip may be for you!
When I went to see the featured themes in my admin area I was greeted with the message “An unexpected HTTP error occurred during the API request”, great! An unexpected error, with seemingly no information to help diagnose it. What I did was first consider the message and what I was trying to do. We know from the error message that the error is related to HTTP and we know that we wanted to see the featured themes which likely means accessing an external website. Since WordPress is largely built using PHP this means it’s possibly one of the of the fopen() or fsockopen() functions which can be used to open files and other resources on the internet. So, a quick test is to write a little PHP script that uses these functions and see what happens.
When I did this ended up with PHP throwing errors about getaddrinfo() failing to resolve the host name. Okay! This is a good thing, so we now know that there is an issue with resolving the host name which points to a DNS issue. I checked the usual suspects like /etc/resolv.conf and /etc/hosts, both looked fine. I tried the typical ping -c2 google.com and it worked great. Then I even tried running my PHP script on the command line, and it worked just fine. Then I thought “What happens if I use the IP address instead of a hostname?”, well I tried that and it worked! So, there was definitely an issue with resolving the hostname which only seemed to occur while accessing my PHP script through the web browser. After a great deal of banging my head against the virtual wall, I began to wonder if this was a side effect of running lighttpd and PHP in the chrooted environment.
This was also pretty easy to test, I undid the chroot and tried my test script, it worked fine! Now I was convinced, it was definitely something related to DNS and it was caused by the chroot. Once I realized that I began to double check that I had moved all the required files into the chroot. A quick way of listing all the libraries PHP needs to run is to use the ldd command. That didn’t really help though because I had moved everything it listed over. Once again I was stumped, what could it be? It was time to exercise my google-fu and see what I could bring up. Well, lucky for me someone else had encountered a similar problem and they were kind enough to post on a forum how they fixed it (I forget where exactly I found this tip), but the solution was to copy libnss_dns.so.2 into the chrooted /lib folder. libnss_dns is a C library used by getaddrinfo() in order to do the DNS lookup, and it turns out that was the key to solving my woes.