What is Django?
Python is a popular programming language. Django is a framework for it. A framework is a ready-made base for building websites. To deploy means to move your app to a server and start it for the public. Django's built-in test server is only for practice. For a real site you need a proper setup: Gunicorn and Nginx. Gunicorn runs your Python code. Nginx faces the visitors and passes requests to Gunicorn.
Some shared plans offer a Python app tool in the control panel. Ask Hostvento support what your plan has.
Warning: Back up your project and database before you deploy.
Steps
- Log in to your server with SSH.
- Install Python tools. On Ubuntu:
sudo apt update sudo apt install python3-venv python3-pip nginx - Copy your project to the server, for example into
/var/www/myproject. Use Git or SFTP. - Create a virtual environment. This is a private box for your project's packages.
cd /var/www/myproject python3 -m venv venv source venv/bin/activate - Install the packages.
pip install -r requirements.txt pip install gunicorn - Edit
settings.pyfor production.- Set
DEBUG = False. - Set
ALLOWED_HOSTS = ['yourdomain.com']. - Set
STATIC_ROOTto a folder for static files. - Keep
SECRET_KEYout of public code.
- Set
- Prepare the database and static files.
python manage.py migrate python manage.py collectstatic - Test Gunicorn.
Replacegunicorn --bind 127.0.0.1:8000 myproject.wsgimyprojectwith the folder that holdswsgi.py. Stop it withCtrl+C. - Run Gunicorn as a service, so it starts by itself. Create
/etc/systemd/system/gunicorn.servicewith aExecStartline that runs the command above, then runsudo systemctl enable --now gunicorn. - Add an Nginx site that passes requests to
127.0.0.1:8000withproxy_pass, and serves your static folder directly. - Test and reload Nginx.
sudo nginx -t sudo systemctl reload nginx - Open your domain in a browser.
Final checks
- Add HTTPS with a free certificate, such as Let's Encrypt.
- Check logs with
journalctl -u gunicornif the site fails.
Quick recap
- Use a virtual environment, Gunicorn and Nginx.
- Set
DEBUG = FalseandALLOWED_HOSTS. - Run
migrateandcollectstatic. - Run Gunicorn as a service and test Nginx before reloading.