How to install and configure Django on Linux VPS and Dedicated Server? Print

  • 0

Here, we will guide you on how to install and configure Django on Linux VPS and Dedicated Server.

Prerequisites of Django

  • Your system or server must have a clean OS.
  • You must have root access or access to a user with Sudo privileges.
  • It requires Python. Please see the below table for the versions of python that work with each version of Django.

    Django version Python versions
    2.2 3.5, 3.6, 3.7, 3.8 (added in 2.2.8), 3.9 (added in 2.2.17)
    3.0 3.6, 3.7, 3.8, 3.9 (added in 3.0.11)
    3.1 3.6, 3.7, 3.8, 3.9 (added in 3.1.3)
    3.2 3.6, 3.7, 3.8, 3.9

    Here, we recommend you to install the latest version of Python on your VPS or dedicated server because it is faster, has more features, and is better supported.

Installation of Django

In order to install Django, first, you need to create a virtual environment for Python by using the virtualenv tool. After then you activate the virtual environment. You can use the pip installer to install Django.
The following are steps to install python, create a virtual environment and install django.

  1. Login into your server or system using SSH with the root user.
  2. Enter the following commands in the terminal according to your Linux distribution.

    CentOS and Fedora:
    # yum install python34
    # yum install python34-pip
    # pip install virtualenv
    Debian and Ubuntu:
    # apt-get install python3
    # apt-get install python3-pip -y
    # pip3 install virtualenv
  3. Login with a non-root user (other than root user) and fire the following commands in the terminal.
    
    $ cd ~
    $ mkdir django-apps
    $ cd django-apps
    $ virtualenv env
    $ source env/bin/activate
    $ pip install django
  4. Congratulations! Django is installed on your server and system. You can check which version is installed on your server and system by typing the following command in the terminal.

    $ django-admin --version

Configuration of Django

Now, we will create and configure a Django application. In order to do this, you need to perform the following steps:

  1. Login into your server or system with a non-root user (other than the root user). Enter the following commands in the terminal.

    $ cd ~/django-apps
    $ django-admin startproject mysite
  2. Use a text editor to open the file settings.py under this path /home/username/django-apps/mysite/mysite/settings.py
    Note: Replace the username with the actual username which you have.
  3. In the file, you need to confirm that debug mode is enabled.

    DEBUG = True


  4. Search the ALLOWED_HOSTS line, and modify it. Replaced ['IP_address'] with the actual IP address of your server and system. Do not forget to close the IP address with single inverted commas like '192.168.2.152'

    ALLOW_HOSTS = ['IP_address']


  5. Step 5: Save the changes to the settings.py file.
  6. Now, you need to start the development web server. Type the following commands in the terminal. Please do not forget to replace the IP_address with the actual IP address of your server or system.

    $ python ~/django-apps/mysite/manage.py runserver IP_address:8000
  7. Go to the browser and visit http://IP_address:8000. Replace the IP_address with your actual server or system IP address. You will get the below message on the screen. The install worked successfully! Congratulations!

Configure the administration interface

Django offers an administration interface which you can use to modify website applications, manage users and groups and more. In order to configure the Django administration interface, you need to perform the following steps.

  1. Login into your server or system with a non-rooted user.
  2. Enter the following commands in the terminal.

    $ cd ~
    $ python ~/django-apps/mysite/manage.py migrate
    $ python ~/django-apps/mysite/manage.py createsuperuser
  3. In the Username field, enter administrator and press enter. You can also use another name which you want to set as an administrator user.
  4. In the Email address field, enter the email address and press enter.
  5. In the Password field, type the password which you want to set.
  6. In the Password again field, enter the same password which you have typed in the password field.
  7. Once you enter the password, you will get a message on the screen "Superuser created successfully".
  8. If the development web server is not running, then enter the following command in the terminal. Replace the IP address with the actual IP address of your system or server.

    $ python ~/django-apps/mysite/manage.py runserver IP_address:8000
  9. Again go to the browser and visit http://IP_address:8000/admin. Replace the IP address with your actual server or system IP address. Once you browse it, you will get the administration login page.



  10. Login into your administration interface using the username and password which you have set in Step 3 and Step 5.



    That’s all.


Was this answer helpful?

« Back

chat