skip to Main Content

I have developed a Django website and hosted the application in an Amazon EC2 instance.
AMI name: ubuntu/images/hvm-ssd/ubuntu-jammy-22.04-amd64-server-20231207
Instance type: t2.micro

I run the project following these steps:

  1. SSH into the EC2 instance from my terminal.
  2. I locate into the proper django project folder.
  3. I run this command: python3 manage.py runserver 0.0.0.0:8000

Now, following these steps, I’m able to run the project and it works fine. But when I close the cmd opened in my local PC that I used to SSH into the EC2 instance and to run the application, then the application doesn’t work anymore.
My aim would be to simply run the django project from my cmd once (of course after having SSH to the EC2) and then close my laptop and having the application still alive.

Do you know how can I do this?

3

Answers


  1. I guess that’s because when closing the ssh connection, you stop the program because it’s run in foreground.

    With the following command it should work:

    python3 manage.py runserver 0.0.0.0:8000 &
    

    PS: you should not start the Django application with a development server, see the deployment page of the Django documentation.

    edit you could also create a systemd service like in this answer.

    Login or Signup to reply.
  2. You can use nohup for running it even after you terminate processes on your terminal/cmd.

    nohup python3 manage.py runserver 0.0.0.0:8000 &
    

    You can also log the output of above command by writing it to a file for debugging purpose.

    nohup python3 manage.py runserver 0.0.0.0:8000 & > runserver.out
    

    Note: Not recommended for production environment.

    Login or Signup to reply.
  3. Please use Nohup

    It ignores the hangup signal, allowing the process to continue running after the terminal is closed.

    Steps :-

    1. SSH into your Instance.
    2. Navigate to the path where you have manage.py file.
    3. Now run nohup python3 manage.py runserver 0.0.0.0:8000 &
    4. You can exit the session now and you woulf

    Run: nohup python3 manage.py runserver 0.0.0.0:8000 &.
    The & at the end puts the process in the background.
    Exit the SSH session; the process will continue to run.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search