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
Related
I am trying to publish a test python flask application to WAMP. I have gone through many tutorials, but face the issue of only seeing directory listings when I navigate to the app. I believe the wsgi module is setup correctly as the apache logs flag that the module has loaded, and as you can see from the screenshot below the server text also suggests the same. Has anyone else came across this, and resolved.
Here is my wsgi file:
import sys
sys.path.insert(0, 'C:/wamp64/www/flaskapp')
from webtool import app as application
Here is my init.py file:
from flask import Flask, request
app = Flask(__ name__)
#app.route('/')
def hello_world():
return "Hello World"
if __ name__ == '__ main__':
app.run()
The additional lines added to my httpd.conf
LoadModule wsgi_module "d:/program files/anaconda3/lib/site-
packages/mod_wsgi/server/mod_wsgi.cp35-win_amd64.pyd"
LoadFile "d:/program files/anaconda3/python35.dll"
WSGIPythonHome "d:/program files/anaconda3"
Virtualhosts file:
<VirtualHost *:80>
ServerName localhost
DocumentRoot c:/wamp64/www
<Directory "c:/wamp64/www/">
Options +Indexes +Includes +FollowSymLinks +MultiViews
AllowOverride All
Require local
</Directory>
</VirtualHost>
<VirtualHost *:80>
ServerName flaskapp
WSGIScriptAlias /flaskapp c:/wamp64/www/flaskapp/flaskapp.wsgi
DocumentRoot c:/wamp64/www/flaskapp
<Directory c:/wamp64/www/flaskapp>
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
It turned out to be my virtual hosts file. I cannot have two references to the same location (*:80) in the one file.
Updated file below:
<VirtualHost *:80>
ServerName localhost
DocumentRoot c:/wamp64/www
<Directory "c:/wamp64/www/">
Options +Indexes +Includes +FollowSymLinks +MultiViews
AllowOverride All
Require local
</Directory>
WSGIScriptAlias /flaskapp c:/wamp64/www/flaskapp/flaskapp.wsgi
<Directory c:/wamp64/www/flaskapp>
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
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
NOTE: This question is different from 'Add a prefix to all Flask routes' as I am trying to resolve this at apache level. Additional, the suggested fix for flask routes did not work!
Following on from this post, I'm trying to set up apache to serve PHP files by default, but point a given alias (i.e. /flaskapp) to a wsgi path. The wsgi file in turn routes requests to a python flask app.
Here's the apache config that I'm trying (under 000-default.conf):
<VirtualHost *:80>
ServerName localhost
ServerAdmin webmaster#localhost
Alias / /var/www/html/
<Directory "/var/www/html">
Order Deny,Allow
Allow from all
Require all granted
</Directory>
WSGIScriptAlias /flaskapp "/var/www/flaskapp/deploy.wsgi"
<Directory /var/www/flaskapp>
Options +ExecCGI
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
After doing a service apache2 restart I find that requests to http://myip/flaskapp result in a 404 error. Everything else works fine.
Things I've tried so far:
Double checking all the file and folder paths (no issues found)
Using the wsgi part of the above code to set up the wsgi app as a standalone virtualhost (works fine)
Adding app.config['APPLICATION_ROOT'] = '/flaskapp' to my app.py file, as suggested the question 'Add a prefix to all Flask routes' (Didn't have any effect)
Where could I be going wrong?
Instead of:
Alias / /var/www/html/
<Directory "/var/www/html">
Order Deny,Allow
Allow from all
Require all granted
</Directory>
use:
DocumentRoot /var/www/html/
<Directory "/var/www/html">
Order Deny,Allow
Allow from all
Require all granted
</Directory>
Using '/' with Alias takes precedence over everything else including mod_wsgi's ability to intercept requests at a sub URL. So for stuff at root of the site you need to use DocumentRoot directive.
I want to get django to run on https server securely without using thirdparty packages like runsslserver, or sslserver for that i was this approach
in the settings.py file i have configured these line.
SECURE_SSL_REDIRECT = True
so when giving the url in the browser url able to redirect to https://192.168.31.2/cp_vm/details but i'm getting secure ssl error.
please suggest me any ways to get the output, or running the django on apache with https also fine but i'm able to find the links
https://www.digitalocean.com/community/tutorials/how-to-serve-django-applications-with-apache-and-mod_wsgi-on-ubuntu-14-04
but after that giving the ip in the browser 192.168.41.5/cp_vm/details it shows unable to connect.
i'm attaching the apache 000.default.conf file also please help me in this, Pleas forgive me if any mistakes in the above kindly suggest me some pointers so that
I can run django on apache server first .
Then getting the https// on the same apache server. please provide me some help would be great if any helping hands.
My config file as follows:
<VirtualHost *:80>
# The ServerName directive sets the request scheme, hostname and port that
# the server uses to identify itself. This is used when creating
# redirection URLs. In the context of virtual hosts, the ServerName
# specifies what hostname must appear in the request's Host: header to
# match this virtual host. For the default virtual host (this file) this
# value is not decisive as it is used as a last resort host regardless.
# However, you must set it for any further virtual host explicitly.
#ServerName www.example.com
ServerName 10.206.51.6
ServerAdmin 10.206.51.6
#ServerAdmin webmaster#localhost
#DocumentRoot /var/www/html
DocumentRoot /var/www/nfvs_portal
## imc urls
ProxyPass /imc http://10.206.50.12:8080/imc
ProxyPassReverse /imc http://10.206.50.12:8080/imc
<Directory /var/www/html/>
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
Allow from all
</Directory>
# Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
# error, crit, alert, emerg.
# It is also possible to configure the loglevel for particular
# modules, e.g.
#LogLevel info ssl:warn
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
# For most configuration files from conf-available/, which are
# enabled or disabled at a global level, it is possible to
# include a line for only one particular virtual host. For example the
# following line enables the CGI configuration for this host only
# after it has been globally disabled with "a2disconf".
#Include conf-available/serve-cgi-bin.conf
Alias /static /opt/hpe_nfvs/nfvs_portal/static
<Directory /opt/hpe_nfvs/nfvs_portal/static>
Require all granted
</Directory>
<Directory /opt/hpe_nfvs/nfvs_portal/nfvs_portal>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
WSGIDaemonProcess nfvs_portal python-path=//opt/hpe_nfvs/nfvs_portal:/opt/hpe_nfvs/nfvs_portal/nfvs_portal/lib/python2.7/site-packages
WSGIScriptAlias / /opt/hpe_nfvs/nfvs_portal/nfvs_portal/wsgi.py
</VirtualHost>
I faced a lot of troubles and did a lot of homework to complete django site + apache with https configuration on my ubuntu server. And finally, I could able to get it configured successfully and my site is working as expected with https now.
The below are the .py and apache configuration files. Please correct your files and let me know if you still face any issues.
wigs.py
import os
from django.core.wsgi import get_wsgi_application
os.environ.setdefault("DJANGO_SETTINGS_MODULE","go_portal_site.settings")
os.environ['HTTPS'] = "on"
application = get_wsgi_application()
/etc/apache2/sites-enabled/000-default.conf
<VirtualHost *:80>
ServerAdmin webmaster#localhost
DocumentRoot /var/www/html
redirect permanent / https://site.site.com
Alias /static /home/ubuntu/django_portal/ci/site_portal/portal_site/static
<Directory /home/ubuntu/django_portal/ci/site_portal/portal_site/static>
Require all granted
</Directory>
<Directory /home/ubuntu/django_portal/ci/site_portal/portal_site/go_portal_site>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
#WSGIDaemonProcess go_portal_site python-path=/home/ubuntu/django_portal/ci/site_portal/portal_site python-home=/home/ubuntu/django_portal/ci/site_portal/portal_site/phase2_env
WSGIProcessGroup go_portal_site
WSGIScriptAlias / /home/ubuntu/django_portal/ci/site_portal/portal_site/go_portal_site/wsgi.py
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
<VirtualHost *:443>
ServerName site.site.com
Alias /static /home/ubuntu/django_portal/ci/site_portal/portal_site/static
<Directory /home/ubuntu/django_portal/ci/site_portal/portal_site/static>
Require all granted
</Directory>
<Directory /home/ubuntu/django_portal/ci/site_portal/portal_site/go_portal_site>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
WSGIDaemonProcess go_portal_site python-path=/home/ubuntu/django_portal/ci/site_portal/portal_site python-home=/home/ubuntu/django_portal/ci/site_portal/portal_site/phase2_env
WSGIProcessGroup go_portal_site
WSGIScriptAlias / /home/ubuntu/django_portal/ci/site_portal/portal_site/go_portal_site/wsgi.py
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
SSLEngine on
SSLCertificateFile /etc/apache2/ssl/site.site.com-cer.pem
SSLCertificateKeyFile /etc/apache2/ssl/site.site.com-key.pem
</VirtualHost>
(phase2_env) ubuntu#ip-10-0-10-125:~/django_portal/ci/site_portal/portal_site$ sudo service apache2 restart
* Restarting web server apache2 [ OK ]
(phase2_env) ubuntu#ip-10-0-10-125:~/django_portal/ci/site_portal/portal_site$
On top of the HTTP server, you need to run an SSL server with Apache2, i.e. configure a <VirtualHost *:443>. Also, I would let Apache2 handle the redirection instead of Django.
<VirtualHost *:80>
# Virtual host for the redirection
ServerName 10.206.51.6
ServerAdmin 10.206.51.6 # <- This should be an email!
DocumentRoot /dev/null/
Redirect permanent / https://10.206.51.6/ # Redirects also what is after the "/"
</VirtualHost>
<VirtualHost *:443>
# SSL site
ServerName 10.206.51.6
ServerAdmin 10.206.51.6 # <- This should be an email!
DocumentRoot /var/www/nfvs_portal/
SSLCertificateFile /etc/apache2/certs/your-server-certificate.pem
SSLCertificateKeyFile /etc/apache2/certs/your-server-certificate.key.pem
# ... [The rest of your site configuration]
</VirtualHost>
If you get into trouble even with this configuration, I would split the problem in (1) making Apache2 manage to redirect and serve a static SSL page in your document root (without using WSGI), and (2) modify that configuration to use your Django site.
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.