Web Hosting Knowledge Base Web Hosting Knowledge Base
  • Home
Get Started
Web Hosting Knowledge Base Web Hosting Knowledge Base
Get Started
Web Hosting Knowledge Base Web Hosting Knowledge Base
  • Home
loading
  1. Home
  2. Developer Corner
  3. Python
  4. How to install and configure Django on a Linux shared hosting account
Updated on June 14, 2024

Developer Corner

  • Folder icon closed Folder open iconDeveloper Corner Resources
    • MySQL development
      • How to connect to MySQL using Node.js
      • How to connect to MySQL using Microsoft .NET
      • How to manage MySQL databases, users, and tables from the command line
      • How to back up MySQL databases using AutoMySQLBackup
      • How to reset the MySQL root password
      • MySQL server runs out of memory or does not start
      • MySQL user privileges on shared servers
      • How to create and execute MySQL stored functions and procedures
      • Using MySQL views
      • Using MySQL triggers
      • Working with MySQL database engines
      • How to check MySQL database and table sizes
      • How to repair MySQL databases and tables
      • How to enable the slow query log in MySQL
      • How to rename a MySQL database
      • How to use indexes to improve MySQL query performance
      • How to restrict MySQL port access
      • How to install phpMyAdmin on managed hosting accounts
      • How to create a MySQL installation for local testing
      • How to troubleshoot MySQL definer issues
      • Using SELECT INTO OUTFILE in MySQL
      • How to determine the MySQL version
      • How to Fix the MySQL Error 1064
    • Ruby developer information
      • How to install Ruby Gems
      • How to use the Interactive Ruby Shell to troubleshoot gems
      • How to set up multiple versions of Ruby with the cPanel Ruby Selector
      • How to configure a Ruby on Rails application on a shared hosting account
    • Linux for the developer
      • How to manage the inode count
      • How to determine high disk usage locations
      • How to use screen
      • How to monitor resource usage
      • How to use rsync
      • How to use cURL
      • How to disable e-mail notifications from cron jobs
      • How to use ImageMagick
      • How to change the time zone in the Linux shell
      • Cron jobs reset to 15-minute intervals
      • How to install and configure Advanced Policy Firewall
      • How to configure a firewall using iptables
      • How to set the default text editor in Linux
      • How to determine a server's memory usage
      • How to generate and verify file checksums on Linux
      • How to install the EPEL repository on CentOS
      • How to run a cron job automatically after a server reboot
      • Conntrack sessions
      • How to install and configure UFW (Uncomplicated Firewall)
    • Version control systems
      • How to use Git
      • How to configure a Git client
      • How to configure Subversion (SVN)
      • How to set up Subversion (SVN) access for multiple users
      • How to use the Mercurial version control system
      • How to configure Concurrent Versions System (CVS)
      • How to configure and use a newer version of Git
      • '403 Forbidden' error message when you try to push to a GitHub repository
    • Client-side technologies
      • How to use the jQuery JavaScript library on your hosting account
      • How to install Ember.js on managed hosting accounts
      • How to use the markItUp JavaScript plugin
      • How to Use Flash on Your Website
      • Does Hostvento Hosting Support MP3 Hosting Files?
      • Hostvento Hosting MIDI (Musical Instrument Digital Interface) Support
      • How to use React on your hosting account
    • Application deployment information
      • How to install the Docker application deployment environment
      • How to install a LAMP stack on an Ubuntu server
    • How to add CAPTCHA protection to your web site
    • How to control search engines and web crawlers using the robots.txt file
    • How to set up URL frame forwarding
    • SQLite information
      • How to connect to SQLite from the command line
      • How to connect to SQLite using PHP
      • How to connect to SQLite using Python
    • Using Lua on Hostvento Hosting servers
    • How to determine if your account uses CageFS
    • How to set up a virtual machine with VirtualBox for local testing
    • Web testing and development
      • How to create a PostgreSQL installation for local testing
      • How to create an Apache installation for local testing
      • How to install development tools on an unmanaged server
    • How to create persistent Node.js applications on shared and reseller hosting accounts
    • How to enable Cross-Origin Resource Sharing (CORS)
    • How to configure frames with X-Frame-Options header
    • Developing ADA-compliant web sites
    • Nginx web server configuration
      • How to install the Nginx web server
    • How to install and configure the AWS Command Line Interface (CLI)
    • How to edit hosts file on Mac
    • How to disable automatic creation of the robots.txt file
    • How to use an SSL certificate in a Node.js app
  • Folder icon closed Folder open iconHow to enable and disable PHP error messages
  • Folder icon closed Folder open iconHow to enable PHP error logging
  • Folder icon closed Folder open iconHow to enable and disable the PHP register_globals directive
  • Folder icon closed Folder open iconHow to use the PHP magic quotes directive
  • Folder icon closed Folder open iconHow to change the PHP time zone setting
  • Folder icon closed Folder open iconHow to enable and disable the PHP allow_url_fopen directive
  • Folder icon closed Folder open iconPHP max_execution_time Guide
  • Folder icon closed Folder open iconHow to change the maximum upload file size for PHP scripts
  • Folder icon closed Folder open iconHow to connect to MySQL from the command line
  • Folder icon closed Folder open iconHow to connect to MySQL using PHP
  • Folder icon closed Folder open iconHow to connect to MySQL using Perl
  • Folder icon closed Folder open iconHow to import and export a MySQL database
  • Folder icon closed Folder open iconHow to set up a remote MySQL connection
  • Folder icon closed Folder open iconUsing MySQL client applications
  • Folder icon closed Folder open iconHow to convert a MySQL database to UTF-8 encoding
  • Folder icon closed Folder open iconHow to convert the time zone in MySQL
  • Folder icon closed Folder open iconHow to back up MySQL databases using cron jobs
  • Folder icon closed Folder open iconInformation schema database in MySQL
  • Folder icon closed Folder open iconHow to connect to PostgreSQL using psql
  • Folder icon closed Folder open iconHow to connect to PostgreSQL using PHP
  • Folder icon closed Folder open iconHow to connect to PostgreSQL using Perl
  • Folder icon closed Folder open iconHow to import and export a PostgreSQL database
  • Folder icon closed Folder open iconHow to set up a remote PostgreSQL connection
  • Folder icon closed Folder open iconHow to use .htaccess files
  • Folder icon closed Folder open iconHow to change the default directory index page using an .htaccess file
  • Folder icon closed Folder open iconHow to enable and disable directory index listings using an .htaccess file
  • Folder icon closed Folder open iconHow to enable data compression using the mod_deflate module
  • Folder icon closed Folder open icon'403 Forbidden' error message
  • Folder icon closed Folder open iconHow to enable pathname information in URLs using the AcceptPathInfo directive
  • Folder icon closed Folder open iconHow to troubleshoot CGI scripts
  • Folder icon closed Folder open iconWhat is PHP?
  • Folder icon closed Folder open iconPHP script basics
  • Folder icon closed Folder open iconHow to view PHP settings with the phpinfo() function
  • Folder icon closed Folder open iconControlling PHP settings with a custom php.ini file
  • Folder icon closed Folder open iconUsing php.ini directives
  • Folder icon closed Folder open iconHow to run PHP scripts from cron jobs
  • Folder icon closed Folder open iconSetting the PHP include path
  • Folder icon closed Folder open iconHow to determine if a PHP function is available
  • Folder icon closed Folder open iconHow to fix '500 Internal Server Error' while running PHP
  • Folder icon closed Folder open iconPython script basics
  • Folder icon closed Folder open iconPerl script basics
  • Folder icon closed Folder open iconHow to troubleshoot Perl scripts
  • Folder icon closed Folder open iconHow to use the shebang
  • Folder icon closed Folder open iconPostgreSQL development
    • How to connect to PostgreSQL using Python
    • How to use PostgreSQL client applications
    • How to back up PostgreSQL databases using cron jobs
    • How to manage PostgreSQL databases and users from the command line
    • How to determine the PostgreSQL and PostGIS versions
    • How to determine the size of PostgreSQL databases and tables
    • How to rename a PostgreSQL database
    • ECCN and HTS numbers for PostgreSQL
  • Folder icon closed Folder open iconApache web server configuration
    • How to change the document root directory
    • How to add or remove the www prefix in domain URLs
    • How To Use Apache Access Log & Apache Error Log
    • How to rewrite URLs in Apache with the mod_rewrite module
    • How to use Server-Side Includes (SSI)
    • How to use the mod_speling Apache module
    • How to modify HTTP headers using .htaccess files
    • How to configure caching with the mod_expires module
    • How to disable entity tags (ETags)
    • How to use keep-alive connections to improve performance
    • How to control site access based on a visitor's country
    • How to turn off caching using .htaccess
    • How to install Apache Tomcat
    • How to protect a website folders using .htaccess
  • Folder icon closed Folder open iconPHP development resources
    • How to change PHP settings and versions on managed hosting accounts
    • How to set up custom .htaccess files for PHP settings
    • Using PHP directives in custom .htaccess files
    • Using PHP to send e-mail messages
    • How to install and configure PEAR packages
    • How to troubleshoot 'String could not be parsed as XML' error message in PHP
    • How to use the Exif extension in PHP
    • How to resolve some common issues after a PHP upgrade
    • How to use a custom php.ini file for cPanel cron jobs
    • How To Use PHP Sessions
    • ionCube PHP Loader support
    • How to specify the MySQL character set in PHP
    • How to fix the 'Unable to allocate memory for pool' PHP error
    • How to optimize Symfony using APC
    • How to use the internationalization (intl) extension in PHP
    • How to install PHP composer
    • How to determine if APC is installed on a server
    • How to use the MailChimp API with PHP
    • 'Allowed memory size exhausted' error message in PHP
    • How to use ionCube Loader with different PHP versions
    • How to enable PHP opcode caching
    • How to send e-mail using PHPMailer
    • How to harden PHP 7 and earlier versions using cPanel
    • Custom PHP settings per directory with .user.ini files
    • ionCube PHP Loader unavailable on PHP 8.0
    • How to change the PHP version for a specific directory
  • Folder icon closed Folder open iconPython
    • How to use virtualenv and pip
    • How to activate a Python virtual environment from a script file
    • How to send e-mail messages using Python
    • How to configure and use a newer version of Python
    • How to install and configure Django on unmanaged servers
    • How to install and configure Django on a Linux shared hosting account
    • How to install and configure Flask on a Linux shared hosting account
    • How to install FastAPI on unmanaged servers
  • Folder icon closed Folder open iconPerl Information Overview

