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.
- 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
$ cd cowboy_fcgi/examples/cowboy_php_hello
$ make run
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
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
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
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!
Try it out, play around with it, and give me feedback. I’d love to hear about use cases, issues, or other thoughts.