Category Archives: python

Python Flask using mod_wsgi in Apache; how to get venv to work?

I've been trying to get this to work, and have searched everywhere and read page up and page down, but doesn't seem to find an answer.

I have Apache with mod_wsgi and a simple test Flask application.

I got it working using this: https://www.jakowicz.com/flask-apache-wsgi/

Then I somehow found that Apache mod_wsgi used the system python and I want to use venv (https://docs.python.org/3/library/venv.html).

My application is in a directory with the normal directory structure of the venv, but how do I get my application to use that?

I found this: http://blog.dscpl.com.au/2014/09/using-python-virtual-environments-with.html

But if I put in python-home my application fails.

A couple of questions: How do I find the Python version that my app is using? How do I find my mod_wsgi version? How do I get my app to use my venv?

I'm new to Python and WSGI, I have mostly worked with PHP.

Hope someone can explain to me what to do...

-- Ronni

Django doesn’t find file just in remote server

I have a project in Django where I have a function that read a file. In my local enviroment using virtualenv all it's ok, it reads the file. But, when I put it in our remote server (using LINUX Ubuntu + Apache 2), it show's that Python can't find the file. What I'm doing wrong?

utils.py

def set_vehicles_negotiations(self):
    # This method will get all current negotiations that means and set it in the database
    with open("reports/dash_diversos.csv", 'rt', encoding='utf8') as csvfile: ...

views.py

...
def dashboard(request):
     csvReaders = CSVReaders()
     csvReaders.set_vehicles_negotiations()

settings.py

...
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static') # This line exist's only in the remote server

Directory structure:

mydashboard
- dashboard
-- utils.py
-- views.py
- reports
-- dash_diversos.csv

The error message:

[Errno 2] No such file or directory: 'reports/dash_diversos.csv'

apache/sites-available/000-default.conf

<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

        ServerAdmin ***
        DocumentRoot /var/www/dashboard

        # 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 /var/www/dashboard/static
        <Directory /var/www/dashboard/static>
                Require all granted
        </Directory>

        Alias /media /var/www/dashboard/media
        <Directory /var/www/dashboard/media>
                Require all granted
        </Directory>

        <Directory /var/www/dashboard/ceodashboard>
                <Files wsgi.py>
                        Require all granted
                </Files>
        </Directory>

    WSGIDaemonProcess dashboard python-home=/var/www/dashboard/dashboardenv python-path=/var/www/dashboard
    WSGIProcessGroup dashboard
    WSGIScriptAlias / /var/www/dashboard/ceodashboard/wsgi.py

</VirtualHost>

I already search in various websites as well it StackOverflow. I just don't understand why it works in my local machine and not in my server.

Django doesn’t find file just in remote server

I have a project in Django where I have a function that read a file. In my local enviroment using virtualenv all it's ok, it reads the file. But, when I put it in our remote server (using LINUX Ubuntu + Apache 2), it show's that Python can't find the file. What I'm doing wrong?

utils.py

def set_vehicles_negotiations(self):
    # This method will get all current negotiations that means and set it in the database
    with open("reports/dash_diversos.csv", 'rt', encoding='utf8') as csvfile: ...

views.py

...
def dashboard(request):
     csvReaders = CSVReaders()
     csvReaders.set_vehicles_negotiations()

settings.py

...
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static') # This line exist's only in the remote server

Directory structure:

mydashboard
- dashboard
-- utils.py
-- views.py
- reports
-- dash_diversos.csv

The error message:

[Errno 2] No such file or directory: 'reports/dash_diversos.csv'

I already search in various websites as well it StackOverflow. I just don't understand why it works in my local machine and not in my server.

Apache + mod_wsgi + flask app: "Unable to get bucket brigade for request" error in logs

I have a apache2/mod_wsgi/flask/python2.7 app. Sometimes such line appears in log file:

Partial results are valid but processing is incomplete: [client 45.132.96.124:34500] mod_wsgi (pid=13920): Unable to get bucket brigade for request., referer: https://my-site.com/some_url/

What is "bucket brigade" and what this error actually means? Is it apache error or wsgi server error? What does lead to this error? If this is some unhandled exception - how to handle it and where?

Thanks in advance.

when running python script in tomcat getting string

After Configure Tomcat to run Python CGI scripts in windows and Run the script in localhost8080 I am getting String for Example.

#!C:\Python27\python.exe
print "Content-type: text/html"
print
print "<html><head>"
print ""
print "</head><body>"
print "Hello "
print "</body></html>"

When I am trying to Run The above script I should get output Hello instead I am getting total Program.

For Configure Tomcat to run Python CGI scripts I have followed the following URL http://lekshmideepu.blogspot.in/2013/03/configure-tomcat-7-to-run-python-cgi.html

Python script does not start again properly after closing it

I am trying to build a robot which I can control myself through a website.
I have coded a Python script which lets the robot drive in a (more or less) straight line. Activating the script through the website was no problem but the problems appear when I try to stop it and restart it.
When I click the stop button it does this in a php file:

exec("sh /home/pi/Maxwheel/ScriptePython/geradeaus/kill.sh");
exec("sudo python /home/pi/Maxwheel/ScriptePython/geradeaus/stopp.py");

kill.sh will execute this command to kill the python script:

sudo pkill -f vorne.py

stopp.py will clean the pins up again. I am already doing this at the end of the Python script but I have experienced when I simply kill the script the robot will still drive forward. I figured that killing the script will not trigger the GPIO.cleanup() At the end of my Python script. Yes the Python Script is being stopped / killed (checked with ps aux | grep python)

The robot then comes to a stop. But when I try to start the script again through a button, the robot does not start properly. It will try to drive but only for a second and then comes to a stop. After that I have to push it and then it will drive again for like a second. This does not make sense to me because the Python script is in a loop so the robot should never stop. Even when I try to start the script manually it won't work.

This is the code from "stopp.py". I just setup all the Pins again like I did it in the Python Script and clean them up right after that.

import RPi.GPIO as GPIO

GPIO.setmode(GPIO.BCM)
GPIO.setup(23, GPIO.OUT)
GPIO.setup(24, GPIO.OUT)
GPIO.setup(18, GPIO.OUT)
GPIO.setup(13, GPIO.OUT)
GPIO.setup(17, GPIO.OUT)
GPIO.setup(27, GPIO.OUT)
GPIO.setup(5, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(6, GPIO.IN, pull_up_down=GPIO.PUD_UP)

GPIO.cleanup()

Operational Error : unable to open database file

Trying to deploy a Django app over Apache . Installed all dependencies, Apache is running fine , no errors, however when I launch the domain, It gives the error :

Operational Error : unable to open database file

This is where the database is stored :

/home/wger/db/database.sqlite

And in order to make sure I have the permissions, I did :

chgrp www-data /home/wger/db/
chgrp www-data /home/wger/db/database.sqlite
chmod g+w /home/wger/db/
chmod g+w /home/wger/db/database.sqlite
chmod 664 /home/wger/db/database.sqlite
chmod 664 /home/wger/db
sudo chown :www-data /home/wger/db
sudo chown :www-data /home/wger/db/database.sqlite
sudo service apache2 restart

However, the issue still persist. what is possibly causing this and how to effectively resolve this ?

PS : I tried existing answers to identical questions, however I was not able to resolve the error!

Error ImportError: No module named ‘encodings’ in virtual environment using pyenv

I am trying to publish the site created by Django using apache. The settings of the server are as follows.

・ CentOS 7.2
・ Python 3.6
・ Django 2.0
・ apache 2.4

I am preparing a virtual environment using pyenv as follows.

git clone https://github.com/yyuu/pyenv.git ~/.pyenv
…
pyenv install anaconda3-5.1.0
pyenv rehash
pyenv global anaconda3-5.1.0
…
yum install httpd httpd-devel
systemctl start httpd
systemctl enable httpd
….
wget https://github.com/GrahamDumpleton/mod_wsgi/archive/4.5.14.tar.gz
tar -zxvf 4.5.14.tar.gz
cd mod_wsgi-4.5.14/
./configure --with-python=/home/username/.pyenv/versions/anaconda3-5.1.0/bin/python
make
sudo make install
…

■ httpd.conf

NameVirtualHost *:80
LoadModule wsgi_module /usr/lib64/httpd/modules/mod_wsgi.so
WSGISocketPrefix /var/run/wsgi
<VirtualHost *:80>

ServerName xxx.com
DocumentRoot /var/www/html

WSGIScriptReloading On
WSGIDaemonProcess xxx python-path=/home/username/.pyenv/versions/anaconda3-5.1.0/lib/python3.6/site-packages python-home=/home/username/.pyenv/versions/anaconda3-5.1.0
WSGIProcessGroup xxx
WSGIScriptAlias / /var/www/html/xxx/xxx/wsgi.py

<Directory "/xxx/">
Order deny,allow
</Directory>
</VirtualHost>

The following error will occur with this setting.

Fatal Python error: Py_Initialize: Unable to get the locale encoding
ModuleNotFoundError: No module named 'encodings'

Setting chmod should be fine. What other reasons can be considered?

Raspberry Pi live video streaming with Flask App works with default development server but not in Apache production

I've hosted a simple Flask App on Apache webserver on a Raspberry Pi to stream real-time video from the PiCamera using tutorial from https://blog.miguelgrinberg.com/post/video-streaming-with-flask . Everything works fine locally for me and I can access my http://localip:5000 to get my live video feed. I now wanted to host this on a production server and I used Apache. I set all the configuration files and directory structures linked my Flask app and set the server for port 80. I visit the http://ip:80 and my webpage loads with a title, heading and static text but the video that is supposed to be below it is now a picture of a "broken image". Inspecting the webpage video element on the browser shows that the server returned nothing.

I saw another post Video Straming on raspberrypi using flask apche2and wsgi server, of a guy who ran into the same problem as mine. He marked an answer saying Apache wasn't having root access to the picamera and hence wasn't able to access it. I know that Apache runs as www-data group user and I don't know which folder/file I should give permissions so that it can access the PiCamera. I don't know how to go about from here.Locally using the inbuild flask dev web server everything is working perfectly but on serving through Apache the camera isn't capturing the images. Please help me. Here is my basic code and configurations (Flask app name is VideoStream):

VideoStream.wsgi

#!/usr/bin/python3
import sys
import logging
logging.basicConfig(stream=sys.stderr)
sys.path.insert(0,"/var/www/VideoStream/")

from VideoStream import app as application

Apache Configuration file:

<VirtualHost *:80>
                ServerName 10.0.0.69
                ServerAdmin [email protected]
                WSGIScriptAlias / /var/www/VideoStream/VideoStream.wsgi
                WSGIDaemonProcess VideoStream user=www-data group=www-data threads=5 home=/var/www/VideoStream/
        <Directory /var/www/VideoStream/VideoStream/>
                WSGIProcessGroup VideoStream
                WSGIApplicationGroup %{GLOBAL}
                WSGIScriptReloading On
                Order allow,deny
                Allow from all
                </Directory>
                ErrorLog ${APACHE_LOG_DIR}/VideoStream-error.log
                LogLevel warn
                CustomLog ${APACHE_LOG_DIR}/VideoStream-access.log combined 
</VirtualHost>

Directory Structure:

/var/www/VideoStream/
VideoStream.wsgi
VideoStream/
    __init__.py
    camera_pi.py
    base_camera.py
    templates/index.html

__init__.py - Flask app :

#!/usr/bin/python3
from importlib import import_module
import os
from flask import Flask, render_template, Response
from VideoStream.camera_pi import Camera


app = Flask(__name__)


@app.route('/')
def index():
   return render_template('index.html')
def gen(camera):

while True:
    frame = camera.get_frame()
    yield (b'--frame\r\n'
           b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n')
@app.route('/video_feed')
def video_feed():

    return Response(gen(Camera()),
                mimetype='multipart/x-mixed-replace; boundary=frame')


if __name__ == '__main__':
    app.run(host='10.0.0.69', threaded=True, debug=True)