Install mod_perl as a non-superuser

Source: Internet
Author: User
Tags perl script
Install mod_perl as a non-superuser-Linux Enterprise Application-Linux server application. For more information, see the following. As you can see in previous articles, mod_perl nbsp; makes Apache composed of two components: nbsp; Perl nbsp; module and nbsp; Apache nbsp; itself. Although it is easy to install Apache with non-superuser permissions, you should know how to install the Perl module in a non-system location. In this article, I will demonstrate several methods to implement this task.
In these examples, I will use stas as the user name and/home/stas as the user's private directory.

Install the Perl module to the selected directory

Since Super User Permissions are not required, you are not allowed to install it to the system directory, such as/usr/lib/perl5. You need to find out how to install these modules in your private directory. This is easy. Nbsp;

First, you need to decide where to install these modules. The simplest way is to simulate the transplantation of the Perl-related root directory file system in your private directory. In fact, we only need two directories: nbsp;


Since they will be automatically created when the first module is installed, we do not have to create them. 99% of the files will be copied to the lib directory. Sometimes some modules will be copied to the bin directory when the Perl script is released. If the directory does not exist, it will be created.
Let's install the CGI. pm package, which contains some other CGI: * module. Download the package from the CPAN library, unbind the package, and enter the new directory.

Now, make a standard perl file Makefile. PL to prepare the Makefile. But at this time, let nbsp; MakeMaker nbsp; Use Your Perl installation directory instead of the default one. Nbsp;

% Nbsp; perl nbsp; Makefile. PL nbsp; PREFIX =/home/stas

PREFIX =/home/stas nbsp; is only a different part of the installation process. Nbsp; Note: if you do not care how MakeMaker selects the remaining directory, or if you are using an old version that needs to explicitly specify all target directories, do this: nbsp;
% Nbsp; perl nbsp; Makefile. PL nbsp; PREFIX =/home/stas nbsp;
INSTALLPRIVLIB =/home/stas/lib/perl5 nbsp;
INSTALLSCRIPT =/home/stas/bin nbsp;
INSTALLSITELIB =/home/stas/lib/perl5/site_perl nbsp;
INSTALLBIN =/home/stas/bin nbsp;
INSTALLMAN1DIR =/home/stas/lib/perl5/man nbsp;
INSTALLMAN3DIR =/home/stas/lib/perl5/man3

The rest is normally the same: nbsp;
% Nbsp; make
% Nbsp; make nbsp; test
% Nbsp; make nbsp; install

Make nbsp; install all files in the private directory. Note that all lost directories are automatically created, so there is no need to create them.
This is what we did (slightly changed): nbsp;

Installing nbsp;/home/stas/lib/perl5/CGI/Cookie. pm
Installing nbsp;/home/stas/lib/perl5/CGI. pm
Installing nbsp;/home/stas/lib/perl5/man3/CGI.3
Installing nbsp;/home/stas/lib/perl5/man3/CGI: Cookie.3
Writing nbsp;/home/stas/lib/perl5/auto/CGI/. packlist
Appending installation information to nbsp;/home/stas/lib/perl5/perllocal. pod

If you have to use explicit target parameters, you do not need a single PREFIX parameter. You will find it useful to create a called file.
For example, nbsp;/. perl_dirs nbsp; (here nbsp; in our example, nbsp;/home/stas) nbsp; contains: nbsp;

PREFIX =/home/stas nbsp;
INSTALLPRIVLIB =/home/stas/lib/perl5 nbsp;
INSTALLSCRIPT =/home/stas/bin nbsp;
INSTALLSITELIB =/home/stas/lib/perl5/site_perl nbsp;
INSTALLBIN =/home/stas/bin nbsp;
INSTALLMAN1DIR =/home/stas/lib/perl5/man nbsp;
INSTALLMAN3DIR =/home/stas/lib/perl5/man3

