CGI won't run just download on Apache server - c

I'm completly new to CGI and Apache but I'm trying out a few things. To start I wrote a simple hello CGI in C.
#include <stdio.h>
void main() {
printf("Content-type: text/html\n\n");
printf("<html>\n");
printf("<head><title>CGI Output</title></head>\n");
printf("<body>\n");
printf("<h1>Hello, world.</h1>\n") ;
printf("</body>\n");
printf("</html>\n");
}
compiled it gcc hello.c -o hello.cgi and placed it in /var/www/mycgi
Afterward I modified httpd.conf to add the following
ScriptAlias /mycgi/ "/var/www/mycgi/"
in the IfModule alias_module and
<Directory "/var/www/mycgi">
Options +ExecCGI
AddHandler cgi-script .cgi
AllowOverride None
Options None
Order allow,deny
Allow from all
</Directory>
I have then restarted Apache and when I go to localhost/mycgi/hello.cgi the browser just downloads the file instead of running it. Help will be greatly apreciated!

According to janos's advice, you should make sure apache2 has loaded the cgi_module: LoadModule cgi_module modules/mod_cgi.so. You can do this by:
sudo ln -s /etc/apache2/mods-available/cgi.load /etc/apache2/mods-enabled/cgi.load
then restart apache2 and it will load the cgi.load file this time.

Even if it's an old question, I think many other guys will get the same problem...
First we must tell Apache what kind of files he must "exec" and where are theses files:
Edit etc/apache2/apache2.conf (using nano for example)
Add
<Directory "/home/johnny/public_html">
Options +ExecCGI
AddHandler cgi-script .cgi
</Directory>
In this case Apache will try to exec all files ending with .cgi located in the public_html of user "johnny". You can add more than one extension for exemple:
AddHandler cgi-script .cgi .pif .gloup
and so on.
To compile: cd to the folder where the .c source is and then:
gcc hello.c
This will give a file named "a" (or maybe a.out, it depend on the OS).
To change the name of the result:
gcc hello.c -o ../public_html/my_cgi.gloup
in case of .gloup has been added with AddHandler and of an exec file you want to access via the web.
In many cases, this will give an error. You can try to change the right, exec and so on, it's of no use.
The problem came from the fact in many cases, we run gcc as "root", so the result owner is root. And when we try to exec it from the web, we're not "root".
The solution is to change user. For exemple if your public_html folder owner is "johnny", when you are logged as root, just do:
su - johnny
and now you "are" johnny. Just cd to the folder wih your c fles, compile using gcc and you'll be able to exec from the web.

Related

Apache2 - WSGI file not found Issue

I run bash script file through python code. The script is located /home/myscript.sh. The script convert html to pdf and print. If I run the python program in development mode it works fine but if I run in WSGI production mode it give me error "File not found" to print. But if I give access 7777 to home directory it works fine. And I need to do this access step every time whenever machine restarted. Also it not good solution.
I search a lot and change apache2.conf file
<Directory /home/>
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
But still there is problem. Please help me. I used Apache 2.4.9 version. Advance thanks
Update
In /etc/apache2/apache2.conf file
"User ${APACHE_RUN_USER}" and "Group ${APACHE_RUN_GROUP}"
And in envvars it sets like
export APACHE_RUN_USER=www-data
export APACHE_RUN_GROUP=www-data
Still problem exist.
If issue is with directory
Example /home
Move the file from /home to /var/www
sudo chown www-data:www-data /var/www/filename
or
sudo chown www-data:www-data /var/www/filename
If the issue with USB port
Example
ttyUSB0
,
ttyACM0
,
ttyACM1
Write script to change the owner
#!/bin/bash
sudo chown www-data:www-data /dev/ttyACM0
Call this script from your code using subprocess.call
But allow the user www-data to run this file as sudo with out password
sudo visudo
and add the below line in the last
www-data ALL = (ALL) NOPASSWD : /var/www/filename
or
www-data ALL = (ALL) NOPASSWD : ALL

Apache 2 Forbidden Access to mounted directory via sshfs

I have following problem:
I would like to give an access for my Apache to index.html which exists on my Virtual Machine created by Vagrant. Directory is mounted by SSHFS with -o allow_other option.
Used tools and versions are:
System: Linux Ubuntu 15.04
Apache: 2.4.12
Mount via SSHFS with -o allow_other
Mount process is correct because directory and file are visible in my system. I can open/modify/etc them.
But when I try to open this file via Apache then I receive
Forbidden
You don't have permission to access / on this server.
Apache/2.4.12 (Ubuntu) Server at xxx.yyy.com Port 443
Configuration for Apache:
<Directory /var/www/interscape-frontend>
Options FollowSymLinks
Order allow,deny
Allow from all
Require all granted
</Directory>
DocumentRoot /var/www/interscape-frontend
Is it possible? If yes then how? :)
Thanks in advance!
What are the permissions on the folder/files? Who owns them?
If you cd /var/www and ls -al what words come up? See an example below.
-rw-rw-r--+ 1 (owner) (group) 277 Mar 16 00:31 build.xml
The left-most stuff is owner / group / all permissions. Following that, the owner and group are indicated. If apache isn't the owner (www-data usually), a part of the group listed, or the file does not permit all to have rw or rwx, then you may encounter the problem you've described.
You can change the owner and group with the following commands, respectively:
sudo chown -R (user) /path/to/directory
sudo chown -R (user):(group) /path/to/directory
Where the -R flag indicates to do it recursively (to all sub-directories).
Also, as per the apache 2.4 upgrade guide you can remove the following from your *.conf:
Order allow,deny
Allow from all
As they only apply to apache 2.2 and under.

Is There Any Location Specific in Apache2

I'm using Apache2 running on Ubuntu-12.04 machine and I run my files through apache2 pointing to my folder in the local file system like
$ cd etc/apache2/
$ vi sites-enabled/000-default
And I change the location pointing to my folders in
DocumentRoot /home/user/foo/
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
<Directory /home/user/foo/>
What is the default location of Apache2 web root where if I would just copy my files the Apache server would pick it. For running hundreds of files through Apache2 each and every time I keep doing the above steps of changing the location manually. Is there any other best way of doing it.
It would be great if anyone could help me out.
If do an ls -l inside /etc/apache2/sites-enabled/ you'll see that 000-default is a symlink to /etc/apache2/sites-available/. The idea being that you create site definition files in sites-enabled, then symlink to them from sites-available... making it very simple to enable and disable sites as needed.
In fact, Ubuntu provides the a2ensite command to enable sites, and a2dissite to disable sites. This means all you have to do is create the vhost file in sites-available, then run a2ensite to enable it.
Take a look at https://help.ubuntu.com/12.04/serverguide/httpd.html for more details.

DatabaseError unable to open database file

My sqlite db file is this: unable to open database file i chowned all folders until my dbfile to root. but i am still getting this error. but i remember that while creating my django project on server, i created a superuser, and now if i do ls -l i see that the user is that superuser. how is it possible to tell apache that this superuser should have that right to write/read the db file? or how to solve the problem, i am not apache/linux guru..
Execute chown www-data:www-data directory on the directory you want apache to be able to write to.
You should be able to just leave the file as owned by the super user and just change the group so that apache can read/write it as well.
Change the group for the sqlite file and the containing directory. Try this:
cd <directory with sqlite file>
sudo chgrp www-data . <sqlitefile>
You can find write group and www user and change permissions.
Say: cat /etc/passwd - for find right user, It may be apache or http or www.
And say to terminal: cat /etc/group -for find right group.
In my system group=apache, user = apache.

Could not reliably determine the server's fully qualified domain name

