Skip to content
This repository has been archived by the owner on Oct 17, 2020. It is now read-only.

Commit

Permalink
nginx and uwsgi for the win
Browse files Browse the repository at this point in the history
  • Loading branch information
avilaton committed Sep 16, 2015
1 parent 6314697 commit ac1f643
Show file tree
Hide file tree
Showing 11 changed files with 294 additions and 18 deletions.
29 changes: 16 additions & 13 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,11 +1,4 @@
alembic==0.7.4
amqp==1.4.6
anyjson==0.3.3
Beaker==1.6.5.post1
billiard==3.3.0.19
blinker==1.3
boto==2.36.0
celery==3.1.17
Flask==0.10.1
Flask-Bootstrap==3.3.0.1
Flask-Compress==1.0.2
Expand All @@ -14,17 +7,27 @@ Flask-HTTPAuth==2.3.0
Flask-Login==0.2.11
Flask-Mail==0.9.1
Flask-Migrate==1.3.0
Flask-Script==2.0.5
Flask-SQLAlchemy==2.0
Flask-Script==2.0.5
Jinja2==2.7.3
Mako==1.0.1
MarkupSafe==0.23
SQLAlchemy==0.9.8
Werkzeug==0.10.1
alembic==0.7.4
amqp==1.4.6
anyjson==0.3.3
argparse==1.2.1
billiard==3.3.0.19
blinker==1.3
boto==2.36.0
celery==3.1.17
gunicorn==19.2.1
guppy==0.1.10
honcho==0.6.3
httplib2==0.9
itsdangerous==0.24
Jinja2==2.7.3
kombu==3.0.24
Mako==1.0.1
MarkupSafe==0.23
oauth2client==1.4.6
psycopg2==2.5.4
pyasn1==0.1.7
Expand All @@ -35,7 +38,7 @@ pytz==2014.10
rsa==3.1.4
simplejson==3.6.5
six==1.9.0
SQLAlchemy==0.9.8
transitfeed==1.2.14
uWSGI==2.0.11.1
uritemplate==0.6
Werkzeug==0.10.1
wsgiref==0.1.2
4 changes: 2 additions & 2 deletions salt/roots/salt/app/init.sls
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@ db:
/home/vagrant/.bashrc:
file.append:
- text:
- "export DATABASE_URI=\"postgresql://{{ pillar['dbuser'] }}:{{ pillar['dbpassword'] }}@localhost:5432/{{ pillar['dbname'] }}\""
- "source venv/bin/activate"
- "cd app/"
- "cd app/"
- "export DATABASE_URL=\"postgresql://{{ pillar['dbuser'] }}:{{ pillar['dbpassword'] }}@localhost:5432/{{ pillar['dbname'] }}\""
4 changes: 2 additions & 2 deletions salt/roots/salt/core/init.sls
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# include:
# - system
include:
- core.system

packages:
pkg.installed:
Expand Down
34 changes: 34 additions & 0 deletions salt/roots/salt/nginx/app.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# app.conf

# the upstream component nginx needs to connect to
upstream django {
server unix:///tmp/app.sock; # for a file socket
}

# configuration of the server
server {
# the port your site will be served on
listen 80;
# the domain name it will serve for
server_name 0.0.0.0; # substitute your machine's IP address or FQDN
charset utf-8;

# max upload size
client_max_body_size 75M; # adjust to taste

# Django media
#location /media {
# alias /path/to/your/django/media; # your Django project's media files
#}

location /static {
alias /home/vagrant/app/static; # your Django project's static files
}

# Finally, send all non-media requests to the Django server.
location / {
uwsgi_pass django;
include uwsgi_params;
# include /home/vagrant/app/uwsgi_params; # the uwsgi_params file you installed
}
}
46 changes: 46 additions & 0 deletions salt/roots/salt/nginx/init.sls
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# nginx/init.sls

include:
- uwsgi

nginx:
pkg:
- installed
service:
- running
- enable: True
- reload: True
- require:
- pkg: nginx
- file: /etc/nginx/nginx.conf
- file: /etc/nginx/sites-available/app.conf
- file: /etc/nginx/sites-enabled/app.conf
- file: /etc/nginx/sites-enabled/default
- service: uwsgi
- watch:
- service: uwsgi

/etc/nginx/nginx.conf:
file:
- managed
- source: salt://nginx/nginx.conf
- require:
- pkg: nginx

/etc/nginx/sites-available/app.conf:
file:
- managed
- source: salt://nginx/app.conf
- require:
- pkg: nginx

/etc/nginx/sites-enabled/app.conf:
file:
- symlink
- target: /etc/nginx/sites-available/app.conf
- require:
- file: /etc/nginx/sites-available/app.conf

/etc/nginx/sites-enabled/default:
file:
- absent
95 changes: 95 additions & 0 deletions salt/roots/salt/nginx/nginx.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
user vagrant;#www-data;
worker_processes 4;
pid /run/nginx.pid;

events {
worker_connections 768;
# multi_accept on;
}

http {

##
# Basic Settings
##

sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
# server_tokens off;

# server_names_hash_bucket_size 64;
# server_name_in_redirect off;

include /etc/nginx/mime.types;
default_type application/octet-stream;

##
# Logging Settings
##

access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;

##
# Gzip Settings
##

gzip on;
gzip_disable "msie6";

# gzip_vary on;
# gzip_proxied any;
# gzip_comp_level 6;
# gzip_buffers 16 8k;
# gzip_http_version 1.1;
# gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;

##
# nginx-naxsi config
##
# Uncomment it if you installed nginx-naxsi
##

#include /etc/nginx/naxsi_core.rules;

##
# nginx-passenger config
##
# Uncomment it if you installed nginx-passenger
##

#passenger_root /usr;
#passenger_ruby /usr/bin/ruby;

##
# Virtual Host Configs
##

include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}


#mail {
# # See sample authentication script at:
# # http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript
#
# # auth_http localhost/auth.php;
# # pop3_capabilities "TOP" "USER";
# # imap_capabilities "IMAP4rev1" "UIDPLUS";
#
# server {
# listen localhost:110;
# protocol pop3;
# proxy on;
# }
#
# server {
# listen localhost:143;
# protocol imap;
# proxy on;
# }
#}
4 changes: 3 additions & 1 deletion salt/roots/salt/top.sls
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@ base:
- core
- virtualenv
- postgresql
- app
- app
- uwsgi
- nginx
28 changes: 28 additions & 0 deletions salt/roots/salt/uwsgi/app_wsgi.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# mysite_uwsgi.ini file
[uwsgi]

# Django-related settings
# the base directory (full path)
chdir = /home/vagrant/app
# Django's wsgi file
# TODO: Investigate if we can set a generic name, or a pillar var.
module = wsgi

# the virtualenv (full path)
# TODO: Build a venv and run uWSGI from it.
home = /home/vagrant/venv

# process-related settings
# master
master = true
# maximum number of worker processes
processes = 10
# the socket (use the full path to be safe
socket = /tmp/app.sock
# ... with appropriate permissions - may be needed
chmod-socket = 664
# clear environment on exit
vacuum = true

# Environment variables
env = DATABASE_URL=postgresql://{{ pillar['dbuser'] }}:{{ pillar['dbpassword'] }}@localhost:5432/{{ pillar['dbname'] }}
54 changes: 54 additions & 0 deletions salt/roots/salt/uwsgi/init.sls
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# uwsgi/init.sls

# include:
# - dependencies

/etc/uwsgi/apps-available/app_wsgi.ini:
file:
- managed
- source: salt://uwsgi/app_wsgi.ini
- user: vagrant
- group: vagrant
- makedirs: True
- template: jinja
- mode: 755
# - require:
# - pkg: python3-pip
# - cmd: install-requirements

/etc/uwsgi/apps-enabled/app_wsgi.ini:
file:
- symlink
- target: /etc/uwsgi/apps-available/app_wsgi.ini
- makedirs: True
- require:
- file: /etc/uwsgi/apps-available/app_wsgi.ini

/etc/init/uwsgi.conf:
file:
- managed
- source: salt://uwsgi/uwsgi.conf
- file_mode: 744
# - require:
# - pkg: python3-pip
# - cmd: install-requirements

/var/log/uwsgi:
file:
- directory
- user: vagrant
- group: vagrant
- makedirs: True
# - require:
# - pkg: python3-pip
# - cmd: install-requirements

uwsgi:
service:
- running
- enable: True
- require:
- file: /etc/uwsgi/apps-enabled/app_wsgi.ini
- file: /etc/init/uwsgi.conf
- file: /var/log/uwsgi

7 changes: 7 additions & 0 deletions salt/roots/salt/uwsgi/uwsgi.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# simple uWSGI script

description "uWSGI in emperor mode"
start on runlevel [2345]
stop on runlevel [06]

exec uwsgi --master --emperor /etc/uwsgi/apps-enabled --uid vagrant --gid vagrant --chmod-socket --die-on-term --logto /var/log/uwsgi/emperor.log
7 changes: 7 additions & 0 deletions wsgi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import os
from app import create_app

application = create_app(os.getenv('FLASK_CONFIG') or 'default')

if __name__ == "__main__":
application.run(host='0.0.0.0')

0 comments on commit ac1f643

Please sign in to comment.