How to deploy static files¶
Δείτε επίσης
Για μια εισαγωγή στη χρήση του module django.contrib.staticfiles
, δείτε στο άρθρο Διαχειρίζοντας τα static files (πχ εικόνες, JavaScript, CSS κλπ).
Εξυπηρετώντας τα static files στην παραγωγή¶
The basic outline of putting static files into production consists of two
steps: run the collectstatic
command when static files change, then
arrange for the collected static files directory (STATIC_ROOT
) to be
moved to the static file server and served. Depending the staticfiles
STORAGES
alias, files may need to be moved to a new location
manually or the post_process
method of
the Storage
class might take care of that.
As with all deployment tasks, the devil’s in the details. Every production setup will be a bit different, so you’ll need to adapt the basic outline to fit your needs. Below are a few common patterns that might help.
Εξυπηρετώντας το site και τα static files σας από τον ίδιο server¶
Αν θέλετε να εξυπηρετήσετε τα static files σας από τον ίδιο server ο οποίος εξυπηρετεί ήδη το site σας, η διαδικασία θα μοιάζει κάπως έτσι:
- Ανεβάστε τον κώδικα σας στον deployment server.
- Στον server, τρέξτε την εντολή
collectstatic
για να αντιγραφτούν όλα τα static files μέσα στο φάκελοSTATIC_ROOT
. - Παραμετροποιήστε τον Web server σας για να εξυπηρετεί τα αρχεία του φακέλου
STATIC_ROOT
στο URLSTATIC_URL
. Για παράδειγμα, δείτε πως μπορείτε να το κάνετε αυτό με τον Apache και το mod_wsgi.
You’ll probably want to automate this process, especially if you’ve got multiple web servers.
Εξυπηρετώντας τα static files από έναν dedicated server¶
Most larger Django sites use a separate web server – i.e., one that’s not also running Django – for serving static files. This server often runs a different type of web server – faster but less full-featured. Some common choices are:
Η παραμετροποίηση αυτών των servers είναι εκτός του πεδίου εφαρμογής του παρόντος άρθρου. Συμβουλευτείτε το εγχειρίδιο καθενός από τους παραπάνω servers για λεπτομέρειες και οδηγίες.
Εφόσον ο static file server δεν θα τρέχει το Django, θα χρειαστεί να αλλάξετε τη στρατηγική του deployment για να μοιάζει κάπως έτσι:
- Όταν τα static files αλλάζουν, τρέξτε την εντολή
collectstatic
τοπικά στον υπολογιστή σας. - Ανεβάστε τον τοπικό σας φάκελο
STATIC_ROOT
στον static file server μέσα στο φάκελο όπου εξυπηρετείται. Η εντολή rsync είναι μια κοινή λύση για αυτό το βήμα καθώς δεν αντιγράφει κάθε φορά όλο το φάκελο, παρά μόνο τις αλλαγές που έγιναν.
Εξυπηρετώντας τα static files από μια υπηρεσία cloud ή CDN¶
Another common tactic is to serve static files from a cloud storage provider like Amazon’s S3 and/or a CDN (content delivery network). This lets you ignore the problems of serving static files and can often make for faster-loading web pages (especially when using a CDN).
Όταν χρησιμοποιείτε αυτές τις υπηρεσίες, η βασική διαδικασία θα μοιάζει λίγο-πολύ με την παραπάνω, εκτός του ότι αντί να χρησιμοποιείτε την εντολή rsync
για να μεταφέρετε τα static files στον server, θα χρειαστεί να μεταφέρετε τα static files στον πάροχο αποθήκευσης αρχείων ή στο CDN.
There’s any number of ways you might do this, but if the provider has an API,
you can use a custom file storage backend
to integrate the CDN with your Django project. If you’ve written or are using a
3rd party custom storage backend, you can tell collectstatic
to use
it by setting staticfiles
in STORAGES
.
Για παράδειγμα, αν έχετε γράψει ένα σύστημα αποθήκευσης S3 στο αρχείο myproject.storage.S3Storage
, μπορείτε να το χρησιμοποιήσετε ως εξής:
STORAGES = {
# ...
"staticfiles": {"BACKEND": "myproject.storage.S3Storage"}
}
Once that’s done, all you have to do is run collectstatic
and your
static files would be pushed through your storage package up to S3. If you
later needed to switch to a different storage provider, you may only have to
change staticfiles
in the STORAGES
setting.
For details on how you’d write one of these backends, see How to write a custom storage class. There are 3rd party apps available that provide storage backends for many common file storage APIs. A good starting point is the overview at djangopackages.org.
The STORAGES
setting was added.
Μάθετε περισσότερα¶
Για πλήρεις λεπτομέρειες πάνω σε όλες τις ρυθμίσεις, εντολές, template tags και άλλα κομμάτια που περιέχονται στο django.contrib.staticfiles
, δείτε στο άρθρο αναφορά στα staticfiles.