librelist archives

« back to archive

issues on webfaction

issues on webfaction

From:
Krisnadi Poedjosoedarmo
Date:
2012-05-10 @ 10:12
Hi fellows...

I have a complex group of questions to ask - hopefully nobody has asked
these questions already.

I am hosting my flask application on webfaction.com, and have an
interesting problem with the server throwing a 500 errors when trying to
access a SQLITE3 database. Webfaction support recommended that I set up
error logging using the logger module. Well, I couldn't get a simple file
logging mechanism to work either.

Before I list source codes, I wanted to mention the configuration setup for
my application on the server. It is set up using python 2.6 mod_wsgi 3.2
(per instructions here: http://flask.pocoo.org/snippets/65/). My apache
httpd.conf is set up very much like how this is set up. I also read through
the related stackoverflow entry (

http://stackoverflow.com/questions/3277657/how-deploy-flask-application-on-webfaction),
and made sure I followed the best suggestions, with the appropriate strings
substituted.

My index.py only has one line:
 1 from shopping_list import app as application

note: I copied the line numbers as well, both in the above source code and
below:

Following is the main application, which I have set up as a module (and not
a package), and imported into the 'index.py':

  1 # all the imports
  2 # from __future__ import with_statement # this apparently needs to be
imported first
  3 import sqlite3
  4 from flask import Flask, request, session, g, redirect, url_for, abort,
render_template, flash
  5 # from contextlib import closing
  6
  7 # configuration
  8 DATABASE = 'tmp/listItems.sdb' # in windows, this path is relative, so
the preceding '/' was dropped
  9 # DEBUG = True
 10 SECRET_KEY = 'development key'
 11 USERNAME = 'admin'
 12 PASSWORD = 'default'
 13
 14 app = Flask(__name__)
 15 # app.debug = True # once in production, this should be turned off
 16 app.config.from_object(__name__)
 17 # app.config.from_envvar('FLASKR_SETTINGS', silent=True)
 18
 19
 20 @app.route('/')
 21 def mainpage(): 22     """
 23     query = 'select id, description, vendor, unitPrice, quantity,
dt_added from list_items'
 24     conn = sqlite3.connect('tmp/listItems.sdb') # connect to database
 25     c = conn.cursor()
 26     c.execute(query)
 27     results = c.fetchall()
 28     c.close()
 29     """
 30     return render_template('index.html')
 31     #return render_template('index.html', results=results)
 32
 33
 34 @app.route('/login', methods=['GET', 'POST'])
 35 def login():
 36     error = None
 37     if request.method == 'POST':
 38         if request.form['username'] != app.config['USERNAME']:
 39             error = 'Invalid username'
 40         elif request.form['password'] != app.config['PASSWORD']:
 41             error = 'Invalid password'
 42         else:
 43             session['logged_in'] = True
44             flash('You were logged in')
 45             return redirect(url_for('mainpage'))
 46     return render_template('login.html', error=error)
 47
 48
 49 @app.route('/logout')
 50 def logout():
 51     session.pop('logged_in', None)
 52     flash('You were logged out')
 53     return redirect(url_for('mainpage'))
 54  55
 56 @app.route('/new', methods=['POST'])
 57 def new_entry():
 58     # process form entry
 59     if request.method == 'POST':
 60         """create a dict to use to insert new record into the database
and also
 61            to provide output"""
 62         nE = {}
 63         nE['description'] = request.form['description']
 64         nE['vendor'] = request.form['vendor']
 65         nE['unitPrice'] = request.form['unitPrice']
 66         nE['quantity'] = request.form['quantity']
 67         from time import gmtime, strftime
 68         nE['dt_added'] = strftime("%Y-%m-%dT%H:%M:%S", gmtime()) # set
the timestamp as a string
 69
 70         # return 'sqlite just does not work'
 71         keys = nE.keys()
 72         query = 'insert into list_items (description, vendor,
unitPrice, quantity, dt_added) values ("{0}","{1}",{2}
    ,{3},"{4}")'.format(nE['description'], nE["vendor"], nE["unitPrice"],
nE["quantity"], nE['dt_added'])
 73
 74         c = conn.cursor()
 75         c.execute(query)
 76         conn.commit()
 77         c.close()
 78         return redirect(url_for('mainpage'))
 79
 80
 81 if __name__ == "__main__":
 82     if not app.debug:
 83         import logging
 84         from logging import Formatter
 85         from logging.handlers import FileHandler
 86         file_handler = FileHandler('error_log')
 87         file_handler.setFormatter(Formatter(
 88             '%(asctime)s %)levelname)s: %(message)s '
 89             '[in %[pathname)s:%(lineno)d]'
 90         ))
 91         app.logger.addHandler(file_handler)
 92
 93     app.run()


