Open benclmnt opened 1 year ago
systemctl logs can become large over time.
journalctl --disk-usage
. sudo journalctl --vacuum-size=500M
or sudo journalctl --vacuum-time=7d
References:
If you are using Nginx, the following should work
server {
server_name YOUR_HOST;
location ^~/static/ {
alias /HOME_FOLDER/SOME_PREFIX/static/;
}
location / {
include proxy_params;
proxy_pass http://127.0.0.1:9001;
}
}
server {
listen 80;
server_name YOUR_HOST;
}
Steps for deploying Django to a VPS, such as digital ocean. These steps assume you have a caddy service running, and will deploy django using gunicorn running on
localhost:8000
and served by caddy forhttps://mywebsite.com
.Caddyfile
Default location:
/etc/caddy/Caddyfile
After editing, don't forget to reload caddy
sudo systemctl reload caddy
Django checklists
Run through
python3 manage.py check --deploy
. Settings to changeDebug = False
mywebsite.com
toALLOWED_HOSTS
andhttps://mywebsite.com
toCSRF_TRUSTED_ORIGINS
SECRET_KEY = os.environ.get("SECRET_KEY")
CSRF_COOKIE_SECURE = True
only if you are running https (which you should)SESSION_COOKIE_SECURE = True
only if you are running https (which you should)SECURE_SSL_REDIRECT = True
+SECURE_PROXY_SSL_HEADER = ("HTTP_X_FORWARDED_PROTO", "https")
. This is not needed for deployment behind a Caddy server you control. We let Caddy terminate TLS for us.cp -r /path/to/pythonlib/site-packages/django/contrib/admin/static /path/to/myproject/static
.python3 manage.py collectstatic
and settingSTATIC_ROOT
but it needsSTATIC_ROOT
to be an empty dir.pip show django | grep Location
For ease of development, you might want to add
Systemd
First create a systemd file for your Django project.
Next, generate and copy a secret key. To generate a secret key, you can run
head /dev/urandom | openssl sha256
Then run
sudo EDITOR=vim systemctl edit <your project name>
which will open an override file. Here you can insert your environment variables, e.g. the secret key that you generated previously.Optionally, run
sudo systemctl enable <your project name>
to run it on the next boot.Running
pip install -r requirements.txt
python3 manage.py migrate
python3 manage.py createsuperuser
python3 manage.py collectstatic && mv <STATIC_ROOT>/* ./static
. This will copy static files (e.g. for admin page) toSTATIC_ROOT
, which is a temporary folder (needs to be empty) + move it to your STATICDIRsudo systemctl start <your project name>
.