Replace Apache or Nginx With Roadrunner for Your PHP Application

Nixon Kosgei
5 min readOct 8, 2020

In this article, we’re going to look how we can use Roadrunner as an alternative to serving your PHP application.

Briefly, we shall look at the following

  1. What is Road runner
  2. Why you would want to replace Apache/Nginx
  3. Example Application using Slimphp framework
  4. Finally, Things to watch for

What is Roadrunner?

According to the official docs, Its a PHP application server, Process manager and a load balance for PHP applications. Its written in golang, it processes requests from go’s net/http package into a corresponding Psr7 HTTP message and invokes your application via pipes or sockets with the prs7 request.

Why you would want to replace Apache/Nginx

  • Speed — roadrunner is fast, much faster than php-fpm on a production application, and where is all this speed coming from you might ask? Well, Turns out, Roadrunner runs your application as a daemon. This makes it skip all the steps of bootstrapping the application each time their is a new request coming in.
  • Easy configuration — You can easily get started using roadrunner by adding it as a composer dependency to your application, creating a .rr config file and running 1 command for initial setup and another to start the server, Its kinda like php -S for production
  • Production Ready — Comes with almost everything if not all features that is required by a production ready app. in addition it can also be expended by calling any Go library. It also has built in middleware that can be implemented before it hits your application, this can come in handy for tasks such as metrics, rate limiting or even authentication
  • Task Queues — this is essentially why I use this library, I allows you to create and run consumers workers quite easily. the package spiral/jobs adds support for amqp, amazon-sqs and beanstalk

It’s Cool — This project in my opinion is a way to modernize the traditional PHP stack, as it encourages writing code using well accepted php standards. Its also a slow transition to moving your existing codebase slowly to a better designed and a compiled language.

Show me Some Code Already!

For this demo, we are going to write a simple application using slimphp framework with only one route in and put all our code in one index.php file

Below is the complete File structure

Project Structure
File Structure

First, we are going to create a composer.json file to add our dependencies, we are using version 4 of slim framework and v1 of roadrunner

Run composer install, while you wait, probably because composer is slow, and you’re still using version 1. Just a little about the dependencies added above

  • slim/psr7 — slim framework implementation of HTTP message interfaces
  • php-di/slim-bridge — its a library that makes it easier to integrate php-di/php-di container easily into a slim application. Slim4 no longer comes with a container by default and php-di is just what i prefer using, this can be easily switched out to any other implementation of a Container
  • slim/http — This just contains slim specific Object Decorators for psr7

After Composer install is done run the following command .\vendor\bin\rr.bat get or ./vendor/bin/rr get on Linux and mac

Create a public folder, and add an index.php file. normally public directory is where we would point the web server e.g. apache or nginx to as the webroot. We don’t have to do this for code runner as it allows to explicitly define a directory where to serve the public files e.g CSS and JS and you could have any file as an entry point. am choosing this as its what most of the applications out their are structured

The index.php start by setting up our container, the we use the slim-di-bridge to create a new slim app and pass in our container.

we then define our home route, which accepts an optional parameter called name, as we would in a normal slim application. The route handler is just a simple closure function that checks if the name has been passed in, if not. it picks the one already set in the container. it finally takes the response and writes to it body then returns it.

The final part is where things start looking different, this is code runner in action. we start by initializing the StreamRelay, which is responsible for the client/server communication. then initialize a Psr7Client and pass in the relay, using the relay.

The Psr7Client, will then run in a continues loop receiving any incoming requests from the server, calls the app handle function and passes in a well formed psr7 request, the app processes the request and returns a psr7 response which is the given back to Roadrunner psr7 client and is sent back to the server

If you’ve ever used slimphp before, you will notice that we are missing $app->run(); This is intentional as this call causes the app to echo the response, and its exactly what we don’t want to . Roadrunner will handle it

.rr.yml is a configuration file that defines basic application settings e.g. the port to bind to. This can also be a json file if you are not the person who likes bringing down an app in production just because you missed and invisible space somewhere in your Yaml file(looking at you docker). See more config options here.

Now that we’re all set lets run the application. From the command line, run .\rr.exe -d -v or ./rr -d -v on linux and mac. -v flag is for verbose logging and -d turns on debug mode

Things to watch for!

  1. Global state: functions like $_GET,$_SERVER,$_SESSION etc. should not be used, you should strictly follow psr coding standards. as the applications are not teared down, memory management also becomes a concern
  2. Roadrunner is a fairly new project and hasn’t been tested and tried like apache or Nginx, I wouldn’t recommend jumping in with both feet, as a start start by moving tasks queues, small applications & api’s then replace services like supervisor for this

Conclusion

Roadrunner is a very promising project that helps modernize php stack with very little context switching, The project itself is very active on GitHub with no signs of slowing down. the initial release of version 1.0 was back in June 2018.

If you notice any typo or anything misleading let me know by writing to nkosgey6@gmail.com. follow me on twitter nixoncode.

--

--