A
A
AntonIgin2017-08-13 11:27:05
Django
AntonIgin, 2017-08-13 11:27:05

How to enable site statics on a production server?

The crux of the issue is in the title. I'm trying to install on a local Ubuntu machine. So far, to complete the task, the following has been done according to the instructions:
I registered a new directory for statics in settings.py:
STATIC_ROOT = '/opt/mag/static/'
But when I try to collect statics:
python manage.py collectstatic
An error appears:

Copying '/home/anton/virtenvs/mag_env/lib/python3.5/site-packages/django/contrib/admin/static/admin/fonts/LICENSE.txt'
Traceback (most recent call last):
  File "manage.py", line 22, in <module>
    execute_from_command_line(sys.argv)
  File "/home/anton/virtenvs/mag_env/lib/python3.5/site-packages/django/core/management/__init__.py", line 367, in execute_from_command_line
    utility.execute()
  File "/home/anton/virtenvs/mag_env/lib/python3.5/site-packages/django/core/management/__init__.py", line 359, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/anton/virtenvs/mag_env/lib/python3.5/site-packages/django/core/management/base.py", line 305, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/home/anton/virtenvs/mag_env/lib/python3.5/site-packages/django/core/management/base.py", line 356, in execute
    output = self.handle(*args, **options)
  File "/home/anton/virtenvs/mag_env/lib/python3.5/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 193, in handle
    collected = self.collect()
  File "/home/anton/virtenvs/mag_env/lib/python3.5/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 124, in collect
    handler(path, prefixed_path, storage)
  File "/home/anton/virtenvs/mag_env/lib/python3.5/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 347, in copy_file
    self.storage.save(prefixed_path, source_file)
  File "/home/anton/virtenvs/mag_env/lib/python3.5/site-packages/django/core/files/storage.py", line 54, in save
    return self._save(name, content)
  File "/home/anton/virtenvs/mag_env/lib/python3.5/site-packages/django/core/files/storage.py", line 321, in _save
    os.makedirs(directory)
  File "/home/anton/virtenvs/mag_env/lib/python3.5/os.py", line 231, in makedirs
    makedirs(head, mode, exist_ok)
  File "/home/anton/virtenvs/mag_env/lib/python3.5/os.py", line 231, in makedirs
    makedirs(head, mode, exist_ok)
  File "/home/anton/virtenvs/mag_env/lib/python3.5/os.py", line 231, in makedirs
    makedirs(head, mode, exist_ok)
  File "/home/anton/virtenvs/mag_env/lib/python3.5/os.py", line 241, in makedirs
    mkdir(name, mode)
PermissionError: [Errno 13] Permission denied: '/opt/mag'

Running the same command via sudo is a crooked solution: what if I had special versions of packages for this particular project?
Okay, I'm skipping this step, leaving the address the same: /home/anton/Django-u/magazine_project/main_site/
In the project, static (as in any folder inside anton) is unpacked normally.
Go to the nginx settings folder:
cd /etc/nginx/sites-available/
Open the default file and add the following:
server {
    listen 8000;
    server_name 192.168.10.5; #либо ip, либо доменное имя
    access_log  /var/log/nginx/example.log;

    location /static/ {
        root /home/anton/Django-u/magazine/main_site/;
        expires 30d;
    }

    location / {
        proxy_pass http://127.0.0.1:8000; 
        proxy_set_header Host $server_name;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
  }

Next:
sudo service nginx restart
I go to the project address (/home/anton/Django-u/magazine/) and download via gunicorn:
gunicorn magazine_project.wsgi:application --bind 192.168.10.3:8000
Result in the console:
(mag_env) anton @anton-X451CA:~/Django-u/magazine$ gunicorn magazine_project.wsgi:application --bind 192.168.10.3:8000
[2017-08-13 15:25:05 +0700] [26544] [INFO] Starting gunicorn 19.7 .1
[2017-08-13 15:25:05 +0700] [26544] [INFO] Listening at: 192.168.10.3:8000 (26544)
[2017-08-13 15:25:05 +0700] [26544] [INFO] Using worker: sync
[2017-08-13 15:25:05 +0700] [26547] [INFO] Booting worker with pid: 26547
Not Found: /static/styles.css
As you can see, the statics still didn't load. When running the project through python manage.py runserverthere are no problems with statics.
What need to do?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
sim3x, 2017-08-13
@AntonIgin

https://www.digitalocean.com/community/tutorials/h...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question