Uwsgi

De Banane Atomic
Aller à la navigationAller à la recherche

Généralités

Web Server Gateway Interface

Installation

Bash.svg
sudo pacman uwsgi

Utilisation

Bash.svg
# lance au démarrage le service pour l'application configurée au /etc/uwsgi/owncloud.ini
sudo systemctl enable uwsgi@owncloud

Configuration

Log

/etc/uwsgi/mysite.ini
; fichier
logger = file:/var/log/uwsgi/mysite.log

; systemd
plugin = systemd_logger
logger = systemd

processes and cheaper

/etc/uwsgi/mysite.ini
; maximum number of worker processes ≈ 2 * cpucores (nproc)
processes = 8
; start with only 4 and spawn the others on demand, maintaining a minimal pool of 4 processes.
cheaper = 4

Emperor mode

Php

Bash.svg
# installation
sudo pacman -S uwsgi uwsgi-plugin-php
/etc/uwsgi/mysite.ini
plugins = php

; jail our php environment
php-docroot = /srv/http/%n

php-index = index.php

; By default, the PHP plugin will happily execute whatever script you pass to it.
; You may want to limit it to only a subset of extensions with the php-allowed-ext option.
php-allowed-ext = .php
/etc/nginx/nginx.conf
location = /index.php {
    include uwsgi_params;
    uwsgi_modifier1 14;
    uwsgi_pass unix:/run/uwsgi/mysite.sock;
}
Bash.svg
# lancer uwsgi
systemctl start uwsgi@mysite
# équivalent à « sudo uwsgi --ini /etc/uwsgi/mysite.ini »

Extensions

/etc/uwsgi/mysite.ini
# charge les extensions dans cette instance de php uniquement. Cela évite d'éditer le fichier global php.ini
php-set = extension=zip.so

Configuration php

/etc/uwsgi/mysite.ini
; modifie la configuration php pour cette instance de php. Cela évite d'éditer le fichier global php.ini
php-set = date.timezone=Etc/UTC
php-set = open_basedir=/tmp/:/usr/share/pear/
php-set = session.save_path=/tmp
php-set = post_max_size=1000M
php-set = upload_max_filesize=1000M

; masquer le header X-Powered-By PHP/x.x.x
php-set = expose_php=Off

Cache

Zend OPcache

Zend OPcache avec uwsgi et sapi = uwsgi
Opcode Caching Disabled
Optimization Disabled
Startup Failed Opcode Caching is only supported in Apache, ISAPI, FPM, FastCGI and LiteSpeed SAPIs
/etc/uwsgi/mysite.ini
; For some mysterious reason, the opcode cache is disabled in the embed SAPI.
; You can bypass the problem by telling the PHP engine that is running under the apache SAPI
php-sapi-name = apache
; php-sapi-name = fpm-fcgi
Php.svg
// par défaut avec uwsgi → sapi = uwsgi
$sapi_type = php_sapi_name();
// avec php-fpm → sapi = fpm-fcgi

Session cache

/etc/uwsgi/mysite.ini
; create a cache with 100 items, 4k per object is enough for sessions (default 64k), 128b per key (default 2048b)
cache2 = name=session,items=100,keysize=128,blocksize=4096
; set the 'uwsgi' session handler
php-set = session.save_handler=uwsgi
; use the 'session' cache for storing sessions
php-set = session.save_path=session

SSL Sessions

Object cache

/etc/uwsgi/mysite.ini
; create a cache with 100 items (default size per-item is 64k)
cache2 = name=objectcache,items=100
; cherche dans le cache un objet correspondant à la clé
route = .* cache:key=${REQUEST_URI},name=objectcache
; s'il n'existe pas, il est stocké avec sa clé dans le cache
; store each successfull request (200 http status code) in the 'objectcache' cache using the REQUEST_URI as key
route = .* cachestore:key=${REQUEST_URI},name=objectcache,expires=60

Statistiques sur le cache

/etc/uwsgi/mysite.ini
stats = /tmp/statsock
Bash.svg
# installer uwsgitop
sudo pacman -S python2-pip
pip2 install uwsgicachetop

# exécuter uwsgicachetop
uwsgicachetop /tmp/statsock

uwsgicachetop

Django

archlinux et Django

Bash.svg
sudo pacman -S uwsgi uwsgi-plugin-python python-django
/etc/uwsgi/mysite.ini
[uwsgi]
; maximum number of worker processes
processes = 4
; the user and group id of the process once it’s started
uid = http
gid = http
socket = /run/uwsgi/%n.sock
master = true
chdir = /home/django_projects/%n
; Django's wsgi file
module = %n.wsgi
plugins = python
; clear environment on exit
vacuum = true
/etc/nginx/nginx.conf
location / {
    include uwsgi_params;
    uwsgi_pass unix:/run/uwsgi/mysite.sock;
}
Bash.svg
# lancer uwsgi
systemctl start uwsgi@mysite
# équivalent à « uwsgi --ini /etc/uwsgi/mysite.ini »

Ruby on Rails

Bash.svg
# installation
sudo pacman -S uwsgi-plugin-rack
/etc/uwsgi/mysite.ini
[uwsgi]
; maximum number of worker processes
processes = 4
; the user and group id of the process once it’s started
uid = http
gid = http
socket = /run/uwsgi/%n.sock
master = true
chdir = /srv/http/%n
; php
plugins = php
; jail our php environment
php-docroot = /srv/http/%n
php-index = index.php
; clear environment on exit
vacuum = true

Exemples

Mediawiki

/etc/uwsgi/mediawiki.ini
[uwsgi]

; log
logger = file:/var/log/uwsgi/%n.log

; maximum number of worker processes
processes = 4

; the user and group id of the process once it’s started
uid = http
gid = http

master = true
socket = /run/uwsgi/%n.sock
chdir = /usr/share/webapps/%n

; clear environment on exit
vacuum = true

; php
plugins = php
php-docroot = /usr/share/webapps/%n
php-index = index.php

; extensions
php-set = extension=mysqli.so
php-set = extension=intl.so
php-set = extension=gd.so
php-set = extension=iconv.so

; php config
php-set = open_basedir=/tmp/:/usr/share/webapps/mediawiki/:/var/lib/mediawiki/:/var/cache/mediawiki/
; This option causes errors and may corrupt data unpredictably; MediaWiki will refuse to install if this option is turned on.
php-set = mbstring.func_overload = 0
; MediaWiki needs sufficiently memory to work. The minimal limit should be something like 20 MB, 
; but if you want MediaWiki to work correctly, consider using at least 50 MB.
;php-set = memory_limit = 128M
; To be able to find GNU diff3 and git during installation process this entry must not contain passthru
;php-set = disable_functions =
; masquer le header X-Powered-By PHP/x.x.x
php-set = expose_php=Off
; Active la compression transparente des pages
php-set = zlib.output_compression=On

; create a cache with 20 items, 4k per object is enough for sessions (default 64k), 128b per key (default 2048b)
cache2 = name=session,items=4,keysize=128,blocksize=4096
; set the 'uwsgi' session handler
php-set = session.save_handler=uwsgi
; use the 'session' cache for storing sessions
php-set = session.save_path=session

; create a cache with 100 items (default size per-item is 64k)
cache2 = name=objectcache,items=50
; at each request check it in the cache
route = load\.php cache:key=${REQUEST_URI},name=objectcache
; store each successfull request (200 http status code) in the 'objectcache' cache using the REQUEST_URI as key
route = load\.php cachestore:key=${REQUEST_URI},name=objectcache,expires=31536000

ownCloud