Skip to navigation

Setting up a Pyplate server

Pyplate admin area

Start by installing Raspbian on an SD card, and changing the default settings.  You can set up the operating system on a hard disk if you need your server to run as fast as possible, but this isn't necessary.  I'm just using SD cards in my servers.

Edit the interfaces file

I'm going to give each server a static IP address.  I could use DHCP, but then it would be difficult to tell which server has which IP address.  There are only four servers, and I only have to set the IP address once for each server.  

Open the interfaces file with this command:

sudo nano /etc/network/interfaces

On the line that starts with 'iface eth0', the word 'dhcp' needs to be replaced with the word 'static'.  You also need to add an IP address and netmask.  The interfaces file should look like this:

auto lo

iface lo inet loopback
iface eth0 inet static

allow-hotplug wlan0
iface wlan0 inet manual
wpa-roam /etc/wpa_supplicant/wpa_supplicant.conf
iface default inet dhcp

To save changes, press control-o and press return.  To close the file, press control-x.

Install Nginx and Pyplate

I'm using Nginx because it has a reputation for serving static content quickly.  Pyplate's page caching mechanism generates static versions of pages, so Pyplate sites are mostly made of static content.  Nginx only serves static files, so it must be used with a WSGI server.  

uWSGI is a server which runs in parallel with Nginx.  Any request that Nginx can't handle by serving a static file is passed to the uWSGI server.  The uWSGI server executes the main script in Pyplate, and returns the result to Nginx.

Install Nginx and some packages needed by uWSGI and Pyplate:

sudo apt-get install nginx python-libxml2 build-essential python-dev

Download and run the Pyplate installation script for Nginx:

curl | sudo bash

Edit the webserver's crontab file:

sudo crontab -e -u www-data

Paste the following command at the end of the file, save the changes, and close the file.

@reboot /usr/share/pyplate/uwsgi /usr/share/pyplate/uwsgi_config.ini

In many Linux distributions the nano editor is used to edit crontab files.  To save changes, press control-o and press return.  To close the file, press control-x.

Back up Nginx's default config file:

sudo cp /etc/nginx/sites-available/default /etc/nginx/sites-available/default.backup

Replace it with the sample config file

sudo cp /usr/share/pyplate/sample_configs/nginx/default /etc/nginx/sites-available/default

Make sure that you've made a note of the password generated by the installation script, and then delete this file:

sudo rm /usr/share/pyplate/wsgi-scripts/

Restart your server

sudo reboot

Login, and change the password to something more memorable than the random string used as a temporary password.

Note that Pyplate can also be installed with Apache or a simple Python webserver.

Tuning Nginx

I did a few simple tests to improve performance.  I did these tests using an ethernet connection rather than wifi.  I used this command to bombard my Banana Pi with HTTP GET requests:

siege -d1 -c350 -t1m

When I tried using more than 350 concurrent users, Nginx started dropping requests.  With no tuning, Nginx handles 407.2 transactions per second.  

Linux sets a limit on the number of file descriptors that a process can open.  There is a hard limit which can be set by root, and there is a soft limit that can be adjusted by users.  Users can't increase the soft limit beyond the hard limit.  

You can find your server's hard limit by typing this command:

ulimit -n

Open /etc/nginx/nginx.conf in a text editor and find worker_connections.  Increase this value to 1024 (or whatever number is printed out by the ulimit command).  When I did this, I ran siege again, and this time the server handled 426.37 transactions per second.

I reduced the number of Nginx worker threads from 4 (default) to 2.  Nginx only needs one worker process for each CPU, and adding extra processes doesn't increase performance. Extra Nginx worker processes are generally considered to be a harmless waste of memory, however, when I retested the server's performance with only two worker processes, I found that getting rid of the spare processes improved performance.  This time the server handled 443.96 transactions per second. 

It's also recommended that you reduce the keepalive_timeout value from 65 to 30, although this didn't make any noticeable difference to performance.

See also: Install Pyplate.

Share this page:

comments powered by Disqus