[PYTHON] When I deploy a Django app with Apache2 and it no longer reads static files

Make a note of what to do if you stop reading CSS and image files after creating a Django app according to the tutorial and deploying it in a production environment.

This slide is easy to understand, so it might be better to read this https://tell-k.github.io/djangocongressjp2019/#43

1. Django's handling of static files

Django handles static files differently during development and in production. To switch this is DEBUG = True / False in setting.py.

/ Static / when DEBUG = True

Since django is designed to be developed by dividing it into folders for each application, CSS and image files, which are static files, are also placed in the application folder. Therefore, while DEBUG = True, which means under development, all folders named / static / under the folder specified by STATIC_URL and STATICFILES_DIRS written in setting.py are handled by Alias called / static /.

For example, if you have a file like project / app1 / static / base.css It treats it as if it were http://hoge.com/static/base.css.

Why django doesn't deliver / static / in production

If you use this function in the production environment, the main process will be unnecessarily overloaded and it may cause vulnerabilities, so you have to turn off this function in the production environment. If you deploy a Django app with Apache2 etc. to force this, only the sample screen will be displayed unless DEBUG = False.

/ Static / when DEBUG = False

When DEBUG = True, Django provided a static file with an Alias of / static /, but when DEBUG = False, it will not be provided, so in the previous example http://hoge.com/static/ This means that base.css does not exist. If you provide this with Apache etc., you can get the same operation as during development.

2. Apache2 setup procedure

Collect static files in one place with python manage.py collectstatic and write the collected location in apahce2.conf so that Apache2 will deliver it as / static /. It's the same as the Django Girls Tutorial .

2-1. Collect static files in one place

Create a folder called static directly under the project as shown below and collect it.

/home/xxxx/django/proj1/


proj1
├── manage.py
├── mysite
│   ├── __init__.py
│   ├── settings.py
│   ├── urls.py
│   └── wsgi.py
├── myvenv
│   └── ...
├── static Where to collect static files
│   ├── hoge.jpg
│   └── base.css
└── requirements.txt

Specifies where to collect static files (STATIC_ROOT). In the following, / home / xxxx / django / proj1 / static / will be STATIC_ROOT.

proj1/mysite/setting.py


STATIC_ROOT = os.path.join(BASE_DIR, 'static')

Executing the collectstatics command will collect static files in the PATH of STATIC_ROOT.

terminal


cd /home/xxxx/django/proj1
python manage.py collectstatics

2-2. Add static file location to apache2.conf

I want to treat /home/xxxx/django/proj1/static/base.css as http://hoge.com/static/base.css, so add Alias to the folder and allow it (Require all granted).

/etc/apache2/apache2.conf


Alias /static/ /home/xxxx/django/proj1/static/
<Directory /home/xxxx/django/proj1/static>
    Require all granted
</Directory>

I changed the settings, so restart Apache2.

terminal


sudo /etc/init.d/apache2 restart

2-3. If it doesn't work

Since collectstatics simply copies the file, you can check the operation by checking if the file is copied to STATIC_ROOT.

If you haven't successfully registered Apache2, you should see Django wsgi Apache2:'AH01630: client denied by server configuration' in the Apache2 error log. Make sure the contents of apache2.conf are the same as STATIC_ROOT.

If you keep the default settings, you can see the Apache2 error log below.

terminal


tail -f /var/log/apache2/error.log

Recommended Posts

When I deploy a Django app with Apache2 and it no longer reads static files
Deploy a Python app on Google App Engine and integrate it with GitHub
Deploy a Django app made with PTVS on Azure
Deploy a real-time web app with swampdragon x apache
How to handle static files when deploying to production with Django
Create a deploy script with fabric and cuisine and reuse it
When I put Django in my home directory, I was addicted to static files with permission errors
Create a temporary file with django as a zip file and return it
A memorandum when I tried to get it automatically with selenium
Create and deploy a Django (PTVS) app using Azure Table storage
Make a scraping app with Python + Django + AWS and change jobs
I made a chatbot with Tensor2Tensor and this time it worked
Build your Django app on Docker and deploy it to AWS Fargate
No reason to think while writing a crawler with Django and Celery
Create a Todo app with Django ④ Implement folder and task creation functions
I made an app for foreign visitors to Japan with a hackathon and won a prize, but when I thought about it carefully, it was useless.
CentOS 6.4 with Python 2.7.3 with Apache with mod_wsgi and Django
I can't deploy with google app engine
I made a WEB application with Django
When I tried to make a VPC with AWS CDK but couldn't make it
When I tried to create a virtual environment with Python, it didn't work
I want to write an element to a file with numpy and check it.
Let's make a nervous breakdown app with Vue.js and Django-Rest-Framework [Part 1] ~ Django setup ~
[Python] I introduced Word2Vec and played with it.
I get a UnicodeDecodeError when running with mod_wsgi
Configure a module with multiple files in Django
I want to upload a Django app to heroku
I made a Docker Image that reads RSS and automatically tweets regularly and released it.
[Introduction to system trading] I drew a Stochastic Oscillator with python and played with it ♬
I made a web application that maps IT event information with Vue and Flask
[Shell art] Only when it is a multiple of 3 and a number with 3 becomes stupid
Quickly create a Python data analysis dashboard with Streamlit and deploy it to AWS
Image processing with Python (I tried binarizing it into a mosaic art of 0 and 1)