settings.py
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
DEBUG = True
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'adminstatic')
mynginx.conf /etc/nginx/sites-enabled/mynginx.conf
you can be sure that I have only this conf file in sites-enabled folder
server {
listen *:80;
server_name _;
access_log /var/log/myapp.access.log;
error_log /var/log/myapp.error.log;
# Django media
location /static/ {
alias /root/proj/myapp/adminstatic/; #your Django project's static files - amend as required
}
# Finally, send all non-media requests to the Django server.
location / {
uwsgi_pass unix:/tmp/myapp.sock;
include /etc/nginx/uwsgi_params; # the uwsgi_params file you installed
}
}
after this I tried collectstatic command
python manage.py collectstatic
It has succesfully collected all the statics in adminstatic folder.I crosscheked, there is a folder named admin in adminstatic folder. and this admin folder has 3 folders named css,img,js.
all these I'm doing in production machine. so when I tried to access the django admin page on my local browser by production's ip, the admin panel is opening but with loading any css/js. I did F12 and see in network tab. there was forbidden error for all statics.
PS: I'm using uWsgi along with nginx.
and as usual I've already looked at few SO threads and blogs. but it didn't help me.
The nginx web process user (usually www-data) probably doesn't have read permission of /root/proj/myapp/adminstatic/. I imagine if you check your myapp.error.log you would see permission denied messages.
You'd need to move your project into a folder the nginx process has permission to read from (you really shouldn't serve these things from /root) or you could chown / chmod /root/proj/myapp/adminstatic/ appropriately to give access to www-data.
I moved my static files to /data/www and it worked.
Move your project to /var/www/html/then run sudo chown -R www-data:www-data project-directory/ .Remember to edit your configurations on /etc/systemd/system/gunicorn.service and /etc/nginx/sites-available/your-project.conf to pint to the new location.
Then run
sudo systemctl daemon-reload,
sudo systemctl restart gunicorn,
sudo systemctl restart nginx
Related
Since I had started Django, I'm tackling some ridiculous problems. Recently, when I start a new project and I run it on the server, Django's admin CSS is not loaded. The last project I ran on the server, after a while it was okay, and the real Django admin template was there and the CSS was loaded and it was working. But this time again the same problem happened and I don't know how to solve it cause the project here is the photo is raw, with no special code.
I don't know if it's Chrome problem or not, but I have tried it on other browsers and it was the same thing.
I would be glad if you can help me
the answer I found was to change STATIC_URL = 'static/' in settings.py to STATIC_URL = '/static/'. Only one '/' may change your whole admin appearance. This problem may not happens to everyone but running code in Pycharm, I had been tackling it for such a long time.
If this problem is happening locally, then just change your settings.py
DEBUG = True
#...
#...
STATIC_ROOT = YOUR_STATIC_ROOT_DIRECTORY
Then run collectstatic, make sure you have a proper STATIC_ROOT directory which is also in your settings.py file
python manage.py collectstatic
If you have deployed your app on a production server then, you have to follow certain things in order to get stylesheet and javascript files.
Firstly
In your urls.py you need to add this code
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) + \
static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
Then you have to use a third-party media file server(CDN) Like AWS S3 or you can serve your css, js or media files from your Django server
If you don't want to use AWS S3, then you can either send css/js using your django app (which is not ideal to do), or you can use nginx or apachee to send your css/js
For Nginx to send js/css
You have to add a Static and Media File Root that can be accessed by your django application, I usually use /var/www/ to serve static and media from any Linux server
STATIC_ROOT = '/var/www/static'
MEDIA_ROOT = 'var/www/media'
Then if you are using nginx
server {
server_name domainname.com;
access_log off;
location /static/ {
root /var/www/static;
}
location /media/ {
root /var/www/media;
}
If it is still not working, then your django app might not be able to use the given static_root and media directory, make sure your app has access to them
If you want to send your js/css from your django app (Better not to do in production)
Then
To install whitenoise
pip install whitenoise
In your settings file
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'whitenoise.middleware.WhiteNoiseMiddleware',
# ...
]
It's my first time I am trying to deploy django project to Digital Ocean droplet(instance) and set up with Gunicorn and Nginx. I'v got problem with static files because there are not loaded. It seems I messed things up because in tutorials static is usually located in (in my case) glboy/ and localy in my project it was located in glboy/app/. So I now there is folder /root/glboy/static and everything is copied there by python manage.py collectstatic . I can't understand what is wrong. I tried various things and restarted after every change sudo service nginx restart but nothing helped. What is likely to be wrong?
In sudo nano /etc/nginx/sites-enabled/closer:
location /static/ {
root /home/root/glboy/;
expires 30d;
}
In sudo nano /etc/nginx/sites-available/default:
location /static/ {
root /home/root/glboy/;
expires 30d;
}
In settings.py:
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
EDIT:
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
Structure is like that. I am not sure what is before in unix. As far as I understand it's like home/root/
glboy/
app/, closer/, denv/, static/, manage.py, myapp.log
EDIT2: Main tutorial I'v been following if helps https://www.digitalocean.com/community/tutorials/how-to-set-up-django-with-postgres-nginx-and-gunicorn-on-ubuntu-14-04
I'm trying to run my first Django project using nginx and uwsgi. And I faced with the problem with nginx. I get the message in the brouser "Unable to connect. Firefox can't establish a connection to the server at localhost:8000". I'm following the instructions from this link Everything is fine with uwsgi and installing nginx. Then I downloaded uwsgi_params file and created mysite_nginx.conf file as the instruction says.
Here's mysite_nginx.conf file:
# mysite_nginx.conf
# the upstream component nginx needs to connect to
upstream django {
# server unix:///path/to/your/mysite/mysite.sock; # for a file socket
server 127.0.0.1:8001; # for a web port socket (we'll use this first)
}
# configuration of the server
server {
# the port your site will be served on
listen 8000;
# the domain name it will serve for
server_name localhost; # 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 /home/ubuntu/myproject/mysite/friends_plans/media; # your Django project's media files - amend as required
}
location /static {
alias /home/ubuntu/myproject/mysite/friends_plans/static; # your Django project's static files - amend as required
}
# Finally, send all non-media requests to the Django server.
location / {
uwsgi_pass django;
include /home/ubuntu/myproject/mysite/uwsgi_params; # the uwsgi_params file you installed
}
}
It worked with server name "localhost" and port 8000 when I was checking uwsgi according to the intruction. I made the commands sudo ln -s ~/path/to/your/mysite/mysite_nginx.conf /etc/nginx/sites-enabled/ , python manage.py collectstatic , sudo /etc/init.d/nginx restart. In my settinds.py: STATIC_ROOT = "/home/ubuntu/myproject/mysite/friends_plans/static/". The command about static files worked properly. But I can't set the connection with the server. Could you please tell me where I have a mistake and how to correct it? Thank you very much in advance.
I am getting the following error when attempting to upload an image, when creating a new record in the admin interface.
Permission denied: '/home/jeffy/django_files/django_test/static/uploaded_files/1407616465_2016587_61DjIaZQI8L.jpg'
Relevant settings.py variables:
STATIC_ROOT = "/home/jeffy/django_files/django_test/static/"
MEDIA_ROOT=STATIC_ROOT
The output of
ls -l /home/jeffy/django_files/django_test/static/
is
drwxrwxr-x 2 jeffy jeffy 4096 Aug 9 16:32 uploaded_files
And the servers were started as user jeffy:
jeffy#originaldjangster:/etc/nginx/sites-available$ sudo service nginx start
jeffy#originaldjangster:/etc/nginx/sites-available$ sudo /home/jeffy/django_files/django_test_venv/bin/gunicorn -c /home/jeffy/django_files/django_test_venv/gunicorn_config.py django_test.wsgi
root
I placed this code at the end of settings.py
import getpass
print(getpass.getuser())
which is why "root" prints out after the gunicorn server is started.
Why can't this file be uploaded?
Here's the original gunicorn_config.py file:
command = '/home/jeffy/django_files/django_test_venv/bin/gunicorn'
pythonpath = '/home/jeffy/django_files/django_test'
bind = '127.0.0.1:8001'
workers = 1
user = 'nobody'
Changing nobody to jeffy makes it work (although it balked at the static and media directories being equal...I made a media folder next to the static one).
This setup is only for me to show off my Django chops. If this were a production environment, I do get that running the server as root is a bad idea.
Not sure how "bad" this setup really is, but it's working at least so far...
I'm using AppFog PaaS system for a few days, and I love it, It's probably the best PaaS system that I've tested (I've used other 3 ones previously), but didn't find information about how to serve static content with the Web server in frontend (Apache https or nginx) I'm not sure what server is being used.
My app is a Python WSGI with CherryPy and works perfectly in AppFog but I don't wan't CherryPy to serve static content, I think that Apache httpd or nginx is a better option for that.
With Ryan's support, I'm finally able to load static files! Here are the steps:
Created a 'static' directory in the project root - here all static files will be collected running the collectstatic command.
Edit the settings.py file:
STATIC_ROOT = os.path.join( os.path.abspath( os.path.dirname(file) ), '../static' ) # May change depending on where your settings.py file is!
STATIC_URL = '/static/'
Add following line in urlpatterns variable in urls.py file:
url(r'^static/(?P.*)$', 'django.views.static.serve', { 'document_root': settings.STATIC_ROOT} ) ,
Finally, run collectstatic command in your local machine. This will copy all static files from the apps you are using:
python manage.py collectstatic
That's it. Push in AF :)
Downside: Need to run collectstatic every time we have a new static file...
Edit your nginx.conf file. In the server section enter...
# serve static files
location ~ ^/(images|javascript|css)/ {
root /var/www/html/appname;
}
images, javascript and css would be folders in your document root folder. Update all your urls accordingly.