How can I deploy Django App to 1and1 (ionos)

Viewed 2613

I created a website with Django and now I need to deploy it and now I have to upload it to 1and1. Do I need a specific tool for this or can I upload it without hesitation like a website that only contains .html and .css files?

2 Answers

Installation

  1. ssh into Ionos
  2. Update pip: python3 -m pip install --upgrade pip
  3. Install django: python3 -m pip install django
  4. Make sure .bash_profile contains source ~/.bashrc
  5. Add django locations to .bashrc: /full_path_to_homefolder/.local/bin and /full_path_to_homefolder/.local/lib, and source ~/.bashrc.

Setup

  1. Create a subfolder to work in, e.g. work [Optional]
  2. Create a project (e.g. mysite) in that folder: django-admin startproject mysite
  3. Create a folder cgi-bin in your work folder.
  4. Create a file application.fcgi in cgi-bin with the following contents:
        # -*- coding: UTF-8 -*-
        
        import os
        import sys
        import traceback
        
        home = '/COMPLETE_PATH_TO_YOUR_FOLDER/work'
        try:        
            project = os.path.join(home, 'mysite')
            # Add a custom Python path.
            sys.path.insert(0, project)
        
            # Switch to the directory of your project.
            os.chdir(project)
        
            # Set the DJANGO_SETTINGS_MODULE environment variable.
            os.environ['DJANGO_SETTINGS_MODULE'] = "mysite.settings"
        
            from django_fastcgi.servers.fastcgi import runfastcgi
            from django.core.servers.basehttp import get_internal_wsgi_application
        
            wsgi_application = get_internal_wsgi_application()
            runfastcgi(wsgi_application, method="prefork", daemonize="false", minspare=1, maxspare=1, maxchildren=1)
        except:
            traceback.print_exc(stdout)
            with open(os.path.join(home, 'tmp/error.log'), 'w') as fp:
        traceback.print_exc(file = fp)
  1. In your work folder, vim .htaccess and add these contents:
        RewriteEngine on
        RewriteBase /
        # The following two lines are for FastCGI:
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteRule ^(.*)$ /apps/cgi-bin/application.fcgi/$1 [QSA,L]
  1. Add your web domain to ALLOW_HOSTS in ~/work/mysite/mysite/settings.py
  2. Create an app inside the ~/work/mysite folder: python manage.py startapp appname
  3. Browse to your_website/work/ to see if it works
  4. Copy over any files you already have. If you don't have any, proceed with the official tutorial, ignoring all the instructions for a development server: https://docs.djangoproject.com/en/3.2/intro/tutorial01/#write-your-first-view
  5. Don't forget to run python manage.py check --deploy to secure your site for deploying.

Just adding answer for those who are still trying to find the answer for deploying Django on shared ionos servers. There a complete guide on GitHub posted by sparagus.

Please make sure you have access for followings on system,

  • SSH access
  • Available Apache supports FCGI
  • Ability to compile from source
  • .htaccess support
  • tar support

If above points are checked please go following steps to achieve django on shared hosting 1and1.

Related