Adding a cgi-bin directory to OpenShift? - .htaccess

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.

Related

Apache is downloading perl files instead of showing them

I'm using apache 2.4.6 and cent-os 7.3
I don't know why but when I'm trying to display perl file my browser is trying to download it instead of showing it.
I have perl installed on my server and I can run perl files.
In /etc/apache2/apache2.conf:
ScriptAlias /cgi-bin/ "/path/to/cgi-bin/" and that your scripts are in that folder.
If your scripts ends on .pl make sure to change the line
AddHandler cgi-script .cgi to AddHandler cgi-script .cgi .pl
in /etc/apache2/apache2.conf and check that it is not commented out.
Options FollowSymLinks +ExecCGI --> needs to set in your virtual host

Execute CGI file without extension

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.

.htaccess file refuses to load

I'm trying to set my Debian server to look for .htaccess files, but no matter what I do, nothing works!
I have already tried editing my /etc/apache2/sites-available/default file and setting AllowOverride All for my /var/www directory, and also checked if "default" was enabled...
I've also inserted into my apache2.conf file the following:
AccessFileName .htaccess
<Directory "/var/www">
AllowOverride All
</Directory>
For some reason it still doesn't work. This is my .htaccess file (tested to work in XAMPP on Windows)
php_value upload_max_filesize 20M
php_value post_max_size 20M
php_value max_execution_time 200
php_value max_input_time 200
RewriteEngine off
DefaultType application/x-httpd-php
Options +MultiViews
Maybe you're not hitting the right Directory.
AllowOverride All should do the trick.
When 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.
When this directive is set to All, then any directive which has the
.htaccess Context is allowed in .htaccess files.
The directive-type can be one of the following groupings of
directives.
Don't forget to restart your apache after editing your apache configuration, check your file permission. You can also tail your error log.
Also make sure you have the mod_php module installed as you're using the php_value command

Centos htaccess not being read

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

How do I configure Apache 2 to run Perl CGI scripts?

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.

Resources