From now on, if you want to install the Perl module locally, You can execute: nbsp;
% Nbsp; perl nbsp; Makefile. PL nbsp; 'cat nbsp;/. perl_dirs'
% Nbsp; make
% Nbsp; make nbsp; test
% Nbsp; make nbsp; install

With this method, you can easily store different Perl modules. For example, you can use one for production nbsp; Perl and the other for development. Nbsp;
% Nbsp; perl nbsp; Makefile. PL nbsp; 'cat nbsp;/. perl_dirs.production'

Or nbsp;
% Nbsp; perl nbsp; Makefile. PL nbsp; 'cat nbsp ;~ /. Perl_dirs.develop'

Write your own script to find the locally installed Module
These Perl modules are usually placed in four main directories. To find these directories, run: nbsp;

% Nbsp; perl nbsp;-V

Output contains important perl installation information. Finally, you will see: nbsp;
Characteristics nbsp; of nbsp; this nbsp; binary nbsp; (from nbsp; libperl ):
Built nbsp; under nbsp; linux
Compiled nbsp; at nbsp; Apr nbsp; 6 nbsp; 1999 nbsp; 23:34:07
@ INC:

This shows us the contents of Perl special variable @ INC, which is usually used by Perl to find modules. It is equivalent to the PATH environment variable in Unix nbsp; shells and is used to find executable programs.
Note that Perl is also in the. Directory (representing the current directory) Search module, which is the last entry path in the above output directory. Of course, this example is from perl and nbsp of MySQL 5.00503. It is installed in Linux of my x86 PC. That's why you see i386-linux and 5.00503. nbsp; if your system runs a different version of perl, operating system, processor or chip structure, then some directories have different names.

I also installed perl nbsp; 5.6.1 to nbsp;/usr/local/lib/nbsp;, so when I do this: nbsp;

% Nbsp;/usr/local/bin/perl5.6.1 nbsp;-V

You will see: nbsp;
@ INC:

Note that this is still Linux, nbsp; but the newer perl version uses the Pentium processor (so it is i586 rather than i386 ). This is useful for compilation optimization of the Pentium processor when the binary Perl extension is created.
All files specified by the platform, such as compiled C files that are bonded with Perl With XS or SWIG, should enter a directory similar to the i386-linux.

Key point: when we have installed the Perl module into a non-standard directory, we have to let Perl know where to search for these four directories. There are two implementation methods: You can set the PERL5LIB nbsp; environment variable or you can modify the @ INC variable in your script.

Suppose we use Perl nbsp; 5.00503. In our example, the directory is as follows: nbsp;


As mentioned above, you can find the exact directory through perl nbsp;-V and replace the global perl installation base directory with your own private directory.
It is easy to modify nbsp; @ INC nbsp. The best way is to use the lib module (compilation instructions), nbsp; by adding the following code snippets at the top of your script, these codes need to be localized to the installed modules:

Use nbsp; lib nbsp; qw (/home/stas/lib/perl5/5.00503/
/Home/stas/lib/perl5/site_perl/5.005 );

