Release commit for 5.9013
[catagits/Catalyst-Manual.git] / lib / Catalyst / Manual / Deployment / Apache / mod_perl.pod
CommitLineData
1d2376f3 1=head1 NAME
2
3Catalyst::Manual::Deployment::Apache::mod_perl - Deploying Catalyst with mod_perl
4
0191b435 5=head1 mod_perl Deployment
1d2376f3 6
5abded07 7The recommended method of deploying Catalyst applications is FastCGI. In
8many cases, mod_perl is not the best solution, but we'll list some pros
9and cons so you can decide for yourself.
1d2376f3 10
0191b435 11=head2 Pros
1d2376f3 12
0191b435 13=head3 Speed
1d2376f3 14
5abded07 15mod_perl is fast, and your entire app will be loaded in memory within
16each Apache process.
1d2376f3 17
0191b435 18=head3 Shared memory for multiple apps
1d2376f3 19
20If you need to run several Catalyst apps on the same server, mod_perl will
21share the memory for common modules.
22
0191b435 23=head2 Cons
1d2376f3 24
0191b435 25=head3 Memory usage
1d2376f3 26
27Since your application is fully loaded in memory, every Apache process will
28be rather large. This means a large Apache process will be tied up while
29serving static files, large files, or dealing with slow clients. For this
30reason, it is best to run a two-tiered web architecture with a lightweight
31frontend server passing dynamic requests to a large backend mod_perl
32server.
33
0191b435 34=head3 Reloading
1d2376f3 35
5abded07 36Any changes made to the code of your app require a full restart of
37Apache. Catalyst does not support Apache::Reload or StatINC. This is
38another good reason to run a frontend web server where you can set up an
39C<ErrorDocument 502> page to report that your app is down for
40maintenance.
1d2376f3 41
42=head4 Cannot run multiple versions of the same app
43
44It is not possible to run two different versions of the same application in
45the same Apache instance because the namespaces will collide.
46
5abded07 47=head3 Cannot run different versions of libraries
1d2376f3 48
49If you have two different applications which run on the same machine,
5abded07 50and each application needs a different versions of a library, the only
51way to do this is to have per-vhost perl interpreters (with different
52library paths). This is entirely possible, but nullifies all the memory
53sharing benefits that you get from having multiple applications sharing
54the same interpreter.
1d2376f3 55
0191b435 56=head1 Setup
1d2376f3 57
58Now that we have that out of the way, let's talk about setting up mod_perl
59to run a Catalyst app.
60
0191b435 61=head2 2. Install Apache with mod_perl
1d2376f3 62
63Both Apache 1.3 and Apache 2 are supported, although Apache 2 is highly
17fad97e 64recommended. With Apache 2, make sure you are using the prefork MPM and not
65the worker MPM. The reason for this is that many Perl modules are not
66thread-safe and may have problems running within the threaded worker
67environment. Catalyst is thread-safe however, so if you know what you're
68doing, you may be able to run using worker.
1d2376f3 69
70In Debian, the following commands should get you going.
71
72 apt-get install apache2-mpm-prefork
73 apt-get install libapache2-mod-perl2
74
0191b435 75=head2 3. Configure your application
1d2376f3 76
77Every Catalyst application will automagically become a mod_perl handler
78when run within mod_perl. This makes the configuration extremely easy.
79Here is a basic Apache 2 configuration.
80
81 PerlSwitches -I/var/www/MyApp/lib
82 PerlModule MyApp
83
84 <Location />
85 SetHandler modperl
86 PerlResponseHandler MyApp
87 </Location>
88
89The most important line here is C<PerlModule MyApp>. This causes mod_perl
90to preload your entire application into shared memory, including all of your
91controller, model, and view classes and configuration. If you have -Debug
92mode enabled, you will see the startup output scroll by when you first
93start Apache.
94
d5eac609 95Also, there have been reports that the block above should instead be (but
96this has not been confirmed):
97
98 <Perl>
99 use lib '/var/www/MyApp/lib';
100 use MyApp;
101 </Perl>
7ce05098 102
d5eac609 103 <Location />
104 SetHandler modperl
105 PerlResponseHandler MyApp
106 </Location>
107
1d2376f3 108For an example Apache 1.3 configuration, please see the documentation for
109L<Catalyst::Engine::Apache::MP13>.
110
d5eac609 111
0191b435 112=head2 Test It
1d2376f3 113
114That's it, your app is now a full-fledged mod_perl application! Try it out
115by going to http://your.server.com/.
116
0191b435 117=head1 Other Options
1d2376f3 118
0191b435 119=head2 Non-root location
1d2376f3 120
121You may not always want to run your app at the root of your server or virtual
122host. In this case, it's a simple change to run at any non-root location
123of your choice.
124
125 <Location /myapp>
126 SetHandler modperl
127 PerlResponseHandler MyApp
128 </Location>
129
130When running this way, it is best to make use of the C<uri_for> method in
131Catalyst for constructing correct links.
132
0191b435 133=head2 Static file handling
1d2376f3 134
135Static files can be served directly by Apache for a performance boost.
136
137 DocumentRoot /var/www/MyApp/root
138 <Location /static>
139 SetHandler default-handler
140 </Location>
141
142This will let all files within root/static be handled directly by Apache. In
143a two-tiered setup, the frontend server should handle static files.
144The configuration to do this on the frontend will vary.
145
1d2376f3 146Note the path of the application needs to be stated explicitly in the
0191b435 147web server configuration for this recipes.
c62b44f3 148
1d2376f3 149=head1 AUTHORS
150
151Catalyst Contributors, see Catalyst.pm
152
153=head1 COPYRIGHT
154
155This library is free software. You can redistribute it and/or modify it under
156the same terms as Perl itself.
157
158=cut
159