Django on Custom Webapp?

Hi

Can I have django on Custom Webapp ? I run django on local pc but I don’t know how to do on a server.

1 Like

Hello,

Django is not suitable for a custom webapp as its déploiement is far different from PHP.

You’ll probably want to look into putting it by hand via ssh on the server using gunicorn and a systemd script then using the redirect app on the local listened port. But even with that, I’m not 100% sure that this will be enough (I think that the static files won’t be able). You’ll probably need to write an yunohost app by yourself sadly :confused:

Hello Bram

Thanks for the information. I will try it when I get time.

Thanks

I am unable to implement Django with gnicorn. Can someone with experience in it explain how to implement it with nginx ?

Solution:
I am using custom web app for the installation.( I have not tested the backup and restore script )
Further I have installed it on the root of the domain.

  1. Install the custom web app:
    yunohost app install https://github.com/YunoHost-Apps/my_webapp_ynh

  2. In-order to have django within virtual environment we need to execute virtualenv command. We can install this with pip.
    sudo apt-get install python-pip sudo pip install virtualenv

  3. As now we have our virtualenv ready, we can go ahead and setup projects directory.
    cd /var/www/my_webapp/www/

  4. Remove the index.html
    rm index.html

  5. Within this project directory, create a Python virtual environment by typing:
    virtualenv myproject_env

  6. By executing this command “myproject_env” named directory will be created. It will be used to install and configure different python environments.
    Now to activate this virtual environment, execute following command:
    source myproject_env/bin/activate

  7. With your virtual environment activated. We can move forward by installing Django, Gunicorn.
    pip install django gunicorn

  8. Now with virtual environment been activated. Start django project using django management command.
    django-admin.py startproject myproject .

  9. Now open settings.py fiile located under myproject folder. Edit this settings.py by replacing following code.
    ALLOWED_HOSTS = ['domain_where_custom_web_app_installed.tld',] STATIC_URL = '/static/' STATIC_ROOT = os.path.join(BASE_DIR, 'static/')

  10. You will need to install the Python and MySQL development headers and libraries:
    sudo apt-get install python-dev libmysqlclient-dev pip install mysqlclient

  11. open again settings.py file located under myproject folder. Enter your database details which are available in /var/www/my_webapp/db_access.txt
    DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', 'NAME': 'my_webapp', 'USER': 'my_webapp', 'PASSWORD': 'password in db_access.txt', 'HOST': 'localhost', 'OPTIONS': { 'init_command': "SET sql_mode='STRICT_TRANS_TABLES'", }, 'PORT': '', } }

  12. Run commands:
    ./manage.py makemigrations ./manage.py migrate ./manage.py createsuperuser ./manage.py runserver 0.0.0.0:8000

  13. For the test in web browser, enter domain_where_custom_web_app_installed.tld:8000 or in command line
    curl domain_where_custom_web_app_installed.tld:8000
    Once you had finished exploring recently created project, hit CNRL + C.

  14. Now let’s test Gunicorn interface.
    gunicorn --bind 0.0.0.0:8000 myproject.wsgi:application
    This will start gunicorn on same interface on which our earlier development server was working.Test the gunicorn as described in step 13.

  15. Now type this command to deactivate the virtual environment:
    deactivate

  16. Now we have to create Gunicorn systemd service file.This service file will be used to start or stop our application server.Open the gunicorn systemd service file using this command.
    sudo vi /etc/systemd/system/gunicorn.service

  17. Edit this service file with following data.

[Unit]
Description=gunicorn daemon
After=network.target

[Service]
User=root
Group=www-data
WorkingDirectory=/var/www/my_webapp/www
ExecStart=//var/www/my_webapp/www/myproject_env/bin/gunicorn --workers 3 --bind unix:/var/www/my_webapp/www/myproject.sock myproject.wsgi:application

[Install]
WantedBy=multi-user.target
18. Save and close the file. Start the Gunicorn service using following command.
sudo systemctl start gunicorn sudo systemctl enable gunicorn
This will create socket file inside your project directory.Always check that the socket file has www-data group and this group should have group ownership.

19.Edit etc/nginx/conf.d/domain_where_custom_web_app_installed.tld.d/my_webapp.conf

                  location / { 
                  include proxy_params;

                    proxy_pass http://unix:/var/www/my_webapp/www/myproject.sock;

              # Prevent useless logs

                location = /favicon.ico {

                    log_not_found off;

                    access_log off;

                }

                  location /static/ {

                    root /var/www/my_webapp/www;

                }

                 location = /robots.txt {

                    allow all;

                    log_not_found off;

                    access_log off;

                 }



              #Deny access to hidden files and directories

             # location ~ ^/(.+/|)\.(?!well-known\/) {

               #  deny all;

             }

         # Include SSOWAT user panel.

            include conf.d/yunohost_panel.conf.inc;

        }

20.Test your Nginx configuration for syntax errors by typing:
sudo nginx -t
If no errors is found, Restart Nginx by typing:
sudo systemctl restart nginx

Note: Components which are installed under virtual environment are isolated from other os global environments.
For static file after adding each static file in your app migrate it with the command:
python manage.py collectstatic
service gunicorn restart

5 Likes

Great write-up @kanhu.

How do you deal with Django+auth? I mean, I suppose it is suppose to craft a Django session cookie from a Nginx+Lua script, but then how do you deal with user objects, without django-ldap?

Take a look into: GitHub - YunoHost-Apps/django_ynh: Glue code to package django projects as yunohost apps. It’s also a python package with helper for django: django-ynh · PyPI

2 Likes

Note: the Packages as been renamed!

The Python package django_yunohost_integration helpers for integrate a Django project as YunoHost package.

A example usage of this package is here: GitHub - YunoHost-Apps/django_example_ynh: Demo YunoHost Application to demonstrate the integration of a Django project under YunoHost.