LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices


Reply
  Search this Thread
Old 03-13-2017, 05:54 PM   #1
chtsalid
Member
 
Registered: Jan 2017
Posts: 67

Rep: Reputation: Disabled
CentOS 7 / Apache / Run a CGI Script


Hi,

I am trying to run a basic perl script on my apache but I am receiving the following error message.

cat /var/log/httpd/test1.example.com-error_log
[Mon Mar 13 23:48:19.580442 2017] [cgi:error] [pid 2987] [client 192.168.122.2:60634] AH01215: (2)No such file or directory: exec of '/cubs/test1.example.com/good.pl' failed
[Mon Mar 13 23:48:19.580476 2017] [cgi:error] [pid 2987] [client 192.168.122.2:60634] End of script output before headers: good.pl


Here is my configuration

[root@rh2 ~]# cat /etc/httpd/conf.d/test1.example.com.conf
<Directory /cubs/test1.example.com/>
Allow from 192.168.122.0/24
AuthName "Private"
AuthType Basic
AuthUserFile /usr/local/apache/passwd/passwords
Require valid-user
Options +ExecCGI
AddHandler cgi-script .cgi .pl
</Directory>
<VirtualHost *:80>
LoadModule cgi_module modules/mod_cgi.so
ServerAdmin webmaster@test1.example.com
DocumentRoot /cubs/test1.example.com/
ServerName test1.example.com
ErrorLog logs/test1.example.com-error_log
TransferLog logs/test1.example.com-access_log
ScriptAlias /cgi-bin/ "/cubs/test1.example.com/"
</VirtualHost>


[root@rh2 ~]# cd /cubs
[root@rh2 cubs]# ls
test1.example.com
[root@rh2 cubs]# cd test1.example.com/
[root@rh2 test1.example.com]# ls
good.pl index.html
[root@rh2 test1.example.com]# ls -lZ
-rwxr-xr-x. root root unconfined_ubject_r:httpd_sys_script_exec_t:s0 good.pl
-rw-r--r--. root root unconfined_ubject_r:httpd_sys_content_t:s0 index.html
[root@rh2 test1.example.com]#


[root@rh2 test1.example.com]# semanage boolean -l|grep httpd_enable_cgi
httpd_enable_cgi (on , on) Allow httpd to enable cgi


Many thanks!
 
Old 03-13-2017, 09:13 PM   #2
norobro
Member
 
Registered: Feb 2006
Distribution: Debian Sid
Posts: 792

Rep: Reputation: 331Reputation: 331Reputation: 331Reputation: 331
Quote:
Originally Posted by chtsalid View Post
[Mon Mar 13 23:48:19.580476 2017] [cgi:error] [pid 2987] [client 192.168.122.2:60634] End of script output before headers: good.pl
Give this a read: https://httpd.apache.org/docs/2.4/ho...i.html#writing

And please use code tags.
 
1 members found this post helpful.
Old 03-14-2017, 04:03 AM   #3
chtsalid
Member
 
Registered: Jan 2017
Posts: 67

Original Poster
Rep: Reputation: Disabled
Hi,

I have done some corrections

ScriptAlias /cgi-bin/ "/cubs/test1.example.com/"
<Directory "/cubs/test1.example.com/">
Allow from 192.168.122.0/24
AuthName "Private"
AuthType Basic
AuthUserFile /usr/local/apache/passwd/passwords
Require valid-user
Options +ExecCGI
AddHandler cgi-script .cgi .pl
</Directory>
<VirtualHost *:80>
ServerAdmin webmaster@test1.example.com
DocumentRoot /cubs/test1.example.com/
ServerName test1.example.com
ErrorLog logs/test1.example.com-error_log
TransferLog logs/test1.example.com-access_log
</VirtualHost>

[root@rh2 ~]# cat /cubs/test1.example.com/good.pl
#!/usr/bin/perl
print "Content-type: text/html\n\n";
print "Good Job!\n";


