Sometimes, when working on a Django project, you might need to keep your Django development server running even after closing your Terminal or SSH session.

There are a few reasons why you may want to do this:

  1. Continuous availability: By keeping the Django development server running in the background, you ensure that your web application remains accessible to users at all times.
  2. Testing and debugging: Running the Django development server in the background allows you to test and debug your application remotely. You can access your web application from any device or location, making it easier to identify and fix issues.
  3. Remote access: When working on a Django project hosted on a remote server, keeping the server running allows you to access and monitor your application from anywhere. It enables you to make changes, analyze logs, and perform other administrative tasks without the need for a direct connection to the server.
  4. Share your Django website project with other Python programmers without having to host it on a live production server. This can be helpful during collaboration, especially when UI/UX design elements are considered.

But first,

Can you run Django server in the background even after closing the Terminal or ssh session?

You can run a Django server in the background even after closing the Terminal or SSH session by using tools like nohup or screen. These tools allow you to detach the process from the current session, keeping it running independently.

Let’s look at how you can run Django development server and keep it running even after closing your Terminal session.

How to run Django server and keep it running

As a beginner programmer, you might wonder if it’s possible to keep your Django server running even after closing the Terminal or SSH session.

The good news is that there are tools available that allow you to achieve this.

Let’s explore two popular options: nohup and screen.

Using nohup to run Django server in the background

One way to keep your Django server running in the background is by using the nohup command. It stands for “no hang up” and prevents the process from being terminated when the Terminal or SSH session is closed.

Here’s how you keep your Django server running continuously using nohup:

Step 1: Navigate into the Django project working directory

Open your Terminal or SSH session.

Using the cd command navigate into your Django project root directory.

For example:

cd path\to\your\project

Step 2: Run the Django server with nohup

To run the Django server using the nohup tool, execute the following command when you are navigated in the project root directory.

$ nohup python manage.py runserver

This command starts your Django server using the runserver command provided by Django.

The nohup command ensures the process continues running in the background even after you close the Terminal or SSH session.

If you want to run the nohup process in the background, append & at the end of the command above like this:

nohup python manage.py runserver &

The & symbol at the end allows the process to run in the background.

Opening a new Browser window and try accessing 127.0.0.1:8000 and you should see that your Django development server works fine even after closing the Terminal window.

Accessing Django development server on port 8000

Step 3: Access the Django server remotely

Once the server is running, you can access it remotely by opening a web browser and entering the IP address or domain name of the server, followed by the port number (usually 8000 for Django’s development server).

For example, if your server’s IP address is 192.168.0.100, you can access the Django application by entering http://192.168.0.100:8000 in your web browser.

Step 4: How to stop Django server running in the background using nohup

To stop the Django server running in the background, you need to find its process ID (PID) and terminate it.

First, find the process ID by running the following command:

ps -ef | grep 'python manage.py runserver'

Look for the line that contains the Django server process and note the corresponding PID.

To stop the server, use the kill command followed by the PID:

 kill <PID>

For example, I have a Django development server running on Process ID: 853519

How to check the process ID running Django development server

To terminate the process, I used the command:

kill 853519

That’s one way to run a Django development server.

Using screen to keep Django server running in the background

Another powerful tool to run your Django server in the background is screen. screen creates a virtual terminal that persists even after you disconnect from the session, keeping your Django server running.

Let’s see how you can utilize screen for your Django server:

Ensure you have Python and Django installed on your system.

Step #1: Install screen app on your machine

Install screen if it is not already available.

For example, on Ubuntu, you can install it using:

sudo apt-get install screen

Step #2: Run Django development server using screen tool

Open your Terminal or SSH session.

Navigate to your Django project directory using the cd command.

Start a new screen session with a custom name by running:

screen -S my_django_server

This command creates a new screen session named “my_django_server”.

Inside the screen session, run the following command to start your Django server:

python manage.py runserver

Step #3: Access your Django server remotely (or even after closing your Terminal window)

Once the server is running in the screen session, you can disconnect from your Terminal or SSH session without stopping the server.

To reconnect to the screen session later, open a new Terminal or SSH session and run:

screen -r my_django_server

Step #4: Stopping the Django server

To stop the Django server running in the screen session, switch back to the screen session by running:

screen -r my_django_server

Once inside the session, press Ctrl + C to stop the server.

To exit the screen session, press Ctrl + A followed by Ctrl + D.

Terminating an active screen created using Screen tool on Linux terminal

By using either nohup or screen, you can run your Django server in the background and ensure its uninterrupted operation, even when you close the Terminal or SSH session.

These tools provide a convenient way to keep your Django application accessible and functional for yourself and others.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *