Running Django Python Server on AWS

I have django running in an AWS Ubuntu machine. Through SSH, I run a server with port 8000. But when I close the ssh window the server stops and I cannot access it via the url. I want to keep the server running all the time after starting it. How to do it? Thank.

+3


source to share


3 answers


You can use Apache or Nginx to deploy your Django application. If you plan to use Nginx, first install Nginx on your server and add Django configurations to your Nginx configuration. You can follow this as a good guide .



+3


source


You can do this in a hacky way: create a bash script that executes the application (just by running the same command you run to run it) and run the bash script with nohup, which will detach the process from the shell and allow the application to continue running when the session is closed:

nohup ./my_bash_script.sh &

      

If you want to do this correctly, create a service file and run the application as a service. You can create a simple service file like this:

[Unit]
Description=My Django app
After=network.target

[Service]
PIDFile=/run/DjangoApp/pid
User=<your user>
Group=<your group>
WorkingDirectory=<working directory of your Django app>
ExecStart=<path to your bash script>
PrivateTmp=True

[Install]
WantedBy=multi-user.target

      

Save the file under /etc/systemd/system/djangoService.service

. You enable the service with this command:



sudo systemctl enable djangoService

      

And start it with this command:

sudo service start djangoService

      

This will keep the service running. Be aware, however, that in order to serve a proper Django application, you can use Gunicorn / wsgi to serve responses, using Nginx for a reverse proxy request.

+1


source


In development mode, Django has a development server sufficient for testing. Once you've finished your web app and are ready to go, the process of setting up your app on the server can be overwhelming for some, especially if this is your first time doing it. This article provides a step-by-step guide to deploying Django-based web applications using mod_wsgi.

You can follow this article for installing mod_wsgi with Apache server. enter link here

If the one you are setting up for development only then you need to start the server in daemon mode.

on Ubuntu

run:>./manage.py runserver 0.0.0.0:8000 > /dev/null 2>&1 &

>exit

      

+1


source







All Articles