Running Django Python Server in AWS - django

I have a django running in AWS Ubuntu machine. Through SSH, I start the server at 8000 port. But when i close the ssh window, server stops and I can't access it through URL. What I want is to run the server all the time once it is started. How to go about it? Thanks.

You can use either Apache or Nginx to deploy Django App. If you are planning to Use Nginx, first install Nginx in the server and add Django configurations to Nginx configuration. You can follow this as a good guide.

You can do it the hacky way: create a bash script that executes the app (just running the same command you execute to run it), and run the bash script with nohup, which detaches the process from the shell and will allow the application to continue running when you close your session:
nohup ./my_bash_script.sh &
If you want to do it properly, create a service file and execute the app 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 run it with this command:
sudo service start djangoService
That will keep the service running. Bear in mind, though, that to service a proper Django app you may want to use Gunicorn/wsgi to serve the responses, using Nginx to reverse proxy the requests.

In development mode, Django has a development server, which is sufficient for testing purposes. Once you complete a web application and it's ready for production, the process of setting up the application on a server might be overwhelming for some, especially if you're doing it for the first time. This article provides a step-by-step guide on how to deploy Django-based web applications using mod_wsgi.
You can follow this article for setting mod_wsgi with Apache server. enter link description here
If the one which you are setting up for development only then you need to run the server in daemon mode.
on Ubuntu
run:>./manage.py runserver 0.0.0.0:8000 > /dev/null 2>&1 &
>exit

Django runserver permanent
Hope this helps, it is the best way to create the screen so we can monitor what is happening and take the control back.

Related

Serve Flask application with GUnicorn on Localhost

I am a noob in this area so please bear with my dumb questions.
I have a Flask application and I want to run that with GUnicorn on my localhost. I looked on Google but almost every tutorial requires a domain name and there isn't much documentation for running it on a mac.
Please tell how can I run the app with GUnicorn on my mac?
I want to use https for the secure communication so how can I change the configuration of Gunicorn to do so?
Any help will be great.
Cheers
Since you want to run it on your localhost you don't need any domain name. You just have to run Python file where you have configured your flask app on your terminal and then you will get a port number where application will be running on your local machine.
Steps to run app
Change your current directory of your terminal to directory where your project is present, using
cd <your directory address>
Now run your app using
python ./<file_name>.Py
Note: If you have both Python 2 and Python 3 installed (Your machine comes with a version of Python 2 but you can install Python 3 as well), you should run
python3 <file_name>.py
Even if you want to deploy on web on a server without your domain name for free you can do it using heroku or any other service as you like.
Running a Flask application on gunicorn is quite simple:
gunicorn <file_name>:app
Gunicorn provides many command-line options – see gunicorn -h. For example, to run a Flask application with 4 worker processes (-w 4) binding to localhost on port 4000 (-b 127.0.0.1:4000):
gunicorn -w 4 -b 127.0.0.1:4000 <file_name>:app

Restart gunicorn in django codes

I deployed my Django application to the internet and every time after changing codes, I have to run this command to make changes happen:
sudo systemctl restart gunicorn
Is there any way to run this command through codes (for example in views)? or schedule it?
Or is there any way to force Gunicorn restarts after changing in codes?
I have tried Subprocess but I couldn't make it work.
You could use fabric script to restart gunicorn.
Check this link Automate Deployment With Fabric Python

How do i run two command lines in AWS simultaneously?

I have a Django app that I am trying to deploy to AWS.
I need two command lines to run the app. One command line will be used to runserver and another one to run a background task. That is how the app runs in my localserver.
How do I start two command lines in AWS?
As i can understand your django app is currently in development mode, I've used tmux for development mode, this page will guide you better to use tmux. Once you have started a tmux session run python3 manage.py runserver and then detach to the session using ctrl-b then press d and now your app will run even after you exit the shell.
To know more about tmux go through this page.
In case you have to run your app in production mode Don't use above method, run your app with any of nginx or apache2 server. This tutorial can guide you to set up your django app with nginx and gunicorn.
Supervisor is a good way to control that. You can run a lot of processes (like server, migration, collectstatic, Celery) easily.
Additionally, you can ensure that all processes will run again when your machine reboots.
And, as everybody said, you have to install a server in production that supports WSGI in order to run Django properly.

How do I restart django server that is running in the background?

I run ssh ubuntu#myapp.com, change to the django directory for my app and then run ps | aux less. I do not see any processes with python in the command. How do I restart my django server as it is running in the background?
It depends how you're running the Django server. If you set the Django server to run with Apache, sudo service apache2 restart will do the trick.
If you're running the test server (manage.py runserver), you could use fg to bring the job into the foreground, and tinker with it, but the Django development server automatically detects changes for you (there's no need to restart).

Deploying Django with Nginx as service

Currently I have a home server (Ubuntu) with nginx running where I use proxy pass in order to pass the requests to django. I am using gevent as my wsgi server.
It all works fine until the server shuts down either because I restart the server for whatever reason or something crashes (electricity). Since nginx is a service, when the server restarts, nginx starts up as well. However my django apps do not. So then I have to manually go to each of my django projects, activate their virtualenvs, and then fire up the gevent process. This is very annoying to say the least.
Is there a standard way of handling all of this automatically?
You need to set up a script for something like Upstart or Supervisor. Personally, I prefer using Supervisor. Here's the script I use to run my gunicorn instances:
[program:gunicorn]
command=/path/to/virtualenv/bin/python manage.py run_gunicorn -c /path/to/gunicorn.conf.py
directory=/path/to/django/project
user=www-data
autostart=true
autorestart=true
redirect_stderr=True
Consider using a process manager to handle this for you. I like supervisor
You tell it how to launch your various processes and then it runs in the background (just like nginx) and will automatically start up on restart and launch your various django backend processes.