Django is a high-level Python web framework that encourages rapid development and clean, pragmatic design. This comprehensive guide will walk you through the steps to install Django and set up a development environment on Ubuntu 22.04 using the root user. Whether you are a newcomer or an experienced developer, this tutorial aims to make the process straightforward and easy to follow.

Step 1: Deploying a Cloud Instance on Shape.host

  • Log in to Shape.host Dashboard:
  • Navigate to the Shape.host website and log in to your account.
  • Create a New Instance:
  • Click on the “Create” button located at the top right corner of the dashboard.
  • From the dropdown menu, select “Instances”.
Shape.host Resources dashboard showing the Create menu and instance rebuild controls.
  • Select Instance Location:
  • Choose the desired location for your server. For this tutorial, we’ll select “New York, USA”.
Shape.host Create New Instance form with the available data center locations.
  • Choose a Plan:
  • Select a plan that fits your requirements. For example, you might choose a plan with 2 cores CPU, 2 GB Memory, and 50 GB SSD disk space.
  • Select an Operating System:
  • Scroll down to the “Choose an image” section and select “Ubuntu 22.04”.
Shape.host instance form with a server plan and Ubuntu image selected.
  • Configure Additional Options:
  • (Optional) You can configure additional options like User Data Configuration and IPv6 Networking.
  • Enter a hostname for your instance, e.g., “Tutorial Ubuntu”.
  • Click on the “Create instance” button to deploy the instance.
Shape.host instance form showing hostname, instance count and the Create Instance button.

Step 2: Connecting to Your Instance

  • Retrieve SSH Credentials:
  • Note the IP address of your newly created instance from the Shape.host dashboard.
  • Connect via SSH:
  • Open a terminal on your local machine.
  • Use the following command to connect to your instance:
    ssh root@your_instance_ip
  • Replace your_instance_ip with the actual IP address of your instance.

Before starting, ensure you have:

  1. Ubuntu 22.04 Server: Access to a server with root privileges.
  2. Python 3: Installed by default on Ubuntu 22.04.
  3. Terminal Access: Ability to execute commands via terminal or SSH.

Step 3: Update and Upgrade Your System

First, ensure your system is up to date by running the following commands:

apt update
apt upgrade -y
Terminal showing APT package management commands and output.

This ensures that your server has the latest package information and security updates installed.

Step 4: Install Python and Pip

Python and pip (Python’s package installer) are essential for installing Django and other dependencies. They are usually pre-installed on Ubuntu 22.04, but it’s good to check and install them if necessary:

apt install python3 python3-pip -y
Terminal showing APT package management commands and output.

Verify the installation:

python3 --version
pip3 --version
Terminal showing python3 commands or output during Install Python and Pip.

Step 5: Install Virtualenv

Using a virtual environment is recommended to manage dependencies for different projects separately. Install virtualenv using pip:

pip3 install virtualenv
Terminal showing Django or Python virtual environment commands.

Step 6: Create a Project Directory

Create a directory for your Django project and navigate into it:

mkdir /root/my_django_project
cd /root/my_django_project
Terminal showing file and directory setup commands.

Step 7: Set Up a Virtual Environment

Create and activate a virtual environment within your project directory:

virtualenv venv
source venv/bin/activate
Terminal showing Django or Python virtual environment commands.
Terminal showing Django or Python virtual environment commands.

Your prompt should change to indicate that you are now working within the virtual environment.

Step 8: Install Django

With the virtual environment activated, install Django using pip:

pip install django
Terminal showing Python environment or package installation output.

Verify the installation by checking the Django version:

django-admin --version
Terminal showing Python environment or package installation output.

Step 9: Create a Django Project

Create a new Django project called myproject (you can name it anything you like):

django-admin startproject myproject
Terminal showing Django or Python virtual environment commands.

Navigate into the project directory:

cd myproject
Terminal showing Django or Python virtual environment commands.

Step 10: Apply Migrations

Django uses migrations to set up your database schema. Apply migrations using the following command:

python manage.py migrate
Terminal showing file and directory setup commands.
Terminal showing python commands or output during Apply Migrations.

Step 11: Create a Superuser

Create an administrative user to access the Django admin interface:

python manage.py createsuperuser
Terminal showing python commands or output during Create a Superuser.
Terminal showing python commands or output during Create a Superuser.

Follow the prompts to set a username, email, and password.

Step 12: Configure ALLOWED_HOSTS

To access your Django project from your browser, you need to configure the ALLOWED_HOSTS setting. Open the settings file:

nano myproject/settings.py
Terminal showing nano commands or output during Configure ALLOWED_HOSTS.

Find the ALLOWED_HOSTS line and update it to include your server’s IP address. For example:

ALLOWED_HOSTS = ['0.0.0.0']

If you want to allow access from any IP address (useful for development purposes only), you can use:

ALLOWED_HOSTS = ['*']

Save and close the file (in nano, press CTRL + X, then Y, and ENTER).

Step 13: Start the Development Server

Start the Django development server to ensure everything is set up correctly:

python manage.py runserver 0.0.0.0:8000
Terminal showing nano commands or output during Start the Development Server.

If you need to bind to your server’s specific IP address:

python manage.py runserver 0.0.0.0:8000

Step 14: Accessing the Django Application

To access your Django application, open a web browser and navigate to:

http://0.0.0.0:8000

You should see the default Django welcome page. To access the admin interface, go to:

http://0.0.0.0:8000/admin
Django sign-in form.
Django administration home page with users and groups controls.

Log in with the superuser credentials you created earlier.

Step 15: Configuring Firewall (Optional)

If you have a firewall running, you need to allow traffic on port 8000:

ufw allow 8000

For reliable and scalable hosting solutions, consider Shape.host’s Cloud VPS services. Shape.host offers robust Linux SSD VPS hosting that complements Django installations seamlessly. With Shape.host, you benefit from high-performance servers, flexible configurations, and excellent support to ensure your Django applications perform optimally.

By following this guide, you have successfully installed Django, set up a development environment, and configured your server to allow access to your Django project on your Ubuntu 22.04 server using root privileges. This setup provides a solid foundation for developing and deploying Django applications.