Open In App

Hosting Your Django Website on a CentOS VPS

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:

  1. Shared Hosting – Usually used for small (single page) websites with limited traffic.
  2. VPS Hosting – VPS ( Virtual Private Server ) hosting is used for websites with good amount of content and medium to high traffic.
  3. 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:

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.

  $ sudo yum install epel-release
  $ 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

  (project_env) $ python manage.py makemigrations
  (project_env) $ python manage.py migrate

Resetting Migration History On Local System

  (local_project_env) $ python manage.py showmigrations

           (Note: Here my-app should be replaced with your app name.)

  (local_project_env) $ python manage.py migrate --fake my-app zero
  (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

  (local_project_env) $ python manage.py dumpdata > dump.json

            (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
  (project_env) $ python manage.py loaddata dump.json
Article Tags :