I have wanted to try out building a Phar for a PHP project for a long time. Now, I’m finally going to do it.

The subject of this Phar exploration will be blogstream ( read more about it here ), with Box doing the heavy lifting. You can build a Phar file manually, but everything I’ve seen indicates Box is the way to go. There are several options for installing Box. Pick the one that works best for you. In my case I opted for Homebrew.

For blogstream the defaults that come with box compile will not work. I need to point it at server.php. So I added box.json.dist and used the main directive.

{
    "main": "server.php"
}

With that, I can now run box compile and get a server.phar file that works. You can start the server with ./server.phar start and you are all good.

There was something else that I wanted to change though. Instead of server.phar in the top level directory, I wanted the Phar file to be called blogstream.phar - in a bin/ sub-directory. The output directive takes care of that.

{
    "main": "server.php",
    "output": "bin/blogstream.phar"
}

Now I can run the server with ./bin/blogstream.phar start and it works just like the non-Phar version.


This whole thing turned out to be way easier than I expected, thanks to Box. I can’t say enough good things about how smooth it made the whole process.

And because I like using a simple Makefile to manage all of the actions, I added a new compile target. All it does is call box compile.