• Stars
    star
    334
  • Rank 126,264 (Top 3 %)
  • Language
  • Created over 5 years ago
  • Updated over 3 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Set up clean Django server for Django projects. Nginx, Gunicorn, Django, Python 3.7, PostgreSQL, Supervisor

Debian Server Set Up for Django Instruction

In this guide we will set up clean Debian server for Python and Django projects. We will configure secure SSH connection, install from Debian repositories and from sources all needed packages and ware it together for working Debian Django server.

Youtube video guide (in Russian)

Create user, setup SSH

Connect through SSH to remote Debian server and update repositories and install some initial needed packages:

sudo apt-get update ; \
sudo apt-get install -y vim mosh tmux htop git curl wget unzip zip gcc build-essential make

Configure SSH:

sudo vim /etc/ssh/sshd_config
    AllowUsers www
    PermitRootLogin no
    PasswordAuthentication no

Restart SSH server, change www user password:

sudo service ssh restart
sudo passwd www

Init — must-have packages & ZSH

sudo apt-get install -y zsh tree redis-server nginx zlib1g-dev libbz2-dev libreadline-dev llvm libncurses5-dev libncursesw5-dev xz-utils tk-dev liblzma-dev python3-dev python-imaging python3-lxml libxslt-dev python-libxml2 python-libxslt1 libffi-dev libssl-dev python-dev gnumeric libsqlite3-dev libpq-dev libxml2-dev libxslt1-dev libjpeg-dev libfreetype6-dev libcurl4-openssl-dev supervisor

Install oh-my-zsh:

sh -c "$(curl -fsSL https://raw.githubusercontent.com/robbyrussell/oh-my-zsh/master/tools/install.sh)"

Configure some needed aliases:

vim ~/.zshrc
    alias cls="clear"

Install python 3.7

mkdir ~/code

Build from source python 3.7, install with prefix to ~/.python folder:

wget https://www.python.org/ftp/python/3.7.3/Python-3.7.3.tgz ; \
tar xvf Python-3.7.* ; \
cd Python-3.7.3 ; \
mkdir ~/.python ; \
./configure --enable-optimizations --prefix=/home/www/.python ; \
make -j8 ; \
sudo make altinstall

Now python3.7 in /home/www/.python/bin/python3.7. Update pip:

sudo /home/www/.python/bin/python3.7 -m pip install -U pip

Ok, now we can pull our project from Git repository (or create own), create and activate Python virtual environment:

cd code
git pull project_git
cd project_dir
python3.7 -m venv env
. ./env/bin/activate

Install and configure PostgreSQL

Install PostgreSQL 11 and configure locales.

wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add - ; \
RELEASE=$(lsb_release -cs) ; \
echo "deb http://apt.postgresql.org/pub/repos/apt/ ${RELEASE}"-pgdg main | sudo tee  /etc/apt/sources.list.d/pgdg.list ; \
sudo apt update ; \
sudo apt -y install postgresql-11 ; \
sudo localedef ru_RU.UTF-8 -i ru_RU -fUTF-8 ; \
export LANGUAGE=ru_RU.UTF-8 ; \
export LANG=ru_RU.UTF-8 ; \
export LC_ALL=ru_RU.UTF-8 ; \
sudo locale-gen ru_RU.UTF-8 ; \
sudo dpkg-reconfigure locales

Add locales to /etc/profile:

sudo vim /etc/profile
    export LANGUAGE=ru_RU.UTF-8
    export LANG=ru_RU.UTF-8
    export LC_ALL=ru_RU.UTF-8

Change postges password, create clear database named dbms_db:

sudo passwd postgres
su - postgres
export PATH=$PATH:/usr/lib/postgresql/11/bin
createdb --encoding UNICODE dbms_db --username postgres
exit

Create dbms db user and grand privileges to him:

sudo -u postgres psql
postgres=# ...
create user dbms with password 'some_password';
ALTER USER dbms CREATEDB;
grant all privileges on database dbms_db to dbms;
\c dbms_db
GRANT ALL ON ALL TABLES IN SCHEMA public to dbms;
GRANT ALL ON ALL SEQUENCES IN SCHEMA public to dbms;
GRANT ALL ON ALL FUNCTIONS IN SCHEMA public to dbms;
CREATE EXTENSION pg_trgm;
ALTER EXTENSION pg_trgm SET SCHEMA public;
UPDATE pg_opclass SET opcdefault = true WHERE opcname='gin_trgm_ops';
\q
exit

Now we can test connection. Create ~/.pgpass with login and password to db for fast connect:

vim ~/.pgpass
	localhost:5432:dbms_db:dbms:some_password
chmod 600 ~/.pgpass
psql -h localhost -U dbms dbms_db

Run SQL dump, if you have:

psql -h localhost dbms_db dbms  < dump.sql

Install and configure supervisor

Now recommended way is using Systemd instead of supervisor. If you need supervisor — welcome:

sudo apt install supervisor

vim /home/www/code/project/bin/start_gunicorn.sh
	#!/bin/bash
	source /home/www/code/project/env/bin/activate
	source /home/www/code/project/env/bin/postactivate
	exec gunicorn  -c "/home/www/code/project/gunicorn_config.py" project.wsgi

chmod +x /home/www/code/project/bin/start_gunicorn.sh

vim project/supervisor.salesbeat.conf
	[program:www_gunicorn]
	command=/home/www/code/project/bin/start_gunicorn.sh
	user=www
	process_name=%(program_name)s
	numprocs=1
	autostart=true
	autorestart=true
	redirect_stderr=true

If you need some Gunicorn example config — welcome:

command = '/home/www/code/project/env/bin/gunicorn'
pythonpath = '/home/www/code/project/project'
bind = '127.0.0.1:8001'
workers = 3
user = 'www'
limit_request_fields = 32000
limit_request_field_size = 0
raw_env = 'DJANGO_SETTINGS_MODULE=project.settings'

More Repositories

1

django-clean-template

Чистый Django шаблон с nginx, gunicorn и systemd конфигами
Python
428
star
2

telegram-finance-bot

Телеграм бот для учёта личных расходов и ведения бюджета
Python
420
star
3

thanks

Система донатов для авторов
253
star
4

nvim-config

Vim Script
226
star
5

phoneinput

JavaScript
183
star
6

eng-ru-dictionary

Англо-русский словарик часто встречающихся ИТ-слов
175
star
7

botanim-bot

Telegram bot for botanim.to.digital
Python
172
star
8

knowledge-base

127
star
9

typed-python-book

Handlebars
119
star
10

weather

Python
96
star
11

tinkoff-analytics

Аналитика доходности инвестиционного портфеля в Тинькофф брокере
Python
65
star
12

youtube-google-sheets-python

Python
44
star
13

postgres-s3-backuper

Backup and restore PostgreSQL database with encryption and S3 (Yandex Object Storage)
Python
40
star
14

debian-set-up-for-php

Set up clean Django server for PHP projects. Apache with mod_php, mod_rewrite, MariaDB, PHP 7.0+
28
star
15

dnd-demo

Drag and drop trello-like board realization in Vanilla JS
JavaScript
23
star
16

youtube-python-structures

Код из видео https://www.youtube.com/watch?v=tsEG0WM3m_M
Python
19
star
17

react-typescript-component-generator

Fast Generate TypeScript React component files
Python
15
star
18

frontend-project-example

Материал для видео на Youtube
TypeScript
9
star
19

fix-donate-telegram

Python
8
star
20

tests

just for tests
3
star
21

drfjsondemo

3
star