Installing and configuring Flask on a Linux shared hosting account
Installing and configuring Django on Linux shared hosting
Section titled “Installing and configuring Django on Linux shared hosting”This guide explains how to install and configure Django on a Linux shared hosting account using cPanel. After following these steps, you will have a Django site that:
- Loads a static homepage.
- Loads the Django administration interface.
- Uses a SQLite database.
Step 1: Create a Python application in cPanel
Section titled “Step 1: Create a Python application in cPanel”-
Log in to cPanel.
-
In the Software section, click Setup Python App.
-
Click CREATE APPLICATION.
-
In the application form:
- Select Python 3.8.1 in the Python version list.
- Set Application root to
myapp. - Select your domain in Application URL. Leave the rest blank.
- Leave Application startup file and Application Entry point blank. cPanel will automatically create
passenger_wsgi.py. - Optionally specify a Passenger log file.
-
Click CREATE. cPanel will create the application and set up the Python environment.
-
Note the command next to Enter to the virtual environment. You will need it in the next step.
Step 2: Configure the Django project
Section titled “Step 2: Configure the Django project”- Log in via SSH.
- Activate the virtual environment (replace with your environment path):
source /home/username/virtualenv/myapp/3.8/bin/activate && cd /home/username/myappImportant: Your prompt should show (myapp:3.8) indicating the active virtual environment. Reactivate the environment if you log out or deactivate it.
- Install Django:
cd ~pip install django==2.1.8- Verify Django installation:
django-admin --version- Create the Django project:
django-admin startproject myapp ~/myapp- Create directories for static files:
mkdir -p ~/myapp/templates/static_pagesmkdir ~/myapp/static_filesmkdir ~/myapp/static_media- Edit
~/myapp/myapp/settings.py:
- Set allowed hosts (replace with your domain):
ALLOWED_HOSTS = ['example.com']- Update the TEMPLATES block:
TEMPLATES = [ { 'BACKEND': 'django.template.backends.django.DjangoTemplates', 'DIRS': [os.path.join(BASE_DIR,'templates')], 'APP_DIRS': True, 'OPTIONS': { 'context_processors': [ 'django.template.context_processors.debug', 'django.template.context_processors.request', 'django.contrib.auth.context_processors.auth', 'django.contrib.messages.context_processors.messages', ], }, },]- Add static and media settings:
STATIC_URL = '/static/'STATIC_ROOT = os.path.join(BASE_DIR, 'static_files')
MEDIA_URL = '/media/'MEDIA_ROOT = os.path.join(BASE_DIR, "static_media")- Edit
~/myapp/myapp/urls.pyto include:
from django.contrib import adminfrom django.urls import pathfrom django.conf import settingsfrom django.conf.urls.static import staticfrom django.conf.urls import urlfrom django.views.generic.base import TemplateView
urlpatterns = [ path('admin/', admin.site.urls), url(r'^$', TemplateView.as_view(template_name='static_pages/index.html'), name='home'),] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)- Edit
~/myapp/passenger_wsgi.py:
import osimport sysimport django.core.handlers.wsgifrom django.core.wsgi import get_wsgi_application
sys.path.append(os.getcwd())os.environ['DJANGO_SETTINGS_MODULE'] = 'myapp.settings'
SCRIPT_NAME = os.getcwd()
class PassengerPathInfoFix(object): def __init__(self, app): self.app = app
def __call__(self, environ, start_response): from urllib.parse import unquote environ['SCRIPT_NAME'] = SCRIPT_NAME request_uri = unquote(environ['REQUEST_URI']) script_name = unquote(environ.get('SCRIPT_NAME', '')) offset = request_uri.startswith(script_name) and len(environ['SCRIPT_NAME']) or 0 environ['PATH_INFO'] = request_uri[offset:].split('?', 1)[0] return self.app(environ, start_response)
application = get_wsgi_application()application = PassengerPathInfoFix(application)- Create a basic
index.htmlin~/myapp/templates/static_pagescontaining simple text, e.g., “Hello world”. - Run migrations and create a superuser:
python ~/myapp/manage.py migratepython ~/myapp/manage.py createsuperuserFollow prompts to set admin username, email, and password.
- Collect static files:
python ~/myapp/manage.py collectstaticType yes if prompted to overwrite files.
Step 3: Restart the Python application
Section titled “Step 3: Restart the Python application”- Log in to cPanel.
- In Software → Setup Python App, locate your
myappapplication and click Restart.
Step 4: Test the Django site
Section titled “Step 4: Test the Django site”- Visit
http://www.example.comto see yourindex.htmlpage. - Visit
http://www.example.com/adminto access the Django admin interface using your superuser credentials.
Tip: If the site does not appear, run:
python ~/myapp/passenger_wsgi.pyThere should be no output. Check for syntax errors if there are any.
More information
Section titled “More information”- Django documentation: http://docs.djangoproject.com
- Django extensions: https://github.com/django-extensions/django-extensions
- South for database migrations: https://pypi.python.org/pypi/South
- Fabric for deployment automation: http://docs.fabfile.org