Django is an excellent choice for developing basic to more advanced websites. The web framework works well in local and production environments. Just the same way the local environment server is not suitable for a production environment, there are things you need to configure before deploying your Django website.
Before deploying a Django web application, ensure that you have configured DEBUG, file handling, domain name, and styling settings to ensure optimal performance, security, efficiency, mobile-friendliness, and Search Engine Optimization.
How to deploy a Django website on various types of hosting
Before spilling all the checklists, I need you to know the different hosting you can use to deploy your Django web application. Otherwise, you can always skip ahead.
Generally, there are different types of hosting that you can use for deploying your Django applications.
These types of hosting are:
- Virtual private server (VPS) hosting
- Dedicated server hosting
- Cloud hosting
- Shared hosting
There are other types of hosting, but for this article, we will focus on VPS, dedicated, cloud, and shared hosting.
- Virtual Private Server (VPS) hosting
A VPS hosting is a form of cloud hosting where virtual server resources are available to an end-user privately.
However, offering private resources does not mean that other users aren’t using the same machine. VPS offers you dedicated private resources on a server with multiple users. A VPS option for deploying Django is more secure and stable than shared hosting.
A VPS option also provides you with a dedicated IP address of your own. Other users on the same machine do not share the same IP address. The advantage of having your IP address is that it reduces the risk of IP blacklisting. Besides, your email deliveries will be much faster and safer.
Here are the companies that I have trusted to provide VPS hosting for Django web applications:
- Digital Ocean – A better option for fast-performing VPS servers suitable for a Django website with high traffic.
- HostPinnacle – A good choice when you’re getting started and considering a low budget for the best server resources
- A2 Hosting
2. Dedicated server hosting
In a dedicated server, you buy exclusive resources of an entire server. Your Django application has all the server resources, meaning it does not have to share with other websites using the same web hosting service.
However, you should note that the dedicated server offers fixed RAM, CPU, AND STORAGE sizes. Therefore, if your web application receives millions of users simultaneously, there is a possibility of lagging if the resources cannot handle the traffic.
Having dedicated server hosting provides you with IP address security, faster email deliveries, security, enhanced performance, and flexibility.
Companies that provide dedicated server hosting for Django web applications include:
- Digital Ocean
3. Cloud hosting
Another expensive option for deploying your Django web applications is cloud hosting. Cloud hosting offers you more flexibility of resources compared to dedicated server hosting.
Cloud hosting offers scalable server resource allocation increasing the Django web application uptime, security, and scalability.
Therefore, your Django web application will perform optimally during low and high traffic spikes.
When you choose cloud hosting, you are in a position to manage the cost better because the bill accumulates based on the number of resources used.
At low to medium traffic, you will probably incur less cost. However, with high traffic, costs will surely skyrocket based on the resources used on the server.
Companies that offer cloud hosting for Django web applications:
- Digital Ocean – A cost-friendly service when you are considering unlimited bandwidth, dedicated IP address, Cloudflare CDN, and unlimited databases
- Google Cloud Platform
- Amazon Web Services (AWS)
- Azure (Microsoft)
Find more information on deploying Django applications on a Linux server such as the one offered in Amazon Web Services, click on the article link below:
4. Shared hosting
The last type of hosting that you can use for your Django application is Shared Hosting. Shared hosting is a cost-effective strategy when testing a new Django project or a simple website.
One of the advantages of deploying Django on Shared hosting is that it is highly affordable.
Check this company that provides support for deploying Django on Shared Hosting using cPanel:
And if you want to know how to deploy Django on Shared Hosting using cPanel, be sure to check the article below:
You can use those four types of hosting to deploy your Django web application.
Now, let’s dive right into how you will configure your Django application after deciding which hosting to use.
Django Website Deployment Checklist
Checklist Number 1: Change your admin URL endpoint and
The default URL for your web application is www.example.com/admin/
You may change your website admin URL to secure your website from brute-force attacks and save server resources, which your app would use for other purposes, such as serving website users.
In your urls.py project file, change the admin URL from admin/ to anything that you want to use for your website.
Initially, your main urls.py would look like this:
Change the admin URL to something like this
Checklist Number 2: Set DEBUG = False
You should set DEBUG to False on Django to suppress the verbose Django error messages that appear whenever there is an error when submitting data or accessing some parts of data.
It would be best if you did not display such errors to users. Instead, suppress Django errors with a customized 404 or 500 error page.
Checklist Number 3: Hide your SECRET_KEY, DEBUG, and ALLOWED_HOSTS values.
You may have noticed that I have not hard-coded essential information about my Django web application on the code. I have used environment variables.
Environment variables store Django configuration values without committing them to a remote location such as GitHub. You may commit sensitive information such as the SECRET_KEY on a public server, and hackers will use that information to gain access to your web application.
Besides, some configuration values may change over time, and it would take a ton of time if you have hard-coded the values on your code. A solution is to store them in one place, where you can change them once without changing all the instances in which the values occur.
One excellent Python package that I use for Django applications is Python Decouple. It is very easy to set up. Check Python Decouple documentation here.
Checklist Number 4: Protect your DATABASE connection parameters
You should store your database connection values as environment variables to avoid exposing them in your Django code.
Before deploying your Django application, ensure that your settings.py DATABASE parameters do not appear on your code like this:
Instead, use environment variables to hide database connection parameters like this:
If you are using Heroku to deploy your Django application, you should use config vars to store the values.
Open the Settings tab on Heroku, and click on the Reveal Config Vars button. Add your variables and their values as you would in a .env file.
Checklist Number 5: Define where your static and media files will reside: cloud service or CDN
To serve your files from a dedicated server, you will need to use the webserver to serve your Django static and media files. Suppose you’re using NGINX as your web server; use it to serve your files.
It is faster but does not offer much of the functionality of serving your files better.
Before uploading your code to the hosting service directory, run the collectstatic command first on a local environment. Then copy all the files and folders to your dedicated server, including the static folder.
Another approach suitable for handling static files is using a CDN service or cloud storage provider such as Amazon S3. CDN caches your static and media files across different servers worldwide to increase your website’s load speed.
Checklist Number 6: Use a secure connection for your website (HTTPS)
Setting up SSL is essential, especially when you are handling users’ data on your website. On top of that, most people wouldn’t trust a website with no SSL connection.
You should always use HTTPS for your Django application.
After you have deployed your Django application on a Domain name configured to use SSL, add this line to your settings.py file always to redirect HTTP requests to an HTTPS connection.
SECURE_SSL_REDIRECT = True
Checklist Number 7: Set favicon, spell-check, and guarantee your website is mobile-friendly
A trustable and professional-looking website should not miss a favicon, have grammatical errors, and do not respond well to smaller devices. Set a favicon for your Django application before deploying it.
After setting your static files settings, add the following lines to your main urs.py file to set the favicon for your Django web app:
from django.urls import path from django.contrib.staticfiles.storage import staticfiles_storage from django.views.generic.base import RedirectView urlpatterns = [ path('favicon.ico', RedirectView.as_view(url=staticfiles_storage.url('img/favicon.ico'))) ]
The code above instructs Django to use the favicon stored inside img/ folder of your static folder. Every time a user accesses your website, the little icon should be displayed on their browser’s address bar.
To design your frontend pages, use popular CSS frameworks such as Bootstrap to make your website look better and mobile-friendly. I have written an article on how to use Django with Bootstrap.
Lastly, ensure you have double-checked your website’s grammar and content layout.
Checklist Number 8: Add your domain name to ALLOWED_HOSTS
Adding a domain name to ALLOWED_HOSTS is something that I have always forgotten when deploying a new Django web application. Before you panic about why you cannot access your website using the domain name, you used to deploy Django, check that you have used it on your settings.
Here’s how you add a domain name to your Django application. Open the main settings.py file and under the ALLOWED_HOSTS variable, add the domain name of your website.
Checklist Number 9: Ensure all FileField and ImageField have the upload_to argument.
You should organize app image uploads according to the app. Each app should have a folder to store the media files, such as images. Add the upload_to argument to ImageField and FileField attributes when defining your models.
Checklist Number 10: Check that you are using the latest server OS and Python version.
After purchasing your hosting service, access your server using SSH and update Ubuntu to the latest version. To do that, connect to your server instance using SSH and type the following commands:
sudo apt update && sudo apt full-upgrade -y
Hit Enter, and wait for your server to update to the latest version if you are using Linux as your server’s operating system.
After updating the system, update Python and NGINX or the web server you’re using to deploy your Django application. Type the following to install and update Python and NGINX to the latest version.
sudo apt install python3 nginx
There are other essential website design and development checklists that you should meet before deploying your website. Here is a list of things that you need to achieve before deployment:
- Check Website Design: Is the website responsive, mobile-friendly, and attractive
- Setup and configure Google Analytics
- Make a robots.txt using Django. Here is a link to an article with more details on setting up a robots.txt file on Django.
- Design a sitemap.xml using Django. Here is a link to an article with more details on setting up a sitemap on Django.
- Check your pages or posts have descriptive, unique, and SEO-friendly titles and URLs
- After deployment, check your site speed on GTmetrix and correct any performance issues.
This article will provide more information on configuring your website before deployment.
Is Django hard to deploy?
Django is one of the most straightforward web frameworks to deploy on shared, VPS, and cloud hosting servers. On VPS and cloud hosting servers, you need to install Linux operating system, Python, and NGINX, and you can deploy your web application.
However, ensure you have completed every checklist I mentioned above to produce a fast, secure, and reliable Django web application.
The most trusted way to deploy Django (What is the best way to deploy Django app?)
There is no best way to deploy your Django application because you can mess some things up when deploying Django.
Besides, the right way to deploy a Django web application depends on cost, the number of users expected to access your web application, and the size of your web application.
Based on cost, choose shared and VPS hosting plans to deploy your Django applications when considering a low budget.
Here is a good company that hosts Django web applications on their VPS and Shared hosting, usually at affordable plans.
However, if you have budgeted for more memory, storage, and CPU resources, choose a dedicated or cloud hosting that will provide you the flexibility to increase resources as your website grows.
In terms of web traffic expected on your website, you may use dedicated, VPS, or Shared hosting plans when you expect a low to medium number of users accessing your website daily.
If you’re expecting millions of users per month on your web application, choose a cloud hosting plan that will offer you the flexibility to scale the computer resources depending on traffic. When there is a traffic spike on your website, the resources should scale according to the number of users on the system.
If your Django web application is small, it is more suitable and economical to deploy it on Shared and VPS hosting. On the other hand, if you have a huge Django web application, use Dedicated and Cloud hosting servers to deploy your application.
That’s it for this article, and I hope to see you next time. Before you go, check these web hosting services that you can use to deploy your Django application on Shared, VPS, and Dedicated servers.