Deploy¶
There are several options for sanic server deployment:
- Standalone server 
- Running a pool of backend servers behind of nginx, HAProxy or other reverse proxy server 
Every method has own benefits and disadvantages.
Running sanic servers behind nginx makes several advantages:
- At first, nginx is the perfect frontend server. It may prevent many attacks based on malformed http protocol etc. 
- Second, running several sanic instances behind nginx allows to utilize all CPU cores. 
- Third, nginx serves static files much faster than built-in sanic static file support. 
But this way requires more complex configuration.
To run servers one needs to:
- copy the systemd files to /etc/systemd/system: - sudo cp ./deploy/systemd-luna-python-matcher@.service /etc/systemd/system/ sudo cp ./deploy/systemd-luna-python-matcher-proxy@.service /etc/systemd/system/ 
- change copied files’ in /etc/systemd/system/ (venv, paths, etc.) 
- reload daemons: - sudo systemctl daemon-reload 
- run luna-python-matcher application: - sudo systemctl start systemd-luna-python-matcher@5098..5099 
- run proxy service with a different port: - sudo systemctl start systemd-luna-python-matcher-proxy@5111 
Template configuration files for nginx are presented below. For more information, you can visit sanic.readthedocs.io.
Configuration file for nginx¶
upstream aiohttp {
    server 127.0.0.1:5099 fail_timeout=0;
    server 127.0.0.1:5098 fail_timeout=0;
}
server {
    listen 5100;
    client_max_body_size 4G;
    server_name 127.0.0.1;
    location / {
      proxy_set_header Host $http_host;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_redirect off;
      proxy_buffering off;
      proxy_pass http://aiohttp;
    }
}
Configuration file for systemd¶
Config should be located in directory /etc/systemd/system/
Python-matcher service config:
[Unit]
Description=Luna-Python-Matcher Application on port %i
After=network.target
[Service]
Restart=always
RestartSec=5
User=luna
Group=luna
SyslogIdentifier=luna-python-matcher-app-%i
WorkingDirectory=/var/lib/luna/current/luna-python-matcher/luna_python_matcher
# Run from luna-configurator
ExecStart=/var/lib/luna/current/luna-python-matcher/venv/bin/python ./run.py --port=%i --workers=1 --config-reload=0 --log_suffix=%i --luna-config=http://127.0.0.1:5070/1
# Run from config file
# /var/lib/luna/current/luna-python-matcher/venv/bin/python ./run.py --port=%i --workers=1 --config-reload=0 --log_suffix=%i --config=./configs/config.conf
[Install]
WantedBy=multi-user.target
Python-matcher proxy service config:
[Unit]
Description=Luna-Python-Matcher-Proxy Application on port %i
After=network.target
[Service]
Restart=always
RestartSec=5
User=luna
Group=luna
SyslogIdentifier=luna-python-matcher-proxy-app-%i
WorkingDirectory=/var/lib/luna/current/luna-python-matcher/luna_python_matcher
# Run from luna-configurator
ExecStart=/var/lib/luna/current/luna-python-matcher/venv/bin/python ./run.py --port=%i --service-type=proxy --workers=1 --config-reload=0 --luna-config=http://127.0.0.1:5070/1 --log_suffix=%i
# Run from config file
# /var/lib/luna/current/luna-python-matcher/venv/bin/python ./run.py --port=%i --service-type=proxy --workers=1 --config-reload=0 --log_suffix=%i --config=./configs/config.conf
[Install]
WantedBy=multi-user.target