Chapter 12. Play in Production

Application deployment, configurations, and so on are slightly different in a production environment since it is affected by various factors, such as security, load/traffic (which is expected to handle), network issues, and so on. In this chapter, we will see how to get our Play application up and running in production. This chapter covers the following topics:

  • Deploying an application
  • Configuring for production
  • Enabling SSL
  • Using a load balancer

Deploying a Play application

A Play Framework provides commands to package and deploy Play applications in production.

The run command, which we used earlier, starts the application in DEV mode and watches the code for changes. When there is a change in the code, the application is recompiled and reloaded. Being watchful is handy during development, but is an unnecessary overhead in production. Also, the default error pages shown in PROD mode are different from the ones shown in DEV mode, that is, they have less information about the errors that are occurring (for security reasons).

Let's look at the different ways in which we can deploy an application in production.

Using the start command

To start an application in PROD mode, we can use the start command:

[PlayScala] $ start
[info] Wrote /PlayScala/target/scala-2.10/playscala_2.10-1.0.pom

(Starting server. Type Ctrl+D to exit logs, the server will remain in background)
 
Play server process ID is 24353
[info] play - Application started (Prod)
[info] play - Listening for HTTP on /0:0:0:0:0:0:0:0:9000

The process ID can be used later to stop the application. By pressing Ctrl + D, we do not lose the logs, since they are also captured in logs/application.log by default (that is, when there's been no change in the logger configuration).

The start command optionally accepts the port number at which the application should be deployed:

[PlayScala] $ start 9123
[info] Wrote /PlayScala/target/scala-2.10/playscala_2.10-1.0.pom

(Starting server. Type Ctrl+D to exit logs, the server will remain in background)
  
Play server process ID is 12502
[info] play - Application started (Prod)
[info] play - Listening for HTTP on /0:0:0:0:0:0:0:0:9123

Using a distribution

Although the start command is good enough to deploy the application, in scenarios where a portable version of the application is required, it may not be sufficient. In this section, we will see how to build a standalone distribution of our application.

The Play Framework supports building a distribution of an application using the sbt-native-packager plugin (refer to http://www.scala-sbt.org/sbt-native-packager/). The plugin can be used to create the .msi (Windows), .deb (Debian), .rpm (Red Hat Package Manager), and .zip (universal) files, as well as the Docker images of our application. The plugin also supports defining settings for the package in the application's build file. Some of the settings are common while others are OS-specific. The common ones are shown in the following table:

Setting

Purpose

Default value

packageName

Name of the created output package without the extension

Project name transformed from mixed case and spaces to lowercase and dash-separated

packageDescription

The description of the package

Project name

packageSummary

Summary of the contents of a Linux package

Project name

executableScriptName

Name of the executing script

Project name transformed from mixed case and spaces to lowercase and dash-separated

maintainer

The name/e-mail address of a maintainer for the native package

 

Now, let's see how we can build packages for different OSes and use them.

Universal distribution

A universal distribution is compatible with all/most operating systems. The generated packages are located at projectHome/target/universal. We can use any of the following commands to create a package as required:

  • universal:packageBin – This command creates an appname-appVersion.zip file of the packaged application
  • universal:packageZipTarball – This command creates an appname-appVersion.tgz file of the packaged application
  • universal:packageOsxDmg – This command creates an appname-appVersion.dmg file of the packaged application (the command only works on OS X)

Note

The universal:packageZipTarball command requires the gzip, xz, and tar command-line tools, while universal:packageOsxDmg requires OS X or systems installed with hdiutil.

To use the package built through these commands, extract the files and execute bin/appname for the Unix-based systems and bin/appname.bat for systems with Windows.

Note

In a Play application, we can use the dist command instead of universal:packageBin. The dist command deletes unnecessary intermediate files created while packaging the application using the universal:packageBin command.

Debian distribution

We can create a distribution that can be installed on Debian-based systems using the debian:packageBin command. The .deb file is located at projectHome/target.

Note

To build the Debian package, the value for packageDescription in the Debian setting should be set in the build file. Other Debian package settings can also be set in the build file.

After packaging, we can install the application using dpkg-deb:

projectHome$ sudo dpkg -i target/appname-appVersion.deb

Once it's installed, we can start the application by executing this:

$ sudo appname

The rpm distribution

An rpm package of the application can be created using the rpm:packageBin command. Some of the settings available for the rpm package are shown in the following table:

Setting

Purpose

rpmVendor

Name of the vendor for this rpm package

rpmLicense

License of the code within the rpm package

rpmUrl

URL to include in the rpm package

rpmDescription

Description of this rpm package

rpmRelease

Special release number for this rpm package

Note

The values for rpmVendor in rpm, packageSummary in rpm, and packageDescription in rpm must be set in the build file to successfully create an rpm package of the application where rpm is the scope, for example the name in rpm:= "SampleProject".

Once the rpm package is generated, we can install it using yum or an equivalent tool:

projectHome$ sudo yum install target/appname-appVersion.rpm

After the installation is completed, we can start the application by executing this:

$ sudo appname

Windows distribution

A Windows installer of the application, appname-appVersion.msi, can be created using the windows:packageBin command. The file is located at projectHome/target.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.143.235.219