Preparing for production
This time we’ve finished the blog engine. Let’s see the few common steps we need to put a Play application in production.
Defining a framework ID
You will typically deploy your application on a different computer (a server) than the one you used for development. So you will have a different Play installation.
Play allows to assign each framework installation a different ID, then to manage different configurations in the same application.conf
file. Let’s say that the server01
will host your production application.
Once the Play framework is installed on this server, let’s define the framework ID using the play id
command. Type:
$ play id
And assign server01
as id. Now we can define special keys in the yabe configuration file that will be used only when the application is running on the server.
Setting the application in PROD mode
The first configuration key we want specialize for the server deployment is the application.mode
property. So far we have used the DEV mode that allows Play to hot reload and recompile Java files and display detailed messages when an error occurs. In PROD mode however, Play will compile all Java sources and templates at startup time and will never check again for changes.
In the yabe/conf/application.conf
file, define:
%server01.application.mode=PROD
Now when you run the yabe application on the server, it will automatically start in PROD mode.
Configuring a MySQL server
For production use, we will use a MySQL server as database server instead of the in-memory H2 Database we have used so far. Play comes with the JDBC driver for MySQL so we don’t need to install anything more.
Edit the database configuration in the yabe/conf/application.conf
file:
%server01.db=mysql:root:secret@yabe
We will now tweak the way Hibernate manages the database schema for us. It’s very useful when Hibernate automatically updates the database schema as the Java model objects change.
Change the jpa.ddl
configuration key:
%server01.jpa.ddl=update
However it’s kind of unpredictable, and running magical things on a production database is never a good thing. So you should always have a backup before a new deployement. If you don’t want Hibernate to update your database, change the jpa.ddl
configuration key to validate
:
%server01.jpa.ddl=validate
Setting up a frontal HTTP server
Now we could just change the embedded HTTP server’s default port to 80 in order to have a real production server. However it will limit us to installing only one Play application on a given server. As we typically want to install several applications in the same server (but using different IP host names), we need to use a frontal HTTP server as reverse proxy.
You can choose any HTTP server and configure it as reverse proxy, however it is generally a good choice to take something light and fast like LIGHTTPD.
The exact configuration of LIGHTTPD is beyond the scope of this tutorial, but should be something like:
server.modules = (
"mod_access",
"mod_proxy",
"mod_accesslog"
)
…
$HTTP["host"] =~ "www.yabe.com" {
proxy.balance = "round-robin" proxy.server = ( "/" =>
( ( "host" => "127.0.0.1", "port" => 9000 ) ) )
}
And then allow a local reverse proxy to connect to your Play application by adding this key to the application.conf
file:
%server01.XForwardedSupport=127.0.0.1
This is just the beginning
If you have read and followed this tutorial, you’re now a seasoned Play developer. You know most of the concepts that drive Play application development.
At this stage, you may want to continue the tutorial with internationalisation and localisation.
There are still more features we haven’t explored yet, especially related to Web services, like JSON or XML. Also Play comes with more modules that provide more features. And Play itself still evolves every day.
If you are convinced that Play will help you save time for your next Java web application, you’re now ready to start. And don’t hesitate to ask us any question on the Play Google Group.
Thank you!