[root@rh2 ~]# cd /cubs/test1.example.com/
[root@rh2 test1.example.com]# ls -lZ
-rwxr-xr-x. root root unconfined_ubject_r:httpd_sys_script_exec_t:s0 good.pl
-rw-r--r--. root root unconfined_ubject_r:httpd_sys_content_t:s0 index.html


[Tue Mar 14 10:03:10.401992 2017] [cgi:error] [pid 3464] [client 192.168.122.2:60802] AH01215: (2)No such file or directory: exec of '/cubs/test1.example.com/good.pl' failed
[Tue Mar 14 10:03:10.402029 2017] [cgi:error] [pid 3464] [client 192.168.122.2:60802] End of script output before headers: good.pl


Any idea?
Many thanks!
 
Old 03-14-2017, 08:05 PM   #4
norobro
Member
 
Registered: Feb 2006
Distribution: Debian Sid
Posts: 792

Rep: Reputation: 331Reputation: 331Reputation: 331Reputation: 331
On Debian, apache 2.4 out of the box ignores the /etc/apache2/conf.d directory. Config files go in conf-available/ and a symlink to each config file in use is put in conf-enabled/. If your server is set up that way try putting "test1.example.com.conf" in conf-available/ and a symlink to it in conf-enabled/.

The deets here: https://www.digitalocean.com/communi...tation-details

Hope this helps.
 
Old 03-14-2017, 08:23 PM   #5
wpeckham
Senior Member
 
Registered: Apr 2010
Location: Continental USA
Distribution: Debian, Ubuntu, Fedora, RedHat, DSL, Puppy, CentOS, Knoppix, Mint-DE, Sparky, Vsido, tinycore, Q4OS
Posts: 3,432

Rep: Reputation: 1496Reputation: 1496Reputation: 1496Reputation: 1496Reputation: 1496Reputation: 1496Reputation: 1496Reputation: 1496Reputation: 1496Reputation: 1496
Quote:
Originally Posted by norobro View Post
On Debian, apache 2.4 out of the box ignores the /etc/apache2/conf.d directory. Config files go in conf-available/ and a symlink to each config file in use is put in conf-enabled/. If your server is set up that way try putting "test1.example.com.conf" in conf-available/ and a symlink to it in conf-enabled/.

The deets here: https://www.digitalocean.com/communi...tation-details

Hope this helps.
An alternative (NOT recommended) is to either compile APACHE HTTP from sources and configure it yourself, or use a different web server. If you are going to use the packages for your distribution (recommended) then you need to pay attention to the distribution documentation (both online, and man/info pages) supplied by the package maintainers. These will solve a LOT of problems and point you in the right direction for our install.
 
Old 03-15-2017, 04:44 AM   #6
chtsalid
Member
 
Registered: Jan 2017
Posts: 67

Original Poster
Rep: Reputation: Disabled
Hi,

conf files in conf.d are included. This option in configured by default in /etc/httpd/conf/httpd.conf.

