Hosting Your Django Website on a CentOS VPS
Last Updated :
09 Nov, 2022
Hosting any website/web application on a live server can sometimes become difficult if proper steps are not taken while deploying it. There are mainly 3 different types of hosting:
- Shared Hosting - Usually used for small (single page) websites with limited traffic.
- VPS Hosting - VPS ( Virtual Private Server ) hosting is used for websites with good amount of content and medium to high traffic.
- Dedicated Hosting - This hosting is generally used for large business websites with a lot of content and high traffic.
In this article, we will be discussing mainly about VPS hosting.
VPS
VPS or Virtual Private Server is a single physical machine used by many of its users. A physical server somewhere in the world equipped with virtualization serves its users as a separate dedicated system for them to use. It divides the available resources among the different users and for each of its users, it appears as if they are using a separate dedicated machine.
Points to be kept in mind before hosting:
- Make sure the server OS is updated and all the other software which it uses are up to date.
- Check with the version of the local environment packages and libraries to be the same as on the VPS also.
- If the versions are different, make sure you uninstall the existing versions and install the required same versions on the server.
- Choose a server which will run on your VPS's OS to serve your website files. ( In this article we will use Nginx).
- Always check that whether your hosting provider has a good support for its customers or not.
Root Access & SSH
When you buy a VPS, you will be provided with the root login credentials. There are some hosting providers that have inbuilt terminal which can be used with root access. In that case, SSH is generally not needed.
SSH (Secure Shell) is a command line interface for managing your VPS. It helps you connect to your VPS shell securely. For the SSH access you might need to contact the hosting provider if it is not enabled. If it is enabled then you may proceed further.
PuTTY
This is a free and open source terminal emulator which helps you access your VPS's shell using SSH from your local system. Download PuTTY on your local system using the official website: https://www.putty.org/
For using PuTTY, first you need to have public and private key pairs. These key pairs can be generated using PuTTYgen or they can be created using the hosting interface (like WHM). After creating the public - private key pairs you can download the same private key to your local system and after opening PuTTY you can upload the private key file via: Connection -> SSH -> Auth as shown in figure below:
Browse Private Key File And Upload it for authorization in PuTTY for SSH access
After uploading, go to session tab and enter the IP address of your server as shown in image below:

Then click open and you will be redirected to a terminal with the ssh access of you server. From that terminal you can manage your server. That terminal is almost same as using a terminal on your VPS machine.
Note: There are many ways to connect to your server using ssh. Above mentioned is just one way of doing it.
Getting Ready For Deployment
Install all the required packages and libraries by verifying their versions with your local environment. Also download the softwares required for running you website.
- Enable the EPEL repository
$ sudo yum install epel-release
- Install the required packages
$ sudo yum install <package/depedency_name>
Now you can transfer your website files from local system to your VPS using secure copy (scp) through PuTTY or you can create a repository of your website on github and from there you can clone it into your VPS.
I recommend uploading your website files in the directory /var/www/<my-website>.
For a Django website, creating a virtual environment is nowadays must since it enables you to have separate projects running on same machine without conflicting each others dependencies and versions. So, start by creating a environment, activating it and ensuring all the dependencies to be installed for your website inside the environment.
Note: This article assumes you have a website ready on your localhost for hosting and thus we will not go through the steps of creating new one.
After you are done installing dependencies and moving your website files, you need to create a database server on you VPS. Here we will be using PostgreSQL.
Setting Up Your Website Database On Your VPS
- Create your database and new role (database user) for the website using psql and update your settings.py file accordingly.
- Run the following command on your ssh terminal to create migrations for your database :
(project_env) $ python manage.py makemigrations
- Ensure that all the migrations are properly created and no errors occur while creating migrations. Now migrate the database :
(project_env) $ python manage.py migrate
Resetting Migration History On Local System
- See the current migrations using command :
(local_project_env) $ python manage.py showmigrations
- Repeat this step to clear the migration history of all apps in your website/project :
(Note: Here my-app should be replaced with your app name.)
(local_project_env) $ python manage.py migrate --fake my-app zero
- Remove the actual migration files by navigating to the migration folder of each app and deleting everything inside that folder except __init__.py file. Be careful about not deleting __init__.py file.
- Create the initial migrations and fake them :
(local_project_env) $ python manage.py makemigrations
(local_project_env) $ python manage.py migrate --fake-initial
Migrating Your Existing Database to PostgreSQL On Your VPS
- Dump existing data on local system :
(local_project_env) $ python manage.py dumpdata > dump.json
- Move the dump.json file from your local system to VPS using either github or secure copy(scp).
- Run the python shell and remove contentType data :
(Note: Run the following commands on server's terminal (ssh))
(project_env) $ python manage.py shell
>>> from django.contrib.contenttypes.models import ContentType
>>> ContentType.objects.all().delete()
>>> quit
- Load the dump.json file to your database:
(project_env) $ python manage.py loaddata dump.json
Similar Reads
Django vs Spring Boot: Which is Better For Your Website
In the fast-paced world of web development, picking the right framework is like choosing the base for a tall building. It's super important! Imagine you're picking the perfect foundation for a skyscraper. Now, there are two big contenders in this web development game: Django and Spring Boot. They're
9 min read
Django - Creating a Multi-Page Website
Django is a python based open-source framework used for web development. Django allows the usage of the M-T-V (Model-Templates-View) architectural pattern for the creation and deployment of a functional website. This article aims at creating a multipage personal profile website using Django to expl
8 min read
How to Override CSS in Django Admin?
Django's admin interface is a powerful tool for managing application data. It comes with a default styling that may not always fit the visual identity of your project. Customizing the admin interface can help create a more cohesive user experience. In this article, we'll walk you through the process
3 min read
Hosting A Static Website On Firebase For Free
Building a web application is always a time-consuming job, especially for the beginner developers who have just landed their feet in the vast world of programming. And one of the most exciting parts is when the application is available for all to see. Being a new bee in programming most beginners la
5 min read
How To Use PostgreSQL with your Django Application on Ubuntu
This article describes how to configure PostgreSQL with the Django application on your Ubuntu machine. First, let's look at an overview of all the tools we use. PostgreSQL is a high-performance, reliable, and robust open-source relational database management system (RDBMS).Django is a robust, free,
4 min read
E-commerce Website using Django
This project deals with developing a Virtual website âE-commerce Websiteâ. It provides the user with a list of the various products available for purchase in the store. For the convenience of online shopping, a shopping cart is provided to the user. After the selection of the goods, it is sent for t
11 min read
How to use Tailwind CSS with Django ?
Tailwind CSS has gained immense popularity among developers for its utility-first approach to styling web applications. Django on the other hand is a robust and flexible web framework written in Python. Combining these two powerful tools can enhance your Django projects. In this article, we will exp
4 min read
How to Add Cart in a Web Page using Django?
A shopping cart allows users to collect and manage items they want to purchase before proceeding to checkout. We will build a simple shopping cart using Django and learn how to create models, views, templates and URLs to add, view and remove items from the cartâstep by step.Create Django Project and
6 min read
How to create a Django project?
Dive into the world of web development with Python by exploring the versatile Django framework. Django is a go-to for many developers due to its popularity, open-source license, and robust security features. It enables fast and efficient project development. In this tutorial, we will guide you throu
5 min read
How to create superuser in Django?
Django comes with a built-in admin panel that allows developers to manage the database, users and other models efficiently. This eliminates the need for creating a separate admin interface from scratch. To access and manage the Django Admin panel, we need to create a superuser i.e. a user with full
2 min read