Can't start django app with daphne - django

I was going through a tutorial on Django Channels until I came across the command that should start my app daphne apps.base.asgi:channel_layer -p 8000.What could be the reason my django app doesn't start when running it in terminal. It doesn't show any errors, when I press Enter it just goes to new line without running the command.

Channel is not able to connect to redis. Start local redis server and try

Related

Why is VS Code loading all my previous logs in the terminal when I run docker-compose up for django rest framework?

I run docker-compose up I don't know why but all the previous logs which were part of previous builts, generate again in the terminal. which is why it takes 5 minutes for all the logs to appear and the server to be up and running.
To avoid that previously I used to quit the server, clear the window, remove the terminal and added new terminal and it solved the problem and started the server from fresh. but now it is not working. kindly help I am new to django and docker.

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.

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