Tag: gunicorn

  • Serve a Django application using Gunicorn

    Serve a Django application using Gunicorn

    Create systemd service file

    vi /usr/lib/systemd/system/backend.service
    

    Add

    [Unit]
    Description=Gunicorn instance to serve Django Application
    After=network.target
    
    [Service]
    User=www-data
    Group=www-data
    Environment="APP=app"
    WorkingDirectory=/path/to/app/backend
    Environment="PATH=/path/to/app/backend/venv/bin"
    ExecStart=/path/to/app/backend/venv/bin/gunicorn -w 3 --timeout 120 --bind 0.0.0.0:8080 backend.wsgi
    
    [Install]
    WantedBy=multi-user.target
    

    On the above, you need to replace “/path/to/app/backend” with the path to your application. In backend.wsgi, backend is the application name here, change it to whatever your Django application name is.

    User=www-data
    Group=www-data
    

    You can change this to whatever user you use to upload your Django application files.

    Enable and start the service

    systemctl enable backend.service
    systemctl start backend.service
    

    Now your Django application will be available on port 8080. You can configure nginx or apache to proxy to this port.

    Here is a sample nginx config

    server {
        listen 443 ssl;
        server_name app.serverok.in;
        ssl_certificate /etc/ssl/app.serverok.in.crt;
        ssl_certificate_key /etc/ssl/app.serverok.in.key;
        root /var/www/app.serverok.in/html/;
        index index.html;
        location /media/ {
            alias /var/www/app.serverok.in/html/media/;
        }
    
        location /api/ {
            include proxy_params;
            proxy_pass http://localhost:8080/api/;
        }
    }
    

    Here Django application is served as /api/ folder, you can change this as required.

    Back to Gunicorn

  • Running Python Application with gunicorn and nginx

    Create a service file for gunicorn

    root@django:~# cat /etc/systemd/system/gunicorn2.service
    [Unit]
    Description=gunicorn2 daemon
    Requires=gunicorn2.socket
    After=network.target
    
    [Service]
    User=ubuntu
    Group=www-data
    WorkingDirectory=/home/ubuntu/myapp/wagtail2
    ExecStart=/home/ubuntu/myapp/venv/bin/gunicorn \
              --access-logfile - \
              --workers 3 \
              --bind unix:/run/gunicorn2.sock \
              wagtailblog4.wsgi:application
    
    [Install]
    WantedBy=multi-user.target
    root@django:~# 
    

    Here

    /home/ubuntu/myapp/wagtail2 = path to the folder where web application is.

    /home/ubuntu/myapp/venv/bin/gunicorn = is where gunicorn installed inside virtualenv.

    Change these path as required.

    Restart gunicorn with

    systemctl restart gunicorn2
    

    use following nginx config

    root@django:~# cat /etc/nginx/sites-enabled/django.conf
    server {
        server_name domain.extn;
    
        location = /favicon.ico { access_log off; log_not_found off; }
        location /static/ {
            root /home/ubuntu/myapp/wagtail2;
        }
        
        location /media/ {
            root /home/ubuntu/myapp/wagtail2;    
        }
    
        location / {
            include proxy_params;
            proxy_pass http://unix:/run/gunicorn2.sock;
        }
    
        listen 443 ssl; # managed by Certbot
        ssl_certificate /etc/letsencrypt/live/domain.extn/fullchain.pem; # managed by Certbot
        ssl_certificate_key /etc/letsencrypt/live/domain.extn/privkey.pem; # managed by Certbot
        include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
        ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
    
    
    }
    server {
        if ($host = www.domain.extn) {
            return 301 https://$host$request_uri;
        } # managed by Certbot
    
        if ($host = domain.extn) {
            return 301 https://$host$request_uri;
        } # managed by Certbot
    
        listen 80;
        server_name domain.extn www.domain.extn;
        return 404; # managed by Certbot
    }
    root@django:~# 
    

    Restart nginx

    systemctl restart nginx
    
  • gunicorn behind Apache web server

    gunicorn is a python application server used to run python applications in production. This is normally run behind web servers like nginx or apache.

    To configre gunicorn behind apache, enable following apache modules.

    a2enmod proxy proxy_ajp proxy_http rewrite deflate headers proxy_balancer proxy_connect proxy_html xml2enc
    

    Restart apache web server

    systemctl restart apache2
    

    For web site running pythin application, add a virtual host like following.

    
        ServerName DOMAIN_NAME
        ServerAdmin [email protected]
        DocumentRoot "/home/flaskapp/myapp"
    
        ErrorLog ${APACHE_LOG_DIR}/flaskapp-error.log
        CustomLog ${APACHE_LOG_DIR}/flaskapp-access.log combined
    
        ProxyPass / http://127.0.0.1:5000/
        ProxyPassReverse / "http://127.0.0.1:5000/"
    
        
            Order allow,deny
            Allow from all
            Options Indexes FollowSymLinks MultiViews
            Satisfy Any