Why am I getting 500 HTTP errors when the application communicates with
sqlite3, and why is the logger not working? I would appreciate some help
with this, because the Webfaction folks can not provide an answer.

Just to note, I started using Python about 2 years or so ago, on and off,
and after testing out cherrypy, web.py and finally flask, I find that Flask
is the best one so far. It works well on my dev environments (Windows and
Mac), but I seem to have issues on the CentOS host. I really would like for
this to work, and now I am regretting getting the shared host service
through them, because I am not fully in control of the environment.

Thanks.

Re: [flask] issues on webfaction

From:
Steven Kryskalla
Date:
2012-05-10 @ 13:34
On Thu, May 10, 2012 at 3:12 AM, Krisnadi Poedjosoedarmo
<neorou@gmail.com> wrote:
> Why am I getting 500 HTTP errors when the application communicates with
> sqlite3, and why is the logger not working? I would appreciate some help
> with this, because the Webfaction folks can not provide an answer.
>

The logging is not being configured because it's inside the if
__name__ == "__main__" block, which means it's only going to be set up
in development mode. Try moving it outside.

Do you have shell access on web faction? Try running the application
manually and seeing if it gives an error.

I've also seen problems with mod_wsgi and file permissions, where the
user that runs apache does not have access to create the sqlite
database. Try changing the DB path from a relative path to an absolute
path, and make sure that the apache user has access to create files at
that path (e.g. make the directory world writable). Also, how are you
initializing the database? (creating the tables)

-Steve

Re: [flask] issues on webfaction

From:
Krisnadi Poedjosoedarmo
Date:
2012-05-12 @ 22:09
Thanks, guys.

I think there may be a specific wsgi setting that I am missing.
I've tried using absolute paths for the database and the log file, and
still it is not working. When I try to run the application from the command
line, it simply starts the flask application at port 5000.

Also, I do not think I am using the logger correctly.

Steven, I've tried putting the logger in different places on the
application, and there are no errors written to the 'error_log' file.

Anyway, my temporary solution is to forego usage of Apache and mod_wsgi,
and run the flask application as a script listening to request from a
specific port #. That seems to run just fine for me.

In the meantime, I will read up on mod_wsgi and look for issues specific to
Webfaction hosting. If I find a solution, and remember to write back to
this e-mail, I will.

Thanks again for all your suggestions.



On Thu, May 10, 2012 at 9:34 AM, Steven Kryskalla <skryskalla@gmail.com>wrote:

> On Thu, May 10, 2012 at 3:12 AM, Krisnadi Poedjosoedarmo
> <neorou@gmail.com> wrote:
> > Why am I getting 500 HTTP errors when the application communicates with
> > sqlite3, and why is the logger not working? I would appreciate some help
> > with this, because the Webfaction folks can not provide an answer.
> >
>
> The logging is not being configured because it's inside the if
> __name__ == "__main__" block, which means it's only going to be set up
> in development mode. Try moving it outside.
>
> Do you have shell access on web faction? Try running the application
> manually and seeing if it gives an error.
>
> I've also seen problems with mod_wsgi and file permissions, where the
> user that runs apache does not have access to create the sqlite
> database. Try changing the DB path from a relative path to an absolute
> path, and make sure that the apache user has access to create files at
> that path (e.g. make the directory world writable). Also, how are you
> initializing the database? (creating the tables)
>
> -Steve
>

