Apache: /cgi-bin/my.cgi was not found on this server - apache

I'm struggling to get my server to recognize cgi-bin in a URL.
If I place my cgi script in any site's root folder, it works perfectly without including cgi-bin in the URL. So my server is processing cgi correctly. If I attempt to use any cgi-bin configuration, it results in a 404 error.
I'm running it on a Mac server with OS 10.10. Ideally I would like to locate the cgi script in /Library/WebServer/CGI-Executables and address it as cgi-bin: Some.com/cgi-bin/my.cgi?yaddayadda .
In pursuit of that, I've edited Apache's httpd.conf file. It was already set with that as the path but resulted in the 404. Currently httpd.conf is:
# "/Library/WebServer/CGI-Executables" should be changed to whatever your ScriptAliased
# CGI directory exists, if you have that configured.
#
ScriptAlias /cgi-bin/ /Library/WebServer/CGI-Executables/
<Directory "/Library/WebServer/CGI-Executables">
AllowOverride None
Options +ExecCGI
AddHandler cgi-script .cgi .wsgi
</Directory>
# To allow CGI program execution for any file ending in .cgi in
# users' directories, you can use the following configuration.
#ScriptAlias /cgi-bin/ /home/*
# <Directory /home/*>
# Options +ExecCGI
# AddHandler cgi-script .cgi
# </Directory>
# If you wish designate a cgi-bin subdirectory of a user's directory
# where everything will be treated as a CGI program, you can use the following.
# ScriptAlias /cgi-bin/ /home/cgi-bin/
# <Directory /home/cgi-bin>
# AllowOverride None
# Options +ExecCGI
# AddHandler cgi-script .cgi
# </Directory>
As this shows, I've tried using a cgi-bin folder within the site folders as well. I've also added the .wsgi handler and gotten the same results with a Python Hello World. It too works from the root folder of any site. I just can't get it to refer to what I want as the cgi-bin. Even if I nest a cgi-bin folder inside CGI-Executables. I always get "The requested URL /cgi-bin/my.cgi was not found on this server."
I do restart Apache after every edit and have rebooted the whole machine several times. Nothing changes.
Unless I'm doing something wrong in my edits above, it's almost as if Apache is ignoring the httpd.conf. I don't know how to confirm that the file is being read when Apache starts. There is no other httpd.conf file located anywhere that should matter and those typically bear names that suggest they're from the past: httpd.conf.pre-update and httpd.conf-previous.
My best guess is that I have some error in the path or aliased path. I have tried every variation on them that I can come up with. The full path to my desired cgi-bin is: Server HD2/Library/WebServer/CGI-Executables. Or a cgi-bin folder within a site's root folder would be: Server HD2/Library/WebServer/Documents/TheSite/cgi-bin
I've also checked for any .htaccess files that could be overriding my edits. None found.
Thanks in advance for any suggestions.

Related

Why Apache configuration works when I use root directory