IncludeOptional conf.d/*.conf

Any other idea, were the problem might be?

Thanks!
 
Old 03-15-2017, 06:11 AM   #7
bathory
LQ Guru
 
Registered: Jun 2004
Location: Piraeus
Distribution: Slackware
Posts: 12,739

Rep: Reputation: 1838Reputation: 1838Reputation: 1838Reputation: 1838Reputation: 1838Reputation: 1838Reputation: 1838Reputation: 1838Reputation: 1838Reputation: 1838Reputation: 1838
Hi,
Quote:
[Tue Mar 14 10:03:10.401992 2017] [cgi:error] [pid 3464] [client 192.168.122.2:60802] AH01215: (2)No such file or directory: exec of '/cubs/test1.example.com/good.pl' failed
[Tue Mar 14 10:03:10.402029 2017] [cgi:error] [pid 3464] [client 192.168.122.2:60802] End of script output before headers: good.pl
Open good.pl in vi/vim and look for not printing characters, especially if you've ftp'd the script from windows.

Another thing to try, is to disable SELinux temporarily and see if it works.
 
Old 03-15-2017, 06:26 AM   #8
chtsalid
Member
 
Registered: Jan 2017
Posts: 67

Original Poster
Rep: Reputation: Disabled
Hi,

thanks for your help! I did setenforce 0, but problem remains. Here is my .pl file

[root@rh2 ~]# cat /cubs/test1.example.com/good.pl
#!/usr/bin/perl
print "Content-type: text/html\n\n";
print "Good Job!\n";

Can you see something sth. to be wrong?

Regards
 
Old 03-15-2017, 06:38 AM   #9
bathory
LQ Guru
 
Registered: Jun 2004
Location: Piraeus
Distribution: Slackware
Posts: 12,739

Rep: Reputation: 1838Reputation: 1838Reputation: 1838Reputation: 1838Reputation: 1838Reputation: 1838Reputation: 1838Reputation: 1838Reputation: 1838Reputation: 1838Reputation: 1838
Quote:
Originally Posted by chtsalid View Post
Hi,

thanks for your help! I did setenforce 0, but problem remains. Here is my .pl file

[root@rh2 ~]# cat /cubs/test1.example.com/good.pl
#!/usr/bin/perl
print "Content-type: text/html\n\n";
print "Good Job!\n";

Can you see something sth. to be wrong?

Regards
I've seen the script and in fact I've copied to my apache and it worked.
That's why I've asked you to check for non printing/control characters. Or you can delete the file good.pl and recreate it using your favorite linux test editor.
 
Old 03-15-2017, 07:42 AM   #10
chtsalid
Member
 
Registered: Jan 2017
Posts: 67

Original Poster
Rep: Reputation: Disabled
Hi,

the http://test1.example.com/good.pl works fine! But the http://test1.example.com/cgi-bin/good.pl not.

Could it be a problem with my ScriptAlias?

<Directory /cubs/test1.example.com>
Allow from 192.168.122.0/24
AuthName "Private"
AuthType Basic
AuthUserFile /usr/local/apache/passwd/passwords
Require valid-user
</Directory>
<VirtualHost *:80>
LoadModule cgi_module modules/mod_cgi.so
ServerAdmin webmaster@test1.example.com
DocumentRoot /cubs/test1.example.com/
ServerName test1.example.com
ErrorLog logs/test1.example.com-error_log
TransferLog logs/test1.example.com-access_log
ScriptAlias /cgi-bin/ /cubs/test1.example.com/
</VirtualHost>
 
Old 03-15-2017, 08:43 AM   #11
bathory
LQ Guru
 
Registered: Jun 2004
Location: Piraeus
Distribution: Slackware
Posts: 12,739

Rep: Reputation: 1838Reputation: 1838Reputation: 1838Reputation: 1838Reputation: 1838Reputation: 1838Reputation: 1838Reputation: 1838Reputation: 1838Reputation: 1838Reputation: 1838
Quote:
the http://test1.example.com/good.pl works fine! But the http://test1.example.com/cgi-bin/good.pl not.

Could it be a problem with my ScriptAlias?
Please define what you mean by "http://test1.example.com/cgi-bin/good.pl not". Because the error you posted at your OP has nothing to do with the ScriptAlias directory. It's a script code error.

Anyways, it's a bad practice to use the same directory for both docroot and cgi-bin. From apache documentation
Quote:
It is safer to avoid placing CGI scripts under the DocumentRoot in order to avoid accidentally revealing their source code if the configuration is ever changed. The ScriptAlias makes this easy by mapping a URL and designating CGI scripts at the same time. If you do choose to place your CGI scripts in a directory already accessible from the web, do not use ScriptAlias. Instead, use <Directory>, SetHandler, and Options as in:

<Directory "/usr/local/apache2/htdocs/cgi-bin">
SetHandler cgi-script
Options ExecCGI
</Directory>

This is necessary since multiple URL-paths can map to the same filesystem location, potentially bypassing the ScriptAlias and revealing the source code of the CGI scripts if they are not restricted by a Directory section.
 
Old 03-17-2017, 01:19 AM   #12
c0wb0y
Member
 
Registered: Jan 2012
Location: Inside the oven
Distribution: Windows
Posts: 417

Rep: Reputation: 74
I suspect permission/ownership/SELinux context is not right. Can you post the output of:

Code:
ls -lZd /cubs
ls -lZd /cubs/test1.example.com
 
Old 03-17-2017, 02:52 AM   #13
bathory
LQ Guru
 
Registered: Jun 2004
Location: Piraeus
Distribution: Slackware
Posts: 12,739

Rep: Reputation: 1838Reputation: 1838Reputation: 1838Reputation: 1838Reputation: 1838Reputation: 1838Reputation: 1838Reputation: 1838Reputation: 1838Reputation: 1838Reputation: 1838
Quote:
Originally Posted by c0wb0y View Post
I suspect permission/ownership/SELinux context is not right. Can you post the output of:

Code:
ls -lZd /cubs
ls -lZd /cubs/test1.example.com
FYI OP posted "ls -lZ" output at post #1
Also OP tried with SELinux disabled and got the same error (post #8)
 
Old 03-19-2017, 09:30 AM   #14
chtsalid
Member
 
Registered: Jan 2017
Posts: 67

Original Poster
Rep: Reputation: Disabled
Hi,

not sure were I was mistaking before, but after I repeated it agaim, finally it works.
Here is my config

[root@rh2 ~]# cat /etc/httpd/conf.d/vrhce2.conf
<Directory /var/vrhce2>
AllowOverride none
Require all granted
</Directory>

<VirtualHost *:80>
ServerAdmin webmaster@vrhce2.example.com
DocumentRoot /var/vrhce2
ServerName vrhce2.example.com
ErrorLog logs/vrhce2.example.com-error_log
TransferLog logs/vrhce2.example.com-access_log
</VirtualHost>


[root@rh2 ~]# cat /var/vrhce2/sysmem.sh
#!/bin/bash
echo -e "Content-type: text\n"
echo
echo "The system memory usage is $(free -m)"

[root@rh2 ~]# cd /var/vrhce2/
[root@rh2 vrhce2]# ls -lZ
-rw-r--r--. root root unconfined_ubject_r:httpd_sys_content_t:s0 index.html
-rwxr-xr-x. root root unconfined_ubject_r:httpd_sys_script_exec_t:s0 sysmem.sh
[root@rh2 vrhce2]#




in httpd.conf


ScriptAlias /cgi-bin/ "/var/vrhce2/"

</IfModule>

#
# "/var/www/cgi-bin" should be changed to whatever your ScriptAliased
# CGI directory exists, if you have that configured.
#
#<Directory "/var/www/cgi-bini">
# AllowOverride None
# Options None
# Require all granted
#</Directory>

<Directory "/var/vrhce2">
AllowOverride None
Require all granted
</Directory>

[root@rh2 vrhce2]# semanage boolean -l|grep cgi
httpd_enable_cgi (on , on) Allow httpd to enable cgi
git_cgi_use_nfs (off , off) Allow git to cgi use nfs
git_cgi_use_cifs (off , off) Allow git to cgi use cifs
git_cgi_enable_homedirs (off , off) Allow git to cgi enable homedirs
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How setup Apache to run CGI Script meligon Debian 1 11-29-2006 07:26 AM
run cgi script as a user other than apache paul_mat Linux - Software 1 06-02-2006 02:01 PM
Apache 2.0.43 will not run cgi scripts RobbieRoy Linux - Networking 5 09-04-2003 02:56 PM
Help CGI won't run under Apache shuttleX Linux - Software 2 06-05-2003 02:37 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Server

All times are GMT -5. The time now is 07:51 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration