Ubuntu Apache can't access cgi-bin folder - python

I created a droplet on DigitalOcean but can not run the python scripts in cgi-bin folder. I tried
sudo a2enmod cgi
chmod a+rwx cgi-bin
Also give chmod 755 to files but It did not work. When I try to acces cgi-bin folder it gives Forbidden error and says You don't have permission to access /cgi-bin/ on this server. Can any one help?
My config file
<VirtualHost *:80>
ScriptAlias /cgi-bin/ "/var/www/test/cgi-bin/"
<Directory /var/www/test/cgi-bin/>
AllowOverride All
Options ExecCGI
AddHandler cgi-script .cgi .pl .tcl .py
Order allow,deny
Allow from all
</Directory>
<Directory "/var/www/test/">
AllowOverride All
Options Indexes MultiViews FollowSymLinks ExecCGI
Require all granted
</Directory>
ServerAdmin webmaster#localhost
DocumentRoot "/var/www/test"
</VirtualHost>
Also I created a test python file in document root and when I opened it, it behaves like a text file.

You can try adding the following lines to your httpd.conf or you web-server config file
<Files ~ "\.(py|cgi)$">
SetHandler python-script
Options +ExecCGI
</Files>
See Also : mod-python

Related

EC2: Deploying a django project with a apache server

I have set up an ec2 instance and I'm able to connect to it via ssh. I've installed apache, django, git and all the necessary dependencies.
I've managed to clone a git repository which is located in /home/ubuntu/.
I've also set up the configuration file in /etc/apache2/sites-enabled/hyper-blog.conf.
This is my hyper-blog.conf file:
<VirtualHost *:80>
ServerName ec2-52-11-240-28.us-west-2.compute.amazonaws.com
ServerAlias ec2-52-11-240-28.us-west-2.compute.amazonaws.com
ServerAdmin webmaster#localhost
DocumentRoot /home/ubuntu/hyper-blog/
AddDefaultCharset UTF-8
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
alias /static/ /home/ubuntu/hyper-blog/static/
alias /media/ /home/ubuntu/hyper-blog/media/
<Directory "/home/ubuntu/hyper-blog/static">
Require all granted
Options -Indexes
</Directory>
<Directory "/home/ubuntu/hyper-blog/media">
Require all granted
Options -Indexes
</Directory>
<Directory /home/ubuntu/hyper-blog/hyper-blog>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
#RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^ https://www.hyperiondev.com%{REQUEST_URI}
WSGIDaemonProcess hyper-blog python-path=/home/ubuntu/hyper-blog:/usr/local/lib/python3.4/dist-packages
WSGIProcessGroup hyper-blog
WSGIScriptAlias / /home/ubuntu/hyper-blog/hyper-blog/wsgi.py
The problem is when I go to my public domain: ec2-52-11-240-28.us-west-2.compute.amazonaws.com it takes a while to load and eventually does not render anything.
I need to know how to get my application up and running. I've tried this documentation on digital ocean https://www.digitalocean.com/community/tutorials/how-to-serve-django-applications-with-apache-and-mod_wsgi-on-ubuntu-14-04,
but still doesn't work.
How can I get my application working without installing virtualenv?

Apache2 Python script not running in browser

I know that there are tons of discussions in several forums, but unfortunately none of the answers and suggestions worked for me. I want to execute a very Simple Python script and view the output in the browser. I followed the instructions according to http://raspberrywebserver.com/cgiscripting/writing-cgi-scripts-in-python.html.
My "hello.py" Python file is located in /usr/lib/cgi-bin. When running it as python hello.py from the Terminal everything works fine. My Apache2 Webserver also seems to run perfectly as it shows:
"It works! This is the default web page for this server. The web server software is running but no content has been added, yet."
when typing the IP of the Raspberry Pi (xxx.xxx.x.xx) in the browser.
When I add the path to my Python file like 192.168.0.12/cgi-bin/hello.py it says 404 - Not Found.
My Apache config file looks like this:
<VirtualHost *:80>
ServerAdmin webmaster#localhost
DocumentRoot /var/www
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
<Directory /var/www/>
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all
</Directory>
ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
<Directory "/usr/lib/cgi-bin">
AllowOverride None
Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
AddHandler cgi-script .cgi .py
Order allow,deny
Allow from all
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel warn
CustomLog ${APACHE_LOG_DIR}/access.log combined
Any ideas so far?
Finally solved it. I haven't noticed that I ran Lighttpd on port 80 and Apache also listens to that port. By setting the Apache to listen to port 8080 it works perfectly. Despite all that I want to thank everyone who contributed to solve my problem.

Apache cannot access the directory that is included in the conf file

I have a directory called media in my django application. I've declared the directory in my apache conf file, and the permissions of the directory and all of its files are 755. but when I try to load an image from it's url apache says:
Forbidden
You don't have permission to access /media/foo.jpg on this server.
here is my apache conf file:
<VirtualHost *:80>
Alias /static/ /root/websites/FJSharif/static/
<Directory /root/websites/FJSharif/static>
Order deny,allow
Allow from all
Require all granted
</Directory>
Alias /media/ /root/websites/FJSharif/media/
<Directory /root/websites/fJSharif/media>
Order deny,allow
Allow from all
Require all granted
</Directory>
<Directory /root/websites/FJSharif/FJSharif>
<Files wsgi.py>
Order deny,allow
Allow from all
Require all granted
</Files>
</Directory>
WSGIDaemonProcess FJSharif python-path=/root/websites/FJSharif:/home/user/myproject/myprojectenv/lib/python2.7/site-packages:/usr/local/lib/python2.7/dist-packages:/usr/lib/python2.7/dist-packages
WSGIProcessGroup FJSharif
WSGIScriptAlias / /root/websites/FJSharif/FJSharif/wsgi.py
</VirtualHost>
when I try to load this url:
http://example.com/media/foo.img
I get the 403 Forbidden error.
EDIT:
Apache can find the files in /static/ directory but not the files in /media/ directory!
Based on our discussion, we eliminated a few possibilities, but it's still worth double-checking the config file for typos (e.g. paths are case-sensitive). A lot of people are often bit by this easily missed detail.

Python not interpreted on apache

I'm trying to set up Python to run on my local apache server on a mac.
On httpd.conf, I've
<VirtualHost *:80>
DocumentRoot "/Users/ptamzz/Sites/python/sandbox.ptamzz.com"
<Directory "/Users/pritams/Sites/python/sandbox.ptamzz.com">
Options Indexes FollowSymLinks MultiViews ExecCGI
AddHandler cgi-script .py
Require all granted
</Directory>
DirectoryIndex index.py
ServerName sandbox.ptamzz.com
ErrorLog "/var/log/apache2/error-log"
CustomLog "/var/log/apache2/access-log" common
</VirtualHost>
On my document root, I've my index.py file as
#!/usr/bin/python
print "Content-type: text/html"
print "<html><body>Pritam's Page</body></html>"
But when I load the page on my browser, the python codes are returned as it is.
What all am I missing?
Executing python in Apache ( cgi )
System : OSX yosmite 10.10.3 , Default apache
uncommented in http config
LoadModule cgi_module libexec/apache2/mod_cgi.so
virtual hosts entry
<VirtualHost *:80>
# Hook virtual host domain name into physical location.
ServerName python.localhost
DocumentRoot "/Users/sj/Sites/python"
# Log errors to a custom log file.
ErrorLog "/private/var/log/apache2/pythonlocal.log"
# Add python file extensions as CGI script handlers.
AddHandler cgi-script .py
# Be sure to add ** ExecCGI ** as an option in the document
# directory so Apache has permissions to run CGI scripts.
<Directory "/Users/sj/Sites/python">
Options Indexes MultiViews FollowSymLinks ExecCGI
AddHandler cgi-script .cgi
AllowOverride All
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
index.py file
#!/usr/bin/env python
print "Content-type: text/html"
print
print "<html><body>Test Page</body></html>"
file was made executable using
chmod a+x index.py
restarted apache and output
For later Versions of Apache (2.4) the answer of Sojan V Jose is mostly still applying, except that I got an authorization failure, that can be resolved by replacing:
Order allow,deny
Allow from all
with:
Require all granted

WSGIScriptAlias overrides Alias

I'm having an issue configuring mod_wsgi with Apache 2.2. I need mod_wsgi to handle everything under root directory of a virtual server except certain paths. For those paths I want ordinary Apache directory listing to work. The following virtual server config half works. mod_wsgi does not kick in for paths directly to files but does for directories. I can't figure this out. Looked at docs here - https://code.google.com/p/modwsgi/wiki/QuickConfigurationGuide. It shows using aliases in the way I have to disable mod_wsgi but does not mention this problem. (Note not that it matters but the foo.wsgi end point is just the hello world example).
<VirtualHost *:80>
ServerAdmin webmaster#test.wsgi.sethandler.localdomain
ServerName test.wsgi.sethandler.localdomain
DocumentRoot /var/www/test.wsgi.sethandler.localdomain
<Directory /var/www/test.wsgi.sethandler.localdomain>
Options Indexes FollowSymLinks MultiViews
AllowOverride FileInfo Options Indexes
Order allow,deny
allow from all
</Directory>
Alias /static /var/www/test.wsgi.sethandler.localdomain/static
WSGIScriptAlias / /var/www/test.wsgi.sethandler.localdomain/cgi-bin/foo.wsgi
<Directory /var/www/test.wsgi.sethandler.localdomain/cgi-bin>
WSGIApplicationGroup %{GLOBAL}
Order deny,allow
Allow from all
</Directory>
</VirtualHost>
I've tried disabling any handler with the following but it does not work:
<Location "/static">
SetHandler None
</Location>
<LocationMatch ^/static>
SetHandler None
</LocationMatch>
Have you tried putting and end slash after static?
Alias /static/ /var/www/test.wsgi.sethandler.localdomain/static/
<Directory /var/www/test.wsgi.sethandler.localdomain/static/>
Require all granted
</Directory>
WSGIScriptAlias / /var/www/test.wsgi.sethandler.localdomain/cgi-bin/foo.wsgi

Categories

Resources