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

You may be wondering why we are doing this all manually. Web2py does provide us with pre-cooked scripts that can take care of almost 90% of these tasks when appropriate permissions are available but I am concerned about the remaining 10% where you may not be using the same OS or webserver. This is where the manual configuration steps help us.

Configuring  postgres for web2py

Make sure that postgres is installed properly and all your databases are created for you.

1. ss –ltn  gives which porst are open for listening

2. psql –U postgres –d (dbname) –h 127.0.0.1 <Enter> provide password for postgres and check if it works .

3. make sure  in /etc/postgresql/9.6/main/postgresql.conf

listen_addresses=‘localhost‘ is turned on

track_counts = on

autovacuum =on

...
listen_addresses = 'localhost' 
...
track_counts = on
...
autovacuum = on   # Enable autovacuum subprocess?  'on'
...

4. Edit /etc/postgresql/9.6/main/pg_hba.conf

...
# "local" is for Unix domain socket connections only
local   all             all                                     trust
# IPv4 local connections:
host    all             all             127.0.0.1/32            trust
# IPv6 local connections:
host    all             all             ::1/128                 trust
...

5. Restart postgres using below

sudo /etc/init.d/postgresql restart

Logs are here

/var/log/postgresql/

Successful Deployment Files and settings

1- Check files in /etc/apache2/ folder

Apache2.conf

httpd.conf  – Though we do not need httpd.conf (in Debian) I created a blank one manually with nothing in it.

Web2py.conf – Append what ever data is specified in web2py deployment documentation , no changes in it

2 – Check files in /home/www-data/web2py/

Routes.py

Wsgihandler.py

These files need to be present in the folder routes.py as specified above and Wsgihandler.py as-is

3 – Sites enabled for me is currently this. Nothing really needs to be done here

default.conf or now 000-default.conf

Sample Application Configuration

We need to insert a user and we need to insert records in settings for that user –

Below folders need permission after a deployment – Otherwise application will throw permission errors and you wouldn’t know where the errors are coming from.

  1. private

If permission is not given to this folder you will receive this error like below
IOError: [Errno 13] Permission denied: u’/home/www-data/web2py/applications/samples/private/af5c0a3a-9e15-4441-be74-14f3eee11c30′

  • databases
  • errors

If permissions are not given to errors folder – Errors will not be logged.

  • sessions
  • static (including following folders so that they can write to these folders – hlogo, images, logo, pat_pics, prod_pics, user_pics)
  • uploads

Use the command – to give permissions from root user.

sudo chmod -R ugo+rw [FOLDER NAME]

Here drwxrwxrwx means we have complete permissions to these folders

  • chmod – change permissions

The breakdown of the above command looks like:

  • sudo – this is used to gain admin rights for the command on any system that makes use of sudo (otherwise you’d have to ‘su’ to root and run the above command without ‘sudo’)
  •  
  • chmod – the command to modify permissions
  • -R – this modifies the permission of the parent folder and the child objects within
  • ugo+rw – this gives User, Group, and Other read and write access.
  • As you can probably surmise, this command opens wide the SHARE folder such that anyone on the system can have access to that folder. As I mentioned earlier, a more secure method would be to use groups. But we’re just using this for the purpose of demonstration.
  • The breakdown of permissions looks like this:
  • u – user
  • g – group
  • o – other
  • The ‘other’ entry is the dangerous one, as it effectively gives everyone permission for the folder/file. The permissions you can give to a file or folder are:
  • r – read
  • w – write
  • x – execute
  • Using the -R switch is important. If you have a number of sub-folders and files within the SHARE directory, and you want the permissions to apply from the parent object (the containing folder) to the child objects (the sub-folders and files), you must use the -R (recursive) switch so the same permissions are applied all the way to the deepest folder, contained within the parent.
  • chown – change ownership.
  •             sudo chown -R bethany /DATA/SHARE
  • Let’s break this down.
  • sudo – admin rights must be used since we are dealing with a folder that belongs to another user
  • chown – the command for changing ownership
  • -R – the recursive switch to make sure all child objects get the same ownership changes
  • bethany – the new owner of the folder
  • /DATA/SHARE – the directory to be modified
  • Should Bethany send the folder back to Jacob, the ownership would need to again be changed (again, this will be simplified with the use of groups).
  • Samples routes.py
  • Next up for HMS is exposing the file in routes.py in web2py directory  
  • Production code for web2py folder for routes.py
  • routers = dict(
  • #base router
  • BASE=dict(
  •        #default_application=’Targez’,
  •        domains= {
  •                 ‘www.targetsoftware.co.in’ : ‘Targez’,
  •                           },
  •        #default_application = ‘Target’,
  •                 applications=[‘Target’,],
  •        default_function = ‘index’,
  • ),
  • Routes.py [Production]
  • Complete from production
  • # -*- coding: utf-8 -*-
  • #  This is an app-specific example router
  • #
  • #  This simple router is used for setting languages from app/languages directory
  • #  as a part of the application path:  app/<lang>/controller/function
  • #  Language from default.py or ‘en’ (if the file is not found) is used as
  • #  a default_language
  • #
  • # See <web2py-root-dir>/router.example.py for parameter’s detail
  • #————————————————————————————-
  • # To enable this route file you must do the steps:
  • #
  • # 1. rename <web2py-root-dir>/router.example.py to routes.py
  • # 2. rename this APP/routes.example.py to APP/routes.py
  • #    (where APP – is your application directory)
  • # 3. restart web2py (or reload routes in web2py admin interfase)
  • #
  • # YOU CAN COPY THIS FILE TO ANY APPLICATION’S ROOT DIRECTORY WITHOUT CHANGES!
  • from gluon.fileutils import abspath
  • from gluon.languages import read_possible_languages
  • #possible_languages = read_possible_languages(abspath(‘applications’, app))
  • #NOTE! app – is an application based router’s parameter with name of an
  • #            application. E.g.’welcome’
  • routers = dict(
  •           #base router
  •           BASE=dict(
  •                    #default_application=’Target’,
  •                    domains= {
  •                     ‘targetsoftware.co.in’ : ‘Targez’,
  •                     ‘thesamples.in’ : ‘samples’,
  •                               },
  •                    #default_application = ‘Target’,
  •                 applications=[‘Targez’, ‘samples’,],
  •                    default_function = ‘index’,
  •                 ),
  •         Target = dict(
  •           default_language = ‘en’,
  •                   languages = [‘en’],
  •           default_function = ‘index’,
  •                    ),
  •           partpic = dict(
  •           default_language = ‘en’,
  •           languages = [‘en’],
  •                   default_function = ‘index’,
  •           map_hyphen = True,
  •                    ),
  •         flock = dict(
  •                 default_language = ‘en’,
  •                 languages = [‘en’],
  •                 default_function = ‘index’,               
  •                 ),
  •         jungleride = dict(
  •                 default_language = ‘en’,
  •                 languages = [‘en’],
  •                 default_function = ‘index’,               
  •                 ),
  •         )
  • #NOTE! To change language in your application using these rules add this line
  • #in one of your models files:
  • #   if request.uri_language: T.force(request.uri_language)
Dhakate Rahul

Dhakate Rahul

Leave a Reply

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