Another way is to explicitly modify @ INC: nbsp;
BEGIN nbsp ;{
Unshift nbsp; @ INC,
Qw (/home/stas/lib/perl5/5.00503
/Home/stas/lib/perl5/site_perl/5.005/i386-linux );

Note that if they exist, they will be automatically added (specifically, when $ dir/$ archname/auto exists), we do not need to list consistent structures to specify directories using the lib module.
Note that both methods take into account the directories searched in @ INC in advance. This allows you to install an updated module and add it to your local database. Perl will use this module to replace the old module that has been installed in the system library.

Both methods modify the value of nbsp; @ INC nbsp; during compilation. This lib module also uses the BEGIN block, but it is internal.

Now let me assume the following scenario. I have installed the nbsp; LWP nbsp; package in my local library. nbsp; now I want to install another module (such as nbsp; mod_perl) nbsp;, which has the nbsp; LWP listed in its preferred list. I know that I have installed nbsp; LWP nbsp;, but when I run perl nbsp; Makefile. pl to prepare the installation module, I am notified that I have not installed LWP.

Perl has no way to know that we have locally installed modules. All of these are listed in the @ INC Directory Search. If there are only four default directories (with the nbsp;. Directory), it is impossible to find the locally installed nbsp; LWP nbsp; Package. We can't solve this problem by adding code modification @ INC, but by changing the PERL5LIB environment variable. If you are using tcsh for interaction, do this: nbsp;

Setenv nbsp; PERL5LIB nbsp;/home/stas/lib/perl5/5.00503:

It should be a single line with a directory, and the directory is separated by colons (:) and non-spaces. If you are a bash user, do this: nbsp;
Export nbsp; PERL5LIB =/home/stas/lib/perl3/5.00503:

Similarly, it is written as a single row. If bash is used, you can use the reverse oblique rod () to input multiple lines of commands, such as: nbsp;
Export nbsp; PERL5LIB =/home/stas/lib/perl3/5.00503:

Because lib is used, Perl automatically considers the structure to specify the directory to nbsp; @ INC nbsp ;.
When we do this, we need to execute perl nbsp;-V as before to confirm the value of @ INC's new configuration. You should see nbsp; @ INC nbsp; modified value: nbsp;

% Nbsp; perl nbsp;-V

Characteristics nbsp; of nbsp; this nbsp; binary nbsp; (from nbsp; libperl): nbsp;
Built nbsp; under nbsp; linux
Compiled nbsp; at nbsp; Apr nbsp; 6 nbsp; 1999 nbsp; 23:34:07
% ENV:
PERL5LIB = "/home/stas/lib/perl5/5.00503:
/Home/stas/lib/perl5/site_perl/5.005 "nbsp;
@ INC:

Add these commands to nbsp when everything works as you wish ;. tcshrc nbsp; or nbsp ;. bashrc nbsp; file. nbsp; when you start the shell next time, the environment will prepare for you to work with the new Perl.
Note: If you have set nbsp; PERL5LIB nbsp; and nbsp;, you do not have to change the value of nbsp; @ INC nbsp; in your script. However, if nbsp; for example, nbsp; other people nbsp; (people who do not want to set in nbsp; shell) want to execute your script, perl cannot find the locally installed module. The best example is the nbsp; crontab nbsp; script, which can be used in different SHELL environments. Therefore, PERL5LIB cannot be set.

The best way is to set the PERL5LIB environment variable and modify the nbsp; @ INC nbsp; Extension code at the beginning of the script, as described above.

CPAN. pm nbsp; Shell nbsp; and nbsp; Local installation Module

CPAN. pm nbsp; shell nbsp; saves a lot of time when processing perl module installation and keeping updates. It does this for us, despite checking the lost modules in the preferred list, taking them out and installing them. So you will want to know if you can use nbsp; CPAN. pm nbsp; To keep your local database.

When you start the nbsp; CPAN nbsp; Interactive Shell, nbsp; first searches for the user's private configuration file and system file. When I am logged as a stas user, the two files installed will be: nbsp;

/Home/stas/. cpan/CPAN/MyConfig. pm
/Usr/lib/perl5/5.00503/CPAN/Config. pm

If your system does not have the nbsp; CPAN nbsp; Shell, when you start the shell for the first time, it will ask you a series of questions and then create the Config. pm file for you.
If you already have a system configuration, You should have/usr/lib/perl5/5.00503/CPAN/Config. pm. nbsp; if you have different versions of Perl, use your Perl version number to modify the path when you are looking for a file. Create a directory (mkdir nbsp;-p nbsp; create the entire path immediately) where the local configuration file will be placed: nbsp;

% Nbsp; mkdir nbsp;-p nbsp;/home/stas/. cpan/CPAN

Now, copy the configuration file of the HA system to your local device. Nbsp;
% Nbsp; cp nbsp;/usr/lib/perl5/5.00503/CPAN/Config. pm nbsp;
/Home/stas/. cpan/CPAN/MyConfig. pm

The only thing left behind is to change the nbsp;. cpan nbsp; base directory to your private directory in your local file. In my machine, I use nbsp;/home/stas nbsp; instead of nbsp;/usr/src /. cpan nbsp; (this is my nbsp ;. cpan nbsp; system directory ). Of course I use perl! Nbsp;
% Nbsp; perl nbsp;-pi nbsp;-e nbsp;'s |/usr/src |/home/stas | 'nbsp;
/Home/stas/. cpan/CPAN/MyConfig. pm

Now you have a local configuration file. When running nbsp; perl nbsp; Makefile. PL nbsp;, you can tell it what parameters must be passed.
Open the file in your favorite editor and replace the downstream: nbsp;

'Makepl _ arg 'nbsp; => nbsp; q [],

Adopt: nbsp;
'Makepl _ arg 'nbsp; => nbsp; q [PREFIX =/home/stas],

Now you have completed the configuration. Assume that you log on as the same user and have prepared local installation (in our example, nbsp; stas), and start like this: nbsp;
% Nbsp; perl nbsp;-MCPAN nbsp;-e nbsp; shell

From now on, any module you want to install will be installed locally. If you have to install some system modules, become a Super User and install them in the same way. When you log on as a Super User, the system configuration file will be used instead of local.
If you have used more than one PREFIX variable, then modify nbsp; MyConfig. pm nbsp; To use them. nbsp; for example, if you use these variables: nbsp;

Perl nbsp; Makefile. PL nbsp; PREFIX =/home/stas nbsp;
INSTALLPRIVLIB =/home/stas/lib/perl5 nbsp;
INSTALLSCRIPT =/home/stas/bin nbsp;
INSTALLSITELIB =/home/stas/lib/perl5/site_perl nbsp;
INSTALLBIN =/home/stas/bin nbsp;
INSTALLMAN1DIR =/home/stas/lib/perl5/man nbsp;
INSTALLMAN3DIR =/home/stas/lib/perl5/man3

Replace nbsp with all the above variables in the following line; PREFIX =/home/stas nbsp;: nbsp;
'Makepl _ arg 'nbsp; => nbsp; q [PREFIX =/home/stas] nbsp;

So this line is changed to: nbsp;
'Makepl _ arg 'nbsp; => nbsp; q [PREFIX =/home/stas nbsp;
INSTALLPRIVLIB =/home/stas/lib/perl5 nbsp;
INSTALLSCRIPT =/home/stas/bin nbsp;
INSTALLSITELIB =/home/stas/lib/perl5/site_perl nbsp;
INSTALLBIN =/home/stas/bin nbsp;
INSTALLMAN1DIR =/home/stas/lib/perl5/man nbsp;
INSTALLMAN3DIR =/home/stas/lib/perl5/man3],

If you can place all the above parameters in a row, you can remove the backslb nbsp ;().
Install native nbsp; Apache

Like the nbsp; Perl nbsp; module, nbsp; if you do not have the permission to install files in the system area, You have to install them locally to your private directory. This is almost the same as simple installation, but you have to run a server with a listening port number greater than 1024, because only the root process can listen on a port number less than 1024.

Another important problem you must solve is how to write the startup and shutdown scripts into the directory as a system service. You will have to ask your system administrator for help.

To install Apache locally, nbsp; all you need to do is to tell the. configure file in the Apache source code directory to use those target directories. If you follow my conventions to make your private directory look like nbsp;/nbsp; root directory, the expected parameter will be: nbsp;

./Configure-prefix =/home/stas

Apache nbsp; replaces the remaining part of the target directory with a prefix instead of the default nbsp;/usr/local/apache. nbsp; if you want to know what they are, add-show-layout nbsp before running; Option: nbsp;
./Configure-prefix =/home/stas-show-layout

You may want to put all Apache files under nbsp;/home/stas/Apache nbsp; according to apache Conventions: nbsp;
./Configure-prefix =/home/stas/apache

If you want to modify the names of some or all automatically created directories: nbsp;
./Configure-prefix =/home/stas/apache nbsp;
-Sbindir =/home/stas/apache/sbin
-Sysconfdir =/home/stas/apache/etc
-Localstatedir =/home/stas/apache/var nbsp;
-Runtimedir =/home/stas/apache/var/run nbsp;
-Logfiledir =/home/stas/apache/var/logs nbsp;
-Proxycachedir =/home/stas/apache/var/proxy

That's all!
Remember that you can only run this script under your users and user groups. Nbsp; you must set an appropriate value in httpd. conf for users and group items.

Manually install the local nbsp; mod_perl nbsp; Enable nbsp; Apache

Now that we know how to install the Apache and Perl modules separately, let's take a look at how to install mod_perl in our private directory to enable Apache. This is almost as simple as separate installation, but there is a problem you need to know, which will be mentioned at the end of this section.

Assume that you unpack the nbsp; Apache nbsp; and nbsp; mod_perl nbsp; source code to the nbsp;/home/stas/src nbsp; directory, just like this: nbsp;

% Nbsp; ls nbsp;/home/stas/src

The nbsp; x. xx nbsp; here is the same version number as before. You want to obtain the Perl module and nbsp from the nbsp;/home/stas/lib/perl5 nbsp; mod_perl nbsp; Package; /home/stas/apache nbsp; the nbsp; Apache nbsp; file in the directory. The following command is used: nbsp;
% Nbsp; perl nbsp; Makefile. PL nbsp;
PREFIX =/home/stas nbsp;
APACHE_PREFIX =/home/stas/apache nbsp;
APACHE_SRC = ../apache_x.x.x/src nbsp;
DO_HTTPD = 1 nbsp;
USE_APACI = 1 nbsp;
% Nbsp; make nbsp; & nbsp; make nbsp; test nbsp; & nbsp; make nbsp; install nbsp;
% Nbsp; cd nbsp; ../apache_x.x.x
% Nbsp; make nbsp; install

If you need to pass some parameters to nbsp ;. configure nbsp; script, nbsp; as we can see in previous chapters, we should use nbsp; APACI_ARGS. nbsp; example: nbsp;
APACI_ARGS = '-sbindir =/home/stas/apache/sbin, nbsp;
-Sysconfdir =/home/stas/apache/etc, nbsp;
-Localstatedir =/home/stas/apache/var, nbsp;
-Runtimedir =/home/stas/apache/var/run, nbsp;
-Logfiledir =/home/stas/apache/var/logs, nbsp;
-Proxycachedir =/home/stas/apache/var/proxy'

Note that the preceding multiple rows only work in bash. The nbsp; tcsh user will have to list all parameters in the same row.
Basically, the installation is complete. The only problem left behind is the nbsp; @ INC nbsp; variable. If you depend on the nbsp; PERL5LIB nbsp; environment variable, @ INC nbsp; will not be correctly set unless you explicitly set it in your startup file. The Startup file must be started before loading any of your local library modules. A better way, as we have seen before, is to use lib to compile instructions, but a little different: using this method in the startup file will affect the execution of all code processed in mod_perl. For example: nbsp; PerlRequire nbsp;/home/stas/apache/perl/startup. pl nbsp; startup. pl nbsp;

Use nbsp; lib nbsp; qw (/home/stas/lib/perl5/5.00503/
/Home/stas/lib/perl5/site_perl/5.005 );

Note that you can still use the hardcoded nbsp; @ INC nbsp; modification in the script, nbsp; but realize that the script modifies the nbsp; @ INC, nbsp; In the BEGIN block, while the mod_perl nbsp; execute the nbsp; BEGIN block only when the script is compiled. As a result, @ INC nbsp; will be reset by the original value after compilation, and the hard encoding settings will be forgotten.
There is only one place where you can change the "original" value in the startup file or put nbsp;

PerlSetEnv nbsp; Perl5LIB nbsp;

To the nbsp; httpd. conf server configuration phase, nbsp; but the latter settings will be ignored if you use nbsp; PerlTanintcheck nbsp. I hope you can use it like this.
The next mod_perl nbsp; configuration and usage are the same, as if you are installing mod_perl as a Super User.

Use nbsp; CPAN. pm nbsp; install nbsp locally; mod_perl nbsp; Enable nbsp; Apache nbsp; suppose you have configured nbsp; CPAN as described earlier. pm nbsp; to install the Perl module, so the installation is simple.

Start nbsp; CPAN. pm nbsp; shell, nbsp; set parameters to pass to nbsp; perl nbsp; Makefile. PL nbsp; (modify the example settings to suit your needs), nbsp; then tell nbsp; do the rest for you: nbsp;

% Nbsp; perl nbsp;-MCPAN nbsp;-eshell
Cpan> nbsp; o nbsp; conf nbsp; makepl_arg nbsp; 'do _ HTTPD = 1 nbsp; USE_APACI = 1 nbsp; EVERYTHING = 1 nbsp;
PREFIX =/home/stas nbsp; APACHE_PREFIX =/home/stas/apache'
Cpan> nbsp; install nbsp; mod_perl

When you use nbsp; CPAN. pm nbsp; for local installation, you must confirm whether the nbsp; makepl_arg nbsp; is returned to the original value after the installation is complete. The simplest way is to exit the Interactive Shell by entering nbsp; quit and then re-input. But if you want to stick to it, the following will take effect if you do not exit the Shell. nbsp; you really want to skip this :)
If you want to continue working without exiting Shell with CPAN, you must:

