Настройка сервера под Python проекты

Матеріал з Wiki.Zina
Перейти до навігації Перейти до пошуку

Собираем python

[ред. | ред. код]
cd /opt
wget https://www.python.org/ftp/python/2.7.12/Python-2.7.12.tar.xz
tar -xf Python-2.7.12.tar.xz
rm Python-2.7.12.tar.xz
mv Python-2.7.12 python-2.7.12
cd python-2.7.12/
PYTHON_VERSION=2.7.12
PYTHONPATH=/opt/python-${PYTHON_VERSION}
./configure --enable-shared --with-system-ffi --with-system-expat --enable-unicode=ucs4 --prefix=${PYTHONPATH} LDFLAGS="-L${PYTHONPATH}/extlib/lib -Wl,--rpath=${PYTHONPATH}/lib -Wl,--rpath=${PYTHONPATH}/extlib/lib" CPPFLAGS="-I${PYTHONPATH}/extlib/include"
make
make install

Собираем uwsgi вебсервер

[ред. | ред. код]
cd /opt
git clone https://github.com/unbit/uwsgi.git
cd uwsgi/
make PROFILE=nolang
PYTHON=/opt/python-2.7.12/bin/python ./uwsgi --build-plugin "plugins/python python27"
./uwsgi --build-plugin "plugins/gevent"

Собираем php если нужно

[ред. | ред. код]
cd /opt
wget http://ua2.php.net/distributions/php-5.3.29.tar.gz && tar -xf php-5.3.29.tar.gz && rm php-5.3.29.tar.gz && cd php-5.3.29/
./configure --enable-debug --prefix=/opt/php-5.3.29 --enable-exif --enable-ftp --with-gd --with-mcrypt --with-mysqli --with-mysql --with-pdo-mysql --enable-embed --enable-mbstring --with-curl --enable-zip --with-zlib --with-jpeg-dir=/usr/lib64
make -j
make install
cd /opt/uwsgi
LD_LIBRARY_PATH=/opt/php-7.0.11/lib LIBRARY_PATH=/opt/php-7.0.11/lib UWSGICONFIG_PHPDIR=/opt/php-7.0.11 /usr/bin/python uwsgiconfig.py --plugin plugins/php default php70
LD_LIBRARY_PATH=/opt/php-5.6.26/lib LIBRARY_PATH=/opt/php-5.6.26/lib UWSGICONFIG_PHPDIR=/opt/php-5.6.26 /usr/bin/python uwsgiconfig.py --plugin plugins/php default php56
LD_LIBRARY_PATH=/opt/php-5.3.29/lib LIBRARY_PATH=/opt/php-5.3.29/lib UWSGICONFIG_PHPDIR=/opt/php-5.3.29 /usr/bin/python uwsgiconfig.py --plugin plugins/php default php53

Конфигурация uwsgi

[ред. | ред. код]

Делаем init скрипт.

cd /etc/init
vim uwsgi.conf
```
#emperor startup script
description "uWSGI Emperor"
start on runlevel [2345]
stop on runlevel [06]
exec /opt/uwsgi/uwsgi --binary-path /opt/uwsgi/uwsgi --emperor /etc/uwsgi/apps --emperor-stats :1717 --die-on-term --uid apache --gid apache --logto /var/log/uwsgi/emperor.log
```
mkdir /var/log/uwsgi
mkdir /etc/uwsgi/ /etc/uwsgi/apps

Запуск сайта

