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:
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.
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
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 |
---|---|---|
|
Name of the created output package without the extension |
Project name transformed from mixed case and spaces to lowercase and dash-separated |
|
The description of the package |
Project name |
|
Summary of the contents of a Linux package |
Project name |
|
Name of the executing script |
Project name transformed from mixed case and spaces to lowercase and dash-separated |
|
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.
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 applicationuniversal:packageZipTarball
– This command creates an appname-appVersion.tgz
file of the packaged applicationuniversal:packageOsxDmg
– This command creates an appname-appVersion.dmg
file of the packaged application (the command only works on OS X)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.
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
.
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
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 |
---|---|
|
Name of the vendor for this |
|
License of the code within the |
|
URL to include in the |
|
Description of this |
|
Special release number for this |
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
3.141.27.74