From 456f929b5ebc4efbac6a0e9f4575aa0c526e8216 Mon Sep 17 00:00:00 2001 From: Bogdan Matican Date: Tue, 5 Mar 2013 15:10:26 +0000 Subject: [PATCH] Default nginx config for django, log folder and bare scripts for startup. Summary: Basic setup for nginx Test Plan: run start and stop scripts and runserver in between. Reviewers: bogdan2412 Reviewed By: bogdan2412 Differential Revision: http://phab.code4fun.de/D5 --- .gitignore | 1 + .nginx_conf_local | 66 ++++++++++++++++++++++++++++++++++++ CodeStreak/settings.py | 2 +- CodeStreak/settings_debug.py | 3 ++ runserver | 64 +++++++++++++++++++++++++++------- start-nginx.sh | 20 +++++++++++ stop-nginx.sh | 7 ++++ 7 files changed, 150 insertions(+), 13 deletions(-) create mode 100644 .nginx_conf_local create mode 100644 CodeStreak/settings_debug.py create mode 100755 start-nginx.sh create mode 100755 stop-nginx.sh diff --git a/.gitignore b/.gitignore index 29fa048..41d7ba6 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,4 @@ *.swp local_settings.py db.sqlite3 +.nginx_conf diff --git a/.nginx_conf_local b/.nginx_conf_local new file mode 100644 index 0000000..2f1da87 --- /dev/null +++ b/.nginx_conf_local @@ -0,0 +1,66 @@ +worker_processes 5; +worker_rlimit_nofile 8192; + +events { + worker_connections 1024; +} + +http { + # replace the following: + # $nginx_etc with /usr/local/etc/nginx for mac or /etc/nginx for linux + # set $project_name to the path to the project/app_name + # eg: /Users/Mati/mati/work/CodeStreak/CodeStreak; + include $nginx_etc/mime.types; + include $nginx_etc/proxy.conf; + default_type application/octet-stream; + + sendfile on; + keepalive_timeout 65; + + error_log /tmp/CodeStreak.nginx.log error; + log_format main + '$remote_addr - $remote_user [$time_local] ' + '"$request" $status $bytes_sent ' + '"$http_referer" "$http_user_agent" ' + '"$gzip_ratio"'; + + upstream django { + server unix:///tmp/CodeStreak.sock; # for a file socket + # server 127.0.0.1:8001; # for a web port socket + } + + server { + root $project_name; + + # the port your site will be served on + listen 8000; + # the domain name it will serve for + server_name 127.0.0.1 localhost; + charset utf-8; + + #Max upload size + client_max_body_size 75M; # adjust to taste + + location ~ ^/static/(.*)$ { + alias static_collected/$1; + expires 1d; + } + + location ~ ^/media/(.*)$ { + alias media/$1; + expires 1d; + } + + location ~ /favicon.ico { + alias static_collected/img/favicon.png; + expires 1d; + } + + # Finally, send all non-media requests to the Django server. + location / { + # all proxy headers should have been set + uwsgi_pass django; + include $nginx_etc/uwsgi_params; + } + } +} diff --git a/CodeStreak/settings.py b/CodeStreak/settings.py index 4b0cda8..4b1ae8f 100644 --- a/CodeStreak/settings.py +++ b/CodeStreak/settings.py @@ -1,7 +1,7 @@ # coding=utf8 # Django settings for CodeStreak project. -DEBUG = True +DEBUG = False TEMPLATE_DEBUG = DEBUG ADMINS = ( diff --git a/CodeStreak/settings_debug.py b/CodeStreak/settings_debug.py new file mode 100644 index 0000000..ad1186f --- /dev/null +++ b/CodeStreak/settings_debug.py @@ -0,0 +1,3 @@ +from CodeStreak.settings import * +DEBUG = True +TEMPLATE_DEBUG = DEBUG diff --git a/runserver b/runserver index 67b59f6..8247a38 100755 --- a/runserver +++ b/runserver @@ -1,22 +1,62 @@ # /bin/bash -MEMCACHED_PORT=11211 +# just inc ase, we define base python here +if [ -z $PYTHON ]; +then + PYTHON=python +fi -start_memcached() +DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" + +start_uwsgi() { - memcached -p $MEMCACHED_PORT & - MEMCACHED_PID=$! - echo "Starting memcached: pid=$MEMCACHED_PID" + echo "Starting uWSGI" + # TODO: fix permissions to 664, put user, nginx in same group and such.. + uwsgi \ + --socket /tmp/CodeStreak.sock \ + --chmod-socket=666 \ + --chdir $DIR \ + --wsgi-file $DIR/CodeStreak/wsgi.py \ + --master true \ + --processes 10 \ + --vacuum true \ + $@ } -kill_memcached() +start_fcgi() { - kill $MEMCACHED_PID - echo "Killing memcached: pid=$MEMCACHED_PID" - exit $? + echo "Starting FCGI" + $PYTHON manage.py runfcgi \ + # method=prefork \ + socket=/tmp/CodeStreak.socket \ + # host=127.0.0.1 port=8080 \ + settings=CodeStreak/settings_debug.py \ + pidfile=/tmp/Codestreak.django.pid \ + $@ } -# trap kill_memcached SIGINT +start_normal() +{ + echo "Starting $PYTHON" + $PYTHON manage.py runserver $@ +} -start_memcached -python manage.py runserver 0.0.0.0:8000 +case $1 in +"--uwsgi") + shift + start_uwsgi $@ + ;; +"--fcgi") + shift + start_fcgi $@ + ;; +"--pypy") + PYTHON=pypy + shift + start_normal $@ + ;; +*) + PYTHON=python + start_normal $@ + ;; +esac diff --git a/start-nginx.sh b/start-nginx.sh new file mode 100755 index 0000000..34d49ad --- /dev/null +++ b/start-nginx.sh @@ -0,0 +1,20 @@ +#! /bin/bash + +DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" + +OS=$(uname -s) +case $OS in +"Darwin") + GROUP=www + ;; +"Linux") + GROUP=www-data + ;; +*) + echo "Unknown OS" + ;; +esac + +sudo nginx \ + -c $DIR/.nginx_conf \ + -g "pid /tmp/CodeStreak.nginx.pid; user $USER $GROUP;" diff --git a/stop-nginx.sh b/stop-nginx.sh new file mode 100755 index 0000000..ab50914 --- /dev/null +++ b/stop-nginx.sh @@ -0,0 +1,7 @@ +#! /bin/bash + +DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" + +sudo nginx \ + -s stop \ + -g "pid /tmp/CodeStreak.nginx.pid;"