Setting up & Running Django on OpalStack.com
Setting up & Running Django on a shared hosting provider like OpalStack.com
3 min read
Mar 15, 2022
A small intro on OpalStack : OpalStack.com is a reputed hosting company with deep expertise in Python as the founders were ex-Webfaction employees. Webfaction has shut down now as it was acquired by Godaddy a couple of years ago but Webfaction was initially python-hosting.com 10-15 yrs ago which marketed Python exclusively. But 15 yrs back there was no hype on Django and it was at version 0.96 or something so python-hosting.com expanded and renamed to Webfaction and offered PHP which is (still) too much in demand than anything else. Even OpalStack.com offers PHP 8.1 and all those bells and whistles but the company is expert in Python and the dashboard and company site is based on Django and TailWindCSS.
Goto : https://my.opalstack.com/apps/
Select Create Application
Enter a name, select Django and select the Shell User
Documentation on setting up Django : https://docs.opalstack.com/topic-guides/django/ - esp this section - https://docs.opalstack.com/topic-guides/django/#serving-your-own-django-project
cd ~/apps/my_app source env/bin/activate pip install django-widget-tweaks
Edit ~/apps/my_app/uwsgi.ini and set the python-path, wsgi-file, and touch-reload options (at the end of the file) to point at your project.
# adjust the following to point to your project python-path = /home/username/apps/my_app/my_app wsgi-file = /home/username/apps/my_app/my_app/my_app/wsgi.py touch-reload = /home/username/apps/my_app/my_app/my_app/wsgi.py
Run the following commands to restart your Django instance:
/home/username/apps/my_app/stop sleep 2 /home/username/apps/my_app/start
What the documentation does not cover is Django media uploads which are later served by your site, i.e. content that you upload to the site via the admin site or whatever UI your site has for that. However, the concept is the same:
- Create a static app to hold the uploaded media.
- Configure your Django project settings (in this case MEDIA_ROOT and MEDIA_URL) as needed.
- Edit your site in your Opalstack dashboard to serve the media app on whichever URL.
In /home/username/apps/my_app/my_app/my_app/settings.py set :
MEDIA_URL = '/media/' MEDIA_ROOT = BASE_DIR / 'media'
Using Environment Variables instead of setting the values (like database password) directly in settings.py
cd ~/apps/my_app source env/bin/activate pip install django-environ cd /home/username/apps/my_app/my_app/my_app/ touch .env vi .env
SECRET_KEY=django-insecure-xxx DATABASE_NAME=my_database DATABASE_USER=my_username DATABASE_PASS=my_password
In settings.py :
import environ env = environ.Env() environ.Env.read_env() SECRET_KEY = env('SECRET_KEY')
To update Django to a newer version :
cd ~/apps/my_app source env/bin/activate python -m pip install --upgrade django==4.0.3