Documentation

You are viewing the documentation for Play 1. The documentation for Play 2 is here.

Preparing for production

This time we’ve finished the blog engine. Let’s see the few common steps we need to set 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 to make the 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 HSQLDB 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. However it’s kind of unpredictable, and running magical things on a production database is never a good thing. So we will let Hibernate create the database schema if it doesn’t exist, but it will never update it automatically.

Change the jpa.ddl configuration key:

%server01.jpa.ddl=create

Setting up a frontal HTTP server

Now we could just change the default port of the embedded HTTP server to 80 in order to have a real production server. However it will limit us to install 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 out of 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 the Play application development.

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.

Thanks a lot!