Deployment of Dancer applications with Starman, Apache and mod_proxy
Introduction
I would like to show a clean and elegant deployment solution for deploying Dancer using Starman.
Starman is a high-performance PSGI/Plack web server, written by Tatsuhiko Miyagawa. PSGI is an interface between a web server and a web application, written in Perl. PSGI supports mod_perl, CGI and FastCGI.
While PSGI is the equivalent to WSGI in Python or Rack for Ruby, Starman is the equivalent to Gunicorn in Python or Unicorn in Ruby, even if not 100% compatible.
The Apache web server is a great web server, but has changed quite a bit with time. Its features and functionality is increasing, but so is its requirements. It is used in production environments to manage applications, virtual hosts and more. However, when Apache was started, it was not meant to serve Perl applications, which is why mod_perl was created.
I appreciate simple applications that do one thing and do it well. This is why I prefer to use Starman which is very good at what it does.
Configure
First, you'll need to install Starman. I use App::cpanminus here.
# if you use a sudo policy: sudo cpanm Starman # if you're root: cpanm Starman
Once installed, you go to the application directory and start the server.
cd app_dir plackup -s Starman app.pl
Notice this will start the Starman web server for this specific application.
That means that if you're already running a web server (either Apache or just
another Starman for a different application), it will not be able to start since
the port will be taken. You can specify the port number using -port
.
Using Apache, you can run Starman as a proxy using mod_proxy and add the port number to direct the requests to.
To be able to use Apache with mod_proxy, you'll need to make sure the required modules are available.
If you're on Debian or Ubuntu, you can activate the Apache modules using the following command:
# using sudo: sudo a2enmod proxy proxy_http cache # as root: a2enmod proxy proxy_http cache
If you're using a different distribution, please consult their documentation on how to enable Apache modules. It can be as simple as installing them or including a configuration file.
Now to configure Apache:
<VirtualHost *:80> ServerName example.com ServerAlias www.example.com DocumentRoot /path/to/dancer/app <Proxy *> Order deny,allow Allow from allow </Proxy> ProxyPass / http://localhost:5000/ ProxyPassReverse / http://localhost:5000/ </VirtualHost>
You can configuration Apache to serve static files, to ease on Starman and your application and provide faster response for these files.
ProxyPass /public/favicon.ico !
Using Debian or Ubuntu, we enable the configuration so Apache will use it:
cd /etc/apache2/sites-available/ # sudo: sudo a2ensite app_dancer # root: a2ensite app_dancer
And restart Apache.
That's it!
Conclusion
This deployment method is certainly a simple solution that is scalable and can even be improved later on by replacing Apache with a lighter server like Cherokee.