Re: [flask] issues on webfaction

From:
Krisnadi Poedjosoedarmo
Date:
2012-05-12 @ 22:10
By the way, I also restarted httpd every time I changed anything.

On Sat, May 12, 2012 at 6:09 PM, Krisnadi Poedjosoedarmo
<neorou@gmail.com>wrote:

> Thanks, guys.
>
> I think there may be a specific wsgi setting that I am missing.
> I've tried using absolute paths for the database and the log file, and
> still it is not working. When I try to run the application from the command
> line, it simply starts the flask application at port 5000.
>
> Also, I do not think I am using the logger correctly.
>
> Steven, I've tried putting the logger in different places on the
> application, and there are no errors written to the 'error_log' file.
>
> Anyway, my temporary solution is to forego usage of Apache and mod_wsgi,
> and run the flask application as a script listening to request from a
> specific port #. That seems to run just fine for me.
>
> In the meantime, I will read up on mod_wsgi and look for issues specific
> to Webfaction hosting. If I find a solution, and remember to write back to
> this e-mail, I will.
>
> Thanks again for all your suggestions.
>
>
>
>
> On Thu, May 10, 2012 at 9:34 AM, Steven Kryskalla <skryskalla@gmail.com>wrote:
>
>> On Thu, May 10, 2012 at 3:12 AM, Krisnadi Poedjosoedarmo
>> <neorou@gmail.com> wrote:
>> > Why am I getting 500 HTTP errors when the application communicates with
>> > sqlite3, and why is the logger not working? I would appreciate some help
>> > with this, because the Webfaction folks can not provide an answer.
>> >
>>
>> The logging is not being configured because it's inside the if
>> __name__ == "__main__" block, which means it's only going to be set up
>> in development mode. Try moving it outside.
>>
>> Do you have shell access on web faction? Try running the application
>> manually and seeing if it gives an error.
>>
>> I've also seen problems with mod_wsgi and file permissions, where the
>> user that runs apache does not have access to create the sqlite
>> database. Try changing the DB path from a relative path to an absolute
>> path, and make sure that the apache user has access to create files at
>> that path (e.g. make the directory world writable). Also, how are you
>> initializing the database? (creating the tables)
>>
>> -Steve
>>
>
>

Re: [flask] issues on webfaction

From:
dorian i
Date:
2012-05-10 @ 14:20
Last time i tried on webfaction a django app with sqlite and i fixed by
doing an absolute path on the config. Try that.

On Thu, May 10, 2012 at 3:34 PM, Steven Kryskalla <skryskalla@gmail.com>wrote:

> On Thu, May 10, 2012 at 3:12 AM, Krisnadi Poedjosoedarmo
> <neorou@gmail.com> wrote:
> > Why am I getting 500 HTTP errors when the application communicates with
> > sqlite3, and why is the logger not working? I would appreciate some help
> > with this, because the Webfaction folks can not provide an answer.
> >
>
> The logging is not being configured because it's inside the if
> __name__ == "__main__" block, which means it's only going to be set up
> in development mode. Try moving it outside.
>
> Do you have shell access on web faction? Try running the application
> manually and seeing if it gives an error.
>
> I've also seen problems with mod_wsgi and file permissions, where the
> user that runs apache does not have access to create the sqlite
> database. Try changing the DB path from a relative path to an absolute
> path, and make sure that the apache user has access to create files at
> that path (e.g. make the directory world writable). Also, how are you
> initializing the database? (creating the tables)
>
> -Steve
>

Re: [flask] issues on webfaction

From:
Ron DuPlain
Date:
2012-05-10 @ 13:27
On Thu, May 10, 2012 at 6:12 AM, Krisnadi Poedjosoedarmo
<neorou@gmail.com> wrote:
> Why am I getting 500 HTTP errors when the application communicates with
> sqlite3

This is likely because you have a relative path in your sqlite3 db
path, and the working directory is not what you think it is.

-Ron