Fading Coder

One Final Commit for the Last Sprint

Home > Tools > Content

Deploying Django 1.8.2 with uWSGI and Nginx on macOS

Tools 2

Configuring uWSGI

Installation

Install uWSGI using pip:

pip3 install uwsgi

Verificasion

You can verify the installaiton in two ways.

Method 1: Using a simple WSGI application

Create a file named wsgi_test.py with the following content:

def simple_app(environ, start_fn):
    start_fn('200 OK', [('Content-Type', 'text/html')])
    return [b'Test Response from uWSGI']

Run the application with uWSGI:

uwsgi --http 0.0.0.0:8080 --wsgi-file wsgi_test.py

Open your browser and navigate to http://127.0.0.1:8080/ to see the response.

Method 2: Running a Django project directly

You can also start a Django project directly with uWSGI. Replace myproject with your project's name.

uwsgi --http :8080 --file myproject/wsgi.py

This method is sufficient for a basic uWSGI + Django deployment. To integrate with Nginx, proceed to the configuraton step.

uWSGI Configuration File

Create a configuration file named uwsgi_config.ini in the same directory as your Django project's manage.py file.

[uwsgi]
http = :9000
socket = 127.0.0.1:8001
chdir = /path/to/your/django_project
wsgi-file = /path/to/your/django_project/django_project/wsgi.py
module = django_project.wsgi:application
master = true
processes = 4
threads = 2
chmod-socket = 664
vacuum = true
daemonize = /path/to/your/django_project/uwsgi_daemon.log

Configuration Parameters Explained:

  • http: The port users will connect to directly if not using Nginx.
  • chdir: The absolute path to your Django project directory (containing manage.py).
  • wsgi-file: The absolute path to your project's wsgi.py file.
  • socket: The port for communication between uWSGI and Nginx (must match the Nginx configurasion).
  • daemonize: Path to the log file for uWSGI daemon output.

Configuring Nginx

Installation

Install Nginx using Homebrew:

brew install nginx

Check the installlation:

nginx -v  # Check version
nginx -t  # Test configuration and show paths

Nginx Configuration

Create an nginx.conf file in your Django project's root directory (alongside manage.py).

worker_processes 1;

events {
    worker_connections 1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;

    server {
        listen 80;
        server_name 127.0.0.1;
        charset utf-8;

        access_log /path/to/your/project/nginx_access.log;
        error_log  /path/to/your/project/nginx_error.log;

        client_max_body_size 75M;

        location /static/ {
            alias /path/to/your/project/static/;
        }

        location / {
            root /path/to/your/project;
            include /usr/local/etc/nginx/uwsgi_params;
            uwsgi_pass 127.0.0.1:8001;
        }
    }
}

Managing the Nginx Process

Find the Nginx binary and manage the service:

# Locate the nginx executable
which nginx

# Restart Nginx (loads new configuration)
nginx -s reload

# Stop Nginx
nginx -s stop
# Alternatively, find and kill the process
# ps aux | grep nginx
# kill -9 <process_id>

# Check if Nginx is running
ps aux | grep nginx

Starting the Integrated Stack (uWSGI + Nginx + Django)

  1. First, test your Nginx configuration:

    nginx -t -c /path/to/your/project/nginx.conf
    

    You may need to copy the mime.types file from /usr/local/etc/nginx/ to your project directory if Nginx reports it's missing.

  2. Start the uWSGI server using your configuration file:

    uwsgi --ini uwsgi_config.ini
    
  3. Start Nginx, specifying your custom configuration file:

    sudo nginx -c /path/to/your/project/nginx.conf
    

    If errors occur regarding missing files (like mime.types or uwsgi_params), copy them to the appropriate location and rerun the command.

  4. Access your Django application by navigating to http://127.0.0.1/ in your browser.

Serving Static Files in Django

To ensure Nginx can serve your Django project's static files correctly:

  1. In your Django project's settings.py file, define the STATIC_ROOT setting:

    import os
    BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
    STATIC_ROOT = os.path.join(BASE_DIR, 'static')
    
  2. Run the Django collectstatic management command to gather all static files into the STATIC_ROOT directory:

    python3 manage.py collectstatic
    
  3. In your main project's urls.py file, add a URL pattern to serve static files during development (this is often handled by Nginx in production). Ensure you import the settings module.

    from django.conf import settings
    from django.conf.urls import url, include
    from django.contrib import admin
    # Optional: For serving static files in dev if needed
    from django.views.static import serve
    
    urlpatterns = [
        url(r'^admin/', admin.site.urls),
        url(r'', include('yourapp.urls')),
        # Development URL for static files
        url(r'^static/(?P<path>.*)$', serve, {'document_root': settings.STATIC_ROOT}),
    ]
    

Related Articles

Efficient Usage of HTTP Client in IntelliJ IDEA

IntelliJ IDEA incorporates a versatile HTTP client tool, enabling developres to interact with RESTful services and APIs effectively with in the editor. This functionality streamlines workflows, replac...

Installing CocoaPods on macOS Catalina (10.15) Using a User-Managed Ruby

System Ruby on macOS 10.15 frequently fails to build native gems required by CocoaPods (for example, ffi), leading to errors like: ERROR: Failed to build gem native extension checking for ffi.h... no...

Capturing Android Screenshots and Screen Recordings with ADB

Two practical ways to grab images and videos from an Android device: Mirror the phone display to a computer and use desktop tools for screenshots and GIFs Use ADB commands (no UI mirroring required)...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.