im trying to get my Django App to run using only uWSGI. The project is not that big, so i would really prefer to leave nginx out.
I just can't get uWSGI to show my static files though. I've gone through the settings multiple times and can't find the problem.
I have the STATIC_URL set to 'module/static/'
STATIC_ROOT set to '/module/static_files/' (i read somewhere that they should not be the same)
and my uwsgi.ini looks like this:
[uwsgi]
module=Module.wsgi:application
master=True
http=:80
processes=4
threads=2
static-map /static= /module/static_files/
the projects file structure is set up in the following way:
-- Project:
---init.py
---settings.py
---urls.py
---wsgi.py
-- Logs
-- module
---static
---static_files
---[... module template, model, urls etc]
-manage.py
-db.sqlite3
I can run collectstatic and generate all the static files in the correct folder.
But when i run the uwsgi script, it wont work and gives me a 404 file not found for all static files.
I would really appreciate any help, I've been stuck on this for an entire week now...
(i have checked out Deployment with Django and Uwsgi
but as far as i can tell, my static-map is set correctly)
you need to add your static files as static-map=/static=/module/static_files/
Related
What I want to do
I'm trying to deploy my Django app using AWS (EC2), nginx, and gunicorn. The current goal is to place the static files in the directory /usr/share/nginx/html/static.
Problem that is occurring
I created /usr/share/nginx/html/static and then ran python manage.py collectstatic command.
I had set STATIC_ROOT in settings.py to STATIC_ROOT='/usr/share/nginx/html/static' before, so I thought I could use the collectstatic command to collect static files from within the Django project and place them in /usr/share/nginx/html/static. However, that wasn't the case. Instead I got an error saying django.core.exceptions.ImproperlyConfigured: You're using the staticfiles app without having set the STATIC_ROOT setting to a filesystem path. Could you please explain why this is so?
I have tried some suggested solutions over the internet but the problem still exists. I’d be really appreciated if you could reply to me.
error message
'STATIC_ROOT'
I tried created a Django project and define STATIC_ROOT='/usr/share/nginx/html/static
The only problem I face is
PermissionError: [Errno 13] Permission denied: '/usr/share/nginx/html/static/admin'
After fixing permission it worked.
So As is see you are running collectstatic with a specific setting file(private_diary.settings) make sure static root is defined in that file.
Make sure you have permission in the static directory
And make sure to include STATIC_ROOT in your settings.py
STATIC_ROOT = "/usr/share/nginx/html/static"
# Extra places for collectstatic to find static files.
STATICFILES_DIRS = [
"/usr/share/nginx/html/static"
]
I am trying to deploy my Django website with Django with Heroku, and it keeps showing me "Internal Server Error." None of the other answers regarding this same problem here at SO solved my problem, and I noticed that I only have this problem when I set DEBUG to False.
My heroku logs commands show me the following error:
raise ValueError("Missing staticfiles manifest entry for '%s'" % clean_name)
ValueError: Missing staticfiles manifest entry for '/images/posting/bike.png'
I have setup my settings the following way:
ALLOWED_HOSTS = ["stratagembetaapp.herokuapp.com"]
STATIC_URL = "/static/"
STATIC_ROOT = os.path.join(BASE_DIR, "staticfiles")
MEDIA_ROOT = os.path.join(BASE_DIR, "media")
MEDIA_URL = "/media/"
django_heroku.settings(locals())
Additionally, gunicorn, django-heroku, and whitenoise are perfectly installed. My requirements.txt and Procfile are also in order.
I have also already run the "python manage.py collectstatic" in the Heroku shell, but I still get the same result.
The error is that you are referencing a static file in your templates which doesn't exist (or isn't in the right place). In DEBUG mode this doesn't generate an error, but in production it does.
Somewhere in your templates you have a reference like this:
{% static '/images/posting/bike.png' %}
I think the error here is just that leading slash, so possibly it will work if you just use {% static 'images/posting/bike.png' %}.
when you deploy to heroku successfully but Application error or internal server error
do the following !
1 with debug=False every thing works fine in development environment
2 make sure you did not import unnecessary packages .
3 check your code very carefully
4 run python manage.py collectstatic
5 after deployed on heroku make sure to run
heroku run python manage.py migrate
6 if you deployed successfully and it does not work .the problems are definitely minor errors in your code check them on dev server with debug=False and make sure every thing worksalso in dev server with debug=False make sure to run
python manage.py collectstatic
Are you using a static file server? From your settings, it doesn't look like you are using one. Heroku won't store your static files. These tutorials can walk you through the process. Storing your files on s3 isn't free but it is super cheap (First 50 TB/ month $0.023 / GB)
https://www.codingforentrepreneurs.com/blog/s3-static-media-files-for-django/
https://simpleisbetterthancomplex.com/tutorial/2017/08/01/how-to-setup-amazon-s3-in-a-django-project.html
https://www.caktusgroup.com/blog/2014/11/10/Using-Amazon-S3-to-store-your-Django-sites-static-and-media-files/
I'm trying to deploy a web app built with Django/Redux/React/Webpack on a Digital Ocean droplet. I'm using Phusion Passenger and Nginx on the deployment server.
I used create-react-app to build a Django app which has a frontend that uses React/Redux, and a backend api that uses django-rest-framework. I built the frontend using npm run build.
The Django app is configured to look in the frontend/build folder for its files and everything works as expected, including authentication. It's based on this tutorial: http://v1k45.com/blog/modern-django-part-1-setting-up-django-and-react/
In settings.py:
ALLOWED_HOSTS = ['*']
TEMPLATES = [
...
'DIRS': [
os.path.join(BASE_DIR, 'frontend/build'),
],
...
]
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'frontend/build/static'),
]
On my development machine, I activate a Python 3.6 virtual environment and run ./manage.py runserver, and the app is displayed at localhost:3000.
On the deployment server, I've cloned the files into a folder in var/www/ and built the frontend.
I've set up Passenger according to the docs with a file passenger_wsgi.py:
import myapp.wsgi
application = myapp.wsgi.application
And the wsgi.py file is in the djangoapp folder below:
import os
from django.core.wsgi import get_wsgi_application
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'myapp.settings')
application = get_wsgi_application()
The Passenger docs only cover a single-part app:
https://www.phusionpassenger.com/library/walkthroughs/start/python.html
https://www.phusionpassenger.com/library/walkthroughs/deploy/python/digital_ocean/nginx/oss/xenial/deploy_app.html
https://www.phusionpassenger.com/library/deploy/wsgi_spec.html
I've tried cloning the tutorial part 1 code directly onto my server and following the instructions to run it. I got this to work on the server by adding "proxy": "http://localhost:8000" to frontend/package.json. If I run the Django server with ./manage.py runserver --settings=ponynote.production_settings xxx.x.x.x:8000
then the app is correctly served up at myserver:8000. However Passenger is still not serving up the right files.
I have changed wsgi.py to say this:
import os
from django.core.wsgi import get_wsgi_application
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myapp.production_settings")
application = get_wsgi_application()
The page served by Passenger at URL root now appears to have the right links to the js files such as "text/javascript" src="/static/bundles/js/main.a416835a.js, but the links don't work: the expected js is not present. Passenger is failing to serve the js files from static/bundles/js, even though the Django server can find them.
Very grateful for any help or ideas.
Create-react-app has a fairly opinionated setup for local and production environments.
Locally, running npm start will run a webpack-dev-server, which you would typically access on port 3000. It runs a local nodejs web server to serve the files. You can route requests to your local Django server via the proxy setting.
It's worth noting that at this point there is little or no connection between your React app and Django. If you use the proxy setting, the only thing connecting the two apps is the routing of any requests not handled by your React app to your Django app via the port.
By default in create-react-app (and as noted in the tutorial you mentioned you are following) in production you would run npm run build which will process your create-react-app files into static JS and CSS files, which are then accessed in Django like static files any other Django app.
One thing Django is missing in order to access the static files is a way to know what files are generated when running npm run build. Running a build will typically result in files output like this:
- css
|- main.e0c3cfcb.css
|- main.e0c3cfcb.css.map
- js
|- 0.eb5a2873.chunk.js
|- 0.eb5a2873.chunk.js.map
|- 1.951bae33.chunk.js
|- 1.951bae33.chunk.js.map
A random hash is added to filenames to ensure cache busting. This is where webpack-bundle-tracker and django-webpack-loader come in. When build files are generated, an accompanying file is also created called manifest.json, listing the files created for the build. This is generated in Webpack and picked up by django-webpack-loader so that Django can know which files to import.
It is possible to run a nodejs server in production, or to use server-side rendering, but if you're following the tutorial you mentioned and using create-react-app default settings, then running npm run build and deploying the static files is the simplest, safest option.
Nothing in any of the Passenger deployment links you mention cover anything beyond deploying a Python/Django app - you would need to manage two apps and deployments to have both Django and React running as servers in production.
Note that the tutorial you mention covers how to get your build files into Django in production, but you will need to ensure that you have webpack-bundle-tracker, django-webpack-loader and your Django staticfiles configuration all configured to work together.
The key missing setting was the 'location' setting in the Passenger config file.
Although the Django server serves up the static files, including the build files for your React app, Nginx doesn't see any static files except those in a 'public' directory.
So to deploy a Django app built with Webpack to production, you need to tell Nginx about those files. If you're using Passenger, these settings are probably in a separate Passenger config file. 'alias' is the command to use in this case where the folder has a different name from 'static' (which is where the web page links point).
If you use a virtual environment for your app, you need to specify where Passenger can find the right Python executable.
/etc/nginx/sites-enabled/myapp.conf
server {
listen 80;
server_name xx.xx.xx.xx;
# Tell Passenger where the Python executable is
passenger_python /var/www/myapp/venv36/bin/python3.6;
# Tell Nginx and Passenger where your app's 'public' directory is
# And where to find wsgi.py file
root /var/www/myapp/myapp/myapp;
# Tell Nginx where Webpack puts the bundle folder
location /static/ {
autoindex on;
alias /var/www/myapp/myapp/assets/;
}
# Turn on Passenger
passenger_enabled on;
}
Passenger uses the wsgi.py file as an entry point to your app. You need a passenger_wsgi.py file one level above the wsgi.py file. This tells Passenger where to find the wsgi.py file.
/var/www/myapp/myapp/passenger_wsgi.py
import myapp.wsgi
application = myapp.wsgi.application
/var/www/myapp/myapp/myapp/wsgi.py
If you are using a separate production_settings.py file, make sure this is specified here.
import os
from django.core.wsgi import get_wsgi_application
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myapp.production_settings")
application = get_wsgi_application()
Is settings.py the same as mysite.settings from this example here:
http://django.readthedocs.io/en/1.2.X/howto/deployment/modwsgi.html
It seems ambiguous to me if it is the same thing or not, my app has a settings.py in it but it seems like this isn't the same as mysite.settings spoken of in the docs. I do have my app running with$ python manage.py runserver just fine on centos, and even see apaches test page on the same box so i know apache is running. Was going through the doc above to try to get apache to server the django app and thats where I got confused as to what it is really asking for...
(also the apache folder didn't exist so I added that and created a django.wsgi with the code below that I think was the right thing to do).
Almost looks like I have to setup a sys.path somewhere?
https://code.google.com/archive/p/modwsgi/wikis/IntegrationWithDjango.wiki
When you define:
DJANGO_SETTINGS_MODULE = 'mysite.settings'
Django looks up for settings.py file in mysite folder. mysite folder has to be on your PYTHONPATH. Make sure you set your PYTHONPATH to something like:
export PYTHONPATH=/home/work/project:/home/work/project/project:$PYTHONPATH
I'm trying to deploy flask app to openshift express. The problem is that links to css files are not working. My application folder layout is as follows:
/wsgi
/static
/myapp
/main
/pages
/static
Here "wsgi" and first "static" folders are provided by openshift. However, I put all static files inside main/static, and created flask app inside myapp/_init_.py file as follows:
app = Flask("myapp", template_folder='main/pages', static_folder='main/static')
Now, readme file inside static folder, provided by openshift says that in order to serve static files from different path, I have to use .htaccess file to rewrite url. But I couldn't get it right. Of course, the problem goes away if I copy all my static files to the first "static" folder provided by openshift. I just do not want that. So, can someone help me to serve my static files from my own static folder?
Can you post your .htaccess file? Also, try running a rhc app tail -a appname to see if there is anything in your log files. They might be able to tell you what directories your app is trying to serve content from.