Deploying Django 1.8.2 with uWSGI and Nginx on macOS
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 (containingmanage.py).wsgi-file: The absolute path to your project'swsgi.pyfile.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)
-
First, test your Nginx configuration:
nginx -t -c /path/to/your/project/nginx.confYou may need to copy the
mime.typesfile from/usr/local/etc/nginx/to your project directory if Nginx reports it's missing. -
Start the uWSGI server using your configuration file:
uwsgi --ini uwsgi_config.ini -
Start Nginx, specifying your custom configuration file:
sudo nginx -c /path/to/your/project/nginx.confIf errors occur regarding missing files (like
mime.typesoruwsgi_params), copy them to the appropriate location and rerun the command. -
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:
-
In your Django project's
settings.pyfile, define theSTATIC_ROOTsetting:import os BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) STATIC_ROOT = os.path.join(BASE_DIR, 'static') -
Run the Django
collectstaticmanagement command to gather all static files into theSTATIC_ROOTdirectory:python3 manage.py collectstatic -
In your main project's
urls.pyfile, add a URL pattern to serve static files during development (this is often handled by Nginx in production). Ensure you import thesettingsmodule.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}), ]