Deployment Recipes Part 5 – Python, Web2py, Apache and Debian

In this section we look at trouble shooting certain problematic areas. This may or may not happen in your case as it all depends on the configuration you have done for apache or if your system admin has done for your web-server.

Troubleshooting

5] Troubleshooting

Apache logs in Debian are stored in /var/log/apache2/

You can check access.log or error.log files to check latest logs to find what is going on or what went wrong. There are other log files as well and these can be pretty helpful to check if the requests are even going to web2py.

Web2py logs can be checked in web2py folder which in our case resides here

 /home/www-data/web2py/logs/

Web2py tickets/errors can be checked in web2py folder which in our case resides here

 /home/www-data/web2py/applications/<application_name>/errors

Other tips

  • All web2py errors should be checked and fixed manually till the application works flawlessly.
  • If  you are saving files and images from your application to  server folders make sure that appropriate permissions are given to those folders.
  • You may want to enable app admin securely from web ..
  • When new updates are done to app or a new patch is provided and you get a ticket for unrecoverable error simply give permissions to mentioned folders in the permissions section of this document chmod..
  • If you cannot see the images properly in production but are seen properly in local just check the file name extension for example file1.JPG or if its file1.jpg. Linux is case sensitive!
  • Delete contents of the sessions directory daily. Or have a policy to delete the contents to keep the disk free

Path separator errors

Use “/” instead of “\\”

Pythonic way

Assuming you know the file name and path, you can return any file to the browser via a controller action that calls response.stream. In a controller (e.g., default.py):

import os

def serve_file():

    filename = request.args(0)

    path = os.path.join(request.folder, 'private', 'file_subfolder', filename)

    return response.stream(path)

In a view, you would then include an image as follows:

<img src=”{{=URL(default, serve_file, args=filename)}}” />

If the images are intended to be public, a better option is to store them in the /static folder — any files in /static can simply be served directly via their URL:

<img src=”{{=URL(‘static’, ‘image_files’, args=filename)}}” />

This method does not require a special controller action to call response.stream.

Reference code from the app

import platform
        is_windows=(platform.system().lower().find("win") > -1)        
        is_linux=(platform.system().lower().find("lin") > -1)        
        is_mac=(platform.system().lower().find("mac") > -1)        

        if (is_windows == True):
            #print "is windows"
            thumbpath = os.path.join(request.folder, 'static', 'thumbs\\') + thumb-nail_filename
        elif (is_linux == True):
            #print "is linux"
            thumbpath = os.path.join(request.folder, 'static', 'thumbs/') + thumb-nail_filename
        elif (is_mac == True):
            #print "is macos"
            thumbpath = os.path.join(request.folder, 'static', 'thumbs/') + thumb-nail_filename
        else: ## specify the default thumbnail
            #print "is macos"
            thumbpath = os.path.join(request.folder, 'static', 'thumbs/') + "de-fault.thumbnail.jpg"

For permission error

Make sure you give permissions to appropriate folders

Next, lets provide appropriate permissions to folders and files including svg files

sudo chmod ugo+rw *.svg inside folder  OR

sudo chmod ugo+rw charts_reports OR

sudo chmod ugo+rw static (for providing permissions to static folder)

sudo chmod ugo+rw user_pics

sudo chmod ugo+rw uploads

sudo chmod ugo+rw  private

Using chmod -R 755 will set this as permissions to all files and folders in the tree. But why on earth would you want to? It might make sense for the directories, but why set the execute bit on all the files?

I suspect what you really want to do is set the directories to 755 and either leave the files alone or set them to 644. For this, you can use the find command. For example:

To change all the directories to 755 (drwxr-xr-x):

find /opt/lampp/htdocs -type d -exec chmod 755 {} \;

To change all the files to 644 (-rw-r–r–):

find /opt/lampp/htdocs -type f -exec chmod 644 {} \;

To force report lab re-installation using PIP for any python program

This resolves ValueError: Attempted relative import in nonpackage

pip install –upgrade –force-reinstall reportlab

To reslove Temporary failure in name resolution [Err -3]

 Temporary failure resolving ‘deb.debian.org’

E: Failed to fetch http://deb.debian.org/debian/pool/main/t/tcl8.6/tcl8.6_8.6.6+dfsg-1+b1_amd64.deb  Temporary failure resolving ‘deb.debian.org’ [Answer] Enter your server here nano /etc/resolv.conf  . Please check DNS servers. If they are google publc dns then replace the servers by following IP.  68.183.239.233  . This will resolve the issue. Please note this IP will be different in your case. I am providing the absolute IP of the system where I had hosted my application.

Other solution

1) Make sure your DNS Resolver config file is writable:
sudo chmod o+r /etc/resolv.conf

2) Temporarily change your DNS to use Google’s nameservers instead of DigitalOcean’s:
sudo nano /etc/resolv.conf

Change the IP address in the file to: 8.8.8.8

Press CTRL+X to save the file.

Dhakate Rahul

Dhakate Rahul

Leave a Reply

Your email address will not be published. Required fields are marked *