2

This started as a question, but I think I've figured out most of the parts, so am posting it here for reference. It is relatively involved, but I think it may be useful to others contemplating this scenario.

I'm a newb with some of these areas, so if mistakes are made in regards to security issues in Apache or other bad practices, please correct.

Also note that, as it stands, the local development version that is produced from following the steps below no longer has git enabled on it due to changes between it and the production code. So I will keep the local git repo in another location.

Desired Behaviour

Option One:
Replicate my current Python 2.7, Bottle, MongoDB OpenShift application locally to speed up development time (during git push etc).

Option Two (if significantly easier):
Replicate my current Python 2.7, Bottle, MongoDB Openshift application locally *without the OpenShift platform* to speed up development time.

Current Behaviour

I have a Python 2.7, Bottle, MongoDB application on OpenShift.

My current workflow is:

  • Edit locally.
  • git add --all
  • git commit -m "here is a message"
  • git push origin master (this updates the live site on openshift)
  • git push github master (this updates github repo)

Obviously this is not ideal for developing due to the time each push takes before I can see the results.

user1063287
  • 10,265
  • 25
  • 122
  • 218
  • An excellent post! Now openshift has moved to docker based. Have you been able to do the same on the new openshift? – minghua Nov 30 '17 at 17:09

1 Answers1

1

Directory Structure

This is the structure of my app now that it is running locally:

enter image description here

Environment

Linux Mint 17 Cinnamon

Steps To Replicate Locally

01) MongoDB 2.4.9 - DONE
Install instructions for MongoDB 2.4.9 on Linux Mint 17:
http://docs.mongodb.org/v2.4/tutorial/install-mongodb-on-ubuntu

02) RockMongo 1.1 (which requires Apache, PHP and MongoDB Driver) - DONE

sudo apt-get install apache2 php5
sudo apt-get install php5-dev php5-cli
sudo apt-get install php-pear
pear version
pecl version
sudo pecl install mongo

At this point, I was prompted with something that included [no] and I just pressed Enter.

cd /etc/php5/apache2
sudo vi php.ini

Add this to the end of the file:

extension=mongo.so

Then restart:

/etc/init.d/apache2 restart

Then install RockMongo:

cd /var/www/html
wget https://github.com/iwind/rockmongo/archive/1.1.7.zip
unzip 1.1.7.zip
mv rockmongo-1.1.7 rockmongo
rm 1.1.7.zip

03) Create clean virtualenv environment and install packages to it - DONE

virtualenv is a Python package that lets you create independent, virtual environments containing their own Python installation and packages.

Install virtualenv through Synaptic Package Manager.

How To Create
https://code.google.com/p/modwsgi/wiki/VirtualEnvironments
By the author of mod_wsgi, Graham Dumpleton.

Why To Create
http://www.dabapps.com/blog/introduction-to-pip-and-virtualenv-python/
This article is so brilliant it almost make me want to cry, kudos, kudos.

Commands

Before doing the following, install python2.7-dev, libxml2-dev, libxslt1-dev and apache2-dev via Synaptic Package Manager to resolve errors when doing pip installs later.

# change to your html folder
cd /var/www/html 

# this will create a folder called ENV that contains its own instance
# of python without inheriting your system's installed python packages.  
# it will also install independent instances of pip and setuptools.  
# the --no-site-packages option is the default setting in recent versions 
# however I added it just to be sure.  

virtualenv --no-site-packages ENV
New python executable in ENV/bin/python
Installing setuptools, pip...done.

# you can 'activate' the virtual environment so that each time you use
# pip it automatically installs packages in the virtual environment.  

# change to your virtual environment folder
cd /var/www/html/ENV
# activate the virtual environment
source bin/activate

# you can deactivate this by typing 'deactivate` and it is also 
# automatically deactivated each time you close the terminal.  

deactivate

# from time to time you can save the names of the packages you have
# installed to your virtual environment via pip to a text file with:

pip freeze > requirements.txt

# note, after installing virtualenv as shown above, you will have some
# packages installted by default.  

pip freeze
argparse==1.2.1
wsgiref==0.1.2

# requirements.txt would allow installation of all required packages via:

pip install -r requirements.txt

# install packages, whilst virtualenv is activated

pip install bottle
pip install https://github.com/FedericoCeratto/bottle-cork/archive/master.zip
pip install requests
pip install pymongo==2.6.2
pip install beautifulsoup4
pip install lxml
pip install Beaker
pip install pycrypto
pip install pillow
pip install tldextract

04) Copy existing application files to new location - DONE

cp -r path/to/open_shift_apps/my-app/. /var/www/html 

05) Remove files and folder unnecessary for *local* production from var/www/html - DONE

rm -r data
rm -r libs
rm -r .openshift
rm -r .git
rm setup.py
rm setup.pyc
rm setup.pyo

06) mod_wsgi - DONE
Through Synaptic Package Manager.
Apache wouldn't work for me unless mod_wsgi was installed at system level, ie it didn't work when mod_wsgi was installed within virtualenv.

07) Understand the relationship between the Apache server, mod_wsgi and your application - DONE

Apache

To run a dynamic website locally, you need a server, in this case we used Apache.

mod_wsgi

mod_wsgi is an Apache module which extends Apache so that rules can be added to its configuration which point to your Python code so that it can be run when a user visits a particular path.

08) Configure Apache rules

/etc/apache2/sites-available/000-default.conf

WSGIPythonHome /var/www/html/ENV
WSGIPythonPath /var/www/html:/var/www/html/ENV/lib/python2.7/site-packages:/var/www/html/wsgi
<VirtualHost *:80>

# for all content in static folder - css, js, img, fonts
Alias /static/ /var/www/html/wsgi/static/

# for rockmongo
Alias /rockmongo /var/www/html/rockmongo

<Directory /var/www/html/rockmongo>
Order deny,allow
Allow from all
</Directory>

    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/html
    WSGIScriptAlias / /var/www/html/wsgi/application

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

</VirtualHost>

When a user visits a particular path, Apache looks for an application object which contains code which will run your Python program.

In this case, the object is located at wsgi/application and is triggered when the user goes to localhost.

/var/www/html/wsgi/application

from mybottleapp import application

09) Check file ownership and permissions

If things don't work at any stage of the process, be sure to look at the permissions of your local files. Not having the right permissions could mean that your application is not imported.

10) mongodump from OpenShift and mongorestore locally

How to mongodump from OpenShift and mongorestore locally on MongoDB 2.4.9?

Further Reading

How Python web frameworks, WSGI and CGI fit together

https://docs.python.org/2/howto/webservers.html

http://wsgi.readthedocs.org/en/latest/servers.html

https://code.google.com/p/modwsgi/

https://www.python.org/dev/peps/pep-0333

http://bottlepy.org/docs/dev/deployment.html#apache-mod-wsgi

Community
  • 1
  • 1
user1063287
  • 10,265
  • 25
  • 122
  • 218