Jump to content

How to use Django on Plesk


Krydos

Recommended Posts

We will update our wiki article on how to get started with Django, but just posting a quick example here so people can comment and ask questions on this thread.

  1. Create a directory on your main domain called djangotest. If you were transferred from cPanel your main domain will be parked on the public_html directory. If you created a new account on Plesk your directory will be httpdocs.
  2. Create an .htaccess file inside the djangotest directory with these contents:
    Options +ExecCGI
    RewriteEngine On
    RewriteBase /
    RewriteRule ^(media/.*)$ - [L]
    RewriteRule ^(admin_media/.*)$ - [L]
    RewriteRule ^(djangotest/dispatch\.wsgi/.*)$ - [L]
    RewriteRule ^(.*)$ djangotest/djangotest/dispatch.wsgi/$1 [QSA,PT,L]
    

     

  3. Next create another djangotest directory within the first djangotest directory. This is the standard directory structure for a Django project so don't give me that look. I'm sure they did it on purpose just to make it more confusing for you. Also you can't name your Django project django either, so don't even bother trying. That's why we're calling this example djangotest.
  4. Inside the second djangotest directory make a file named dispatch.wsgi with these contents:
    import os, sys
    
    # edit path below
    sys.path.append("/home/domain.helioho.st/httpdocs/djangotest")
    
    from django.core.wsgi import get_wsgi_application
    os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'djangotest.settings')
    application = get_wsgi_application()
  5. Make sure you edit the path in the dispatch.wsgi file. On Plesk your path is /home/ and then your main domain, and then httpdocs if you're a new account or public_html if you've been transferred from cPanel.

  6. Inside the second djangotest directory create an empty file named __init__.py

  7. Inside the second djangotest directory make a file named urls.py with these contents:
    from django.contrib import admin
    from django.urls import path
    
    urlpatterns = [
    #    path('admin/', admin.site.urls),
    ]
  8. Inside the second djangotest directory make a file named settings.py with these contents:
    from pathlib import Path
    
    # Build paths inside the project like this: BASE_DIR / 'subdir'.
    BASE_DIR = Path(__file__).resolve().parent.parent
    
    # Quick-start development settings - unsuitable for production
    # See https://docs.djangoproject.com/en/4.1/howto/deployment/checklist/
    # SECURITY WARNING: keep the secret key used in production secret!
    SECRET_KEY = 'django-makeyoursecretbetterthanthis'
    
    # SECURITY WARNING: don't run with debug turned on in production!
    DEBUG = True
    
    ALLOWED_HOSTS = ['*']
    
    # Application definition
    INSTALLED_APPS = [
        'django.contrib.admin',
        'django.contrib.auth',
        'django.contrib.contenttypes',
        'django.contrib.sessions',
        'django.contrib.messages',
        'django.contrib.staticfiles',
    ]
    MIDDLEWARE = [
        'django.middleware.security.SecurityMiddleware',
        'django.contrib.sessions.middleware.SessionMiddleware',
        'django.middleware.common.CommonMiddleware',
        'django.middleware.csrf.CsrfViewMiddleware',
        'django.contrib.auth.middleware.AuthenticationMiddleware',
        'django.contrib.messages.middleware.MessageMiddleware',
        'django.middleware.clickjacking.XFrameOptionsMiddleware',
    ]
    ROOT_URLCONF = 'djangotest.urls'
    TEMPLATES = [
        {
            'BACKEND': 'django.template.backends.django.DjangoTemplates',
            'DIRS': [],
            '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',
                ],
            },
        },
    ]
    WSGI_APPLICATION = 'djangotest.wsgi.application'
    # Database
    # https://docs.djangoproject.com/en/4.1/ref/settings/#databases
    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.sqlite3',
            'NAME': BASE_DIR / 'db.sqlite3',
        }
    }
    # Password validation
    # https://docs.djangoproject.com/en/4.1/ref/settings/#auth-password-validators
    AUTH_PASSWORD_VALIDATORS = [
        {
            'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
        },
        {
            'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
        },
        {
            'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
        },
        {
            'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
        },
    ]
    # Internationalization
    # https://docs.djangoproject.com/en/4.1/topics/i18n/
    LANGUAGE_CODE = 'en-us'
    TIME_ZONE = 'UTC'
    USE_I18N = True
    USE_TZ = True
    # Static files (CSS, JavaScript, Images)
    # https://docs.djangoproject.com/en/4.1/howto/static-files/
    STATIC_URL = 'static/'
    # Default primary key field type
    # https://docs.djangoproject.com/en/4.1/ref/settings/#default-auto-field
    DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
  9. Make sure your directory structure and files look like this:
    djangotest/
    ├── djangotest
    │   ├── dispatch.wsgi
    │   ├── __init__.py
    │   ├── settings.py
    │   └── urls.py
    └── .htaccess
    
    1 directory, 5 files
  10. If you did everything right it should look like this: https://krydos.heliohost.org/djangotest/
Edited by Krydos
some accounts need execcgi
  • Thanks 1
Link to comment
Share on other sites

  • 1 month later...
52 minutes ago, amalgd said:

I am trying to setup a Django project by adopting instructions as above. It is not working

https://mainstay.heliohost.org/mainstayerp

I see this in the log:

[core:alert] /home/mainstay.heliohost.org/public_html/mainstayerp/.htaccess: Options not allowed here

can you please help?

 

Edit your .htaccess file and remove this line:

Options +ExecCGI

Your .htaccess file now should look like this:

RewriteEngine On
RewriteBase /
RewriteRule ^(media/.*)$ - [L]
RewriteRule ^(admin_media/.*)$ - [L]
RewriteRule ^(djangotest/dispatch\.wsgi/.*)$ - [L]
RewriteRule ^(.*)$ djangotest/djangotest/dispatch.wsgi/$1 [QSA,PT,L]

 

If you'd like you can download this zip file: https://kairion.helioho.st/djangotest.zip

And extract it on your web root (httpdocs for Plesk's new account or public_html for cPanel's transfered account). It'll create all directories and files necessary to run this Django example. You just have to open the file /djangotest/djangotest/dispatch.wsgi and find this:

sys.path.append("/home/YOUR_DOMAIN_HERE/httpdocs/djangotest")

And change YOUR_DOMAIN_HERE with your actual domain, e.g.:

sys.path.append("/home/kairion.helioho.st/httpdocs/djangotest")

And then you can access your Django example: https://kairion.helioho.st/djangotest/

Link to comment
Share on other sites

Thank you guys!

I removed the line from from .htaccess, now the page is showing 404 Page not found.

May be it is the missing external packages that my project is using.

I need:

django-nested-admin, django-bootstrap-modal-forms

could I have these install, please?

I found some typos in my .htaccess file and fixed those. Now the error code is 500, which is expected due to the missing dependencies, I guess.

I found this in the log:

TypeError: sequence of byte string values expected, value of type str found

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...