Подготовка сырого linux сервера для python проекта
Подготовка linux сервера и установка необходимых компонентов
sudo apt-get update ; \
sudo apt-get install -y vim mosh tmux htop git \
curl wget unzip zip gcc build-essential make
sudo apt-get install -y zsh tree redis-server nginx libssl-dev zlib1g-dev \
libbz2-dev libreadline-dev libsqlite3-dev llvm libncurses5-dev libncursesw5-dev \
xz-utils tk-dev libffi-dev liblzma-dev python3-dev libffi-dev \
libssl-dev gnumeric libsqlite3-dev libpq-dev libxml2-dev libxslt1-dev \
libjpeg-dev libfreetype6-dev
sh -c "$(curl -fsSL https://raw.githubusercontent.com/robbyrussell/oh-my-zsh/master/tools/install.sh)"
Мои необязательные строки в .zshrc
ZSH_THEME="norm"
plugins=(git zsh-syntax-highlighting zsh-autosuggestions)
autoload -U compinit
compinit
alias neofetch='neofetch --ascii_distro arch'
alias hist='history'
alias gotop='gotop -b -c monokai -s'
Языковые пакеты
sudo apt-get install locales
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
Установка postgresql
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 ;
pg_ctlcluster 11 main start
Базовая настройка postgresql
sudo passwd postgres
su - postgres
export PATH=$PATH:/usr/lib/postgresql/11/bin
createdb --encoding UNICODE example_database --username postgres
exit
sudo -u postgres psql
create user www with password 'example-password';
ALTER USER www CREATEDB;
grant all privileges on database example_database to www;
\c example_database
GRANT ALL ON ALL TABLES IN SCHEMA public to www;
GRANT ALL ON ALL SEQUENCES IN SCHEMA public to www;
GRANT ALL ON ALL FUNCTIONS IN SCHEMA public to www;
CREATE EXTENSION pg_trgm;
ALTER EXTENSION pg_trgm SET SCHEMA public;
UPDATE pg_opclass SET opcdefault = true WHERE opcname='gin_trgm_ops';
\q
Настройка доступности базы данных
cd /etc/postgresql/11/main ;\
sudo vim postgresql.conf
Изменяем содержимое строки
listen_addresses = '*'
Аналогично для второго файла
sudo vim pg_hba.conf
host all www 0.0.0.0/0 password
Перезапуск сервиса для применения новой конфигурации:
sudo systemctl restart postgresql
Пример создания схемы в базе данных
create schema main_data;
GRANT ALL ON ALL TABLES IN SCHEMA main_data to www;
GRANT ALL ON ALL SEQUENCES IN SCHEMA main_data to www;
GRANT ALL ON ALL FUNCTIONS IN SCHEMA main_data to www;
GRANT USAGE ON SCHEMA main_data TO www;
commit;
Создание виртуального окружения python3
apt-get install python3-venv
python3 -m venv env
Tempalte systemd gunicorn
cd /etc/systemd/system ;\
vim example_name.service
vim example_name.service
[Unit]
Description=gunicorn daemon
After=network.target
[Service]
Environment=SECRET_KEY="your env var"
User=www
Group=www-data
WorkingDirectory=/home/www/code/service/webservice
WorkingDirectory=/home/www/path/to/your_code
ExecStart=/home/www/code/service/env/bin/gunicorn --workers 5 --threads 2 --bind unix:/home/www/code/service/env/bin/gunicorn.sock webservice.wsgi:application --bind 0.0.0.0:5000 --access-logfile /home/www/webserver.log --error-logfile /home/www/webserver_errors.log
[Install]
WantedBy=multi-user.target