{"id":120268,"date":"2024-12-18T01:51:36","date_gmt":"2024-12-18T01:51:36","guid":{"rendered":"\/tutorials\/?p=120268"},"modified":"2024-12-20T12:26:58","modified_gmt":"2024-12-20T12:26:58","slug":"how-to-deploy-django-app","status":"publish","type":"post","link":"\/in\/tutorials\/how-to-deploy-django-app","title":{"rendered":"How to deploy a Django application on a VPS"},"content":{"rendered":"<p>Deploying a Django app is the final step in bringing your application from development to production, making it accessible to users worldwide. This process involves following best practices to ensure your app is secure, scalable, and performs optimally in a live environment.<\/p><p>In this article, we&rsquo;ll guide you through the essential steps every Django developer should know to deploy their app to a VPS successfully.<\/p><p>\n\n\n\n\n\n\n<\/p><h2 class=\"wp-block-heading\" id=\"h-prerequisites\">Prerequisites<\/h2><p>Before deploying your Django application, ensure you have a VPS with sufficient resources based on your projected load and traffic. Since determining the exact requirements is tricky, we recommend starting with a system with a lower hardware allocation and upgrading as needed.<\/p><p>Starting at <strong>\u20b9599.00\/month<\/strong>, <a href=\"\/in\/vps\/django-hosting\">Hostinger Django hosting&rsquo;s<\/a> KVM 1 plan can be an excellent option. Its<strong> 1 vCPU<\/strong> <strong>core<\/strong>, <strong>4 GB RAM<\/strong>, and<strong> 50 GB NVMe SSD storage <\/strong>should be enough for a small-scale website or application.<\/p><p>As your project grows, you can upgrade to up to KVM 8, which has <strong>8 vCPU cores<\/strong>, <strong>32 GB RAM<\/strong>, and 4<strong>00 GB NVMe disk space<\/strong>. Hostinger VPS upgrade process only takes a few minutes, and our system will migrate all your data automatically to simplify the process.<\/p><p>In addition to reliable hardware, Hostinger VPS has various features that help streamline server setup and management. For example, you install Django on your server without commands by simply choosing the OS template.<\/p><p>This helps save time since before deploying your project, you must <a href=\"\/in\/tutorials\/how-to-install-django\">install Django<\/a>, Python, and other essential software on your server. Check out our tutorial to learn how to install Django in 4 steps.<\/p><p>You can also access your server&rsquo;s command-line interface via a web browser using <strong>Browser terminal<\/strong>, which will log you in as root automatically. Moreover, <strong>Kodee AI assistant <\/strong>lets you easily write commands or code using simple prompts.<\/p><?xml encoding=\"utf-8\" ?><figure class=\"wp-block-image size-large\"><a href=\"\/in\/vps-hosting\" target=\"_blank\" rel=\"noreferrer noopener\"><img decoding=\"async\" width=\"1024\" height=\"300\" src=\"https:\/\/www.hostinger.com\/tutorials\/wp-content\/uploads\/sites\/2\/2023\/02\/VPS-hosting-banner-1024x300.png\" alt=\"\" class=\"wp-image-77934\" srcset=\"https:\/\/www.hostinger.com\/in\/tutorials\/wp-content\/uploads\/sites\/52\/2023\/02\/VPS-hosting-banner.png 1024w, https:\/\/www.hostinger.com\/in\/tutorials\/wp-content\/uploads\/sites\/52\/2023\/02\/VPS-hosting-banner-300x88.png 300w, https:\/\/www.hostinger.com\/in\/tutorials\/wp-content\/uploads\/sites\/52\/2023\/02\/VPS-hosting-banner-150x44.png 150w, https:\/\/www.hostinger.com\/in\/tutorials\/wp-content\/uploads\/sites\/52\/2023\/02\/VPS-hosting-banner-768x225.png 768w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure><h2 class=\"wp-block-heading\" id=\"h-how-to-deploy-a-django-application\">How to deploy a Django application<\/h2><p>To get your Django application live and accessible to users, we&rsquo;ll guide you through the steps to set up your server and configure the necessary services.<\/p><h3 class=\"wp-block-heading\" id=\"h-1-preparing-the-django-project-for-deployment\">1. Preparing the Django project for deployment<\/h3><p>To begin, prepare your Django application so it will be ready for deployment. Start by turning off the debug mode, which is typically enabled during development for troubleshooting and debugging.<\/p><p>While useful during production, enabling this setting in the live environment can be risky since it can expose important details about your application to the public. To disable it, open <strong>settings.py <\/strong>and set the <strong>DEBUG<\/strong> parameter to <strong>False<\/strong>:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">DEBUG = False<\/pre><p>Next, sanitize your code by removing sensitive data from your application, such as API keys and other credentials. This prevents important information from becoming public after you upload the project to a repository.<\/p><p>Now, upload all your project files to a Git repository. For this tutorial, we will use GitHub, but you can choose an alternative platform like GitLab.<\/p><p>If you haven&rsquo;t created an account and a repository, check out our <a href=\"\/in\/tutorials\/what-is-github\">GitHub tutorial<\/a> to learn how to do so. Once set, follow these steps to upload the project files:<\/p><ol class=\"wp-block-list\">\n<li>Navigate to the main page of your repository.<\/li>\n\n\n\n<li>Click the<strong> Add file<\/strong> button &rarr; <strong>Upload files<\/strong>. <\/li>\n<\/ol><div class=\"wp-block-image\">\n<figure data-wp-context='{\"imageId\":\"69ebdc43dfd52\"}' data-wp-interactive=\"core\/image\" class=\"aligncenter size-large wp-lightbox-container\"><img decoding=\"async\" width=\"1024\" height=\"260\" data-wp-class--hide=\"state.isContentHidden\" data-wp-class--show=\"state.isContentVisible\" data-wp-init=\"callbacks.setButtonStyles\" data-wp-on-async--click=\"actions.showLightbox\" data-wp-on-async--load=\"callbacks.setButtonStyles\" data-wp-on-async-window--resize=\"callbacks.setButtonStyles\" src=\"https:\/\/www.hostinger.com\/tutorials\/wp-content\/uploads\/sites\/2\/2024\/12\/the-upload-file-menu-on-github-1024x260.jpg\" alt=\"The upload file menu on GitHub\" class=\"wp-image-120270\" srcset=\"https:\/\/www.hostinger.com\/in\/tutorials\/wp-content\/uploads\/sites\/52\/2024\/12\/the-upload-file-menu-on-github-1024x260.jpg 1024w, https:\/\/www.hostinger.com\/in\/tutorials\/wp-content\/uploads\/sites\/52\/2024\/12\/the-upload-file-menu-on-github-300x76.jpg 300w, https:\/\/www.hostinger.com\/in\/tutorials\/wp-content\/uploads\/sites\/52\/2024\/12\/the-upload-file-menu-on-github-150x38.jpg 150w, https:\/\/www.hostinger.com\/in\/tutorials\/wp-content\/uploads\/sites\/52\/2024\/12\/the-upload-file-menu-on-github-768x195.jpg 768w, https:\/\/www.hostinger.com\/in\/tutorials\/wp-content\/uploads\/sites\/52\/2024\/12\/the-upload-file-menu-on-github.jpg 1460w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><button class=\"lightbox-trigger\" type=\"button\" aria-haspopup=\"dialog\" aria-label=\"Enlarge\" data-wp-init=\"callbacks.initTriggerButton\" data-wp-on-async--click=\"actions.showLightbox\" data-wp-style--right=\"state.imageButtonRight\" data-wp-style--top=\"state.imageButtonTop\">\n\t\t\t<svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"12\" height=\"12\" fill=\"none\" viewbox=\"0 0 12 12\">\n\t\t\t\t<path fill=\"#fff\" d=\"M2 0a2 2 0 0 0-2 2v2h1.5V2a.5.5 0 0 1 .5-.5h2V0H2Zm2 10.5H2a.5.5 0 0 1-.5-.5V8H0v2a2 2 0 0 0 2 2h2v-1.5ZM8 12v-1.5h2a.5.5 0 0 0 .5-.5V8H12v2a2 2 0 0 1-2 2H8Zm2-12a2 2 0 0 1 2 2v2h-1.5V2a.5.5 0 0 0-.5-.5H8V0h2Z\"><\/path>\n\t\t\t<\/svg>\n\t\t<\/button><\/figure><\/div><ol start=\"3\" class=\"wp-block-list\">\n<li>Select all the files and folders you want to upload. <\/li>\n\n\n\n<li>Add the commit message to inform other repo users about the new upload. <\/li>\n\n\n\n<li>Select the branch to which you want to upload the files. Since we are starting anew, choose the <strong>Main branch<\/strong>.<\/li>\n\n\n\n<li>Click<strong> Propose changes<\/strong>.<\/li>\n<\/ol><div class=\"wp-block-image\">\n<figure data-wp-context='{\"imageId\":\"69ebdc43e1c72\"}' data-wp-interactive=\"core\/image\" class=\"aligncenter size-large wp-lightbox-container\"><img decoding=\"async\" width=\"1024\" height=\"502\" data-wp-class--hide=\"state.isContentHidden\" data-wp-class--show=\"state.isContentVisible\" data-wp-init=\"callbacks.setButtonStyles\" data-wp-on-async--click=\"actions.showLightbox\" data-wp-on-async--load=\"callbacks.setButtonStyles\" data-wp-on-async-window--resize=\"callbacks.setButtonStyles\" src=\"https:\/\/www.hostinger.com\/tutorials\/wp-content\/uploads\/sites\/2\/2024\/12\/github-commit-page-1024x502.jpg\" alt=\"GitHub's commit page\" class=\"wp-image-120272\" srcset=\"https:\/\/www.hostinger.com\/in\/tutorials\/wp-content\/uploads\/sites\/52\/2024\/12\/github-commit-page-1024x502.jpg 1024w, https:\/\/www.hostinger.com\/in\/tutorials\/wp-content\/uploads\/sites\/52\/2024\/12\/github-commit-page-300x147.jpg 300w, https:\/\/www.hostinger.com\/in\/tutorials\/wp-content\/uploads\/sites\/52\/2024\/12\/github-commit-page-150x74.jpg 150w, https:\/\/www.hostinger.com\/in\/tutorials\/wp-content\/uploads\/sites\/52\/2024\/12\/github-commit-page-768x377.jpg 768w, https:\/\/www.hostinger.com\/in\/tutorials\/wp-content\/uploads\/sites\/52\/2024\/12\/github-commit-page.jpg 1460w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><button class=\"lightbox-trigger\" type=\"button\" aria-haspopup=\"dialog\" aria-label=\"Enlarge\" data-wp-init=\"callbacks.initTriggerButton\" data-wp-on-async--click=\"actions.showLightbox\" data-wp-style--right=\"state.imageButtonRight\" data-wp-style--top=\"state.imageButtonTop\">\n\t\t\t<svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"12\" height=\"12\" fill=\"none\" viewbox=\"0 0 12 12\">\n\t\t\t\t<path fill=\"#fff\" d=\"M2 0a2 2 0 0 0-2 2v2h1.5V2a.5.5 0 0 1 .5-.5h2V0H2Zm2 10.5H2a.5.5 0 0 1-.5-.5V8H0v2a2 2 0 0 0 2 2h2v-1.5ZM8 12v-1.5h2a.5.5 0 0 0 .5-.5V8H12v2a2 2 0 0 1-2 2H8Zm2-12a2 2 0 0 1 2 2v2h-1.5V2a.5.5 0 0 0-.5-.5H8V0h2Z\"><\/path>\n\t\t\t<\/svg>\n\t\t<\/button><\/figure><\/div><p>Alternatively, you can also upload new files to your GitHub repository by running commands in your integrated development environment (IDE) terminal. Check out <a href=\"https:\/\/docs.github.com\/en\/repositories\/working-with-files\/managing-files\/adding-a-file-to-a-repository\" target=\"_blank\" rel=\"noreferrer noopener\">GitHub&rsquo;s documentation about adding files<\/a> to learn more about it.<\/p><h3 class=\"wp-block-heading\" id=\"h-2-setting-up-your-server\">2. Setting up your server<\/h3><p>Next, you need to prepare your VPS for deployment and download your code to it:<\/p><ol class=\"wp-block-list\">\n<li>Connect to your VPS via SSH. If you use <strong>Hostinger&rsquo;s Browser terminal<\/strong>, skip this step since you will log in automatically:<\/li>\n<\/ol><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">ssh root@your_vps_ip<\/pre><ol start=\"2\" class=\"wp-block-list\">\n<li>Enter your root account&rsquo;s password. <\/li>\n\n\n\n<li>Install the <strong>Nginx <\/strong>web server and <strong>Gunicorn <\/strong>application server:<\/li>\n<\/ol><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">sudo apt install nginx<\/pre><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">sudo pip3 install gunicorn<\/pre><ol start=\"4\" class=\"wp-block-list\">\n<li>Retrieve code from GitHub. Remember to replace the URL with you actual repository address:<\/li>\n<\/ol><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">git clone https:\/\/github.com\/your-repository\/project-name.git<\/pre><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">cd project-name<\/pre><ol start=\"5\" class=\"wp-block-list\">\n<li>Start a <a href=\"\/in\/tutorials\/how-to-create-a-python-virtual-environment\">Python virtual environment<\/a> and install dependencies:<\/li>\n<\/ol><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">python3 -m venv env_name<\/pre><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">source env_name\/bin\/activate<\/pre><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">pip3 install -r requirements.txt<\/pre><ol start=\"6\" class=\"wp-block-list\">\n<li>Run migrations and collect <a href=\"\/in\/tutorials\/django-static-files\">static files<\/a>:<\/li>\n<\/ol><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">python manage.py migrate<\/pre><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">python manage.py collectstatic<\/pre><h3 class=\"wp-block-heading\" id=\"h-3-setting-up-web-servers\">3. Setting up web servers<\/h3><p>After configuring your VPS, follow these steps to set up the web and application server:<\/p><ol class=\"wp-block-list\">\n<li>Create a service file for the Gunicorn application server:<\/li>\n<\/ol><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">sudo nano \/etc\/systemd\/system\/gunicorn.service<\/pre><ol start=\"2\" class=\"wp-block-list\">\n<li>Add the following configuration. Make sure to enter your actual username and project directory path instead of <strong>your-username<\/strong> and <strong>\/your\/project\/directory<\/strong>.<\/li>\n<\/ol><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">[Unit]\nDescription=gunicorn daemon\nAfter=network.target\n\n[Service]\nUser=your-username\nGroup=www-data\nWorkingDirectory=\/your\/project\/directory\nExecStart=\/your\/project\/directory\/env_name\/bin\/gunicorn --access-logfile - --workers 3 --bind unix:\/your\/project\/directory.sock myproject.wsgi:application\n\n[Install]\nWantedBy=multi-user.target<\/pre><ol start=\"3\" class=\"wp-block-list\">\n<li>Start and enable Gunicorn:<\/li>\n<\/ol><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">sudo systemctl start gunicorn<\/pre><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">sudo systemctl enable gunicorn<\/pre><ol start=\"4\" class=\"wp-block-list\">\n<li>Create a configuration file for your NGINX web server:<\/li>\n<\/ol><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">sudo nano \/etc\/nginx\/sites-available\/myproject<\/pre><ol start=\"5\" class=\"wp-block-list\">\n<li>Add the following configuration. Remember to replace <strong>\/your\/project\/directory <\/strong>with the actual path:<\/li>\n<\/ol><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">server {\n    listen 80;\n    server_name yourdomain.com www.yourdomain.com;\n\n    location \/ {\n        proxy_pass http:\/\/unix:\/your\/project\/directory\/project-name.sock;\n        proxy_set_header Host $host;\n        proxy_set_header X-Real-IP $remote_addr;\n        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;\n        proxy_set_header X-Forwarded-Proto $scheme;\n    }\n\n    location \/static\/ {\n        alias \/your\/project\/directory\/static\/;\n    }\n\n    location \/media\/ {\n        alias \/your\/project\/directory\/media\/;\n    }\n}<\/pre><h3 class=\"wp-block-heading\" id=\"h-4-implementing-environment-variables\">4. Implementing environment variables<\/h3><p>Environment variables enable you to call data in your application&rsquo;s code without hard-coding them. In addition to simplifying modification, they allow you to store sensitive details like the API secret key in another location to improve security.<\/p><p>Since we created an environment variable called <strong>env_name<\/strong> in the previous step, we can set up the variables directly. If you haven&rsquo;t set it up, run the following command:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">python3 -m env env_name<\/pre><p>Now, install <strong>Python dotenv<\/strong> so we can load environment variables into the application more easily:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">pip install python-dotenv<\/pre><p>Create a file called<strong> .env <\/strong>to store your project&rsquo;s environment variables using this command:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">sudo nano .env<\/pre><p>Enter your environment variables in a key-value pair, which depends on the sensitive data you remove when sanitizing the code. For example, we&rsquo;ll set two for a secret key and database URL:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">SECRET_KEY=your_secret_key<\/pre><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">DATABASE_URL=postgres:\/\/user:password@localhost:5432\/dbname<\/pre><p>\n\n\n<div><p class=\"important\"><strong>Important!<\/strong> Since your <strong>.env<\/strong> file contains sensitive information, never commit it to the Git repository. Put it in <strong>.gitignore<\/strong> so Git will automatically exclude it.<\/p><\/div>\n\n\n\n<\/p><p>Now, insert the variables into your project&rsquo;s <strong>settings.py<\/strong> file. Your code might look like the following:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import os\nfrom dotenv import load_dotenv\n\n# Load environment variables from .env file\nload_dotenv()\n\n# Access environment variables \nSECRET_KEY = os.getenv('SECRET_KEY', 'default_secret_key') \nDATABASE_URL = os.getenv('DATABASE_URL')<\/pre><h3 class=\"wp-block-heading\" id=\"h-5-securing-your-django-application\">5. Securing your Django application<\/h3><p>After deploying your application, let&rsquo;s safeguard it from cyber criminals. There are various security practices, but the most basic one is issuing an SSL certificate and enabling HTTP. To do so, run the following commands:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">sudo apt install certbot python3-certbot-nginx<\/pre><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">sudo certbot --nginx -d yourdomain.com -d www.yourdomain.com<\/pre><h3 class=\"wp-block-heading\" id=\"h-6-monitoring-and-troubleshooting-post-deployment\">6. Monitoring and troubleshooting post-deployment<\/h3><p>After the deployment, it is crucial to monitor and troubleshoot your Django application to ensure it runs properly. Here are several important tasks to consider.<\/p><p><strong>Enable logging<\/strong><\/p><p>Logs give you details about all events that happened in your hosting environment, including errors, suspicious behaviors, or performance issues.<\/p><p>While you can use third-party tools, Django has a built-in framework that lets you easily enable logging. To use it, open your project&rsquo;s <strong>settings.py<\/strong> file and add your configuration to the <strong>LOGGING<\/strong> dictionary.<\/p><p>You can change various settings, including where you want to output the log message. Read <a href=\"https:\/\/docs.djangoproject.com\/en\/5.1\/topics\/logging\/\" target=\"_blank\" rel=\"noreferrer noopener\">Django&rsquo;s logging documentation<\/a> to learn more about the configuration. <\/p><p><strong>Track errors and handle exception<\/strong><\/p><p>Automated error tracking enables you to respond quickly to exceptions in your application, preventing them from worsening and affecting more people. There are different ways to enable it, but a common one in Django is using Sentry.<\/p><p>Install it by running the following command:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">pip install sentry-sdk<\/pre><p>Then, add its configuration into your settings.py file. Your code might look as follows:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import sentry_sdk\nfrom sentry_sdk.integrations.django import DjangoIntegration\n\nsentry_sdk.init(\n    dsn=\"https:\/\/your_sentry_dsn_here\",\n    integrations=[DjangoIntegration()],\n)<\/pre><p><strong>Monitor performance and uptime<\/strong><\/p><p>Actively monitor your server to identify potential performance issues or downtime. Although VPS hosting providers like Hostinger provide a <a href=\"https:\/\/support.hostinger.com\/en\/articles\/4725768-how-to-check-vps-resources-usage\" target=\"_blank\" rel=\"noopener\">built-in tool to track system usage<\/a>, integrating a third-party tool makes the report more comprehensive.<\/p><p>There are various solutions for monitoring your server, but some of the most popular ones include <strong>New Relic<\/strong> and <strong>Datadog<\/strong>. Since the steps to set up these tools vary, check their documentation.<\/p><p>Importantly, set up automatic alerting in these tools so you get notifications when your server slows down or experiences downtime.<\/p><h2 class=\"wp-block-heading\" id=\"h-conclusion\">Conclusion<\/h2><p>Deploying your Django project from your local development environment to your server is a crucial step in making your application accessible online. Before doing so, ensure the host has ample resources and has the framework installed.<\/p><p>Then, follow these steps to Deploy your Django application to your VPS:<\/p><ol class=\"wp-block-list\">\n<li>Prepare your Django application by changing sensitive settings and uploading it to your Git repository.<\/li>\n\n\n\n<li>Prepare your VPS by downloading the necessary web servers and cloning your project repository.<\/li>\n\n\n\n<li>Configure the NGINX and Gunicorn web servers so they can accept user requests.<\/li>\n\n\n\n<li>Set up environment variables to separate sensitive information from your application&rsquo;s codebase.<\/li>\n\n\n\n<li>Issue an SSL certificate and enable HTTPS to secure your Django application.<\/li>\n\n\n\n<li>Monitor and troubleshoot your Django application using tools like <strong>Sentry<\/strong> and <strong>New Relic<\/strong>. <\/li>\n<\/ol><p>We hope this tutorial helped you deploy your Django application. If you need assistance, ask our Kodee AI assistant for a quick solution or leave us a comment below. <\/p><h2 class=\"wp-block-heading\" id=\"h-how-to-deploy-django-app-faq\">How to deploy Django app FAQ<\/h2><div class=\"schema-faq wp-block-yoast-faq-block\"><div class=\"schema-faq-section\" id=\"faq-question-1734485831996\"><h3 class=\"schema-faq-question\">What is the best way to manage dependencies in deployment?<\/h3> <p class=\"schema-faq-answer\">The best way to manage dependencies in Django deployment is by using <strong>pip<\/strong> to install packages listed in a<strong> requirements.txt<\/strong> file. Generate it using <strong>pip freeze &gt; requirements.txt<\/strong>, which records the exact versions of each dependency, ensuring consistency across environments. Use virtual environments to isolate dependencies per project.<\/p> <\/div> <div class=\"schema-faq-section\" id=\"faq-question-1734485833270\"><h3 class=\"schema-faq-question\">Can I use Docker to deploy my Django app?<\/h3> <p class=\"schema-faq-answer\">Yes, Docker is ideal for deploying Django apps by packaging the application and its dependencies in a consistent environment. Create a <strong>Dockerfile<\/strong> to define the app&rsquo;s environment and a <strong>docker-compose.yml<\/strong> file to manage multi-container setups, such as the app server and database. This approach simplifies scaling and portability.<\/p> <\/div> <div class=\"schema-faq-section\" id=\"faq-question-1734485834514\"><h3 class=\"schema-faq-question\">What monitoring tools can I use for my deployed Django app?<\/h3> <p class=\"schema-faq-answer\">For monitoring a deployed Django app, consider tools like <strong>Prometheus<\/strong> and <strong>Grafana<\/strong> for real-time metrics, <strong>Sentry<\/strong> for error tracking, and <strong>New Relic<\/strong> or <strong>Datadog<\/strong> for full application performance monitoring. These tools help track performance, log errors, and visualize resource usage, improving app stability and user experience.<\/p> <\/div> <\/div>\n","protected":false},"excerpt":{"rendered":"<p>Deploying a Django app is the final step in bringing your application from development to production, making it accessible to users worldwide. This process involves following best practices to ensure your app is secure, scalable, and performs optimally in a live environment. In this article, we&rsquo;ll guide you through the essential steps every Django developer [&#8230;]<\/p>\n<p><a class=\"btn btn-secondary understrap-read-more-link\" href=\"\/in\/tutorials\/how-to-deploy-django-app\">Read More&#8230;<\/a><\/p>\n","protected":false},"author":185,"featured_media":120144,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"rank_math_title":"","rank_math_description":"","rank_math_focus_keyword":"","footnotes":""},"categories":[],"tags":[],"class_list":["post-120268","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry"],"hreflangs":[{"locale":"en-US","link":"https:\/\/www.hostinger.com\/tutorials\/how-to-deploy-django-app","default":1},{"locale":"en-GB","link":"https:\/\/www.hostinger.com\/uk\/tutorials\/how-to-deploy-django-app","default":0},{"locale":"en-MY","link":"https:\/\/www.hostinger.com\/my\/tutorials\/how-to-deploy-django-app","default":0},{"locale":"en-PH","link":"https:\/\/www.hostinger.com\/ph\/tutorials\/how-to-deploy-django-app","default":0},{"locale":"en-IN","link":"https:\/\/www.hostinger.com\/in\/tutorials\/how-to-deploy-django-app","default":0}],"_links":{"self":[{"href":"https:\/\/www.hostinger.com\/in\/tutorials\/wp-json\/wp\/v2\/posts\/120268","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.hostinger.com\/in\/tutorials\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.hostinger.com\/in\/tutorials\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.hostinger.com\/in\/tutorials\/wp-json\/wp\/v2\/users\/185"}],"replies":[{"embeddable":true,"href":"https:\/\/www.hostinger.com\/in\/tutorials\/wp-json\/wp\/v2\/comments?post=120268"}],"version-history":[{"count":5,"href":"https:\/\/www.hostinger.com\/in\/tutorials\/wp-json\/wp\/v2\/posts\/120268\/revisions"}],"predecessor-version":[{"id":120618,"href":"https:\/\/www.hostinger.com\/in\/tutorials\/wp-json\/wp\/v2\/posts\/120268\/revisions\/120618"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.hostinger.com\/in\/tutorials\/wp-json\/wp\/v2\/media\/120144"}],"wp:attachment":[{"href":"https:\/\/www.hostinger.com\/in\/tutorials\/wp-json\/wp\/v2\/media?parent=120268"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.hostinger.com\/in\/tutorials\/wp-json\/wp\/v2\/categories?post=120268"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.hostinger.com\/in\/tutorials\/wp-json\/wp\/v2\/tags?post=120268"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}