Here some simple tips to optimize your application for production.
Configure your application.conf
First off, the best-mode to specify production-give a specific ID to your production framework. Let's pick production as an example. Refer manage application.conf in several environments.
Set the framework in PROD mode:
%production.application.mode=prod
In this mode, the framework would pre-compile all Java sources and templates. If An error is found on this step, the application would not start. Source modifications is not being hot reloaded.
Define a real database:
If you had used a development database (either db=mem or db=fs), you should configure a more robust dat Abase Engine:
%production.db.url=jdbc:mysql://localhost/prod%production.db.driver=com.mysql.jdbc.Driver%production.db.user=root%production.db.pass=1515312
Disable JPA Automatic schema update:
If you had used the automatic schema update feature provided by Hibernate, you should disable this feature for production .
For your production server, it's usually a bad idea to let Hibernate automatically ALTER your production database ' s schema and data ...
The initial deployment is potentially a different issue. In this case only specify:
%production.jpa.ddl=create
Define a secure secret key:
The Play secret key is used to secure cryptographic functions, like the session signature. Your application must keep this key very secret.
%production.application.secret=c12d1c59af499d20f4955d07255ed8ea333
You can use the play secret command to generate a new secure and random key (at least on a ' real ' OS). If you plan to distribute your application to several servers, remember to use the same key for all application I nstances!
Configure Logging
For production It's a good idea to use rolling log files. Don't send logging to the Console, since it'll be written to the logs/system.out file and it'll grow without bound!
Create a custom log4j.properties in the conf/ directory:
log4j.rootLogger=ERROR, Rolling log4j.logger.play=INFO # Rolling fileslog4j.appender.Rolling=org.apache.log4j.RollingFileAppenderlog4j.appender.Rolling.File=application.loglog4j.appender.Rolling.MaxFileSize=1MBlog4j.appender.Rolling.MaxBackupIndex=100log4j.appender.Rolling.layout=org.apache.log4j.PatternLayoutlog4j.appender.Rolling.layout.ConversionPattern=%d{ABSOLUTE} %-5p ~ %m%n
Set-up a front-end HTTP server
You can easily deploy your application as a stand-alone server by setting the application HTTP port to:
%production.http.port=80
If you plan to host several applications in the same server or load balance several instances of your application for Scalability or fault tolerance, you can use a front-end HTTP server.
Note that using a front-end HTTP server would never give you better performance than using Play server directly!
Set-up with LIGHTTPD
This example shows the Configure LIGHTTPD as a front-end Web server. Note that can does the same with Apache, and if you have need virtual hosting or load balancing, lighttpd is a very good Choice and much easier to configure!
The /etc/lighttpd/lighttpd.conf file should define things like this:
server.modules = ( "mod_access", "mod_proxy", "mod_accesslog" )...$HTTP["host"] =~ "www.myapp.com" { proxy.balance = "round-robin" proxy.server = ( "/" => ( ( "host" => "127.0.0.1", "port" => 9000 ) ) )} $HTTP["host"] =~ "www.loadbalancedapp.com" { proxy.balance = "round-robin" proxy.server = ( "/" => ( ( "host" => "127.0.0.1", "port" => 9000 ), ( "host" => "127.0.0.1", "port" => 9001 ) ) )}
Set-up with Apache
The example below shows a simple set-up with Apache httpd server running in front of a standard Play configuration.
LoadModule proxy_module modules/mod_proxy.so...<VirtualHost *:80> ProxyPreserveHost On ServerName www.loadbalancedapp.com ProxyPass / http://127.0.0.1:9000/ ProxyPassReverse / http://127.0.0.1:9000/</VirtualHost>
Apache as a front proxy to allow transparent upgrade of your application
The basic idea was to run 2 Play instances of your Web application and let the front-end proxy load-balance them. In case one was not available, it would forward all the requests to the available one.
Let's start the same Play application the Times:one on port 9999 and one on port 9998.
Copy the Application 2 times and edit the application.conf in the Conf directory to change the port numbers.
For each Web application directory:
play start mysuperwebapp
Now, let's configure our Apache Web server to a load balancer.
In Apache, I has the following configuration:
<VirtualHost mysuperwebapp.com:80> ServerName mysuperwebapp.com <Location /balancer-manager> SetHandler balancer-manager Order Deny,Allow Deny from all Allow from .mysuperwebapp.com </Location> <Proxy balancer://mycluster> BalancerMember http://localhost:9999 BalancerMember http://localhost:9998 status=+H </Proxy> <Proxy *> Order Allow,Deny Allow From All </Proxy> ProxyPreserveHost On ProxyPass /balancer-manager ! ProxyPass / balancer://mycluster/ ProxyPassReverse / http://localhost:9999/ ProxyPassReverse / http://localhost:9998/</VirtualHost>
The important part is balancer://mycluster. This declares a load balancer. The +H option means that the second Play application are on stand-by. But can also instruct it to load-balance.
Every time want to upgrade Mysuperwebapp, here are what we need to do:
play stop mysuperwebapp1
The load-balancer then forwards everything to MYSUPERWEBAPP2. In the meantime update MYSUPERWEBAPP1. Once You is done:
play start mysuperwebapp1
You can now safely update mysuperwebapp2.
Apache also provides a to view the status of your cluster. Simply Point your browser To/balancer-manager to view the current status of your clusters.
Because Play is completely stateless you don't have to manage sessions between the 2 clusters. You can actually easily scale to more than 2 Play instances.
Advanced proxy Settings
When using a HTTP frontal server, request addresses is seen as coming from the HTTP server. In a usual set-up, where both has the play app and the proxy running on the same machine, the play app would see the R Equests coming from 127.0.0.1.
Proxy servers can add a specific header to the request of the proxied application where the request came from. Most Web servers would add an X-FORWARDED-FOR header with the remote client IP address as first argument. If you enable the forward on your Play app configuration:
XForwardedSupport=127.0.0.1,10.0.0.25
Play would change the request.remoteaddress from the proxy's IP to the client ' s IP. You have a to list the IP addresses of your proxy servers for the.
However, the host header is untouched, it's ' ll remain issued by the proxy. If you use the Apache 2.x, you can add a directive like:
ProxyPreserveHost on
The Host:header is the original host request header issued by the client. By combining theses-techniques, your app would appear to be directly exposed.
Continuing the discussion
Next: Deployment options.
Put your application in production