[ред. | ред. код]
su -s /bin/bash torgashovocom
cd ~/www/torgashovo.com
virtualenv -p /opt/python-2.7.12/bin/python venv
#в файле прописуем зависимости проекта в формате pip
vim requerments.txt
```
Django==1.10
django-cleanup==0.4.2
django-constance==1.2
django-crispy-forms==1.6.0
django-debug-toolbar==1.5
django-filter==0.14.0
django-picklefield==0.3.2
django-pipeline==1.6.8
django-rosetta==0.7.12
git+https://github.com/shmel210/django.js#egg=django.js
easy-thumbnails==2.3
Fabric==1.12.0
gevent==1.1.2
gevent-socketio==0.3.6
gevent-websocket==0.9.5
greenlet==0.4.10
jsonate==0.4.0
jsonfield==1.0.3
lorem-ipsum-generator==0.3
lxml==3.6.1
MySQL-python==1.2.5
Pillow==3.3.0
pytils==0.3
pytz==2016.6.1
requests==2.11.0
six==1.10.0
socketIO-client==0.7.0
transliterate==1.8.1
websocket-client==0.37.0
```
venv/bin/pip install -r requerments.txt

mkdir -m 777 tmp logs tasks

vim .gitignore
```
node_modules/**
*.pyc
*.sock
*.pid
/venv/*
/public/**
/cache/*
/logs/*
/tmp/*
/tasks/*
.idea
/src/torgashovo/local_settings.py
*.sublime-*
/conf/local_uwsgi.ini
nohup.out
*___jb_bak___
*.bak
/webstat/*
.~lock.*
*.swp
!.gitkeep
/fabric_settings.json

```

#возвращаемся  к пользователю root
Ctrl+D
cd /etc/uwsgi/apps
vim torgashovo.ini 
```
[uwsgi]
plugins-dir = /opt/uwsgi
plugin = python27
# variables
project_name = torgashovo
project_domain = %(project_name).com
root = /var/www/torgashovocom/data/www/torgashovo.com
`
# main config
venv = %(root)/venv
chdir = %(root)/src
pythonpath = %(root)/src
module = %(project_name).wsgi:application
uid = apache
gid = apache
master = true
lang = ru_RU.utf8
env = LANG=ru_RU.utf8


# control
pidfile = %(root)/tmp/%(project_name).pid
socket = %(root)/tmp/%(project_name).sock
stats = %(root)/tmp/%(project_name).stats.sock
#chown-socket = apache:apache
touch-reload = %(root)/logs/reload.txt
logto = %(root)/logs/uwsgi.log
vacuum = true

# perfomance
processes = %k
enable-threads = true
threads = 4
cache2 = name=%(project_name),items=1000
spooler = %(root)/tasks
spooler-import = %(root)/src/notifications/tasks.py
harakiri = 20
max-requests = 5000

# async
offload-threads = %k
attach-daemon = %(root)/venv/bin/python %(root)/src/manage.py runserver_socketio

#cron jobs
```

#настраиваем nginx (фронтенд сервер). Идем /manager/ www домены-выбрать домен-конфиг (иконка с права вверху) - nginx
```
    server {
        server_name torgashovo.com www.torgashovo.com;
        listen 77.120.120.73;
        charset UTF-8;
        set $root_path /var/www/torgashovocom/data/www/torgashovo.com;
        root $root_path;
        access_log /var/www/httpd-logs/torgashovo.com.access.log;
        error_log /var/www/httpd-logs/torgashovo.com.error.log notice;
        location /public/ {
            access_log off;
            expires 10d;
        }
        location /static/ {
            alias $root_path/public/static/;
            access_log off;
            expires 10d;
        }
        location /media/ {
            alias $root_path/public/media/;
            access_log off;
            expires 10d;
        }
        location /robots.txt {
            access_log off;
        }
        location / {
            include uwsgi_params;
            uwsgi_pass unix://$root_path/tmp/torgashovo.sock;
            proxy_set_header Host $host;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Forwarded-Proto $scheme;
            proxy_set_header X-Real-IP $remote_addr;
        }
        location ^~ /webstat/ {
            auth_basic "Restricted area";
            auth_basic_user_file /var/www/torgashovocom/data/etc/114032647.passwd;
            try_files $uri @fallback;
        }
        include /usr/local/ispmgr/etc/nginx.inc;
    }
```

#запускаем сервер
initctl stop uwsgi
initctl start uwsgi