📒 

Django is a high-level Python web framework that encourages rapid development and clean, pragmatic design. It is widely used for building web applications due to its robustness and scalability. This article will guide you through the process of installing Django on a hosting server.

1. Prerequisites

Before you begin, ensure you have the following:

  • A hosting server with a Linux distribution (Ubuntu, Debian, etc.) or a VPS.
  • SSH access to the server.
  • Python and pip installed on the server. (Python 3 is recommended.)

2. Connect to Your Server

Open your terminal and connect to your hosting server using SSH:

ssh username@server_ip

Replace username with your actual username and server_ip with your server’s IP address.

3. Install Python and pip

If Python and pip are not already installed, you can install them using the following commands:

Step 1: Update Package Index

sudo apt update

Step 2: Install Python and pip

sudo apt install python3 python3-pip -y

4. Set Up a Virtual Environment

Using a virtual environment is recommended for managing dependencies and ensuring that your Django project is isolated from other projects on the server.

Step 1: Install virtualenv

Install the virtualenv package using pip:

sudo pip3 install virtualenv

Step 2: Create a Virtual Environment

Navigate to your project directory (or create a new one) and set up a virtual environment:

mkdir myproject cd myproject virtualenv venv

Step 3: Activate the Virtual Environment

Activate the virtual environment:

source venv/bin/activate

You will notice that your command prompt has changed to indicate that the virtual environment is active.

5. Install Django

With the virtual environment activated, you can now install Django using pip:

pip install django

6. Create a New Django Project

Step 1: Start a New Project

Use the following command to create a new Django project:

django-admin startproject myproject .

This command creates a new Django project named myproject in the current directory.

7. Configure Database Settings

Open the settings.py file located in the project directory:

nano myproject/settings.py

Step 1: Set Database Configuration

By default, Django uses SQLite. To configure another database (e.g., PostgreSQL or MySQL), you will need to modify the DATABASES setting in settings.py. For example, for PostgreSQL:

DATABASES = { ‘default’: { ‘ENGINE’: ‘django.db.backends.postgresql’, ‘NAME’: ‘mydatabase’, ‘USER’: ‘myuser’, ‘PASSWORD’: ‘mypassword’, ‘HOST’: ‘localhost’, ‘PORT’: ”, } }

8. Apply Migrations

Once the database is configured, run the following command to apply migrations:

python manage.py migrate

9. Run the Development Server

You can start the Django development server to test your application:

python manage.py runserver 0.0.0.0:8000

This command binds the server to all available IP addresses on port 8000. You can access your Django application by navigating to http://server_ip:8000 in your web browser.

10. Conclusion

You have successfully installed Django on your hosting server and set up a new project. Django provides a robust framework for building web applications, and by following this guide, you can begin developing your projects. For production deployment, consider using a web server like Nginx or Apache and a WSGI server like Gunicorn or uWSGI to serve your Django application.