How to install and configure Django on a Linux shared hosting account

Estimated reading: 7 minutes 159 views

Django is a Python-based framework that enables you to quickly and easily create powerful websites. This article demonstrates how to install and configure Django on a Linux shared hosting account that uses cPanel.

After completing the following procedures, you will have a functioning Django site on your account that:

  • Loads a static homepage for the domain.
  • Loads the Django administration interface.
  • Uses a SQLite database.

Although we have tested and run this Django configuration on shared hosting accounts, it is not supported. You can use this configuration as a starting point for your own Django projects, but Hostvento Hosting cannot help you troubleshoot or debug any custom configurations.

Step 1: Create a Python application in cPanel

The first step is to create a Python application within cPanel that will host the Django project. To do this, follow these steps:

  1. Log in to cPanel.If you do not know how to log in to your cPanel account, please see this article.
  2. Open the Python App tool:
    • If you are using the Jupiter theme, on the Tools page, in the Software section, click Setup Python App:cPanel - Software - Setup Python App icon
    • If you are using the Paper Lantern theme, in the SOFTWARE section of the cPanel home page, click Setup Python App:
      cPanel - Software - Setup Python App icon
  3. Click CREATE APPLICATION:
    cPanel - Python Selector - Create Application buttonThe application form appears:cPanel - Python - Create application form
  4. In the Python version list box, select 3.8.1.
  5. In the Application root text box, type myapp.
  6. In the Application URL list box, select the domain. Leave the rest of the URL blank.
  7. Leave the Application startup file text box and Application Entry point text box blank.
    When these text boxes are blank, cPanel automatically creates a passenger_wsgi.py startup file and default application object for you.
  8. In the Passenger log file text box, you can optionally specify a log file for the application.
  9. In the top right corner of the page, click CREATE:
    cPanel - Python Selector - Create buttoncPanel creates the application and sets up the Python environment.
  10. At the top of the page, next to Enter to the virtual environment. To enter to virtual environment, run the command, copy the command. You will need this information in the following procedure.

Step 2: Configure the Django project

After you create the Python application in cPanel, you are ready to do the following tasks at the command line:

  • Install Django.
  • Create and configure the Django project.
  • Configure Passenger to work with the Django project.

To do this, follow these steps:

  1. Log in to your account using SSH.
  2. Activate the virtual environment, using the command you noted in step 10 above. For example:Copysource /home/username/virtualenv/myapp/3.8/bin/activate && cd /home/username/myapp The command prompt now starts with (myapp:3.8) to indicate that you are working in the myapp virtual environment with Python 3.8. All of the following commands in this article assume that you are working in the Python virtual environment. If you log out of your SSH session (or deactivate the virtual environment by using the deactivate command), make sure you reactivate the virtual environment before following any of the steps below.
  3. To install Django, type the following commands:
    Copycd ~ pip install django==2.1.8To verify the version of Django that is installed, type the following command:
    Copydjango-admin –version
  4. To create a Django project, type the following command:
    Copydjango-admin startproject myapp ~/myapp
  5. To create directories for the static project files, type the following commands:
    Copymkdir -p ~/myapp/templates/static_pages mkdir ~/myapp/static_files mkdir ~/myapp/static_media
  6. Use a text editor to open the ~/myapp/myapp/settings.py file, and then make the following changes:
    • Locate the ALLOWED_HOSTS line, and then modify it as follows. Replace example.com with your own domain name:ALLOWED_HOSTS = [‘example.com‘]
    • Locate the TEMPLATES block, and then modify it as follows:
      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’, ], }, }, ]
    • Locate the STATIC_URL line, and then add the following lines beneath it:
      STATIC_URL = ‘/static/’ STATIC_ROOT = os.path.join(BASE_DIR, ‘static_files’) MEDIA_URL = ‘/media/’ MEDIA_ROOT = os.path.join(BASE_DIR, “static_media”)
  7. Use a text editor to open the ~/myapp/myapp/urls.py file. Delete all of the existing text, and then copy the following text into the file:
    from django.contrib import admin from django.urls import path, include from django.conf import settings from django.conf.urls.static import static from django.conf.urls import url from 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)
  8. Use a text editor to open the ~/myapp/passenger_wsgi.py file. Delete all of the existing text, and then copy the following text into the file:
    import os import sys import django.core.handlers.wsgi from django.core.wsgi import get_wsgi_application # Set up paths and environment variables sys.path.append(os.getcwd()) os.environ[‘DJANGO_SETTINGS_MODULE’] = ‘myapp.settings’ # Set script name for the PATH_INFO fix below SCRIPT_NAME = os.getcwd() class PassengerPathInfoFix(object):     “””         Sets PATH_INFO from REQUEST_URI because Passenger doesn’t provide it.     “””     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) # Set the application application = get_wsgi_application() application = PassengerPathInfoFix(application)
  9. Use a text editor to create a basic index.html file in the ~/myapp/templates/static_pages directory. The file can be as simple as a text file that says Hello world.
  10. Type the following command:
    Copypython ~/myapp/manage.py migrate
  11. Set up the superuser account:
    • Type the following command:Copypython ~/myapp/manage.py createsuperuser
    • At the Username prompt, type the administrator username, and then press Enter.
    • At the Email address prompt, type the administrator e-mail address, and then press Enter.
    • At the Password prompt, type the administrator password, and then press Enter.
  12. Type the following command to collect the static files:
    Copypython ~/myapp/manage.py collectstaticIf you are asked if you want to overwrite existing files, type yes and then press Enter.
  13. In cPanel, restart the Python application:
    • Log in to cPanel.If you do not know how to log in to your cPanel account, please see this article.
    • In the SOFTWARE section of the cPanel home screen, click Setup Python App.
    • Under WEB APPLICATIONS, locate the myapp application, and then click the Restart icon.
  14. Test the Django site:
    • Use your browser to go to http://www.example.com, where example.com represents your domain name. The index.html file should load.
    • Use your browser to go to http://www.example.com/admin, where example.com represents your domain name. You should see the Django administration login page. To log in, use the superuser credentials that you created earlier.
    If the web site does not appear in your browser, try running the passenger_wsgi.py file manually. To do this, type the following command:Copypython ~/myapp/passenger_wsgi.py There should not be any text output to the console when you run this file. If there are any errors, check the syntax in the configuration files.

Still stuck? How can we help?

How can we help?

Was this page helpful? Yes No

Share this Doc

How to install and configure Django on a Linux shared hosting account

Or copy link

Clipboard Icon
CONTENTS
  • Step 1: Create a Python application in cPanel
  • Step 2: Configure the Django project
Leaf Illustration

© 2023 All Rights Reserved by Hostvento