Your Daily Source for Apache News and Information |
Breaking News | Preferences | Contribute | Triggers | Link Us | Search | About |
\n'); } if ( plugin ) { document.write(' '); } else if (!(navigator.appName && navigator.appName.indexOf("Netscape")>=0 && navigator.appVersion.indexOf("2.")>=0)) { document.write(''); } //--> |
|
By In the previous article, I passed along quite amazing Web performance reports from companies that have deployed mod_perl heavily. You might be surprised by this, but you can quite easily get similarly amazing results if you move your service to mod_perl as well. In fact, getting started with mod_perl shouldn't take you more than 30 minutes, the time it takes to compile and configure the server on a decent machine and get it running. In this article I'll show step-by-step installation and configuration scenarios, and chances are you will be able to run the basic statically compiled mod_perl setup without reading any other documents. Of course, you will want and need to read the documentation later, but I think you will agree with me that it's ultimately cool to be able to get your feet wet without knowing much about the new technology up-front. The mod_perl installation was tested on many mainstream Unix platforms, so unless you have some very non-standard system you shouldn't have any problems when building the basic mod_perl server. If you are a Windows user, the easiest way is to use the binary package available from http://perl.apache.org/distributions.html. From the same location you can download the Linux RPM version and CVS snapshots. However, I recommend to always build the mod_perl from source, and as you will see in a moment, it's an easy thing to do. Installing mod_perl in 10 Minutes and 10 Command LinesSo let's start with the installation process. If you are an experienced Unix user, you need no explanation for the following commands. Just copy and paste them and you will get the server installed. I'll use a % cd /usr/src % lwp-download http://www.apache.org/dist/apache_1.3.12.tar.gz % lwp-download http://perl.apache.org/dist/mod_perl-1.24.tar.gz % tar -zvxf apache_1.3.12.tar.gz % tar -zvxf mod_perl-1.24.tar.gz % cd mod_perl-1.24 % perl Makefile.PL APACHE_SRC=../apache_1.3.12/src \ DO_HTTPD=1 USE_APACI=1 PERL_MARK_WHERE=1 EVERYTHING=1 % make && make test && make install % cd ../apache_1.3.12 % make install That's all! What's left is to add a few configuration lines to If you have stumbled upon a problem at any of the above steps, don't despair--the next section will explain in detail each and every step. Installing mod_perl in 10 Minutes and 10 ParagraphsJust as advertised in the previous section--the building time stays the same: 10 minutes, but if one of the steps didn't work for you or you didn't have the courage to try it, I will explain each step in fine details here. If everything was just fine you can skip this section and move on to the next one. First you have to become a root user in order to install the files in a protected area. If you don't have root access, you can install all the files under your home directory as well. We will talk about the nuances of this approach in a future article. I'll also assume that you have perl and cc or an equivalent C compiler installed. I assume that all builds are being done in the % cd /usr/src Now we download the latest source distributions of Apache and mod_perl. If you have the % lwp-download http://www.apache.org/dist/apache_1.3.12.tar.gz % lwp-download http://perl.apache.org/dist/mod_perl-1.24.tar.gz You can make sure that you're downloading the latest stable versions by visiting the following distribution directories: http://www.apache.org/dist/ and http://perl.apache.org/dist/. As you have guessed already, the former URL is the main Apache distribution directory, the latter is the same thing for mod_perl. Untar both sources. You have to uncompress and untar the files. In addition to its main usage for tarring and untarring files, the GNU % tar -zvxf apache_1.3.12.tar.gz % tar -zvxf mod_perl-1.24.tar.gz If you have a non-GNU % gzip -d apache_1.3.12.tar.gz % gzip -d mod_perl-1.24.tar.gz Then untar them with: % tar -xvf apache_1.3.12.tar % tar -xvf mod_perl-1.24.tar If you don't have Now go into the mod_perl source distribution directory: % cd mod_perl-1.24 The next step is to create the % perl Makefile.PL APACHE_SRC=../apache_1.3.12/src \ DO_HTTPD=1 USE_APACI=1 PERL_MARK_WHERE=1 EVERYTHING=1 mod_perl accepts a variety of parameters, in this scenario we are going to use those that will allow you to do almost everything with mod_perl. Once you learn more about mod_perl you will be able to fine tune the list of parameters passed to
If you choose to install mod_perl with help of the This step also executes the Now you should build the % make This command prepares mod_perl extension files, installs them in the Apache source tree and builds the httpd executable (the web server itself) by compiling all the required files. Upon completion of the
% make test This command starts the server on a non-standard port (8529) and tests whether all parts of the built server function correctly. If something goes wrong, the process will report it to you.
% make install You can use the following commands concatenation style: % make && make test && make install It simplifies the installation, since you don't have to wait for each command to complete before starting the next one. When installing mod_perl for the first time, it's better to do it step by step. If you choose the all-in-one approach, you should know that if Finally, change to the Apache source distribution directory and run make install to create the Apache directory tree and install Apache header files ( % cd ../apache_1.3.12 % make install Note that, as with a plain Apache installation, any configuration files left from a previous installation won't be overwritten by this process. You don't need to backup your previously working configuration files before the installation. When the /usr/local/apache/bin/apachectl /usr/local/apache/conf/httpd.conf So far we have completed the building and installation of the mod_perl enabled Apache. The next steps are to configure Configuring and Starting mod_perl ServerFirst thing first: we want to make sure our Apache was built correctly and that we can serve plain HTML files with it. Why do that? To minimize the number of possible trouble makers, if we find out that mod_perl doesn't work. After you know that Apache can serve HTML files, you don't have to worry about it anymore. And if something goes wrong with mod_perl, you have eliminated the possibility that the Configure Apache as you always do. Set When you have edited the configuration file, it's time to start the server. One of the ways to start and stop the server is to use the % /usr/local/apache/bin/apachectl start And stop it with: % /usr/local/apache/bin/apachectl stop Note that you have to be root when starting the server if the server is going to listen on port 80 or another privileged port (<1024). After you start the server, check in the [Thu Jun 22 17:14:07 2000] [notice] Apache/1.3.12 (Unix) mod_perl/1.24 configured -- resuming normal operations Now point your browser to If everything works as expected, shut the server down, open Assuming that you put all scripts that should be executed by the mod_perl enabled server in the Alias /perl/ /home/httpd/perl/ PerlModule Apache::Registry <Location /perl> SetHandler perl-script PerlHandler Apache::Registry Options ExecCGI PerlSendHeader On allow from all </Location> Save the modified file. This configuration causes every URI starting with Preparing the Scripts DirectoryNow create a % chmod 0777 /home/httpd/perl This is very very insecure and you should not follow this approach on the production machine. This is good enough when you just want to try things out and want to have as few obstacles as possible. Once you understand how things work, you should tighten the permissions of files served by Apache. In future articles we will talk about setting proper file permissions. The "mod_perl rules" Apache::Registry ScriptAs you probably know, mod_perl allows you to reuse CGI scripts written in Perl that were previously used under mod_cgi. Therefore our first test script can be as simple as: mod_perl_rules1.pl ------------------ print "Content-type: text/plain\r\n\r\n"; print "mod_perl rules!\n"; Save this script in the mod_perl_rules1.pl ------------------ #!/usr/bin/perl print "Content-type: text/plain\r\n\r\n"; print "mod_perl rules!\n"; Of course you can write the same script using the Apache Perl API: mod_perl_rules2.pl ------------------ my $r = shift; $r->send_http_header('text/plain'); $r->print("mod_perl rules!\n"); Save this script in the /home/httpd/perl/mod_perl_rules2.pl file. Now make both of the scripts executable and readable by the server. Remember that when you execute scripts from a shell, they are being executed by the user-name you are logged with. When instead you try to run the scripts by issuing requests, Apache needs to be able to read and execute them. Apache is running under a user-name specified by the % chown nobody /home/httpd/perl/mod_perl_rules1.pl \ /home/httpd/perl/mod_perl_rules2.pl % chmod 0755 /home/httpd/perl/mod_perl_rules1.pl \ /home/httpd/perl/mod_perl_rules2.pl The first command makes the files belong to user nobody, the second sets the proper execution and read permissions. You can test % perl /home/httpd/perl/mod_perl_rules1.pl You should see the following output: mod_perl rules! You cannot test the second script by executing it from the command line since it uses the mod_perl API which is available only when run from within the mod_perl server. Make sure the server is running and issue these requests using your favorite browser: http://localhost/perl/mod_perl_rules1.pl http://localhost/perl/mod_perl_rules2.pl In both cases you will see on the following response: mod_perl rules! If you see it--congratulations! You have a working mod_perl server. If you're using port 8080 instead of 80, you should use this number in the URL: http://localhost:8080/perl/mod_perl_rules1.pl http://localhost:8080/perl/mod_perl_rules2.pl The http://your.server.name/perl/mod_perl_rules1.pl If there is any problem please refer to the error_log file for the error reports. Now it's a time to move your CGI scripts from Some of your scripts might not work immediately and will require some minor tweaking or even a partial rewrite to work properly with mod_perl. Chances are that if you are not practicing sloppy programming, the scripts will work without any modifications at all. If you have a problem with your scripts, a good approach is to replace PerlModule Apache::PerlRun <Location /perl> SetHandler perl-script PerlHandler Apache::PerlRun Options ExecCGI PerlSendHeader On allow from all </Location> Now your scripts should work for sure, unless there is something in them mod_perl doesn't accept. We will discuss these nuances in future articles. The "mod_perl rules" Apache Perl Modulemod_perl is about running both scripts and handlers. Although I have started to present mod_perl using scripts because it's easier if you have written CGI scripts before, the more advanced use of mod_perl is about writing handlers. But have no fear. As you will see in a moment, writing handlers is almost as easy as writing scripts. To create a mod_perl handler module, all I have to do is to wrap the code I have used for the script into a Just as with scripts you can use either the CGI API you are probably used to: ModPerl/Rules1.pm ---------------- package ModPerl::Rules1; use Apache::Constants qw(:common); sub handler{ print "Content-type: text/plain\r\n\r\n"; print "mod_perl rules!\n"; return OK; } or the Apache Perl API that allows you to interact more intimately with the Apache core by providing an API unavailable under regular Perl. Of course in the simple example that I show, using any of the approaches is fine, but when you need to use the API, this version of the code should be used. ModPerl/Rules2.pm ---------------- package ModPerl::Rules2; use Apache::Constants qw(:common); sub handler{ my $r = shift; $r->send_http_header('text/plain'); print "mod_perl rules!\n"; return OK; } Create a directory called To find out what the % perl -le 'print join "\n", @INC' On my machine it reports: /usr/lib/perl5/5.00503/i386-linux /usr/lib/perl5/5.00503 /usr/lib/perl5/site_perl/5.005/i386-linux /usr/lib/perl5/site_perl/5.005 . Now add the following snippet to PerlModule ModPerl::Rules1 <Location /mod_perl_rules1> SetHandler perl-script PerlHandler ModPerl::Rules1 </Location> Now you can issue a request to: http://localhost/mod_perl_rules1 and just as with our mod_perl rules! as the response. To test the second module <ModPerl::Rules2> add the same configuration, while replacing all 1's with 2's: PerlModule ModPerl::Rules2 <Location /mod_perl_rules2> SetHandler perl-script PerlHandler ModPerl::Rules2 </Location> And to test use the URI: http://localhost/mod_perl_rules2 Is This All I Need to Know About mod_perl?Obviously the next question you'll ask is: "Is this all I need to know about mod_perl?". The answer is: Yes and No. The Yes part:
The No part:
There are many more things to learn about mod_perl and web programming in general. In future articles I'll talk in details about all these issues. AcknowledgementsMany thanks to Eric Cholet for reviewing this article. About the Authoris an author of the mod_perl Guide and is currently co-athoring a book about mod_perl for O'Reilly and Associates, Inc together with Eric Cholet. He is a member of the Apache Software Foundation. You can find more about his works and joys at his personal web site. |
|
|
About Triggers | Media Kit | Security | Triggers | Login |
All times are recorded in UTC. Linux is a trademark of Linus Torvalds. Powered by Linux 2.4, Apache 1.3, and PHP 4 Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy. |