I am relatively new to Python/Django and have successfully deployed my first app. I want to update it now with some new changes, but I am not sure what the proper process is. My setup is ubuntu/nginx/gunicorn/postgres.
At the moment I am taking the following steps:
Stop nginx: sudo service nginx stop
Stop gunicorn: sudo service gunicorn stop
Backup the db? (not implemented - cant find it on the server)
Git Pull
python manage.py migrate
python manage.py collectstatic
restart gunicorn: sudo service gunicorn start
restart nginx: sudo service nginx restart
This is working, but I would appreciate some guidance if this is the complete, most accurate and safest way to do this please?
One lazy (yet recommended and professional) way of going about app updates is running automation script, like Fabric or Ansible.
However, if you wish to proceed the manual way (which is tedious), you might do something like:
Pull from git
Run migrations python manage.py migrate (This should ensure changes you made locally to your models reflect in production DB)
Run static collections to ensure new statics are reflected in server /static/ folder like so: python manage.py collectstatic
Then, restart your Django Server not Nginx. So something like: sudo service your_django_server_running_instance restart
On digitalOcean for instance (when used One-Click Install), your django server running instance is likely called gunicorn
Then you might want to look into automating your postgresql db as well
Related
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
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.
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.
What is the best practice when I have an update for my Django app pushed in my production? Shall I restart both gunicorn and nginx services, with
sudo service gunicorn restart
sudo service nginx restart
or restarting only gunicorn is enough? Finally does the order of the restarts makes any difference if I have to do both the restarts? Thanks!
It entirely depends on how you've configured your box.
To keep downtime to an absolute minimum, I actually load my new release into a different directory on the box while the old release is still running. I create a new virtual environment based on my new release's requirements.txt. Then I start a second instance of gunicorn with the new release running in it (done via supervisord with entries in supervisord.conf), and leave the old instance still running.
I then update my nginx vhost file to point the server to the new release's gunicorn socket, and finally reload nginx. I do a quick check that the new site is up and functioning, and then I stop the old gunicorn instance. If for some reason it's not responding, I switch my nginx config back to point to the old one again, and then go figure out what's wrong.
I do all this using an Ansible script, but here's a great article with some Fabric scripts to do something similar: https://medium.com/#healthchecks/deploying-a-django-app-with-no-downtime-f4e02738ab06
If, on the other hand, you just update your code in-place, then there should be no changes needed to your nginx config, so you shouldn't need to reload it. Just reload gunicorn and you're good to go.
I tried to dockerize my django/apache web application.
In the big picture the dockerization worked out.
For deployment Annot is running in 3 containers. The apache_annot container holds the apache server and the postgresql database The media_annot container serves as web application media folder. The annot container holds the django python related code.
For development only 2 containers are needed: the apache_annot container and media_annot container. The django and python related code is placed into a directory at the host machine. apache_annot then simply mounts this directory as a data volume under /var/www/annot/.
Here the link to the Docker files:
https://gitlab.com/biotransistor/dock/tree/master
Issue:
Even the dockerized version works, it seems not to be done the real docker way.
Most annoying issue: Every time apache is restarted (which happens a lot in development) the apache_annot container and as such the whole web application stops.
To continuer apache_annot have to be restarted:
docker exec -ti apache_annot /bin/bash
Then postgresql database have to be restarted:
/etc/init.d/postgresql start
And cd into /var/www/annot/ to execute django development specific python code like:
python3 manage.py makemigrations.
It is nearly not possible to do development with this solution.
I can not be the only one who like to use a dockerize django apache developer environment. What do I do wrong?
Is there a way to run apache_annot so that the apache daemon can be rebooted without shouting down the apache_annot docker container?