Skip to content

Instantly share code, notes, and snippets.

@nicobytes
Last active September 21, 2022 06:06
Show Gist options
  • Save nicobytes/feb06206977901c918293d8a926c5702 to your computer and use it in GitHub Desktop.
Save nicobytes/feb06206977901c918293d8a926c5702 to your computer and use it in GitHub Desktop.
Remeber you fuck steps, config for deploy django project

Conectarse con el server

  1. ssh user@your_ip
  2. Generar llave ssh-keygen -b 4096 (dentro de .ssh)
  3. Enviar llave al server ssh-copy-id -i .ssh/name.pub user@your_ip
  4. Conectar: ssh user@your_ip

Install Nginx

  1. update apt-get update
  2. Ingresar como root apt-get install nginx (todo lo que sea instalar con apt-get con root)
  3. Revisar desde la web si esta la pagina de bienvenida de nginx

Install Postgress

  1. Install postgresql apt-get install postgresql postgresql-contrib
  2. Log w/ user sudo -i -u postgres
  3. Verificar si puede ingresar a psql

Create user

  1. sudo adduser postgres_user
  2. Log w/ user sudo -i -u postgres
  3. psql
  4. CREATE USER postgres_user WITH PASSWORD 'password'; (debe hacer match con el usuario creado);
  5. CREATE DATABASE my_postgres_db OWNER postgres_user;
  6. \q
  7. exit logout
  8. Log w/ user sudo -i -u postgres_user
  9. Check psql my_postgres_db

Install virtualenv

  1. Install apt-get install python-pip
  2. Install pip install virtualenv

create virtualenv

  1. create workarea mkdir workarea; cd workarea
  2. create virtualenv virtualenv venv
  3. activate source venv/bin/activate

Download repo

  1. activate source venv/bin/activate
  2. Create ssh key for download project from repo server (bitbuket, github etc)
  3. ssh-keygen -b 4096 (inside server) as root user
  4. copy key and add in server repo (bitbuket, github etc)
  5. git clone url_repo
  6. pip install -r requirements.txt (inside virtualenv)

Config Database in django and static files

'default': {
    'ENGINE': 'django.db.backends.postgresql',
    'NAME': 'my_postgres_db',
    'USER': 'postgres_user',
    'PASSWORD': '.....', 
    'HOST': 'localhost',
    'PORT': '',
}

STATIC_ROOT = os.path.join(BASE_DIR, "static/")

Config django

  1. python manage.py migrate --settings=app.settings.production
  2. python manage.py createsuperuser --settings=app.settings.production
  3. python manage.py collectstatic --settings=app.settings.production
  4. python manage.py runserver 0.0.0.0:8000 --settings=app.settings.production
  5. check in web server run django in port 8000 (remember enabled port in dashboard azure, amazon etc) (http://server_domain_or_IP:8000)

Work with Gunicorn

  1. install Gunicorn pip install gunicorn (inside virtualenv)
  2. run gunicorn -b 0.0.0.0:8000 app.wsgi (app is name folder the core django project)
  3. check in web server run django in port 8000 (remember enabled port in dashboard azure, amazon etc) (http://server_domain_or_IP:8000)

Work with Supervisor

  1. Install Supervisor apt-get install supervisor (with root user)
  2. enter in config supervisor cd /etc/supervisor/conf.d/
  3. create myapp.conf vim myapp.conf:
[program:myapp]
directory=/home/your_user/your_workspace/your_project
command = /home/your_user/your_workspace/venv/bin/gunicorn -b 0.0.0.0:8080 app.wsgi
user = your_user
  1. supervisorctl reread
  2. supervisorctl update
  3. supervisorctl restart all
  4. supervisorctl status
  5. Project already without run commands (projecy run in port 8000)

Config Nginx for project

  1. enter in config nginx cd /etc/nginx/sites-available/
  2. create file vim myapp:
upstream myapp_app {
        server 127.0.0.1:8000;
}

server {
        listen 80;
        server_name your_domain;
        access_log /var/log/nginx/myapp.log;
        error_log /var/log/nginx/myapp.error.log;

        location / {
                proxy_pass http://myapp_app;
                proxy_redirect  off;

                proxy_set_header        Host            $http_host;
                proxy_set_header        X-Real-IP       $remote_addr;
                proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;

                client_max_body_size            10m;
                client_body_buffer_size         128k;

                proxy_connect_timeout           90;
                proxy_send_timeout              90;
                proxy_read_timeout              90;

                proxy_buffer_size               4k;
                proxy_buffers                   4 32k;
                proxy_busy_buffers_size         64k;
                proxy_temp_file_write_size      64k;
        }

        location /static/ {
                root /home/your_user/your_workarea/your_project;
        }
}
  1. Enter in cd /etc/nginx/sites-enabled
  2. Create link simbol ln -s /etc/nginx/sites-available/myapp .
  3. run nginx -t
  4. run service nginx restart

🎉 project run in port 80 check your domain

Common errors

Failed building wheel for cryptography ´sudo apt-get install build-essential libssl-dev libffi-dev python-dev´ settings --setting django

References

https://www.digitalocean.com/community/tutorials/how-to-install-the-django-web-framework-on-ubuntu-14-04

https://www.digitalocean.com/community/tutorials/how-to-set-up-django-with-postgres-nginx-and-gunicorn-on-ubuntu-14-04

https://www.digitalocean.com/community/tutorials/how-to-install-and-use-postgresql-on-ubuntu-16-04

https://www.digitalocean.com/community/tutorials/how-to-create-remove-manage-tables-in-postgresql-on-a-cloud-server

@QuanSai
Copy link

QuanSai commented Apr 23, 2018

This gist saved my life.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment