| 1 |
# PODNAME: Dancer2::Manual::Deployment |
| 2 |
# ABSTRACT: A non-exhaustive guide to put your Dancer2 app into production |
| 3 |
|
| 4 |
__END__ |
| 5 |
|
| 6 |
=pod |
| 7 |
|
| 8 |
=encoding UTF-8 |
| 9 |
|
| 10 |
=head1 NAME |
| 11 |
|
| 12 |
Dancer2::Manual::Deployment - A non-exhaustive guide to put your Dancer2 app into production |
| 13 |
|
| 14 |
=head1 VERSION |
| 15 |
|
| 16 |
version 2.2.2 |
| 17 |
|
| 18 |
=head1 Dancer2 Deployment Guide |
| 19 |
|
| 20 |
Deploying a Dancer2 application involves choosing the right method for |
| 21 |
running your app in production. This guide walks you through the most |
| 22 |
common deployment options, from using the development web server to |
| 23 |
setting up robust production configurations. |
| 24 |
|
| 25 |
=head1 Stand-Alone Development Server |
| 26 |
|
| 27 |
Dancer2 applications come with a built-in development server that is |
| 28 |
useful for local testing and debugging. This server is not suitable for |
| 29 |
production environments due to its lack of performance optimizations and |
| 30 |
scalability. |
| 31 |
|
| 32 |
=head2 Running the Stand-Alone Server with C<plackup> |
| 33 |
|
| 34 |
To start your application with the built-in development server, run the |
| 35 |
following command from the directory containing your application: |
| 36 |
|
| 37 |
plackup -r bin/app.psgi |
| 38 |
|
| 39 |
The C<-r> flag enables live reloading of the application code, which is |
| 40 |
handy during development. |
| 41 |
|
| 42 |
For a quick test, navigate to L<http://localhost:5000> in your browser. |
| 43 |
This method is great for development but should not be used in production. |
| 44 |
|
| 45 |
=head3 Auto reloading with plackup and Shotgun |
| 46 |
|
| 47 |
There may be circumstances where Plack's built-in reloader won't work for |
| 48 |
you, be it for the way it looks for changes, or because there are many |
| 49 |
directories you need to monitor, or you want to reload the application any |
| 50 |
time one of the modules in Perl's F<lib/> path changes. |
| 51 |
L<Plack::Loader::Shotgun> makes this easy by recompiling the application |
| 52 |
on every request. |
| 53 |
|
| 54 |
To use Shotgun, specify it using the loader argument to C<plackup (-L)>: |
| 55 |
|
| 56 |
plackup -L Shotgun bin/app.psgi |
| 57 |
|
| 58 |
The Shotgun, while effective, can quickly cause you performance issues, even |
| 59 |
during the development phase of your application. As the number of plugins |
| 60 |
you use in your application grows, as the number of static resources (images, |
| 61 |
etc.) grows, the more requests your server process needs to handle. Since |
| 62 |
each request recompiles the application, even simple page refreshes can get |
| 63 |
unbearably slow over time. Use with caution. |
| 64 |
|
| 65 |
You can bypass Shotgun's auto-reloading of specific modules with the |
| 66 |
C<-M> switch: |
| 67 |
|
| 68 |
plackup -L Shotgun -M<MyApp::Foo> -M<MyApp::Bar> bin/app.psgi |
| 69 |
|
| 70 |
On Windows, the Shotgun loader is known to cause huge memory leaks in a |
| 71 |
fork-emulation layer. If you are aware of this and still want to run the |
| 72 |
loader, please use the following command: |
| 73 |
|
| 74 |
PLACK_SHOTGUN_MEMORY_LEAK=1 plackup -L Shotgun bin\app.psgi |
| 75 |
|
| 76 |
B<Please note:> if you are using Dancer 2's asynchronous capabilities, using |
| 77 |
Shotgun will kill Twiggy. If you need async processing, consider an |
| 78 |
alternative to Shotgun. |
| 79 |
|
| 80 |
=head1 Running a PSGI-Based Web Server |
| 81 |
|
| 82 |
For production environments, use a PSGI-compatible web server. These |
| 83 |
servers provide improved performance, scalability, and better resource |
| 84 |
management. |
| 85 |
|
| 86 |
=head2 Using C<plackup> |
| 87 |
|
| 88 |
You can use C<plackup> in production by configuring it to run without |
| 89 |
live reloading and with proper performance settings: |
| 90 |
|
| 91 |
plackup -E production --host 0.0.0.0 --port 5000 bin/app.psgi |
| 92 |
|
| 93 |
This approach is simple but lacks advanced features like process |
| 94 |
management and load balancing. |
| 95 |
|
| 96 |
=head2 Using Starman |
| 97 |
|
| 98 |
L<Starman> is a high-performance, pre-forking PSGI server for Perl |
| 99 |
applications. It is designed for production environments. |
| 100 |
|
| 101 |
To run your app with Starman: |
| 102 |
|
| 103 |
plackup -s Starman --workers 5 --port 5000 bin/app.psgi |
| 104 |
|
| 105 |
The C<--workers> flag specifies the number of worker processes to handle |
| 106 |
requests. Adjust this based on your server's resources. |
| 107 |
|
| 108 |
=head2 Using Gazelle |
| 109 |
|
| 110 |
L<Gazelle|https://metacpan.org/pod/Gazelle> is another PSGI server that |
| 111 |
focuses on long-lived connections and minimal overhead. |
| 112 |
|
| 113 |
Run your app with Gazelle like this: |
| 114 |
|
| 115 |
plackup -s Gazelle -p 5000 bin/app.psgi |
| 116 |
|
| 117 |
Gazelle provides options for optimizing keep-alive connections and |
| 118 |
handling large numbers of requests efficiently. |
| 119 |
|
| 120 |
=head2 Using Server::Starter for Zero-Downtime Deployments |
| 121 |
|
| 122 |
L<Server::Starter> provides you with a superdaemon and instance script |
| 123 |
for managing PSGI-based Perl application servers. The superdaemon is |
| 124 |
responsible for starting new processes and shutting down old ones. It |
| 125 |
provides hot-deployment capability by watching for restart requests and |
| 126 |
ensuring new processes successfully start up before terminating the old |
| 127 |
processes. F<start_server> is the command-line program that allows you |
| 128 |
to interact with the superdaemon. |
| 129 |
|
| 130 |
You can manage most PSGI-based app server with F<start_server>: |
| 131 |
|
| 132 |
start_server --port 5000 -- plackup -s Gazelle -a app.psgi |
| 133 |
|
| 134 |
=head2 Using uWSGI |
| 135 |
|
| 136 |
L<uWSGI|https://uwsgi-docs.readthedocs.io/en/latest/> is a robust |
| 137 |
application server that supports PSGI. It is written in C and provides |
| 138 |
excellent performance. |
| 139 |
|
| 140 |
For configuring uWSGI with a Dancer2 application, refer to the official |
| 141 |
documentation: L<https://uwsgi-docs.readthedocs.io/en/latest/Perl.html>. |
| 142 |
|
| 143 |
A basic configuration might look like this: |
| 144 |
|
| 145 |
uwsgi --http :5000 --plugin psgi --psgi bin/app.psgi |
| 146 |
|
| 147 |
uWSGI is powerful and supports features like process management, load |
| 148 |
balancing, and integration with NGINX. |
| 149 |
|
| 150 |
=head1 Running Behind a Reverse Proxy |
| 151 |
|
| 152 |
Using another webserver like NGINX to reverse proxy your application can |
| 153 |
improve the scalability and security of your application. A reverse proxy |
| 154 |
handles incoming requests, forwards them to your PSGI server, and manages |
| 155 |
features like SSL termination and caching. Reverse proxies can help |
| 156 |
improve the performance of your applications by serving static content |
| 157 |
more efficiently than your application can. |
| 158 |
|
| 159 |
When running your Dancer2 application behind a reverse proxy, make sure |
| 160 |
to set the C<behind_proxy> configuration setting to make sure all URLs |
| 161 |
and headers are set properly: |
| 162 |
|
| 163 |
behind_proxy: 1 |
| 164 |
|
| 165 |
For more information, see L<the configuration guide|Dancer2::Manual::Config/behind_proxy (boolean)>. |
| 166 |
|
| 167 |
=head2 Example: Using NGINX with Starman |
| 168 |
|
| 169 |
Here’s a basic NGINX configuration for a Dancer2 app running on Starman: |
| 170 |
|
| 171 |
server { |
| 172 |
listen 80; |
| 173 |
server_name example.com; |
| 174 |
|
| 175 |
location / { |
| 176 |
proxy_pass http://127.0.0.1:5000; |
| 177 |
proxy_set_header Host $host; |
| 178 |
proxy_set_header X-Real-IP $remote_addr; |
| 179 |
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; |
| 180 |
} |
| 181 |
} |
| 182 |
|
| 183 |
Start NGINX and Starman, and your app will be accessible through the proxy. |
| 184 |
|
| 185 |
=head2 Example: Using NGINX with uWSGI |
| 186 |
|
| 187 |
For uWSGI, the NGINX configuration looks like this: |
| 188 |
|
| 189 |
server { |
| 190 |
listen 80; |
| 191 |
server_name example.com; |
| 192 |
|
| 193 |
location / { |
| 194 |
include uwsgi_params; |
| 195 |
uwsgi_pass 127.0.0.1:5000; |
| 196 |
} |
| 197 |
} |
| 198 |
|
| 199 |
This setup routes requests from NGINX to your uWSGI server. |
| 200 |
|
| 201 |
=head1 Running Through CGI/FastCGI |
| 202 |
|
| 203 |
While PSGI servers are recommended, Dancer2 can also run through CGI or |
| 204 |
FastCGI, typically with Apache. |
| 205 |
|
| 206 |
=head2 Running as a CGI Script |
| 207 |
|
| 208 |
In a CGI setup, requests are processed one at a time, making it |
| 209 |
unsuitable for high-traffic environments; a new instance of your |
| 210 |
application is compiled and served on every request. To enable CGI: |
| 211 |
|
| 212 |
=over 4 |
| 213 |
|
| 214 |
=item 1. Place your C<app.psgi> file in a directory accessible to your Apache server. |
| 215 |
|
| 216 |
=item 2. Configure Apache to execute the PSGI script as a CGI: |
| 217 |
|
| 218 |
=back |
| 219 |
|
| 220 |
<VirtualHost *:80> |
| 221 |
ServerName example.com |
| 222 |
|
| 223 |
DocumentRoot /path/to/your/app/public |
| 224 |
ScriptAlias / /path/to/your/app/bin/app.psgi |
| 225 |
</VirtualHost> |
| 226 |
|
| 227 |
=head2 Running as a FastCGI Script |
| 228 |
|
| 229 |
FastCGI is a more efficient alternative to CGI. Install a PSGI-to-FastCGI adapter like C<FCGI::PSGI> and configure Apache: |
| 230 |
|
| 231 |
<VirtualHost *:80> |
| 232 |
ServerName example.com |
| 233 |
|
| 234 |
DocumentRoot /path/to/your/app/public |
| 235 |
<Location /> |
| 236 |
SetHandler fcgid-script |
| 237 |
Options +ExecCGI |
| 238 |
FCGIWrapper /path/to/your/app/bin/app.psgi |
| 239 |
</Location> |
| 240 |
</VirtualHost> |
| 241 |
|
| 242 |
=head1 AUTHOR |
| 243 |
|
| 244 |
Dancer Core Developers |
| 245 |
|
| 246 |
=head1 COPYRIGHT AND LICENSE |
| 247 |
|
| 248 |
This software is copyright (c) 2026 by Alexis Sukrieh. |
| 249 |
|
| 250 |
This is free software; you can redistribute it and/or modify it under |
| 251 |
the same terms as the Perl 5 programming language system itself. |
| 252 |
|
| 253 |
=cut |