1) Remember the value of nbsp; makepl_arg nbsp;

2) Change it to suit your new installation

3) Build and install nbsp; mod_perl

4) install nbsp; mod_perl nbsp; and then restore it.

This is a very troublesome task, but I believe that nbsp; CPAN. pm nbsp; will eventually be improved and easier to handle.

So if you are still with me, start nbsp; Shell: nbsp; as usual;

% Nbsp; perl nbsp;-MCPAN nbsp;-eshell

First, read the value of nbsp; makepl_arg nbsp;: nbsp;
Cpan> nbsp; o nbsp; conf nbsp; makepl_arg
PREFIX =/home/stas

If you configure CPAN. pm to locally install the module, it will be a string like nbsp; PREFIX =/home/stas nbsp. Save this value: nbsp;
Cpan> nbsp; o nbsp; conf nbsp; nbsp; PREFIX =/home/stas

Second, set a new value, nbsp; mod_perl nbsp; which will be used by the installation process. (You can add parameters to or delete parameters as needed) nbsp;
Cpan> nbsp; o nbsp; conf nbsp; makepl_arg nbsp; 'do _ HTTPD = 1 nbsp; USE_APACI = 1 nbsp; EVERYTHING = 1 nbsp;
PREFIX =/home/stas nbsp; APACHE_PREFIX =/home/stas/apache'

Third, ask nbsp; to build and install nbsp for you; mod_perl: nbsp;
Cpan> nbsp; install nbsp; mod_perl

Fourth, nbsp; reset makepl_arg to the original value. We can print these saved variable values and assign them to nbsp; makepl_arg to implement. nbsp;
Cpan> nbsp; o nbsp; conf nbsp;
PREFIX =/home/stas
Cpan> nbsp; o nbsp; conf nbsp; makepl_arg nbsp; PREFIX =/home/stas

It is not very concise, but it is an effective method. You can write this value on a piece of paper instead of saving it in nbsp;, nbsp; but you are more likely to make mistakes. Nbsp; reference
Apache nbsp Web site: nbsp;

Mod_perl nbsp; website URL: nbsp; nbsp;

CPAN nbsp; Is nbsp; Perl nbsp; the abbreviation of integrated collection Network (Comprehensive nbsp; Perl nbsp; Archive nbsp; Network). nbsp; the main site URL is nbsp;

In the world, CPAN nbsp; has multiple image sites: nbsp; 100 nbsp.
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.