Apache2 Python script not running in browser - python

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.

Related

why let's encrypt certificate(SSL) error installing django site which is hosted in Ubuntu vps

I have successfully deployed the Django project in my ubuntu VPS server. but when i try to install sudo certbot --apache -d amritshahi.com. I am getting the following error.
Enabling available site: /etc/apache2/sites-available/365-le-ssl.conf
Error while running apache2ctl configtest.
Action 'configtest' failed.
The Apache error log may have more information.
AH00526: Syntax error on line 25 of /etc/apache2/sites-enabled/365.conf:
Name duplicates previous WSGI daemon definition.
Rolling back to previous server configuration...
Error while running apache2ctl configtest.
Action 'configtest' failed.
The Apache error log may have more information.
AH00526: Syntax error on line 25 of /etc/apache2/sites-enabled/365.conf:
Name duplicates previous WSGI daemon definition.
inside 365.conf file:
<VirtualHost *:80>
ServerAdmin me#amritshahi.com
ServerName amritshahi.com
ServerAlias www.amritshahi.com
DocumentRoot /var/www/personal
Alias /static /var/www/personal/Personal_Blog/static
<Directory "/var/www/personal/Personal_Blog/static">
Options Indexes FollowSymLinks
Order allow,deny
Allow from all
Require all granted
</Directory>
Alias /media /var/www/personal/Personal_Blog/media
<Directory "/var/www/personal/Personal_Blog/media">
Options Indexes FollowSymLinks
Order allow,deny
Allow from all
Require all granted</Directory>
ErrorLog ${APACHE_LOG_DIR}/shahi_error.log
CustomLog ${APACHE_LOG_DIR}/shahi_access.log combined
WSGIDaemonProcess Personal_Blog python-home=/var/www/personal/Personal_>
WSGIProcessGroup Personal_Blog
WSGIScriptAlias / /var/www/personal/Personal_Blog/project/wsgi.py
<Directory /var/www/personal/Personal_Blog/project>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
</VirtualHost>
Help Please??
Issue resloved using following step:
comment affected line
install certificate
agan come to orign one and change WSGIDaemonProcess name
restar apache server

Deploying Flask app on a vps (Apache/Debian)

I'm trying to deploy a Flask app on a vps which OS is Debian and which is using Apache. I'm completely new to Apache so my problem may look simple but yet I was not able to find any solution on the web (I read a couple of tutorials and searched in this forum).
The server is running fine (it is already used for other purposes). When I try to access my app with www.myservername/web_app I get a 404 Not Found Error. Following http://flask.pocoo.org/docs/0.12/deploying/mod_wsgi/, here is what I've done:
I placed my application in /var/www/web_app/. In /var/www/web_app/app/init.py I created an instance of my app (named app) and configured it.
I placed the following .wsgi file in /var/www/web_app/:
activate_this = '/var/www/web_app/.pyenv/bin/activate_this.py'
execfile(activate_this, dict(__file__=activate_this)
import sys
from app import app as application
Here is the /etc/apache2/sites-available/000-default.conf file (the site is enabled) :
<VirtualHost *:80>
ServerAdmin webmaster#localhost
DocumentRoot /var/www
<Directory /var/www/html/redmine>
RailsBaseURI /redmine
PassengerResolveSymlinksInDocumentRoot on
</Directory>
<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
Order allow,deny
Allow from all
</Directory>
WSGIDaemonProcess app user=www-data group=www-data threads=5
WSGIScriptAlias /web_app /var/www/web_app/app.wsgi
<Directory /var/www/web_app>
WSGIProcessGroup app
WSGIApplicationGroup %{GLOBAL}
Order allow,deny
Allow from all
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
LogLevel warn
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
I also tried to replace /web_app with /var/www/web_app in the WSGIScriptAlias and also to access myservername/var/www/web_app. Moreover I tried to comment out the Rails/Passenger directives in the first Directory tag.
Of course I installed the wsgi module. I restarted the server before trying to access the page. Here is the access log:
... "GET /web_app HTTP/1.1" 404 508
And the error log :
[error] ... File does not exist : /var/www/web_app
Finally, my flask app - when using flask development server - works fine.
Thanks for your help!

how to configure django on apache and make https in the apache so that django url should open with https://<ip>:80 port

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.

Django app hangs when attempting to connect to database via Apache

I am having difficulty troubleshooting this issue. I have a Django app running on an Ubuntu 14.04 server (with Apache 2.4 and mod_wsgi for Python 3.4). It connects to SQL Server via pymssql.
In development, the app works fine. I query the database, and the database returns the expected results.
In production (under the Apache user), however, the script hangs at the exact point that a database query is made. My browser (Chrome or Firefox) shows a spinning wheel that continues to spin as long as the browser window is open.
I have the following in my apache2.conf file:
ServerName localhost
# WSGIDaemonProcess application
WSGIPythonPath /home/production_code/python3env/lib/python3.4/site-packages:/home/production_code/school
# WSGIProcessGroup application
WSGIScriptAlias / /home/production_code/school/school/wsgi.py
# Python virtualenv home
WSGIPythonHome /home/production_code/python3env
# Include the virtual host configurations:
IncludeOptional sites-enabled/*.conf
And the following in my sites-enabled/000-default.conf file:
<VirtualHost *:80>
ServerAdmin *****#school.edu
ServerName localhost
DocumentRoot /var/www/html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
Alias /static/ /home/production_code/school/static/
<Directory /home/production_code/school/>
Require all granted
</Directory>
<Directory /home/production_code/school/>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
<Directory /home/production_code/school/static>
Require all granted
</Directory>
</VirtualHost>
Does anyone have any idea what might be causing this or how I might troubleshoot this? The Apache error logs and access logs are not particularly helpful in this situation, since a response to the request is never rendered. Similarly, Django debugging is also not useful here.
Instead of:
# WSGIDaemonProcess application
WSGIPythonPath /home/production_code/python3env/lib/python3.4/site-packages:/home/production_code/school
# WSGIProcessGroup application
use:
WSGIDaemonProcess application python-path=/home/production_code/python3env/lib/python3.4/site-packages:/home/production_code/school
WSGIProcessGroup application
WSGIApplicationGroup %{GLOBAL}
A key part of this is the WSGIApplicationGroup directive, with it being set to %{GLOBAL}.
This is to get around faulty third party extension modules for Python that don't work in sub interpreters and which can fail with a dead lock or crash.
See:
https://code.google.com/p/modwsgi/wiki/ApplicationIssues#Python_Simplified_GIL_State_API
It is also recommend you go back to using daemon mode. It is generally not a good idea to use embedded mode.
http://blog.dscpl.com.au/2012/10/why-are-you-using-embedded-mode-of.html

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

Categories

Resources