I have just installed Apache 2.2.17, and I am using it for the first time.
Now when I try to start the server using the command service httpd start it gives me the message:
httpd: Could not reliably determine the server's fully qualified domain name, using ::1 for ServerName
Now I think I have to set ServerName and the IP address as I search through Google. But I don't know in which file I have to set.
How can I fix this problem?
sudo vim /etc/apache2/httpd.conf
Insert the following line at the httpd.conf: ServerName localhost
Just restart the Apache: sudo /etc/init.d/apache2 restart
Yes, you should set ServerName:
http://wiki.apache.org/httpd/CouldNotDetermineServerName
http://httpd.apache.org/docs/current/mod/core.html#servername
You can find information on the layouts used by the various httpd distributions here:
http://wiki.apache.org/httpd/DistrosDefaultLayout
In your case the file to edit is /etc/httpd/conf/httpd.conf
I was NOT getting the ServerName wrong. Inside your VirtualHost configuration that is causing this warning message, it is the generic one near the top of your httpd.conf which is by default commented out.
Change
#ServerName www.example.com:80
to:
ServerName 127.0.0.1:80
Under Debian Squeeze;
Edit Apache2 conf file : vim /etc/apache2/apache2.conf
Insert the following line at the apache2.conf: ServerName localhost
Restart Apache2: apache2ctl restart or /etc/init.d/apache2 restart
Should work fine (it did solve the problem in my case)
tks noodl for the link on the different layouts. :)
sudo nano /etc/apache2/httpd.conf
search for a text ServerName in nano editor <Ctrl + W>
Insert the following line at the httpd.conf: ServerName localhost
Just restart the Apache: sudo /usr/sbin/apachectl restart
Another option is to ensure that the full qualified host name (FQDN) is listed in /etc/hosts.
This worked for me on Ubuntu v11.10 without having to change the default Apache configuration.
" To solve this problem You need set ServerName.
1: $ vim /etc/apache2/conf.d/name
For example set add ServerName localhost or any other name:
2: ServerName localhost
Restart Apache 2
3: $ service apache restart
For this example I use Ubuntu 11.10.1.125"
FQDN means the resolved name over DNS. It should be like "server-name.search-domain".
The warning you get just provides a notice that httpd can not find a FQDN, so it might not work right to handle a name-based virtual host. So make sure the expected FQDN is registered in your DNS server, or manually add the entry in /etc/hosts which is prior to hitting DNS.
If you are using windows there is something different sort of situation
First open c:/apache24/conf/httpd.conf.
The Apache folder is enough not specifically above path
After that you have to configure httpd.conf file.
Just after few lines there is pattern like:
#Listen _____________:80
Listen 80
Here You have to change for the localhost.
You have to enter ipv4 address for that you can open localhost.
Refer this video link and after that just bit more.
Change your environment variables:
In which you have to enter path:
c:apache24/bin
and
same in the SYSTEM variables
If any query feel free to ask.
Two things seemed to do it for me:
Put all aliases for 127.0.0.1 in /etc/hosts in a single line (e.g. 127.0.0.1 localhost mysite.local myothersite.local
Set ServerName in my httpd.conf to 0.0.0.0 (localhost or 127.0.0.1 didn't work for me)
Editing /etc/hosts got rid of long response times and setting the ServerName got rid of OP's warning for me.
who are still couldnt resolve the problem and using mac then follow this
1.goto the root folder /
cd usr/local/etc/apache2/2.4
3.sudo nano httpd.conf
4.change #servername to ServerName 127.0.0.1:8080 press ctrl+o,+return+ctrl x
5.then restart the server apachectl restart
If you are using windows, remove comment on these lines and set them as:
Line 227 : ServerName 127.0.0.1:80
Line 235 : AllowOverride all
Line 236 : Require all granted
Worked for me!
Here's my two cents. Maybe it's useful for future readers.
I ran into this problem when using Apache within a Docker container. When I started a container from an image of the Apache webserver, this message appeared when I started it with docker run -it -p 80:80 my-apache-container.
However, after starting the container in detached mode, using docker run -d -p 80:80 my-apache-container, I was able to connect through the browser.
I am using ubuntu 22.04
I installed the apache2 at the location '/usr/local/apache2'
I just edited the '/usr/local/apache2/conf/httpd.conf' file.
run the following commands
cd /usr/local/apache2/conf
sudo nano httpd.conf
find this comment
#ServerName www.example.com:80, in my case it is at line 197
after that add this
ServerName localhost
don't modify anything else in this file!
Thank you!

Resources