I have a VirtualHost that I am using to serve requests to files in /var/www/project/src.
I also have a Perl script (CGI binary) that is in /var/www/project/src/cgi-bin/index.pl.
Here's the part of the directive relevant to my question:
<VirtualHost example.com:443>
...
DocumentRoot "/var/www/project/src"
<Directory "/var/www/project/src">
Require all granted
Options +MultiViews +ExecCGI
AddHandler cgi-script .pl
DirectoryIndex /cgi-bin/index.pl
</Directory>
</VirtualHost>
The server starts, but access to the host fails as I get a 403 error.
The Apache logs indicate that the web server cannot find {document-root}/cgi-bin/index.pl:
[Thu Nov 09 11:37:03.578316 2017] [autoindex:error]
[pid 122783] [client example-client.com:57203] AH01276:
Cannot serve directory /var/www/project/src/: No matching
DirectoryIndex (/cgi-bin/index.pl) found, and server-
generated directory index forbidden by Options directive
This directory and index.pl file are in the right location, are owned by the apache user, and have permissions which allow others to read the contents of directories and execute the index.pl CGI-bin.
Moreover, if I change paths in DocumentRoot and Directory variables, the following configuration works:
<VirtualHost example.com:443>
...
DocumentRoot "/"
<Directory "/">
Require all granted
Options +MultiViews +ExecCGI
AddHandler cgi-script .pl
DirectoryIndex /var/www/project/src/cgi-bin/index.pl
</Directory>
</VirtualHost>
Apache starts up, and my access to the host in turn loads /cgi-bin/index.pl, which is rendered correctly.
Question: What would cause the first set of directives to fail, where the second set works?
More specifically: What is preventing the first set of directives from finding /cgi-bin/index.pl in the specified document root, while the second set correctly finds the fully-qualified path /var/www/project/src/cgi-bin/index.pl?
Note: The items in ... do not seem relevant to the issue — whether I remove them, alter them, or leave them, the error and log messages are the same in any case — so I am leaving them out for brevity.
Try changing permissions for /var/www, /var/www/project, /var/www/project/src and/or /var/www/project/src/cgi-bin
Update: Also, don't forget to restart Apache server after making changes.
What's happening is your excessive zeal an excess in your zeal... in the first example, DirectoryIndex /cgi-bin/index.pl points towards your ROOT directory, then searching for a folder caller cgi-bin. This also happens in the second example, but since you give the full path, Apache finds it. Try removing the / before cgi-bin and the issue should fix itself.

Why does "Require all denied" in apache2.conf apply to all sub-directories?

I'd like to understand this behaviour which does not make sense to me.
apache2.conf has the default configuration at the bottom.
As you can see, it has "Require all denied" for / and "Require all granted" for /var/www/
I have a website under /var/www/HM/
After using URL rewriting, Apache is asked to send the result of for example /var/www/HM/subdir/
Instead of correctly returning the content of index.php located in subdir, it denies access. I found out that the / "Require all denied" config in apache2.conf is to blame, but this does not make sense to me especially as "Require all granted" is given to /var/www/
I have managed to set "Require all granted" for /var/www/HM/ and it seems to work then. But still I'd like to understand why the / directive in apache2.conf applies to everything below and that the /var/www/ directive does not!
Also if the directive for /var/www/ does not apply to sub-directories, I wonder if my directive for /var/www/HM/ will apply to its own sub-directories...
Thanks for your help!
# Sets the default security model of the Apache2 HTTPD server. It does
# not allow access to the root filesystem outside of /usr/share and /var/www.
# The former is used by web applications packaged in Debian,
# the latter may be used for local directories served by the web server. If
# your system is serving content from a sub-directory in /srv you must allow
# access here, or in any related virtual host.
<Directory />
Options FollowSymLinks
AllowOverride All
Require all denied
</Directory>
<Directory /usr/share>
AllowOverride All
Require all granted
</Directory>
<Directory /var/www/>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
The documentation states that the Directory directive includes sub-directories:
Enclose a group of directives that apply only to the named file-system
directory, sub-directories, and their contents.
I skimmed a bit through the apache-httpd code and couldn't find any special handling of the root directory or any other directory. (okay okay, root is special: it's always first in the directory list)
Also: experience tells me that you probably had another Directory directive in another config file that messed up the directory permissions, or a .htaccess in the subdirectory with Deny from all specified.

How to Run perl script through URL in Ubuntu

I would like to run perl as CGI . i.e Want to run it in localhost rather than command prompt.
I have installed perl . I have kept my file at /var/www/cgi-bin/
and i have made following changes in /etc/apache2/apche2.conf
ScriptAlias /cgi-bin/ /var/www/cgi-bin/
<Directory /var/www/cgi-bin/>
Options ExecCGI
AddHandler cgi-script cgi pl
</Directory>
Its giving me 404 not found error.
Following is the url I am accessing:
http://localhost/cgi-bin/testscript.pl
If your file is at /var/www/cgi-bin/, and your configuration says:
ScriptAlias /cgi-bin/ /home/www/cgi-bin/
Then I'd say that the problem is pretty obvious. /var/www/cgi-bin/ and /home/www/cgi-bin/ are not the same directory.
Update: Ok. So you say that you have fixed the value of ScriptAlias and it still didn't work. Assuming that you're still getting the 404 error, then the next obvious step is to look in your error log and see exactly which file Apache is looking for. There's probably a mismatch between how Apache is configures to map between URLs and files and how you think it is configured.
When you get that error message, add it to your question.

