PHP on Cowboy Web Server – Give It a Shot

In previous post, I gave some background to making PHP run on Cowboy web server. In this post, I’m going to provide technical details how to run your own setup. But it’s really not complicated at all. So, let’s install, run and play with unix1/cowboy_fcgi.

Prerequisites

  • Erlang (19.0 or newer) installed
    • on Linux use your distro’s package manager or download binary from Erlang Solutions
    • on MacOS run brew install erlang
  • GNU make and git in your path
  • php-fpm is in your path
    • on Linux it might be in /usr/sbin/php-fpm

Steps

$ git clone https://github.com/unix1/cowboy_fcgi.git

$ cd cowboy_fcgi/examples/cowboy_php_hello

$ make run

Enjoy

With your browser go to http://localhost:33080/hello.php – you should see the output from the hello.php script.

Modify or add more PHP files

Your currently running application release is located in _rel/cowboy_php_hello_release/lib/cowboy_php_hello-0.1.0/priv/www relative to your current working directory. In that directory you’ll find the hello.php file that produced the output to the browser. You can modify that file, or add more files in that directory.

Change php-fpm configuration

The example includes a php-fpm.conf file in priv/conf/php-fpm.conf. When starting the application, it starts php-fpm with that configuration file. You can modify it prior to building and running the application. For example, try increasing max_children setting from 5 to some other number.

What happened behind the scenes

When make run executed, the following happened:

  • dependencies were fetched
  • Erlang project was compiled and release made
  • released application was started, which, in turn
  • started the php-fpm

Given this, to fully stop both the Erlang application and php-fpm, you could either:

  • hit Ctrl-C twice in Erlang application prompt and manually kill all php-fpm processes; OR
  • type application:stop(cowboy_php_hello). and press enter in the Erlang prompt and then exit out of it

Try more things

Run your own php-fpm

For the sake of simplicity, this example by default started the php-fpm processes for you. But maybe typically you wouldn’t want to do that – instead, you might want to start php-fpm separately as a service. You can do this, and then in src/cowboy_php_hello_app.erl replace this line

cowboy_php_hello:start_with_phpfpm(33080, 33000),

with

cowboy_php_hello:start(33080, 33000),

And replace 33000 with whatever port you ran your php-fpm service on.

Add your own Cowboy handlers

One of the exciting parts of this setup is that you can host both PHP and Erlang applications under one web server. The example project only includes one Cowboy dispatch rule that routes all incoming HTTP requests to the cowboy_fcgi handler. However, this doesn’t need to be the case. You can add your own routing rules to route specific requests to specific handlers, allowing you to have mixed Erlang and PHP environment for your web applications!

Feedback

Try it out, play around with it, and give me feedback. I’d love to hear about use cases, issues, or other thoughts.

PHP on Cowboy Web Server – The Update

Background

Cowboy is an awesome web server – it’s simple, small, fast, standards compliant, and highly scalable. It also provides easy ways to get started with RESTful web applications. Its downside is that, unlike with Apache, nginx, and other more mainstream servers, it’s only usable in Erlang (and related BEAM languages) environments.

There used to be a couple of projects long time ago (2011!) that added FCGI handler module for Cowboy version 0.4, but they were never really maintained beyond the initial release. They quickly became incompatible with rapid development of Cowboy.

Current work

Now that the modern version of Cowboy is 2.0 (as of this writing), I decided took take a stab at updating those 2 projects to work with it. And here’s the result:

https://github.com/unix1/cowboy_fcgi

There are enough quick start steps and examples in that repo to get most developers experimenting. Regardless, in the next post, I’ll go deeper into how one can play around with this setup in greater detail.