Django deployment

Discussion in 'Installation/Configuration' started by anark10n, Apr 16, 2019.

  1. anark10n

    anark10n Member

    So i this tutorial on how to install django on my server, and I managed to get it functional up until the point where i can reach the django project if i go to domain.tld:8000. The problem is getting it to work with ISPConfig. These are my apache directives
    Code:
    Alias /static /var/www/clients/client1/web1/web/static
    <Directory /var/www/clients/client1/web1/web/static
            Require all granted
    </Directory>
    
    WSGIDaemonProcess testware python-home=/var/www/clients/client1/web1/web/testwareenv python-path=/var/www/clients/client1/web1/web
    WSGIProcessGroup testware
    WSGIScriptAlias / /var/www/clients/client1/web1/web/testware/wsgi.py
    <Directory /var/www/clients/client1/web1/web/testware>
            <Files wsgi.py>
                    Require all granted
            </Files>
    </Directory>
    This is my testware/wsgi.py
    Code:
    import os
    
    from django.core.wsgi import get_wsgi_application
    
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "testware.settings")
    
    application = get_wsgi_application()
    
    Forgot to add the error message
    I get a 403 Forbidden page
    Code:
    Forbidden
    You don't have permission to access / on this server.
    
    Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.
    
     
  2. anark10n

    anark10n Member

    Ok, so I managed to "isolate" the problem. It seems that the 403 forbidden crops up when i try to access to any folder in the web root folder. So even if i make a directory, i'm not able to access it. And it also crops up when i delete all the files in the web root folder. Any ideas why this would occur?
     
  3. till

    till Super Moderator Staff Member ISPConfig Developer

    A 403 error in apache and nginx is normal when you try to access a web folder which has no standard index file inside.
     
  4. anark10n

    anark10n Member

    Ah, okay. Anyway around that? Because if i leave the index.html in the web root, it's all i get when i go to my site instead of the django page.
     
  5. till

    till Super Moderator Staff Member ISPConfig Developer

    Did you check the generated vhost file to see if your Django directives are in there, maybe the file is saved with .err ending which means apache did not accept that config. If that's ok, try to replace /var/www/clients/client1/web1 with /var/www/yourdomain.tld in your config wherever it occurs.
     
  6. anark10n

    anark10n Member

    I managed to solve the 403 Forbidden with the following to the Apache Directive, notable the following line
    HTML:
    <Directory /var/www/clients/client1/web1/web/exampleproject>
            Options Indexes FollowSymLinks Includes ExecCGI
            <Files wsgi.py>
                Require all granted
            </Files>
        </Directory>
     
  7. anark10n

    anark10n Member

    And for anyone that might be struggling with deploying django on IPSConfig, here's the step i used to get mine up and running.
    This tutorial assumes that you've gone through the Perfect Server installation and configuration for Debian in it's entirety on Debian.

    Log into your ISPConfig and create your client and website. Make sure CGI and python are enabled when creating the website.

    Create a shell user for your client:
    Code:
    Sites -> Command Line -> Shell User -> Add new Shell User
    In the Shell User tab select the site(example.com for this tutorial)
    Enter a suffix that will be appended to the username that you entered when creating the client.
    Enter your password
    Make sure the Chroot Shell is None for now
    Set to Active
    Save the new user.

    Login into your server as your administrator via SSH

    Disable mod_python:
    Code:
    sudo a2dismod python
    Restart apache:
    Code:
    sudo service apache2 restart
    Install mod_wsgi

    For python 2:
    Code:
    sudo apt-get install libapache2-mod-wsgi
    For python 3:
    Code:
    sudo apt-get install libapache2-mod-wsgi-py3
    Install virtualenv

    For Debian 8:
    Code:
    sudo apt-get install python-setuptools
    sudo easy_install pip
    sudo pip install virtualenv
    For Debian 9:
    Code:
    sudo apt-get install vitrualenv
    Login into your server using the the new user that you just created via SSH(client(n) is your users group: e.g. client1, and web(m) is your user: e.g. web1).

    You will be in the /var/www/clients/client(n)/web(m)/home/user/ directory

    Switch to the web directory of your user, which will serve as the project parent directory for this tutorial:
    Code:
    cd ../../web
    Create your virtualenv (we will name our virtual environment venv):
    Code:
    virtualenv venv
    You can specify which python to use with.
    For python 2:
    Code:
    virtualenv venv -p python2
    For python 3:
    Code:
    virtualenv venv -p python3

    Start your virtual environment:
    Code:
    source venv/bin/activate
    Install django:
    Code:
    pip install django
    Create your django project root directory (we will use exampleproject for this tutorial):
    Note: the . at the end of the command
    Code:
    django-admin.py startproject exampleproject .
    Your directory structure should resemble the list structure below:
    ../venv
    ../exampleproject
    ../manage.py
    ../exampleproject/__init.py
    ../exampleproject/settings.py
    ../exampleproject/urls.py
    ../exampleproject/wsgi.py

    Adjust your project settings using your preferred editor.
    Code:
    nano exampleproject/settings.py
    Find the ALLOWED_HOSTS line and edit to allow the hosts you want:
    Code:
    ...
    ALLOWED_HOSTS = ["example.com", "(server IP address)", "127.0.0.1"]
    ...
    At the bottom fo the file, set the project's STATIC_ROOT by appending the following:
    Code:
    STATIC_ROOT = os.path.join(BASE_DIR, 'static/')
    Complete the project setup:
    Code:
    ./manage.py makemigrations
    ./manage.py migrate
    Create an administrative user for your project:
    Code:
    ./manage.py createsuperuser
    Follow on-screen prompts to create the user.

    Collect all your static content in the STATIC_ROOT directory
    Code:
    ./manage.py collectstatic
    Adjust the wsgi.py file:
    Code:
    nano exampleproject/wsgi.py
    Make it resembles the below:
    Code:
    import os
    import sys
    
    from django.core.wsgi import get_wsgi_application
    
    sys.path.append('/var/www/clients/client1/web1')
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "exampleproject.settings")
    
    application = get_wsgi_application()
    Exit the virtual environment using:
    Code:
    deactivate
    Log into your ISPConfig panel and go to your settings:
    Code:
    Sites -> Websites -> Website
    Select your site and go to the Options tab.
    In the Apache Directives text area, enter the following([example] can be any name you choose):
    Code:
       Alias /static /var/www/clients/client1/web1/web/static
       <Directory /var/www/clients/client1/web1/web/static>
           Require all granted
       </Directory>
    
       <Directory /var/www/clients/client1/web1/web>
           Options Indexes FollowSymLinks Includes ExecCGI
           <Files wsgi.py>
               Require all granted
           </Files>
       </Directory>
    
    WSGIDaemonProcess [example] user=web1 group=client1 python-home=/var/www/clients/client1/web2/web/venv python-path=/var/www/clients/client1/web2/web
    WSGIProcessGroup [example]
    WSGIScriptAlias / /var/www/clients/client1/web1/web/exampleproject/wsgi.py
    Hope this helps. This my first time presenting instructions, so let me know if something is unclear. Good luck.
     
    Last edited: Jun 25, 2019
  8. anark10n

    anark10n Member

    Sorry, had to revert from solved, because when applying the above steps on my production server, i get an ERROR 500 - Internal Server Error.
    Error log in /var/log/ispconfig/httpd/domain.tld/
    Code:
    [my-home-ip-address] - - [27/Apr/2019:19:23:25 +0000] "GET / HTTP/1.1" 500 2119 "-" "Mozilla/5.0 (X11; Linux x86_64; rv:66.0) Gecko/20100101 Firefox/66.0"
    /var/log/apache2/error.log has no pertinent errors, or maybe i'm just looking at the error logs.
    my wsgi.py
    Code:
    import os
    import sys
    
    from django.core.wsgi import get_wsgi_application
    
    sys.path.append('/var/www/clients/client25/web30')
    os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'domain.settings')
    
    application = get_wsgi_application()
    
    
    my apache directives:
    Code:
    Alias /static /var/www/clients/client25/web30/web/static
    <Directory /var/www/clients/client25/web30/web/static>
           Require all granted
    </Directory>
    
    <Directory /var/www/clients/client25/web30/web/domain>
           Options Indexes FollowSymLinks Includes ExecCGI
           <Files wsgi.py>
               Require all granted
           </Files>
    </Directory>
    
    WSGIDaemonProcess mydomain user=web30 group=client25 python-home=/var/www/clients/client25/web30/web/venv python-path=/var/www/clients/client25/web30/web
    WSGIProcessGroup mydomain
    WSGIScriptAlias / /var/www/clients/client25/web30/web/domain/wsgi.py
    
    I'm at a complete loss as to what could be the cause of this. Any help on what to check would be much appreciated.
     

Share This Page