Posts Tagged ‘apache’

HPHP - First thoughts

Tuesday, February 2nd, 2010

Facebook have released HipHop, a piece of software that converts a PHP script into C++ and then compiles it into a static binary. That binary operates it own HTTP server.

About a year ago I wrote a couple of posts about my thoughts on Apache and how we don’t really use it much and actually why I think it’s still relevant. In one fell swoop Facebook has provided an answer/solution to my ruminations; it is now possible (I think) to run PHP as a standalone application server (kinda like Rails with Mongrel, or Python with CherryPy et al).

Most of the applications I write have a single entry point that bootstraps the framework and handles all the routing - my “index.php”. For this scenario, using HPHP sounds like a win: you compile your index.php and instead of using .htaccess to route a set of urls to it, you just configure Apache to reverse-proxy those requests to your backend HPHP instance/s (not sure yet if it will thread, fork or block yet). You could also still run regular php scripts on that Apache server, and just offload some requests to the HPHP daemon.

It sounds like it will really come into its own when you’re essentially running Apache as a container for a monolithic PHP application. Instead of running Apache+PHP on a backend server, you just run the HPHP application. Win! (This is going to rock for API/REST services.)

Some questions:-

  • How will it handle autoloading and dynamic classes? Presumably we’d have to give the compiler a list of directories of libraries to include in the compilation?
  • What about 5.3? I’ve personally switched, does HPHP support it? (Also, will HPHP and PHP releases be synced?)
  • How does the HTTP/server stuff work? Is it a single process that manages worker-pools, does it thread or what?
  • How will multiple applications work? I’m guessing each one will need a new HPHP daemon.

My Development Environment

Sunday, November 1st, 2009

The Old

Most developers I’ve encountered run a local LAMP stack (WAMP5, MAMP, etc), but I think they suck:  they abstract away the cool stuff, like compiling extensions, modules and other fun software that you might want to play with. How’s a newbie going to learn some Apache administration if all they do is press a button called “Start Apache”? Some, myself included, run a separate Linux server. The benefits of having your own linux box for development include having an environment closer to production (assuming you deploy on linux), more flexibility in the software that you compile on it, and somewhere to practice your linux-fu. It’s just better.

I use an online box for development (an Amazon EC2 instance specifically) which means it’s accessible anywhere I am, so long as I have a decent internet connection. Others have a machine on their local network, which removes the internet requirement, but introduces a physical constraint. Some crazy fools run linux as a desktop, which is probably not acceptable for most people. Recently the network connection at my office has been crappy and is beyond my control. This has interupted my coding  so I’ve taken the opportunity to freshen things up and switch to using a virtual machine, as well as finally dropping ZDE 5.5.

The New

My new environment is similar to the old: I still use my Mac for writing code and I still run a linux server; the difference is by running the linux server as a virtual machine (via VMWare) I eliminate the network connection. This is the best of both worlds: I write code locally (on my pretty Mac) but also have a linux environment.

VMWare Fusion can be configured to provde your instances with static IPs, which lets you make an entry in /etc/hosts for a domain like devserver.

There isn’t anything special about this setup but I’m very happy with the glue/configuration that holds everything together.

MacFuse: exposing the VM’s filesystem

MacFuse is a cracking piece of kit. It allows you to “mount” a remote filesystem using SSH (or whatever protocol you like). This means my Mac has a path /Volumes/devserver that points to the /home/richard path on devserver. My IDE can now open and edit files on my linux server as if they were local.

I also run a SSH server on devserver and shell into it from my Mac Terminal. The VMWare window is permanently minimized and I rarely interact with the GUI.

Netbeans 6.7 - win!

Finally. I can say farewall to my old friend Zend Studio 5.5 :D. It’s a bit of a shame Zend went down the Eclipse route imho, I liked the old ZDE, but it’s time to move on. I’m not sure entirely what changed since Netbeans 6.5, but whatever it is I like. It took a few hours of fighiting but I beat it into submission  (I really dislike their “Run Configuration” dialog when creating a new project). In Netbeans-speak, my projects are setup as “local server”, but with a “remote” hostname (devserver). The sources directory is set to something like /Volumes/devserver/projects/myproject.

The feature that made it possible for me to switch is the new “Path Mapping” capability for debugging (Project Properties -> Run Configuration -> Advanced…). It maps a remote path (/home/richard/projects/myproject/www/foo.php) into a local path, so that the IDE can open up a local file to display the debugging stuff (ie /Volumes/devserver/projects/myproject/www/foo.php). In my setup, the translated local path is actually the remote file, due to the MacFuse magic. This means Netbeans will display exactly the same file as the debugger on the remote server is working with. Neat trick! (I also had to create a mapping to my devserver’s PHP include_path, which was /usr/local/lib/php => /Volumes/devserver/.phplib, where the .phplib was a symlink to /usr/local/lib/php). (This allows me to debug any PHP libraries that I have installed.)

