cPanel - Software - Setup Python App icon

How one can set up and configure Django on Linux shared internet hosting

Django is a Python-based framework that allows you to rapidly and simply create highly effective web sites. This text demonstrates easy methods to set up and configure Django on a Linux shared internet hosting account that makes use of cPanel.

After finishing the next procedures, you’ll have a functioning Django website in your account that:

  • Masses a static homepage for the area.
  • Masses the Django administration interface.
  • Makes use of a SQLite database.

Though we’ve got examined and run this Django configuration on shared internet hosting accounts, it isn’t supported. You should use this configuration as a place to begin to your personal Django tasks, however A2 Internet hosting can not assist you troubleshoot or debug any customized configurations.

Step 1: Create a Python software in cPanel

Step one is to create a Python software inside cPanel that can host the Django challenge. To do that, observe these steps:

  1. Log in to cPanel.
    Should you have no idea easy methods to log in to your cPanel account, please see this text.
  2. Within the SOFTWARE part of the cPanel house display screen, click on Setup Python App:

    cPanel - Software - Setup Python App icon

  3. Click on CREATE APPLICATION:

    cPanel - Python Selector - Create Application button

    The applying type seems:

    cPanel - Python - Create application form

  4. Within the Python edition checklist field, choose 3.8.1.
  5. Within the Utility root textual content field, kind myapp.
  6. Within the Utility URL checklist field, choose the area. Depart the remainder of the URL clean.
  7. Depart the Utility startup file textual content field and Utility Entry level textual content field clean.

    When these textual content packing containers are clean, cPanel robotically creates a passenger_wsgi.py startup file and default software object for you.

  8. Within the Passenger log file textual content field, you may optionally specify a log file for the applying.
  9. Within the prime proper nook of the web page, click on CREATE:

    cPanel - Python Selector - Create button

    cPanel creates the applying and units up the Python surroundings.

  10. On the prime of the web page, subsequent to Enter to the digital surroundings. To enter to digital surroundings, run the command, copy the command. You will want this data within the following process.
READ  Learn how to Migrate A Web site from Shared Internet hosting to VPS

Step 2: Configure the Django challenge

After you create the Python software in cPanel, you’re able to do the next duties on the command line:

  • Set up Django.
  • Create and configure the Django challenge.
  • Configure Passenger to work with the Django challenge.

To do that, observe these steps:

  1. Log in to your account utilizing SSH.
  2. Activate the digital surroundings, utilizing the command you famous in step 10 above. For instance:
    supply /house/username/virtualenv/myapp/3.8/bin/activate && cd /house/username/myapp
    

    The command immediate now begins with (myapp:3.8) to point that you’re working within the myapp digital surroundings with Python 3.8. The entire following instructions on this article assume that you’re working within the Python digital surroundings. Should you sign off of your SSH session (or deactivate the digital surroundings through the use of the deactivate command), ensure you reactivate the digital surroundings earlier than following any of the steps beneath.

  3. To put in Django, kind the next instructions:

    cd ~
    pip set up django==2.1.8

    To confirm the edition of Django that’s put in, kind the next command:

    django-admin --version
  4. To create a Django challenge, kind the next command:

    django-admin startproject myapp ~/myapp
    
  5. To create directories for the static challenge information, kind the next instructions:

    mkdir -p ~/myapp/templates/static_pages
    mkdir ~/myapp/static_files
    mkdir ~/myapp/static_media
    
  6. Use a textual content editor to open the ~/myapp/myapp/settings.py file, after which make the next adjustments:

    • Find the ALLOWED_HOSTS line, after which modify it as follows. Exchange instance.com with your individual area title:
      ALLOWED_HOSTS = ['example.com']
    • Find the TEMPLATES block, after which 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',
                  ],
              },
          },
      ]
      
    • Find the STATIC_URL line, after which add the next traces beneath it:

      STATIC_URL = '/static/'
      STATIC_ROOT = os.path.be part of(BASE_DIR, 'static_files')
      
      MEDIA_URL = '/media/'
      MEDIA_ROOT = os.path.be part of(BASE_DIR, "static_media")
      
  7. Use a textual content editor to open the ~/myapp/myapp/urls.py file. Delete the entire current textual content, after which copy the next textual content into the file:

    from django.contrib import admin
    from django.urls import path, embrace
    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 textual content editor to open the ~/myapp/passenger_wsgi.py file. Delete the entire current textual content, after which copy the next textual content into the file:

    import os
    import sys
    
    import django.core.handlers.wsgi
    from django.core.wsgi import get_wsgi_application
    
    # Arrange paths and surroundings variables
    sys.path.append(os.getcwd())
    os.environ['DJANGO_SETTINGS_MODULE'] = 'myapp.settings'
    
    # Set script title for the PATH_INFO repair beneath
    SCRIPT_NAME = os.getcwd()
    
    class PassengerPathInfoFix(object):
        """
            Units PATH_INFO from REQUEST_URI as a result of Passenger does not present 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:].break up('?', 1)[0]
            return self.app(environ, start_response)
    
    # Set the applying
    software = get_wsgi_application()
    software = PassengerPathInfoFix(software)
    
  9. Use a textual content editor to create a primary index.html file within the ~/myapp/templates/static_pages listing. The file may be so simple as a textual content file that claims Hi there world.
  10. Sort the next command:

    python ~/myapp/handle.py migrate
  11. Arrange the superuser account:

  12. Sort the next command to gather the static information:

    python ~/myapp/handle.py collectstatic

    If you’re requested if you wish to overwrite current information, kind sure after which press Enter.

  13. In cPanel, restart the Python software:

    • Log in to cPanel.
      Should you have no idea easy methods to log in to your cPanel account, please see this text.
    • Within the SOFTWARE part of the cPanel house display screen, click on Setup Python App.
    • Beneath WEB APPLICATIONS, find the myapp software, after which click on the Restart icon.
  14. Take a look at the Django website:

    • Use your browser to go to http://www.instance.com, the place instance.com represents your area title. The index.html file ought to load.
    • Use your browser to go to http://www.instance.com/admin, the place instance.com represents your area title. You must see the Django administration login web page. To log in, use the superuser credentials that you simply created earlier.

    If the website doesn’t seem in your browser, attempt working the passenger_wsgi.py file manually. To do that, kind the next command:

    python ~/myapp/passenger_wsgi.py
    

    There shouldn’t be any textual content output to the console whenever you run this file. If there are any errors, examine the syntax within the configuration information.

Extra Data

Now that you’ve got a Django-enabled website up and working, you can begin the true work of creating your individual functions. The next sources may help:

READ  Easy methods to deploy Vue js on shared internet hosting?

Leave a Reply

Your email address will not be published. Required fields are marked *