DEV Community

Tikam Singh Alma
Tikam Singh Alma

Posted on

How to make apache2 server faster - Part-02 [Implementation]

I will explain here how to increase latency of apache2 server,using Django deployment example.At the end of the article you'll get to know that this can be applies to any backend server be it Django or node server.

Installing mod_wsgi the easy way

pip install mod_wsgi
Enter fullscreen mode Exit fullscreen mode

mod_wsgi - module for apache web server

No Apache Configuration Required

$ mod_wsgi-express start-server hello.wsgi
Enter fullscreen mode Exit fullscreen mode

Django Framework Integration

INSTALLED_APPS = [

'django.contrib.admin',

'django.contrib.auth',

.....

.....

'mod_wsgi.server',

]

Send logging to terminal

settings.py

LOGGING = {
    #  ... omitting the formatters and handlers for brevity ...
    'loggers': {
        # ...  you may have other loggers here as well ...
        'django': {
            'handlers': ['name_of_your_file_handler_goes_here'],
            'level': 'DEBUG',
            'propagate': True,
        }
    }
Enter fullscreen mode Exit fullscreen mode

Or

LOGGING = {
        'version':1,
        'disable_existing_loggers': False,
        'handlers': {
                'console': {
                        'class' : 'logging.StreamHandler',
                 },
            },
            'loggers' : {
                    'django' : {
                            'handlers' : ['console'],
                            'level' : os.getenv('DJANGO_LOG_LEVEL','INFO'),
                    },
                },
    }

Enter fullscreen mode Exit fullscreen mode

Run Django With Management command

python [manage.py](http://manage.py) runmodwsgi
Enter fullscreen mode Exit fullscreen mode

Automatic code reloading

python [manage.py](http://manage.py) runmodwsgi reload-on-changes
Enter fullscreen mode Exit fullscreen mode

Django DEBUG settings

settings.py

if os.environ.get('MOD_WSGI_DEBUG_MODE'):
        DEBUG = True

if os.environ.get('MOD_WSGI_DEBUGGER_ENABLED'):
        DEBUG_PROPAGATE_EXCEPTIONS = True
Enter fullscreen mode Exit fullscreen mode

Interactive Debugger

python [manage.py](http://manage.py) runmodwsgi enable-debugger
Enter fullscreen mode Exit fullscreen mode

Production configuration

python [manage.py](http://manage.py) runmodwsgi \
--server-root /etc/wsgi-port-80  \
--user www-data --group www-data\
--port 80  --setup-only
Enter fullscreen mode Exit fullscreen mode
/etc/wsgi-port-80/apachectl start 
/etc/wsgi-port-80/apachectl restart 
/etc/wsgi-port-80/apachectl stop 
Enter fullscreen mode Exit fullscreen mode

Use daemon mode of mod_wsgi

WSGIRestrictedEmbedded On
WSGIDaemonProcess myapp python-home=/../env

WSGIScriptAlias / /../src/wsgi.py \ 
        process-group=myapp application-group=%{GLOBAL}

<Directory /../src>
        <Files wsgi.py>
        Require all granted
        </Files>
</Directory>
Enter fullscreen mode Exit fullscreen mode

Just add whatever the backend server Django or Node anything, putting this above virtualhost on line-1 will boost your latency.
I have explained here in detail what is Embedded mode and daemon mod of apache2 server.Read theory - https://dev.to/tikam02/how-to-make-apache2-server-fasterrrrrrr-part-01-theory-1dne

WSGIRestrictedEmbedded On
Enter fullscreen mode Exit fullscreen mode

Resources:

https://youtu.be/H6Q3l11fjU0

mod_wsgi - mod_wsgi 4.8.0 documentation

Discussion (0)