Deploying on Linux
Deploying A Lona Script
In this section we will deploy a simple Lona Script using systemd and Apache2 as reverse proxy
Lona
# installing the necessary debian/ubuntu packages
$ sudo apt install apache2 python3 python3-venv
# setup /srv/lona and python virtualenv
$ sudo mkdir -p /srv/lona
$ sudo python3 -m venv /srv/lona/env
$ source /srv/lona/env/bin/activate
$ (env) pip install lona
# setup lona script
$ sudo touch /srv/lona/my-script.py
# change ownership of /srv/lona to www-data
$ sudo chown -R www-data:www-data /srv/lona
# /srv/lona/my-script.py
from lona.html import HTML, Button, Div, H1
from lona import LonaApp, LonaView
app = LonaApp(__file__)
app.settings.MAX_RUNTIME_THREADS = 50
app.settings.MAX_WORKER_THREADS = 100
app.settings.MAX_STATIC_THREADS = 20
@app.route('/')
class MyView(LonaView):
def handle_request(self, request):
message = Div('Button not clicked')
button = Button('Click me!')
html = HTML(
H1('Click the button!'),
message,
button,
)
self.show(html)
# this call blocks until the button was clicked
input_event = self.await_click(button)
if input_event.node == button:
message.set_text('Button clicked')
return html
if __name__ == '__main__':
app.run(port=8080)
Apache2
$ sudo a2enmod headers rewrite proxy proxy_wstunnel proxy_http proxy_balancer lbmethod_byrequests
# /etc/apache2/sites-available/lona.conf
# needed modules: headers rewrite proxy proxy_wstunnel proxy_http proxy_balancer lbmethod_byrequests
<VirtualHost *:80>
ServerAdmin webmaster@localhost
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
# application
Header add Set-Cookie "ROUTEID=.%{BALANCER_WORKER_ROUTE}e; path=/" env=BALANCER_ROUTE_CHANGED
<Proxy balancer://httprewrite>
BalancerMember http://localhost:8080 route=1
ProxySet stickysession=ROUTEID
</Proxy>
<Proxy balancer://wsrewrite>
BalancerMember ws://localhost:8080 route=1
ProxySet stickysession=ROUTEID
</Proxy>
RewriteEngine On
RewriteCond %{HTTP:Upgrade} =websocket [NC]
RewriteRule (.*) balancer://wsrewrite%{REQUEST_URI} [P,L]
RewriteCond %{HTTP:Upgrade} !=websocket [NC]
RewriteRule (.*) balancer://httprewrite%{REQUEST_URI} [P,L]
</VirtualHost>
$ sudo a2ensite lona.conf
Systemd
# /etc/systemd/system/lona-server.service
[Unit]
Description=Lona Server
[Service]
User=www-data
Type=simple
WorkingDirectory=/srv/lona/
ExecStart=/srv/lona/env/bin/python /srv/lona/my-script.py
[Install]
WantedBy=multi-user.target
$ sudo systemctl enable lona-server.service
$ sudo systemctl start lona-server.service
Deploying A Lona Project
In this section we will deploy lona-project-template using Apache2 and systemd.
Lona collects all static file at startup once and places them in /srv/lona/static to make them available for Apache.
The Lona server service sets up a Lona Shell Server using a unix domain socket in /srv/lona/lona-project-template/lona_project for debugging and monitoring.
When the project is deployed like defined here, you can attach a Lona Shell when the server is running.
$ sudo /srv/lona/lona-project-template/env/bin/rlpython file:///srv/lona/lona-project-template/lona_project/socket
Lona
# installing the necessary debian/ubuntu packages
$ sudo apt install apache2 build-essential python3 python3-venv
# setup /srv/lona and python virtualenv
$ sudo mkdir -p /srv/lona
$ sudo mkdir -p /srv/lona/static
$ sudo git clone https://github.com/lona-web-org/lona-project-template /srv/lona/lona-project-template
$ cd /srv/lona/lona-project-template
$ sudo make env
# change ownership of /srv/lona to www-data
$ sudo chown -R www-data:www-data /srv/lona
Apache2
$ sudo a2enmod headers rewrite proxy proxy_wstunnel proxy_http proxy_balancer lbmethod_byrequests
# /etc/apache2/sites-available/lona.conf
# needed modules: headers rewrite proxy proxy_wstunnel proxy_http proxy_balancer lbmethod_byrequests
<VirtualHost *:80>
ServerAdmin webmaster@localhost
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
# static files
DocumentRoot /srv/lona/static
Alias /static /srv/lona/static
<Directory /srv/lona/static>
Require all granted
</Directory>
# application
Header add Set-Cookie "ROUTEID=.%{BALANCER_WORKER_ROUTE}e; path=/" env=BALANCER_ROUTE_CHANGED
<Proxy balancer://httprewrite>
BalancerMember http://localhost:8080 route=1
ProxySet stickysession=ROUTEID
</Proxy>
<Proxy balancer://wsrewrite>
BalancerMember ws://localhost:8080 route=1
ProxySet stickysession=ROUTEID
</Proxy>
RewriteEngine On
RewriteCond %{HTTP:Upgrade} =websocket [NC]
RewriteRule !^static($|/) balancer://wsrewrite%{REQUEST_URI} [P,L]
RewriteCond %{HTTP:Upgrade} !=websocket [NC]
RewriteRule !^static($|/) balancer://httprewrite%{REQUEST_URI} [P,L]
</VirtualHost>
$ sudo a2ensite lona.conf
Systemd
# /etc/systemd/system/lona-server.service
[Unit]
Description=Lona Server
[Service]
User=www-data
Type=simple
WorkingDirectory=/srv/lona/lona-project-template/lona_project
ExecStartPre=/srv/lona/lona-project-template/env/bin/lona collect-static /srv/lona/static/ -s settings.py deployment_settings.py --clean
ExecStart=/srv/lona/lona-project-template/env/bin/lona run-server --host=localhost --port=8080 -s settings.py deployment_settings.py --shell-server-url=file://socket
[Install]
WantedBy=multi-user.target
$ sudo systemctl enable lona-server.service
$ sudo systemctl start lona-server.service