Comment installer une application Python Django sur cPanel

Django est un framework basé sur Python qui vous permet de créer rapidement et facilement des sites Web puissants. Cet article montre comment installer et configurer une application démo Django sur un compte d'hébergement Web mutualisé Linux qui utilise cPanel. Cette application démo utilise une base de données SQLite.

 

Notez que vous pouvez installer au besoin une version plus récente de SQLite :

Comment installer la dernière version de SQLite sur cPanel

 

1. Connectez-vous à votre compte cPanel

cpanel_login_fr.png

 

2. Créez une application Python

python_wizard_en.png
python_wizard_en.png
a) Créez une nouvelle application

python_wizard_create_en.png

b) Entrez les informations de l'application

Pour le projet démo, utilisez la version 3.11.x

python_wizard_config_en.png

python_wizard_config_env_en.png

c) Confirmez les informations

python_wizard_config_create_en.png

d) Copiez la commande qui permettra d'accéder à l'environnement virtuel

Une fois la création de l'application terminée, un message est affichée en haut de l'écran. Copiez la commande, vous en aurez besoin plus loin.

source /home/[votre_utilisateur]/virtualenv/demo_django_app/[version_python]/bin/activate && cd /home/[votre_utilisateur]/demo_django_app

 

3. Configurez le projet Django

a) Utilisez le Terminal ou connectez-vous avec SSH

advanced_title_fr.png
terminal_en.png

b) Activez l'environnement virtuel

Utilisez la commande copiée précédemment. Vous pouvez retourner dans les configurations de votre application Python pour retrouver la commande.

$ source /home/[votre_utilisateur]/virtualenv/demo_django_app/[version_python]/bin/activate && cd /home/[votre_utilisateur]/demo_django_app
c) Mettez à jour le programme "pip"
$ pip install --upgrade pip
d) Téléchargez et décompressez le projet démo
$ wget https://demo.vtinyhosting.com/vtiny-django-demo.zip
$ unzip vtiny-django-demo.zip
$ rm vtiny-django-demo.zip
e) Créez les répertoires pour les fichiers statiques
$ mkdir -p templates/static_pages
$ mkdir static_files
$ mkdir static_media
f) Installer les "packages"
$ pip install -r requirements.txt
g) Remplacez le fichier "passenger_wsgi.py"
$ mv passenger_wsgi.py passenger_wsgi_bkp.py
$ wget https://demo.vtinyhosting.com/passenger_wsgi.zip
$ unzip passenger_wsgi.zip
$ rm passenger_wsgi.zip

Pour vos projets, n'oubliez pas de modifier "demo_project.settings" par le nom de votre projet Django.

$ nano passenger_wsgi.py

Le contenu final du fichier :

import os
import sys

import django.core.handlers.wsgi
from django.core.wsgi import get_wsgi_application

sys.path.append(os.getcwd())
os.environ['DJANGO_SETTINGS_MODULE'] = 'demo_project.settings'

SCRIPT_NAME = ''

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)

application = get_wsgi_application()
application = PassengerPathInfoFix(application)

Pour enregistrer le fichier : Ctrl+O et Entrée
Pour sortir de l'éditeur Nano : Ctrl+X

h) Créez un compte super utilisateur

Ce compte vous permettra d'accéder au module d'administration Django :
https://[votre_application_url]/admin

$ python manage.py createsuperuser
i) Collectez les fichiers statiques
$ python manage.py collectstatic
j) Redémarrez l'application Python
software_title_fr.png
python_wizard_en.png

python_wizard_restart_en.png

 

4. Testez le site

Dans votre navigateur, entrez l'url de votre application (section 2.b) :
https://[votre_application_url]

 

Lien de la démo Vtiny Hosting :
https://django.demo.vtinyhosting.com/

  • cPanel, Developer, Django, Python
  • 823 Utilisateurs l'ont trouvée utile
Cette réponse était-elle pertinente?

Articles connexes

Comment installer la dernière version de SQLite sur cPanel

Puisque toutes les versions de SQLite peuvent être facilement installées localement sur vos...

Comment installer une application PHP Symfony sur cPanel

Symfony est un framework MVC libre écrit en PHP qui vous permet de créer rapidement et facilement...

Powered by WHMCompleteSolution