Deploying OpenShift applications using SQLite

Code: 
KB-E1002
Applies to: 
OpenShift
Issue: 
When I configure and deploy my application, I need to know where to place my SQLite database
Resolution: 

The short solution to this issue is to store your SQLite database one level up from your git repository in the ‘data’ directory.

The long explanation is that when you deploy new versions of your application, the OpenShift service will replace your application tree with a fresh copy of your git repository. The result of this is that any files that were created by your application will be deleted if they are stored in your directories under your git repository. The directory structure for new applications looks like the following:

PHP
|-- Application Name
| |-- conf
| | |-- magic -> /etc/httpd/conf/magic
| | `-- php.ini
| |-- conf.d
| |-- data
| |-- logs
| | |-- access_log
| | `-- error_log
| |-- modules -> /usr/lib64/httpd/modules
| |-- repo
| | |-- data
| | | `-- README
| | |-- libs
| | | `-- README
| | |-- php
| | | |-- health_check.php
| | | `-- index.php
| | `-- README
| |-- run
| |-- sessions
| `-- tmp

WSGI
|-- Application Name
| |-- conf
| | `-- magic -> /etc/httpd/conf/magic
| |-- conf.d
| |-- data
| |-- logs
| | |-- access_log
| | `-- error_log
| |-- modules -> /usr/lib64/httpd/modules
| |-- repo
| | |-- data
| | | `-- SAFETOREMOVE.txt
| | |-- libs
| | | `-- SAFETOREMOVE.txt
| | |-- README
| | `-- wsgi
| | `-- application
| |-- run
| |-- sessions
| `-- tmp

Rack
|-- Application Name
| |-- conf
| | `-- magic -> /etc/httpd/conf/magic
| |-- conf.d
| |-- data
| |-- deploy
| | |-- config.ru
| | |-- public
| | `-- tmp
| |-- logs
| | |-- access_log
| | `-- error_log
| |-- modules -> /usr/lib64/httpd/modules
| |-- rack
| | `-- tmp
| `-- run

The ‘repo’ directory for PHP and WSGI applications, and the ‘deploy’ directory for Rack applications contain the contents of your git repository. When a new application is created using rhc-create-app, the following default directory structure is created on your hard drive when rhc-create-app pulls down the new git repository:

PHP
Application Name
|-- data
| `-- README
|-- libs
| `-- README
|-- php
| |-- health_check.php
| `-- index.php
`-- README

WSGI
Application Name
|-- data
| `-- SAFETOREMOVE.txt
|-- libs
| `-- SAFETOREMOVE.txt
|-- README
`-- wsgi
`-- application

Rack
Application Name
`-- config.ru

For security purposes, it is best practice to make sure that your database files are not directly accessible from the Internet. As a result, the recommendation is to place your database outside of the git repository which in the case of OpenShift is the ‘repo’ directory for PHP and WSGI or the ‘deploy’ directory for Rack.

If you are deploying a fresh application, you should have your application create your database in the correct location. If you have an existing database that you want to use when deploying your application, you can:

  1. temporarily add it to your git repository
  2. write some code at /copydb that when accessed will copy the database from inside your git repository to one level up from your git repository
  3. remove the database from your git repository and disable the /copydb URI