Commits

Anonymous committed 19f8033

improved media and static ngnix configuration

Comments (0)

Files changed (3)

fagungis/conf/nginx.conf

                 proxy_pass   http://%(gunicorn_bind)s;
         }
 
-        location /static/
+        location %(django_media_location)s/
         {
-                root %(code_root)s/;
+                root %(django_media_root)s/;
         }
 
-        location /media/
+        location %(django_static_location)s/
         {
-                root %(code_root)s/;
+                root %(django_static_root)s/;
         }
 }

fagungis/example_fabfile.py

     env.code_root = join(env.projects_path, env.project)
     #  the path where manage.py and settings.py of this project is located
     env.django_project_root = join(env.code_root, 'sites', 'prod')
-    #  django media dir, if not in code_root please adjust also nginx.conf
+    #  django media dir
     env.django_media_path = join(env.code_root, 'media')
-    #  django sattic dir, if not in code_root please adjust also nginx.conf
+    #  django static dir
     env.django_static_path = join(env.code_root, 'static')
     #  do you use south in your django project?
     env.south_used = False

fagungis/tasks.py

 #!/usr/bin/env python
 # -*- coding: utf-8 -*-
+from copy import copy
 from datetime import datetime
-from os.path import join, abspath, dirname, isfile
+from os.path import join, abspath, dirname, isfile, basename
 from fabric.api import env, puts, abort, cd, hide, task
 from fabric.operations import sudo, settings, run
 from fabric.contrib import console
         template = 'conf/nginx.conf'
     else:
         template = '%s/conf/nginx.conf' % fagungis_path
+    context = copy(env)
+    # Media directory
+    path = env.django_media_path.rstrip('/')
+    context['django_media_location'] = basename(path)
+    context['django_media_root'] = dirname(path)
+    # Static directory
+    path = env.django_static_path.rstrip('/')
+    context['django_static_location'] = basename(path)
+    context['django_static_root'] = dirname(path)
+    # Template
     upload_template(template, env.nginx_conf_file,
-                    context=env, backup=False)
+                    context=context, backup=False)
     sudo('ln -sf %s /etc/nginx/sites-enabled/' % env.nginx_conf_file)
     _test_nginx_conf()
     sudo('nginx -s reload')
 
 
 def _prepare_media_path():
-    sudo('chmod -R 775 %s' % env.django_media_path)
+    path = env.django_media_path.rstrip('/')
+    sudo('mkdir -p %s' % path)
+    sudo('chmod -R 775 %s' % path)
 
 
 def _upload_rungunicorn_script():