Apache Magic: mod_vhost_alias

The module mod_vhost_alias allows you to dynamically create new virtualhosts without editing any config files. I have it setup so that a domain in the format XXX.devserver points to the document root /home/richard/projects/XXX/www. So when I create a new project in my editor at /Volumes/devserver/projects/new-project it is immediately available to view, debug or profile at http://new-project.devserver.

Conclusion

I’m happy. This feels like a nice, self-contained environment.

  • Develop on a linux server.
  • Write code on my Mac (or Windows).
  • Not reliant on the network.
  • Can do “remote” debugging.
  • Netbeans is lightweight and pretty.
  • Bonus: Kcachegrind is available to me through the VMWare instance.

Apache - good for nothing?

Friday, October 31st, 2008

Apache (the http server) is one of the major reasons why PHP is so popular; but with today’s trend of using front-controllers have we obsoleted our old friend?

In the olden days we used to write web scripts in Perl and Apache would have to fork a new interpreter process to execute each script request. This was really slow, especialy on the hardware available at the time. PHP came along and provided an embedded interpreter (mod_php) and gave a huge performance boost to the web-scripting world. This was a major motivator for migrating from Perl to PHP. It doesn’t hurt that Apache also happens to be a solid piece of software that can be (and probably has been) be compiled on pretty much all operating systems out there. PHP has unashamedly piggy-backed on its host’s popularity, and for that, we salute you Apache!

Another reason PHP gained popularity is that you could drop a PHP file into any directory and Apache would just run it - no need for the “cgi-bin”. Today, website best-practice would typically recommend against executing “physical .php files” like add-user.php, and instead have pretty urls like /users/add (good for readability and for SEO). Apache provides us with such functionality though a variety of methods, the most popular being mod_rewrite. We can easily use rewrite rules to route /users/add => add-user.php. Thing is, we’re not happy with that. We’ve all got drunk on “MVC” and cool applications use front-controllers (a single entry point for all requests, normally “index.php”). So instead of using Apache’s built-in functionality, we’re re-implementing the wheel inside our own applications. Why use mod_rewrite when we can do the same thing, but slower, using Zend_Controller_Router_Route_Regex? /me rolls-eyes.

We’re even ditching Apache for static stuff too. If you don’t already, it’s probably only a matter of time before you run CSS and Javascript request through PHP too (for good reason). Yahoo’s frontend performance guidelines suggest reducing the number of HTTP requests is important, so it makes sense to generate (and cache) a single CSS file and a single JS file. You might also do clever stuff like postfixing the urls with a version number to help caching, and/or gzip/minify/manipulate headers. These are good things to do, but you’re now using Apache even less.

So what is left? Images, video, flash? Lots of sites which graduate beyond a single-server use a “static file server” to handle this stuff. There’s also no point using a feature-rich, but slower, server like Apache for doing basic static file serving, when alternatives like Lighty/nginx can do this more efficiently. Another option might be to locate static content on a NAS of some sort which Apache will alias (Alias /images /mnt/nas/images). We’ve now got a document root that looks like this:-

index.php
.htaccess (to route everything to index.php)

Wow, clean! (Our PHP code is outside the docroot in some include_path location). I just had a thought - we could get rid of the document root entirely, but sticking this in our virtualhost: “php_value auto_prepend_file /path/to/app/index.php”. (That would be pretty funny, running a site without a docroot).

We don’t really use Apache for much, so why do we use it at all? I think it boils down to being a nice host environment for PHP. There’s nothing really wrong with that, except PHP is tightly coupled to Apache. I think Apache rocks, but if we just end up using it as a host for PHP, then that’s bloated overkill? What about if we stripped out the features of Apache that we dont use until we’ve got a lightweight http wrapper for our PHP app to run in… isn’t that what Rubyists do with Mongrel?

The type of code I write in PHP today is request/response stuff. I get an incoming HTTP request, do some processing, and create an HTTP response. That sounds obvious, but the subtle difference to what I was doing 5 years ago, is that my PHP code is taking care of ALL of the request lifecycle. I don’t use Apache for authentication, logging, uri-routing, headers setting, gzip, caching etc - everything is done by my code. It’s this slight shift in paradigm that makes me think “scripting application servers” like Mongrel+Rails or CherryPy are worth keeping an eye on, and another reason why I question the future of PHP.