Running a Pyinstaller Django application using nginx

1.7k views Asked by At

I have packaged a django application using pyinstaller

python pyinstaller.py --name=executablename Tool/manage.py

The problem is that the default server is single threaded and cannot handle much load.

Therefore i want to use a standard server with my packaged django executable. I have two choices eighther to use

1. Apache
2. Nginx with uwsgi

I can easily setup them with code, but the issue is related to packaged application. Here both server want a wsgi file which is normally present in Tool\wsgi.py but since this is a packaged application so not wsgi.py is present therefore both server cannot be attached.

Does anyone one know a way to achieve this. I know advised way would be to go with source but i really do not want to distribute my source in python files.

2

There are 2 answers

1
Graham Dumpleton On

If using uWSGI, there is a command line option which allows you to say you are providing it with a module path instead of a file path. For standard Apache with mod_wsgi, or with uWSGI defaults, you can create a WSGI script file which imports the WSGI application from your module by its path into the WSGI script file and then refer to that WSGI script file. If using mod_wsgi-express, then it has an option like uWSGI which allows you to say you are providing it with a module path instead.

0
Enzoooo_ On

uwsgi can't run not python fileļ¼Œso you can use another python file to start uwsgi service.

start.py

import os 


init_file = "./uwsgi.ini"

result = os.system("uwsgi --ini {}".format(init_file))

and then use pyinstaller to run start.spec

# -*- mode: python ; coding: utf-8 -*-
from PyInstaller.utils.hooks import collect_submodules, collect_all


block_cipher = None

hiddenimports = collect_submodules('rest_framework')

hiddenimports.extend(
    [
        'django.contrib.contenttypes',
        'django.contrib.staticfiles',
        'django_filters',
        'rest_framework',
        'api',
        'coreschema',
    ]
)

a = Analysis(['start_service.py'],
             pathex=['/src'],
             binaries=[],
             datas=[
               (r'/env/lib/python3.6/site-packages/rest_framework/', './rest_framework'),
               (r'/env/lib/python3.6/site-packages/django_filters/', './django_filters')
             ],
             hiddenimports=hiddenimports,
             hookspath=[],
             runtime_hooks=[],
             excludes=[],
             win_no_prefer_redirects=False,
             win_private_assemblies=False,
             cipher=block_cipher,
             noarchive=False)
pyz = PYZ(a.pure, a.zipped_data,
             cipher=block_cipher)
exe = EXE(pyz,
          a.scripts,
          [],
          exclude_binaries=True,
          name='manage',
          debug=False,
          bootloader_ignore_signals=False,
          strip=False,
          upx=True,
          console=True )
coll = COLLECT(exe,
               a.binaries,
               a.zipfiles,
               a.datas,
               strip=False,
               upx=True,
               upx_exclude=[],
               name='start')

run code

pyinstaller start.spec

it will generate file in /dist/start/start

and run ./dist/start/start, it will start uwsgi service