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

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).

Related

How to run a Django's application server on a VPS server

I have an application that should run via VPS terminal-based so that the web app can be online permanently.
The command: python manage.py runserver 173.249.8.237:8000 should be executed via the VPS terminal because when I use putty via my laptop, whenever I switch off my putty software via my laptop, the application won't be accessible.
Please, suggest to me a way open a terminal in order to run the Django application server.
Or, is there any method that can allow me to my Django web application on VPS?
Thanks in advence
Don't do that.
runserver according Django Docs
DO NOT USE THIS SERVER IN A PRODUCTION SETTING. It has not gone
through security audits or performance tests. (And that’s how it’s
gonna stay. We’re in the business of making web frameworks, not web
servers, so improving this server to be able to handle a production
environment is outside the scope of Django.)
You need to configure your Django application to run in production environments. You change choose docker for that.
Or simple running with gunicorn.
Access your VPS through SSH or Putty.
Copy your application code inside VPS
Configure your application to run with gunicorn or docker
Access your VPS address in your browser.
And turn of the DEBUG mode, by setting DEBUG=False

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.

Django code changes don't reflect on production server

I made so changes in one of my model-forms in my Django apps - I added new input fields for the user. I then tested the changes in my environment and everything works fine. I then committed and pushed the changes to remote repo. I pulled the changes on my production server which runs on AWS. I ran pkill -f runserver in terminal to restart the server, however the changes didn't take place. Only the changes regarding html tags were visible (new labels and etc...).
The changes that weren't present are the ones that come from the model-from: new input fields for the user - those were just missing completely from the template page.
What can be causing this behaviour?
you need to restart gunicorn service every time you make changes to the code.
run sudo systemctl restart gunicorn
Do it and changes will reflect.
After you push changes to the production server you also need to migrate the database changes.
On your local repo (where you make your model changes) you run the makemigrations command
python manage.py makemigrations
And after you fetch your changes on your production server you run the migrate command
python manage.py migrate
It depends on the server you're using : nginx, gunicorn, or apache..
If you setup the server using nginx and gunicorn , you could probably try restarting those services manually, but i'd recommend using something like supervisord to ease out the restart procedure..
If ur using apache, probably use
sudo service httpd restart
If you use supervisor then you need to run
sudo supervisorctl reload
I was having the same problem, this command worked for me on AWS - Ubuntu Server 18.04 LTS.

Running Django Python Server in AWS

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.

How to keep django app running in the background within the vagrant?

I have a Ubuntu 14.04 host headless Server.
Using root user, I vagrant up a VM that is using VirtualBox.
Inside this VM, is a Django Python 3 app.
Every time I vagrant up and vagrant ssh this VM, I need to run sudo service gunicorn start.
If I exit from the vagrant ssh, and then switch to another user, the app dies.
How do I maintain this Django app running from the VM permanently?
If the host machine has to reboot for whatever reason, how can the Django app automatically run itself?
In summary:
how to allow vagrant and the gunicorn inside the VM run for a very long time while I switch between users in the host OS?
Is there a way to automatically revive the vagrant and the gunicorn inside, whenever the host OS is rebooted?
Use:
sudo service gunicorn start &
The & sign will make your command to run on a different process then the terminal one, so you can close the terminal without closing the gunicorn.
By the way, this is not a vagrant related, it happens on all linux-like terminals.
For your second question, you need to use something like supervisor to handle this for you.