I would like to configure Apache 2 running on Kubuntu to execute Perl CGI scripts. I've tried some steps that I came across by googling, but nothing seems to work.
What is the right way of achieving this?
This post is intended to rescue the people who are suffering from *not being able to properly setup Apache2 for Perl on Ubuntu. (The system configurations specific to your Linux machine will be mentioned within square brackets, like [this]).
Possible outcome of an improperly setup Apache 2:
Browser trying to download the .pl file instead of executing and giving out the result.
Forbidden.
Internal server error.
If one follows the steps described below with a reasonable intelligence, he/she can get through the errors mentioned above.
Before starting the steps. Go to /etc/hosts file and add IP address / domain-name` for example:
127.0.0.1 www.BECK.com
Step 1: Install apache2
Step 2: Install mod_perl
Step 3: Configure apache2
open sites-available/default and add the following,
<Files ~ "\.(pl|cgi)$">
SetHandler perl-script
PerlResponseHandler ModPerl::PerlRun
Options +ExecCGI
PerlSendHeader On
</Files>
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
<Directory [path-to-store-your-website-files-like-.html-(perl-scripts-should-be-stored-in-cgi-bin] >
####(The Perl/CGI scripts can be stored out of the cgi-bin directory, but that's a story for another day. Let's concentrate on washing out the issue at hand)
####
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all
</Directory>
ScriptAlias /cgi-bin/ [path-where-you-want-your-.pl-and-.cgi-files]
<Directory [path-where-you-want-your-.pl-and-.cgi-files]>
AllowOverride None
Options ExecCGI -MultiViews +SymLinksIfOwnerMatch
AddHandler cgi-script .pl
Order allow,deny
allow from all
</Directory>
<Files ~ "\.(pl|cgi)$">
SetHandler perl-script
PerlResponseHandler ModPerl::PerlRun
Options +ExecCGI
PerlSendHeader On
</Files>
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
<Directory [path-to-store-your-website-files-like-.html-(perl-scripts-should-be-stored-in-cgi-bin] >
####(The Perl/CGI scripts can be stored out of the cgi-bin directory, but that's a story for another day. Let's concentrate on washing out the issue at hand)
####
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all
</Directory>
ScriptAlias /cgi-bin/ [path-where-you-want-your-.pl-and-.cgi-files]
<Directory [path-where-you-want-your-.pl-and-.cgi-files]>
AllowOverride None
Options ExecCGI -MultiViews +SymLinksIfOwnerMatch
AddHandler cgi-script .pl
Order allow,deny
allow from all
</Directory>
Step 4:
Add the following lines to your /etc/apache2/apache2.conf file.
AddHandler cgi-script .cgi .pl
<Files ~ "\.pl$">
Options +ExecCGI
</Files>
<Files ~ "\.cgi$">
Options +ExecCGI
</Files>
<IfModule mod_perl.c>
<IfModule mod_alias.c>
Alias /perl/ /home/sly/host/perl/
</IfModule>
<Location /perl>
SetHandler perl-script
PerlHandler Apache::Registry
Options +ExecCGI
</Location>
</IfModule>
<Files ~ "\.pl$">
Options +ExecCGI
</Files>
Step 5:
Very important, or at least I guess so, only after doing this step, I got it to work.
AddHandler cgi-script .cgi .pl
<Files ~ "\.pl$">
Options +ExecCGI
</Files>
<Files ~ "\.cgi$">
Options +ExecCGI
</Files>
<IfModule mod_perl.c>
<IfModule mod_alias.c>
Alias /perl/ /home/sly/host/perl/
</IfModule>
<Location /perl>
SetHandler perl-script
PerlHandler Apache::Registry
Options +ExecCGI
</Location>
</IfModule>
<Files ~ "\.pl$">
Options +ExecCGI
</Files>
Step 6
Very important, or at least I guess so, only after doing this step, I got it to work.
Add the following to you /etc/apache2/sites-enabled/000-default file
<Files ~ "\.(pl|cgi)$">
SetHandler perl-script
PerlResponseHandler ModPerl::PerlRun
Options +ExecCGI
PerlSendHeader On
</Files>
Step 7:
Now add, your Perl script as test.pl in the place where you mentioned before in step 3 as [path-where-you-want-your-.pl-and-.cgi-files].
Give permissions to the .pl file using chmod and then, type the webaddress/cgi-bin/test.pl in the address bar of the browser, there you go, you got it.
(Now, many of the things would have been redundant in this post. Kindly ignore it.)
You'll need to take a look at your Apache error log to see what the "internal server error" is. The four most likely cases, in my experience would be:
The CGI program is in a directory which does not have CGI execution enabled. Solution: Add the ExecCGI option to that directory via either httpd.conf or a .htaccess file.
Apache is only configured to run CGIs from a dedicated cgi-bin directory. Solution: Move the CGI program there or add an AddHandler cgi-script .cgi statement to httpd.conf.
The CGI program is not set as executable. Solution (assuming a *nix-type operating system): chmod +x my_prog.cgi
The CGI program is exiting without sending headers. Solution: Run the program from the command line and verify that a) it actually runs rather than dying with a compile-time error and b) it generates the correct output, which should include, at the very minimum, a Content-Type header and a blank line following the last of its headers.
(Google search brought me to this question even though I did not ask for perl)
I had a problem with running scripts (albeit bash not perl). Apache had a config of ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/ however Apache error log showed File does not exist: /var/www/cgi-bin/test.html.
Tried putting the script in both /usr/lib/cgi-bin/ and /var/www/cgi-bin/ but neither were working.
After a prolonged googling session what cracked it for me was
sudo a2enmod cgi and everything fell into place using /usr/lib/cgi-bin/.
If you have successfully installed Apache web server and Perl please follow the following steps to run cgi script using perl on ubuntu systems.
Before starting with CGI scripting it is necessary to configure apache server in such a way that it recognizes the CGI directory (where the cgi programs are saved) and allow for the execution of programs within that directory.
In Ubuntu cgi-bin directory usually resides in path /usr/lib , if not present create the cgi-bin directory using the following command.cgi-bin should be in this path itself.
mkdir /usr/lib/cgi-bin
Issue the following command to check the permission status of the directory.
ls -l /usr/lib | less
Check whether the permission looks as “drwxr-xr-x 2 root root 4096 2011-11-23 09:08 cgi- bin” if yes go to step 3.
If not issue the following command to ensure the appropriate permission for our cgi-bin directory.
sudo chmod 755 /usr/lib/cgi-bin
sudo chmod root.root /usr/lib/cgi-bin
Give execution permission to cgi-bin directory
sudo chmod 755 /usr/lib/cgi-bin
Thus your cgi-bin directory is ready to go. This is where you put all your cgi scripts for execution. Our next step is configure apache to recognize cgi-bin directory and allow execution of all programs in it as cgi scripts.
Configuring Apache to run CGI script using perl
A directive need to be added in the configuration file of apache server so it knows the presence of CGI and the location of its directories. Initially go to location of configuration file of apache and open it with your favorite text editor
cd /etc/apache2/sites-available/
sudo gedit 000-default.conf
Copy the below content to the file 000-default.conf between the line of codes “DocumentRoot /var/www/html/” and “ErrorLog $ {APACHE_LOG_DIR}/error.log”
ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
<Directory "/usr/lib/cgi-bin">
AllowOverride None
Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
Require all granted
Restart apache server with the following code
sudo service apache2 restart
Now we need to enable cgi module which is present in newer versions of ubuntu by default
sudo a2enmod cgi.load
sudo a2enmod cgid.load
At this point we can reload the apache webserver so that it reads the configuration files again.
sudo service apache2 reload
The configuration part of apache is over now let us check it with a sample cgi perl program.
Testing it out
Go to the cgi-bin directory and create a cgi file with extension .pl
cd /usr/lib/cgi-bin/
sudo gedit test.pl
Copy the following code on test.pl and save it.
#!/usr/bin/perl -w
print “Content-type: text/html\r\n\r\n”;
print “CGI working perfectly!! \n”;
Now give the test.pl execution permission.
sudo chmod 755 test.pl
Now open that file in your web browser http://Localhost/cgi-bin/test.pl
If you see the output “CGI working perfectly” you are ready to go.Now dump all your programs into the cgi-bin directory and start using them.
NB: Don't forget to give your new programs in cgi-bin, chmod 755 permissions so as to run it successfully without any internal server errors.
As of Ubuntu 12.04 (Precise Pangolin) (and perhaps a release or two before) simply installing apache2 and mod-perl via Synaptic and placing your CGI scripts in /usr/lib/cgi-bin is really all you need to do.
There are two ways to handle CGI scripts, SetHandler and AddHandler.
SetHandler cgi-script
applies to all files in a given context, no matter how they are named, even index.html or style.css.
AddHandler cgi-script .pl
is similar, but applies to files ending in .pl, in a given context. You may choose another extension or several, if you like.
Additionally, the CGI module must be loaded and Options +ExecCGI configured. To activate the module, issue
a2enmod cgi
and restart or reload Apache. Finally, the Perl CGI script must be executable. So the execute bits must be set
chmod a+x script.pl
and it should start with
#! /usr/bin/perl
as its first line.
When you use SetHandler or AddHandler (and Options +ExecCGI) outside of any directive, it is applied globally to all files. But you may restrict the context to a subset by enclosing these directives inside, e.g. Directory
<Directory /path/to/some/cgi-dir>
SetHandler cgi-script
Options +ExecCGI
</Directory>
Now SetHandler applies only to the files inside /path/to/some/cgi-dir instead of all files of the web site. Same is with AddHandler inside a Directory or Location directive, of course. It then applies to the files inside /path/to/some/cgi-dir, ending in .pl.
I'm guessing you've taken a look at mod_perl?
Have you tried the following tutorial?
EDIT: In relation to your posting - perhaps you could include a sample of the code inside your .cgi file. Perhaps even the first few lines?
For those like me who have been groping your way through much-more-than-you-need-to-know-right-now tutorials and Docs, and just want to see the thing working for starters, I found the only thing I had to do was add:
AddHandler cgi-script .pl .cgi
To my configuration file.
http://httpd.apache.org/docs/2.2/mod/mod_mime.html#addhandler
For my situation this works best as I can put my perl script anywhere I want, and just add the .pl or .cgi extension.
Dave Sherohman's answer mentions the AddHandler solution also.
Of course you still must make sure the permissions/ownership on your script are set correctly, especially that the script will be executable. Take note of who the "user" is when run from an http request - eg, www or www-data.
Related
I am new to CGI or Apache.
I am trying to install an application called as mooshak.
The installation is finishing fine. But when I open the local site, it simply downloads the file instead of executing it.
When I open http://localhost/~mooshak/cgi-bin/execute, it simply downloads the file as shown:
#!/bin/sh
# the next line restarts using tclsh \
PATH=$PATH:/usr/local/bin:/usr/contrib/bin ; exec tclsh "$0" "$#"
#-*-Mode: TCL; iso-accents-mode: t;-*-
set errorCode NONE
cd ../..
lappend auto_path packages
source .config
execute::command_line
My /etc/apache2/mods-enabled/userdir.conf file looks like this :
<IfModule mod_userdir.c>
<Directory /home/*/public_html/cgi-bin>
Options +ExecCGI -Includes -Indexes
SetHandler cgi-script
Order allow,deny
Allow from all
</Directory>
</IfModule>
What should I do ?
Try like this
<Directory /home/*/public_html>
Options -Includes -Indexes
Order allow,deny
Allow from all
</Directory>
<Directory /home/*/public_html/cgi-bin>
Options +ExecCGI -Includes -Indexes
SetHandler cgi-script
</Directory>
And give to script chmod +x
Try to use /urs/lib/cgi-bin for cgi-scripts. Every thing else will give you a haedache if you not know what to do.
The cgi-scripts need to be executable and readable for the user www-data
For Apache version >=2.2 you will need to add an entry in /etc/apache2/conf-available. My config looks like this ...
admin#suse:~$ cat /etc/apache2/conf-available/cgi-bin.conf
<IfModule mod_alias.c>
<IfModule mod_cgi.c>
Define ENABLE_USR_LIB_CGI_BIN
</IfModule>
<IfModule mod_cgid.c>
Define ENABLE_USR_LIB_CGI_BIN
</IfModule>
<IfDefine ENABLE_USR_LIB_CGI_BIN>
ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
<Directory "/usr/lib/cgi-bin">
Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
AllowOverride AuthConfig
AuthType Basic
AuthName "Suleiman's Heimat 2.0"
AuthBasicProvider file
AuthUserFile /etc/apache2/passwd
Require valid-user
</Directory>
</IfDefine>
</IfModule>
These are the default settings for Debian systems, and additional password protection.
Then you call a2enconf cgi-bin.
This has nothing to do with /etc/apache2/mods-enabled ! I cant find a reason to put cgi-code in there. Instead you will need to activate the common-gateway-interface via
a2enmod cgi
This will put a entry into /etc/apache2/mods-enabled.
Last but not least, you need to restart the server via /etc/init.d/apache2 restart
There you go.
Hope this will help.
Addition
You should run apache2ctl configtest to check your configuration, before restarting the server!
/var/log/apache2/ has also good info when something goes south.
You can also use the command systemctl status apache2.service to check the status of your service and journalctl -b -n 30 -u apache2.service to get the last 30 protocol entries.
I am trying to add a cgi-bin directory to an OpenShift project, it should be simple, but I think I am missing something.
I have created a cgi-bin directory under my /data/ directory, and did a chmod 755 on it. I moved a "hello_world.pl" file to the directory and did a chmod 755 on it. I then added a .htaccess file to that directory with these contents:
AddHandler perl-script .pl
AddHandler cgi-script .cgi
Options +ExecCGI
I then created a link to a "cgi-bin" directory in my /repo/ directory to this new cgi-bin directory with 755 permissions. Lastly, I added another .htaccess file with these contents to the repo directory:
ScriptAlias /cgi-bin/ var/lib/openshift/54fcd2cde...000015a/app-root/data/cgi-bin/
Options +ExecCGI
AddHandler cgi-script .cgi .pl
<Directory var/lib/openshift/54fcd2cde...000015a/app-root/data/cgi-bin>
SetHandler cgi-script .cgi .pl
Options +ExecCGI
Order allow,deny
Allow from all
</Directory>
I then restarted this application under OpenShift.
The first thing I am trying to do is see if I can get this hello_world.pl to execute from a browser - it won't - I get an Internal Server Error. But I can do a "perl hello_world.pl" from the command line with no problems.
Anybody have any ideas what I am missing? Thanks all.
OK, I've been having some issues with aws or something, such that I cannot seem to get mod_rewrite to work.
Just for testing purposes I've done the following:
1 used aws console to deploy fresh ami 64 bit instance from wizard
2 yum installed apache
3 edited /etc/httpd/conf/httpd.conf:
so that
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
looks like
<Directory />
Options FollowSymLinks
AllowOverride All
</Directory>
4 made sure that
LoadModule rewrite_module modules/mod_rewrite.so
is in the file and uncommented.
5 restarted apache:
sudo /sbin/service httpd restart
6 created two new files:
/var/www/html/test1.html
contains:
this is test1!
/var/www/html/test2.html
contains:
this is test2!
7 created file:
/var/www/html/.htaccess
contains (IN TOTAL):
RewriteEngine on
RewriteRule ^test1\.html$ test2.html [L]
8 went to:
http://[my aws server]/test1.html
Am getting "this is test1!"
I am doing something wrong here, but for the life of me I have no idea what. Any help is greatly appreciated...
EDIT: I added nonsense chars/numbers to the beginning of my .htaccess file, and restarted apache (not 100% sure that is needed, but what the hey...), and nothing happened. In other words, I expected that going to the url [aws server]/test1.html would result in some kind of error, but it did not. I suspect apache is not even reading the .htaccess file.
EDIT: I added the following to my httpd.conf file:
RewriteLog "/tmp/rewrite.log"
RewriteLogLevel 9
The file is created when I restart apache, but nothing ever goes in there when I go to either page I've set up. I'm failing to do something very, very basic here, but I'm not sure what...
Not sure if this is the cause of your problems, but you shouldn't mess with the
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
line, and it should be something like:
<Directory />
Options FollowSymLinks
AllowOverride None
Deny from all
</Directory>
You should add the directory of your document root as a different container:
<Directory /var/www/html/>
Options FollowSymLinks
AllowOverride All
Allow from all
</Directory>
Took me a while to find this but in some installs Apache will use multiple config files.
Look in "/etc/apache2/sites-enabled/000-default" and check that AllowOveride is set to All
Try it. This work for me.
The first, you need to make sure the .htaccess file put in correct directory.
For this, you go to sites-enabled folder and check which the .conf files are enabled.
cd /etc/apache2/sites-enabled
ls
Ex: 000-default.conf
Then, goto sites-available folder to edit that .conf file.
cd ../sites-available
sudo gedit 000-default.conf
Find to DocumentRoot and check directory again.
If you put .htaccess file in /var/www/html/.htaccess so this line look like this:
DocumentRoot /var/www/html/
The second, You need modify <Directory> block look like this.
<Directory /var/www/html>
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all
# This directive allows us to have apache2's default start page
# in /apache2-default/, but still have / go to the right place
#RedirectMatch ^/$ /apache2-default/
</Directory>
Finally, you save file and restart apache
service apache2 restart
Hope this help!
I'm working on a new server and I installed via yum the "Web Server" group. Php and mysql work fine but I can't get .htaccess to work.
Heres my test .htaccess file:
WASD_TEST_CALL_ERROR
I put this as .htaccess in a test folder along with an index.html page.
Instead of reporting an error it goes ahead and loads the index page without displaying any errors.
Thanks
Make sure AccessFileName set to .htaccess
Search httpd.conf for AccessFileName directive. It defines name of the distributed configuration file:
grep -i AccessFileName httpd.conf
Make sure users are allowed to use .htaccess file
What you can put in these files is determined by the AllowOverride directive. This directive specifies, in categories, what directives will be honored if they are found in a .htaccess file. If this directive is set to None, then .htaccess files are completely ignored. In this case, the server will not even attempt to read .htaccess files in the filesystem.
grep -i AllowOverride httpd.conf
When this directive is set to All, then any directive which has the .htaccess Context is allowed in .htaccess files:
AllowOverride All
Save and close the file. Restart httpd:
service httpd restart
Have you set AllowOverride in Apache config? If not, set AllowOverride from none to all.
On CentOS7, the following should help:
Under string AllowOverride controls what directives may be placed in .htaccess files in /etc/httpd/conf/httpd.conf:replace AllowOverride None to AllowOverride All
In /etc/httpd/conf.modules.d/00-base.conf check rewrite_module must be:
LoadModule rewrite_module modules/mod_rewrite.so
.htaccess file must be in /var/www/html/
Example content .htaccess:
AuthUserFile /var/www/.htpasswd
AuthGroupFile /dev/null
AuthName "Please Enter Password"
AuthType Basic
Require valid-user
check and restart Apache:
httpd -t
/bin/systemctl restart httpd.service
Go to /etc/httpd/conf/httpd.conf and find below line. htaccess Context is allowed in .htaccess files: AllowOverride All
Save and close the file. Restart httpd:
/etc/httpd/conf/httpd.conf:replace AllowOverride None to AllowOverride All
httpd -M | grep rewrite_module should result
rewrite_module (shared)
Still not working even after making sure all above in place? Then make sure your .htaccess has below line at the top.
RewriteEngine On
I need to disable that indexing when I enter on my root directory on a apache2 server, any tips?
Edit your apache2 configuration file which normally is on the dir: "/etc/apache2/httpd.conf".
Add the following or edit if your already have some configurations for the default web server dir (/var/www):
<Directory /var/www>
Options -Indexes
AllowOverride All
Order allow,deny
Allow from all
</Directory>
This will disable the indexing to all the public directories.
Usually done like this:
Options -Indexes
The minus means "no"...
If it's only one directory that you want to protect from viewing contents, you can also just add an index.html or index.php that will show whenever someone browses to that directory.
sudo nano /etc/apache2/apache2.conf
Located this section <Directory /var/www/> in the file
Add a minus to Indexes (Denied)
Add a plus to FollowSymLinks
Result :
<Directory /var/www/>
Options -Indexes +FollowSymLinks
AllowOverride None
Require all granted
</Directory>
Works in Raspbian
You'll get the message : "You don't have permission to access "Directory" on this server."
If you have the a2dismod utility on your distro you can remove the module entirely if you don't need directory indexes at all:
sudo a2dismod --force autoindex
Use the --force or -f flag to avoid the following warning:
WARNING: The following essential module will be disabled.
This might result in unexpected behavior and should NOT be done
unless you know exactly what you are doing!
autoindex
To continue type in the phrase 'Yes, do as I say!' or retry by passing '-f': Yes, do as I say!
Module autoindex disabled.
To activate the new configuration, you need to run:
systemctl restart apache2
Here are the docs for mod_autoindex
Make sure that you also add -Indexes to the config files in your sites-enabled (or sites-available as it was in my case) directory, they're usually inside the "/etc/apache2/" directory.