Static Site Generator

How-to: How to use Stapy with Apache HTTP Server?

How to use Stapy with Apache HTTP Server?

Stapy can be hosted on a remote server, as a proxy behind Apache. This can be usefull to:

You can use a protected subdomain to manage and preview pages, and access the published static site on the main domain.

Stapy service

Add your Stapy site in the /var/www/stapy directory. Give on the directory the permissions for the user who will launch Stapy. Create a specific user and group for Stapy if needed. Example:

chown -R stapy:stapy /var/www/stapy

On a distribution with systemd, create a new script to start Stapy:

nano /etc/init.d/stapy.sh
#!/bin/sh
python3 /var/www/stapy/stapy.py > /dev/null &

Note: On some systems the command can be python instead of python3.

chmod +x /etc/init.d/stapy.sh

Create a Unit file to define a systemd service:

nano /lib/systemd/system/stapy.service
[Service]
Description=Stapy
Type=oneshot
RemainAfterExit=yes
ExecStart=/etc/init.d/stapy.sh
User=stapy
Group=stapy

[Install]
WantedBy=multi-user.target

Note: Update the User and the Group with the appropriate values.

chmod 644 /lib/systemd/system/stapy.service

Once you have a unit file, you are ready to start Stapy:

systemctl start stapy

Check the status of the service:

systemctl status stapy

Apache vhost

We need to use Stapy service as a proxy with Apache. Add a new Apache vhost file:

<VirtualHost *:80>
    ServerAdmin webmaster@localhost

    ServerName admin.example.com

    ErrorLog ${APACHE_LOG_DIR}/stapy-error.log
    CustomLog ${APACHE_LOG_DIR}/stapy-access.log combined        

    LogLevel warn

    RewriteEngine On
    RewriteCond %{HTTP:Connection} Upgrade [NC]
    RewriteCond %{HTTP:Upgrade} websocket [NC]
    RewriteRule /(.*) ws://127.0.0.1:1985/$1 [P,L]
    ProxyRequests off
    ProxyPass / http://127.0.0.1:1985/
    ProxyPassReverse / http://127.0.0.1:1985/
    ProxyPreserveHost On
</VirtualHost>

Use Let's Encrypt to create a specific vhost for SSL.

Enable host and restart Apache. You can now access Stapy from your subdomain (e.g. http://admin.example.com, http://admin.example.com/_editor).

We recommand to protect this host with IP restriction or htpasswd.

Add now a new vhost to share the result of building your site on the main domain:

<VirtualHost *:80>
    ServerAdmin webmaster@localhost

    DocumentRoot /var/www/stapy/web/prod
    ServerName www.example.com

    <Directory /var/www/stapy/web/prod>
        Options Indexes FollowSymLinks MultiViews
    </Directory>
</VirtualHost>

Don't forget to add cache and security policies (mod_headers.c, mod_expires.c...).

Next