How to Set AllowOverride all

I want to set the AllowOverride all But I don't know how to do it. I have found the following code by searching the google and pasted it in .htaccess:
<Directory>
AllowOverride All
</Directory>
But after pasting it I started receiving "Internal Server Error"
Can anyone guide me where to put this code or how to do it?
In case you are on Ubuntu, edit the file /etc/apache2/apache2.conf (here we have an example of /var/www):
<Directory /var/www/>
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
and change it to;
<Directory /var/www/>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
then,
sudo service apache2 restart
You may need to also do sudo a2enmod rewrite to enable module rewrite.
The main goal of AllowOverride is for the manager of main configuration files of apache (the one found in /etc/apache2/ mainly) to decide which part of the configuration may be dynamically altered on a per-path basis by applications.
If you are not the administrator of the server, you depend on the AllowOverride Level that theses admins allows for you. So that they can prevent you to alter some important security settings;
If you are the master apache configuration manager you should always use AllowOverride None and transfer all google_based example you find, based on .htaccess files to Directory sections on the main configuration files. As a .htaccess content for a .htaccess file in /my/path/to/a/directory is the same as a <Directory /my/path/to/a/directory> instruction, except that the .htaccess dynamic per-HTTP-request configuration alteration is something slowing down your web server. Always prefer a static configuration without .htaccess checks (and you will also avoid security attacks by .htaccess alterations).
By the way in your example you use <Directory> and this will always be wrong, Directory instructions are always containing a path, like <Directory /> or <Directory C:> or <Directory /my/path/to/a/directory>. And of course this cannot be put in a .htaccess as a .htaccess is like a Directory instruction but in a file present in this directory. Of course you cannot alter AllowOverride in a .htaccess as this instruction is managing the security level of .htaccess files.
Goto your_severpath/apache_ver/conf/
Open the file httpd.conf in Notepad.
Find this line:
#LoadModule vhost_alias_module modules/mod_vhost_alias.so
Remove the hash symbol:
LoadModule vhost_alias_module modules/mod_vhost_alias.so
Then goto <Directory />
and change to:
<Directory />
Options FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
</Directory>
Then restart your local server.
On Linux, in order to relax access to the document root, you should edit the following file:
/etc/httpd/conf/httpd.conf
And depending on what directory level you want to relax access to, you have to change the directive
AllowOverride None
to
AllowOverride All
So, assuming you want to allow access to files on the /var/www/html directory, you should change the following lines from:
<Directory "/var/www/html">
AllowOverride None
</Directory>
to
<Directory "/var/www/html">
AllowOverride All
</Directory>
If you are using Linux you may edit the code in the directory of
/etc/httpd/conf/httpd.conf
now, here find the code line kinda like
# AllowOverride controls what directives may be placed in .htaccess files.
# It can be "All", "None", or any combination of the keywords:
# Options FileInfo AuthConfig Limit
#
AllowOverride None
#
# Controls who can get stuff from this server.
#
Order allow,deny
Allow from all
</Directory>
Change the AllowOveride None to AllowOveride All
Now now you can set any kind of rule in your .httacess file inside your directories
if any other operating system just try to find the file of httpd.conf and edit it.
As other users explained here about the usage of allowoveride directive, which is used to give permission to .htaccess usage. one thing I want to point out that never use allowoverride all if other users have access to write .htaccess instead use allowoveride as to permit certain modules.
Such as AllowOverride AuthConfig mod_rewrite Instead of
AllowOverride All
Because module like mod_mime can render your server side files as plain text.
enter code hereif you are using linux you have to edit the
`/etc/apache2/sites-available/000-default.conf`
under the Documentroot . add the following code
`<Directory /var/www/>
AllowOverride all
Require all granted
</Directory>`
then ,
`sudo service apache2 restart`
and you have to enable the apache mod rewrite
`sudo a2enmod rewrite`
I think you want to set it in your httpd.conf file instead of the .htaccess file.
I am not sure what OS you use, but this link for Ubuntu might give you some pointers on what to do.
https://help.ubuntu.com/community/EnablingUseOfApacheHtaccessFiles
I also meet this problem, and I found the solution as 2 step below:
1. In sites-enabled folder of apache2, you edit in Directory element by set "AllowOverride all" (should be "all" not "none")
2. In kohana project in www folder, rename "example.htaccess" to ".htaccess"
I did it on ubuntu. Hope that it will help you.
There are several answers but there a number of things wrong with this question and I would like to address these:
If you get an error (e.g. 500), look in the log files (if you have access to them). e.g. /var/log/apache2/ssl_error.log
e.g.
cat /var/log/apache2/ssl_error.log
[Tue Jun 01 19:05:34 2021] [alert] [pid 31154] config.c(2119):
[client *******] /var/www/mysite/public/tmp/.htaccess:
<Directory not allowed here [lid YLZo3quRlv2EKOAABVoFLwAAAIM]
Putting AllowOverrides in a .htaccess makes no sense and is not allowed. See Context. See also my explanation below. It should be defined in the Apache configuration (e.g. /etc/apache2)
Allowing everything is usually not the best idea. Be as restrictive as possible!
the Directory directive is missing a directory, should be e.g. <Directory /var/www/html/etc>
the Directory directive does not make sense in an .htaccess. The location of the .htaccess in a directory already has the effect of making the statements within apply to a specific directory
do not mix and match snippets that are intended to be put in the Apache configuration (e.g. in /etc/apache2/...) with statements that are intended to be put in .htaccess - though most of the time, they will be identical, there are some subtle differences
If you have the possibility to modify the Apache configuration directly, do not use .htaccess and deactivate it. (for performance reasons, among others. Also you can have all configuration in one place, put it in version control or manage it via a software configuration management tool, e.g. Puppet, Ansible, SaltStack)
Unless you really cannot access and modify the Apache configuration directly, you do not need .htaccess. This is a common misconception.
That you saw a 500 error proves my point. If you change configuration in the Apache configuration directly (and not in .htaccess), you will usually get an error message with an explanation and information about the error and the line number (e.g. when you do service apache2 reload or apachectl configtest) - which gives you the possibility to fix the error before applying this in production(!).
Also, look in the documentation. It is really quite good. For most directives, you can find where they apply (see "Context").
For example, for IfModule, you can see:
Context: server config, virtual host, directory, .htaccess
For, AllowOverrides it is:
Context: directory
Note the missing .htaccess in the Context!
Instead of googling for information which repeat the same mistakes over and over, look in the documentation!
Docs
AllowOverrides
https://www.danielmorell.com/guides/htaccess-seo/basics/dont-use-htaccess-unless-you-must
SuSE Linux Enterprise Server
Make sure you are editing the right file
https://www.suse.com/documentation/sles11/book_sle_admin/data/sec_apache2_configuration.html
httpd.conf
The main Apache server configuration file. Avoid changing this file. It primarily contains include statements and global settings. Overwrite global settings in the pertinent configuration files listed here. Change host-specific settings (such as document root) in your virtual host configuration.
In such case vhosts.d/*.conf must be edited
Plus those upvoted correct answers sometimes same error could be seen because of mismatched and different settings on SSL part of webserver configurations. (Obviously when not using .htaccess file).

How do I disable directory browsing?

I want to disable directory browsing of /galerias folder and all subdirectories
Index of /galerias/409
* Parent Directory
* i1269372986681.jpg
* i1269372986682.jpg
* i1269372988680.jpg
Create an .htaccess file containing the following line:
Options -Indexes
That is one option. Another option is editing your apache configuration file.
In order to do so, you first need to open it with the command:
vim /etc/httpd/conf/httpd.conf
Then find the line: Options Indexes FollowSymLinks
Change that line to: Options FollowSymLinks
Lastly save and exit the file, and restart apache server with this command:
sudo service httpd restart
(You have a guide with screenshots here.)
The best way to do this is disable it with webserver apache2. In my Ubuntu 14.X - open /etc/apache2/apache2.conf change from
<Directory /var/www/>
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
to
<Directory /var/www/>
Options FollowSymLinks
AllowOverride None
Require all granted
</Directory>
then restart apache by:
sudo service apache2 reload
This will disable directory listing from all folder that apache2 serves.
Apart from the aformentioned two methods (edit /etc/apache2/apache2.conf or add Options -Indexes in .htaccess file), here is another one
a2dismod autoindex
Restart the apache2 server afterwards
sudo service apache2 restart
Edit/Create an .htaccess file inside /galerias with this:
Options -Indexes
Directory browsing is provided by the mod_autoindex module.
You can place an empty file called index.html into each directory that you don't want listed. This has several advantages:
It (usually) requires zero configuration on the server.
It will keep working, even if the server administrator decides to use "AllowOverride None" in the the server configuration. (If you use .htaccess files, this can lead to lots of "Error 500 - internal server error" messages for your users!).
It also allows you to move your files from one server to the next, again without having to mess with the apache configuration.
Theoretically, the autoindexing might be triggered by a different file (this is controlled by the DirectoryIndex option), but I have yet to encounter this in the real world.
One of the important thing is on setting a secure apache web server is to disable directory browsing. By default apache comes with this feature enabled but it is always a good idea to get it disabled unless you really need it.
Open httpd.conf file in apache folder and find the line that looks as follows:
Options Includes Indexes FollowSymLinks MultiViews
then remove word Indexes and save the file. Restart apache. That's it
If you choose to modify your httpd.conf file to solve this and you have multiple Options directives, then you must add a - or a + before each directive. Example:
Options -Indexes +FollowSymLinks
This is not an answer, just my experience:
On my Ubuntu 12.04 apache2, didn't find Indexes in either apache2.conf or httpd.conf, luckily I found it in sites-available/default. After removing it, now it doesn't see directory listing. May have to do it for sites-available/default-ssl.
To complete #GauravKachhadiya's answer :
IndexIgnore *.jpg
means "hide only .jpg extension files from indexing.
IndexIgnore directive uses wildcard expression to match against directories and files.
a star character , it matches any charactes in a string ,eg : foo or foo.extension, in the following example, we are going to turn off the directory listing, no files or dirs will appear in the index :
IndexIgnore *
Or if you want to hide spacific files , in the directory listing, then we can use
IndexIgnore *.php
*.php => matches a string that starts with any char and ends with .php
The example above hides all files that end with .php
Open Your .htaccess file and enter the following code in
Options -Indexes
Make sure you hit the ENTER key (or RETURN key if you use a Mac) after entering the "Options -Indexes" words so that the file ends with a blank line.
Add this in your .htaccess file:
Options -Indexes
If it is not work for any reason, try this within your .htaccess file:
IndexIgnore *
Try this in .htaccess:
IndexIgnore *.jpg
In Directory Section ( /etc/httpd/httpd.conf)
Remove Line - Options Indexes FollowSymLinks
New Line - Options FollowSymLinks
I found another way of doing this with virtual hosts:
<VirtualHost *:80>
DocumentRoot C:/WAMP/Apache24/htdocs/
ServerName vehiclesspares.com
<Directory C:/WAMP/Apache24/htdocs/vehiclesspares.com>
Options FollowSymLinks
AllowOverride None
Require all granted
</Directory>
</VirtualHost>
This worked for me on Apache 2.4.54 on my local windows machine with the host file (C:\Windows\System32\drivers\etc\hosts) containing the line:
127.0.0.1 vehiclesspares.com
This configuration also had vehiclesspares.com under the docroot: C:\WAMP\Apache24\htdocs\vehiclesspares.com