This post shows how to deploy a Play! application to a server that is running Apache2, mostly as a note to myself.
My server hosts multiple domains, and I want the Play! application to run on the example.org domain (which is, obviously, merely an example domain name). The other domains are being served by Apache, so just starting Play! on port 80 is not an option — it’s already in use. Instead, we will let run Play! on the default port (9000) and have Apache translate requests to example.org to the Play! instance. Apache, in this case, takes the role of a reverse proxy.
Prepare the Play! application
I’ll assume you’ve installed Play! on your server and uploaded the Play! application you want to run on it. Then change to the application’s directory and create an id for this specific deployment:
$ play id
Just enter a name you associate with your server, e.g., server1
.
Now open the conf/application.conf
file and add some deployment-specific options using the newly created id.
Tell Play! to launch the application in production mode on this server:
%server1.application.mode=PROD
Give database credentials, e.g., for a MySQL database server:
%server1.db=mysql:username:password@database
Tell JPA to only create the database schema once when it’s empty — so it doesn’t try updating it on its own:
%server1.jpa.ddl=create
Finally tell Play! that it will be behind a reverse proxy which we’ll configure in a second:
%server1.XForwardedSupport=127.0.0.1
If you so wished, you could now also change the port the Play! application will run on. This would be necessary when having multiple Play! applications running on the same server.
%server1.http.port=9001
Now start Play! so Apache will find it running when we restart it later:
$ play start
This starts the Play! application in the background. You stop it using play stop, restart it using play restart
, and play status
will give you some information on the current state of the application.
Make Apache a reverse proxy
Apache2 organizes configuration files into sites, probably to be found in /etc/apache2/sites-available
and activated by being linked to in /etc/apache2/sites-enabled
. Open the configuration file for the site in question and, for the example.org virtual host, add the ProxyPass and ProxyPassReverse directives. Your site configuration might look like this, then:
ProxyPass / http://127.0.0.1:9001/
ProxyPassReverse / http://127.0.0.1:9001/
ServerName example.org
Now we need to add some additional modules to Apache so it actually knows how to do what we tell it to. All it takes is adding three soft links in the /etc/apache2/mods-enabled
directory:
$ ln -s ../mods-available/headers.load headers.load
$ ln -s ../mods-available/proxy.load proxy.load
$ ln -s ../mods-available/proxy_http.load proxy_http.load
Now restart Apache like so:
$ /etc/init.d/apache2 graceful
And that’s it — opening example.org in your web browser should now bring up your Play! application.