Last active
October 19, 2020 11:43
-
-
Save osw4l/a4699f1b8abe946e8a2ad113829315bd to your computer and use it in GitHub Desktop.
django_deploy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # first steps if you have locales issues | |
| sudo dpkg-reconfigure locales | |
| sudo apt install language-pack-en | |
| sudo locale-gen en_US en_US.UTF-8 | |
| nano /etc/default/locale | |
| LANG=en_US.UTF-8 | |
| LC_ALL="en_US.UTF-8” | |
| LANGUAGE="en_US:en" | |
| # first steps :) | |
| sudo apt-get update | |
| sudo apt-get upgrade | |
| sudo dpkg-reconfigure locales | |
| sudo apt-get install language-pack-id | |
| sudo apt-get install redis-server python3-pip python3-venv libpq-dev python3-dev supervisor nginx redis-server htop git | |
| wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add - | |
| echo "deb http://apt.postgresql.org/pub/repos/apt/ `lsb_release -cs`-pgdg main" |sudo tee /etc/apt/sources.list.d/pgdg.list | |
| sudo apt update | |
| sudo apt-cache madison postgresql postgresql-contrib postgis postgresql-12-postgis-3-scripts | |
| sudo apt update -y | |
| sudo apt install -y postgresql postgresql-contrib postgis postgresql-12-postgis-3-scripts | |
| # gdal | |
| sudo apt-add-repository ppa:ubuntugis/ubuntugis-unstable | |
| sudo apt-get update | |
| sudo apt-get install gdal-bin | |
| sudo apt-get install libgdal-dev | |
| export CPLUS_INCLUDE_PATH=/usr/include/gdal | |
| export C_INCLUDE_PATH=/usr/include/gdal | |
| pip install --upgrade setuptools | |
| pip install GDAL==$(gdal-config --version) --global-option=build_ext --global-option="-I/usr/include/gdal" | |
| # postgis | |
| sudo apt install postgis | |
| sudo su - postgres | |
| createuser --interactive -P | |
| createdb --owner project project | |
| psql | |
| grant all privileges on database project to project; | |
| logout | |
| # in project | |
| # create base logs folder and default loggin file for django | |
| mkdir /opt/logs | |
| touch /opt/logs/debug.log | |
| # create uwsgi logs | |
| mkdir /opt/logs/uwsgi && touch /opt/logs/uwsgi/uwsgi.log /opt/logs/uwsgi/uwsgi.error.log | |
| # create websockets worker logs | |
| mkdir /opt/logs/websockets_worker && touch /opt/logs/websockets_worker/ws.log /opt/logs/websockets_worker/ws.error.log | |
| # create daphne logs | |
| mkdir /opt/logs/daphne && touch /opt/logs/daphne/daphne.log /opt/logs/daphne/daphne.error.log | |
| # create celery worker logs | |
| mkdir /opt/logs/celery_worker && touch /opt/logs/celery_worker/celery.log /opt/logs/celery_worker/celery.error.log | |
| # create celery beat logs | |
| mkdir /opt/logs/celery_beat && touch /opt/logs/celery_beat/beat.log /opt/logs/celery_beat/beat.error.log | |
| mkdir /opt/public/ | |
| sudo chown -R root:www-data /opt/public/ | |
| sudo chown -R www-data:www-data /opt/logs/ | |
| cd /etc/supervisor/conf.d/ | |
| touch daphne.conf uwsgi.conf websockets_worker.conf | |
| # if you need celery conf inside /etc/supervisor/conf.d/ | |
| touch celery_worker.conf | |
| # if you need celery beat conf inside /etc/supervisor/conf.d/ | |
| touch celery_beat.conf | |
| # create virtualenv install requeriments | |
| sudo apt-get install -y python3-venv | |
| # in opt/ | |
| python3 -m venv env | |
| source env/bin/activate | |
| cd /opt/project | |
| pip install -r requirements.txt | |
| # if terminal shows wheel error just run | |
| pip3 install wheel | |
| # make migrations | |
| ./migrate.sh | |
| #services | |
| supervisorctl reread && supervisorctl update && supervisorctl restart all | |
| # create nginx logs | |
| mkdir /opt/logs/nginx/ && touch /opt/logs/nginx/nginx-access.log /opt/logs/nginx/nginx-error.log; | |
| cd /etc/nginx/sites-enabled | |
| rm default | |
| touch default | |
| # copy all contenf of default and paste with nano | |
| sudo service nginx restart | |
| python manage.py collectstatic | |
| pg_dump name_of_database > name_of_backup_file | |
| sudo su - postgres | |
| psql empty_database < backup_file |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| [program:celery_beat] | |
| command=/opt/env/bin/celery --app=project.celery:app beat -l info --scheduler django_celery_beat.schedulers:DatabaseScheduler | |
| directory=/opt/project | |
| user=root | |
| stdout_logfile=/opt/logs/celery_beat/beat.log | |
| stderr_logfile=/opt/logs/celery_beat/beat.error.log | |
| autostart=true | |
| autorestart=true | |
| startsecs=10 | |
| ; need to wait for any tasks still running | |
| stopwaitsecs=900 | |
| ; send SIGKILL to destroy all processes as a group | |
| killasgroup=true | |
| ; if rabbitmq is supervised, set it to a higher priority | |
| priority=998 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| [program:celery_worker] | |
| command=/opt/env/bin/celery --app=project.celery:app worker --concurrency=5 | |
| directory=/opt/project | |
| user=root | |
| stdout_logfile=/opt/logs/celery_worker/celery.log | |
| stderr_logfile=/opt/logs/celery_worker/celery.error.log | |
| autostart=true | |
| autorestart=true | |
| startsecs=10 | |
| ; need to wait for any tasks still running | |
| stopwaitsecs=900 | |
| ; send SIGKILL to destroy all processes as a group | |
| killasgroup=true | |
| ; if rabbitmq is supervised, set it to a higher priority | |
| priority=998 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ;daphne worker supervisor | |
| ; ================================== | |
| [program:daphne] | |
| ; Set full path to daphne program if using virtualenv | |
| command=/opt/env/bin/daphne project.asgi:channel_layer --port 8001 -b 0.0.0.0 | |
| directory=/opt/project | |
| user=root | |
| numprocs=1 | |
| autostart=true | |
| autorestart=true | |
| startsecs=10 | |
| stdout_logfile=/opt/logs/daphne/daphne.log | |
| stderr_logfile=/opt/logs/daphne/daphne.error.log | |
| environment=DJANGO_SETTINGS_MODULE='project.settings' | |
| ; When resorting to send SIGKILL to the program to terminate it | |
| ; send SIGKILL to its whole process group instead, | |
| ; taking care of its children as well. | |
| killasgroup=true | |
| ; Set daphne priority higher than default (999) | |
| ; so, if rabbitmq is supervised, it will start first. | |
| ;priority=1000 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # nginx | |
| upstream project { | |
| # fail_timeout=0 means we always retry an upstream even if it failed | |
| # to return a good HTTP response (in case the Unicorn master nukes a | |
| # single worker for timing out). | |
| server localhost:8000 fail_timeout=0; | |
| } | |
| upstream websockets { | |
| server localhost:8001 fail_timeout=0; | |
| } | |
| server { | |
| server_name testing.wogoapi.com; | |
| fastcgi_read_timeout 300000; | |
| proxy_read_timeout 300000; | |
| client_max_body_size 4G; | |
| access_log /opt/logs/nginx/nginx-access.log; | |
| error_log /opt/logs/nginx/nginx-error.log; | |
| location /static/ { | |
| gzip_static on; | |
| alias /opt/project/staticfiles/; | |
| include /etc/nginx/mime.types; | |
| } | |
| location /media/ { | |
| alias /opt/project/staticfiles/; | |
| } | |
| location / { | |
| # an HTTP header important enough to have its own Wikipedia entry: | |
| # http://en.wikipedia.org/wiki/X-Forwarded-For | |
| proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; | |
| proxy_set_header X-Forwarded-Proto $scheme; | |
| # enable this if and only if you use HTTPS, this helps Rack | |
| # set the proper protocol for doing redirects: | |
| # proxy_set_header X-Forwarded-Proto https; | |
| # pass the Host: header from the client right along so redirects | |
| # can be set properly within the Rack application | |
| proxy_set_header Host $http_host; | |
| # we don't want nginx trying to do something clever with | |
| # redirects, we set the Host: header above already. | |
| proxy_redirect off; | |
| # set "proxy_buffering off" *only* for Rainbows! when doing | |
| # Comet/long-poll stuff. It's also safe to set if you're | |
| # using only serving fast clients with Unicorn + nginx. | |
| # Otherwise you _want_ nginx to buffer responses to slow | |
| # clients, really. | |
| # proxy_buffering off; | |
| # Try to serve static files from nginx, no point in making an | |
| # *application* server like Unicorn/Rainbows! serve static files. | |
| proxy_pass http://project; | |
| } | |
| location /ws/ { | |
| proxy_pass http://websockets; | |
| proxy_http_version 1.1; | |
| proxy_set_header Upgrade $http_upgrade; | |
| proxy_set_header Connection "upgrade"; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (venv) $ pip uninstall psycopg2 | |
| Successfully uninstalled psycopg2-2.7.3 | |
| (venv) $ pip install --no-binary :all: psycopg2 | |
| Collecting psycopg2 | |
| Downloading psycopg2-2.7.3.tar.gz (425kB) | |
| 100% |████████████████████████████████| 430kB 601kB/s | |
| Installing collected packages: psycopg2 | |
| Running setup.py install for psycopg2 ... done | |
| (venv) $ python | |
| Python 3.4.6 (default, Aug 24 2017, 12:52:24) | |
| [GCC 7.2.0] on linux | |
| Type "help", "copyright", "credits" or "license" for more information. | |
| >>> import psycopg2 | |
| >>> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ;uwsgi worker supervisor | |
| ; ================================== | |
| [program:uwsgi] | |
| ; Set full path to uwsgi program if using virtualenv | |
| command=/opt/env/bin/uwsgi --ini uwsgi.ini | |
| directory=/opt/project | |
| user=root | |
| numprocs=1 | |
| autostart=true | |
| autorestart=true | |
| startsecs=10 | |
| stdout_logfile=/opt/logs/uwsgi/uwsgi.log | |
| stderr_logfile=/opt/logs/uwsgi/uwsgi.error.log | |
| environment=DJANGO_SETTINGS_MODULE='project.settings' | |
| ; Need to wait for currently executing tasks to finish at shutdown. | |
| ; Increase this if you have very long running tasks. | |
| ;stopwaitsecs = 600 | |
| ; When resorting to send SIGKILL to the program to terminate it | |
| ; send SIGKILL to its whole process group instead, | |
| ; taking care of its children as well. | |
| killasgroup=true | |
| ; Set uwsgi priority higher than default (999) | |
| ; so, if rabbitmq is supervised, it will start first. | |
| ;priority=1000 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ;workers worker supervisor | |
| ; ================================== | |
| [program:websockets_worker] | |
| ; Set full path to workers program if using virtualenv | |
| command=/opt/env/bin/python /opt/project/manage.py runworker | |
| directory=/opt/project | |
| user=root | |
| numprocs=1 | |
| autostart=true | |
| autorestart=true | |
| startsecs=10 | |
| stdout_logfile=/opt/logs/websockets_worker/ws.log | |
| stderr_logfile=/opt/logs/websockets_worker/ws.error.log | |
| environment=DJANGO_SETTINGS_MODULE='project.settings' | |
| ; When resorting to send SIGKILL to the program to terminate it | |
| ; send SIGKILL to its whole process group instead, | |
| ; taking care of its children as well. | |
| killasgroup=true | |
| ; Set workers priority higher than default (999) | |
| ; so, if rabbitmq is supervised, it will start first. | |
| ;priority=1000 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment