I had a Django project I wanted to clone into a folder in a directory different than the apps directory opalstack creates. Turns out to be really simple. Here's what I did. Hope this helps someone else.
In Opalstack, when you provision a new app, it is put in the ~/apps
folder. I wanted to deploy a Django app but keep the source code for the site in ~/git/site
but use the Opalstack Django goodness to get python3, a virtualenv and the uwsgi set up. This all turned out to be remarkably easy. Here is what I did.
- Provisioned a database for the Django app as normal. (Nothing new here).
- Made a
~/git
directory and checked out the code
$ cd git
$ git clone git@github.com/me/mywebsite.git
- Created a new Django Application called 'mydjango' using the control panel
This created ~/apps/mydjango
with a starter Django project in it already. But it also created some other things: it created a Python3 virtualenv and a uwsgi.ini that links the webserver to the Python/Django code.
- Next, I needed to activate my Python env and install my django dependencies. I ran into a tiny hiccup here. One of my dependencies it
psycopg2
- the Postgres adaptor for Django. The pip
install didn't work correctly the first time. This was because the Postgres executables were not on my path. The simple solution was to put /usr/psql-11/bin/
onto my PATH while I executed these commands. (I should probably make this permanent, but I didn't.)
$ export PATH=/usr/pgsql-11/bin/:$PATH
$ source apps/mydjango/env/bin/activate
$ pip install psycopg2
$ pip install django psycopg2
$ pip install django-bootstrap3
$ pip install stripe
$ pip install django-registration-redux
- Next, I needed to modify the
uwsgi.ini
that Opalstack installed to point to the code in my git folder, rather than the apps folder. For my application, it looked like this
[uwsgi]
master = True
http = 127.0.0.1:28089
virtualenv = /home/me/apps/mydjango/env/
daemonize = /home/me/logs/mydjango/uwsgi.log
pidfile = /home/me/apps/mydjango/tmp/uwsgi.pid
workers = 2
threads = 2
# adjust the following to point to your project
python-path = /home/me/apps/mydjango/myproject
wsgi-file = /home/me/apps/mydjango/myproject/myproject/wsgi.py
touch-reload = /home/me/apps/mydjango/myproject/myproject/wsgi.py
So far, so good. Opalstack basically tells us what to do in the last three lines.
[uwsgi]
master = True
http = 127.0.0.1:28089
virtualenv = /home/me/apps/mydjango/env/
daemonize = /home/me/logs/mydjango/uwsgi.log
pidfile = /home/me/apps/mydjango/tmp/uwsgi.pid
workers = 2
threads = 2
# adjust the following to point to your project
python-path = /home/me/git/mywebsite/labsproject
wsgi-file = /home/me/git/mywebsite/labsproject/labsproject/wsgi.py
touch-reload = /home/me/git/mywebsite/labsproject/labsproject/wsgi.py
Debugging
Along the way, I made a few mistakes. In particular, I initially set th python-path
incorrectly. Opalstack provides some logs to help find the mistakes. For mydjango
there is a log file created called ~/logs/mydjango/uwsgi.log
. This log file shows the problems starting the Python code. Since Opalstack has terminal windows, it was easy to see problems by starting two terminals and viewing the log in one, and starting and stopping the website in another.
$ tail -f logs/mydjango/uwsgi.log
$ apps/mydjango/stop
$ apps/mydjango/start
Summary
It was all pretty painless. Next, I modified the Django settings.py and added a static app to serve the assets. It all works great.
-Tom