From c89a4a0390fd7250179e7a3730aa53a6ce2ede68 Mon Sep 17 00:00:00 2001 From: lombardofr Date: Sun, 2 Sep 2018 16:15:25 +0200 Subject: [PATCH] nginx integration Change-Id: I66c63d7a8d3ea7674c9fe1c1d0514f63472bc67f Signed-off-by: lombardofr --- .gitignore | 3 +++ Dockerfile | 13 +++++++--- django.ini | 17 +++++++++++++ nginx-app.conf | 58 +++++++++++++++++++++++++++++++++++++++++++++ sf_t3d/settings.py | 40 ++++++++++++++++--------------- supervisor-app.conf | 6 +++++ 6 files changed, 115 insertions(+), 22 deletions(-) create mode 100644 django.ini create mode 100644 nginx-app.conf create mode 100644 supervisor-app.conf diff --git a/.gitignore b/.gitignore index be73e08..da69467 100644 --- a/.gitignore +++ b/.gitignore @@ -29,6 +29,9 @@ pip-delete-this-directory.txt # Django stuff: *.log +#uwsgi +*.pid + # Sphinx documentation docs/_build/ diff --git a/Dockerfile b/Dockerfile index 4914e93..e9d3030 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,22 +1,29 @@ -FROM ubuntu:16.04 +FROM ubuntu:16.04 WORKDIR /usr/src/app COPY . /usr/src/app RUN apt-get update -RUN apt-get install -y npm git python-pip +RUN apt-get install -y npm git python-pip nginx supervisor RUN npm install -g bower RUN ln -s /usr/bin/nodejs /usr/bin/node RUN bower install --allow-root RUN pip install -r requirements.txt +RUN pip install uwsgi +RUN echo "daemon off;" >> /etc/nginx/nginx.conf +COPY nginx-app.conf /etc/nginx/sites-available/default +COPY supervisor-app.conf /etc/supervisor/conf.d/ # delete the copy of the database inside the container (if exists) RUN rm -f db.sqlite3 +ENV DJANGO_ENV=prod RUN python manage.py makemigrations authosm RUN python manage.py migrate +RUN python manage.py collectstatic --noinput EXPOSE 80 -CMD ["python", "manage.py", "runserver", "0.0.0.0:80"] + +CMD ["supervisord", "-n"] \ No newline at end of file diff --git a/django.ini b/django.ini new file mode 100644 index 0000000..3eb767f --- /dev/null +++ b/django.ini @@ -0,0 +1,17 @@ +[uwsgi] +module = sf_t3d.wsgi:application +master = true +pidfile = django.uwsgi.pid +enable-threads = true +http = 127.0.0.1:8000 +processes = 5 +# respawn processes taking more than 50 seconds +harakiri = 50 +# respawn processes after serving 5000 requests +max-requests = 5000 +# clear environment on exit +vacuum = true +# optional path to a virtualenv +#home = env +# background the process +daemonize = django.uwsgi.log \ No newline at end of file diff --git a/nginx-app.conf b/nginx-app.conf new file mode 100644 index 0000000..c5bbd68 --- /dev/null +++ b/nginx-app.conf @@ -0,0 +1,58 @@ +# nginx-app.conf + +# configuration of the server +server { + + listen 80; + + charset utf-8; + + # max upload size + client_max_body_size 75M; + + location /static/ { + alias /usr/src/app/static/; + } + + location / { + proxy_pass http://localhost:8000; + proxy_set_header Upgrade $http_upgrade; + proxy_set_header Connection "upgrade"; + proxy_set_header Host $host; + proxy_set_header X-Real-IP $remote_addr; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + } + + # ----------- + # Enable GZIP + # ----------- + gzip on; + gzip_disable "msie6"; + gzip_vary on; + gzip_types text/plain + text/css + application/json + application/javascript + application/x-javascript + text/xml + application/xml + application/xml+rss + text/javascript + image/svg+xml; + gzip_proxied any; + gzip_comp_level 6; + gzip_buffers 16 8k; + gzip_http_version 1.0; + + # ------------ + # Cache assets + # ------------ +# location ~* .(ogg|ogv|svg|svgz|eot|otf|woff|mp4|ttf|css +# |rss|atom|js|jpg|jpeg|gif|png|ico|zip|tgz +# |gz|rar|bz2|doc|xls|exe|ppt|tar|mid|midi +# |wav|bmp|rtf)$ { +# expires max; +# log_not_found off; +# access_log off; +# } +} \ No newline at end of file diff --git a/sf_t3d/settings.py b/sf_t3d/settings.py index 0649cde..753537a 100644 --- a/sf_t3d/settings.py +++ b/sf_t3d/settings.py @@ -21,11 +21,12 @@ BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) # SECURITY WARNING: keep the secret key used in production secret! SECRET_KEY = 'o5+o2jv(3-dqr(&ia#-@79cgr%xi*s+6xjws^8cxp211ge#buf' - -# SECURITY WARNING: don't run with debug turned on in production! -DEBUG = True - -ALLOWED_HOSTS = [] +if os.getenv('DJANGO_ENV') == 'prod': + DEBUG = False +else: + DEBUG = True + print DEBUG +ALLOWED_HOSTS = ['*'] AUTH_USER_MODEL = "authosm.OsmUser" @@ -159,20 +160,21 @@ USE_TZ = True # https://docs.djangoproject.com/en/1.9/howto/static-files/ STATIC_URL = '/static/' - -STATICFILES_FINDERS = ( - 'django.contrib.staticfiles.finders.FileSystemFinder', - 'django.contrib.staticfiles.finders.AppDirectoriesFinder', - 'django.contrib.staticfiles.finders.DefaultStorageFinder' -) - -STATICFILES_DIRS = ( - # Put strings here, like "/home/html/static" or "C:/www/django/static". - # Always use forward slashes, even on Windows. - # Don't forget to use absolute paths, not relative paths. - os.path.join(BASE_DIR, "static"), -) +if DEBUG: + STATICFILES_FINDERS = ( + 'django.contrib.staticfiles.finders.FileSystemFinder', + 'django.contrib.staticfiles.finders.AppDirectoriesFinder', + 'django.contrib.staticfiles.finders.DefaultStorageFinder' + ) + STATICFILES_DIRS = ( + # Put strings here, like "/home/html/static" or "C:/www/django/static". + # Always use forward slashes, even on Windows. + # Don't forget to use absolute paths, not relative paths. + os.path.join(BASE_DIR, "static"), + ) +else: + STATIC_ROOT = os.path.join(BASE_DIR, "static/") LOCALE_PATHS = ( os.path.join(BASE_DIR, 'locale'), -) \ No newline at end of file +) diff --git a/supervisor-app.conf b/supervisor-app.conf new file mode 100644 index 0000000..0aa033e --- /dev/null +++ b/supervisor-app.conf @@ -0,0 +1,6 @@ +[program:app-uwsgi] +command = /usr/local/bin/uwsgi --ini /usr/src/app/django.ini +environment=DJANGO_ENV="prod" + +[program:nginx-app] +command = /usr/sbin/nginx \ No newline at end of file -- 2.25.1