How to install Piwigo on a Raspberry Pi

In this posting, we will be installing the Piwigo (photo gallery), along with Nginx (lightweight web server), PHP and MySql (database package). At a later stage, we will move the Piwigo directory structure to an external hard disk and also install and configure Samba to allow the directory containing photographs to be shared with a Windows PC.

Preparation of the Raspberry Pi

  • Follow the instructions at How to install the Raspbian OS on a Raspberry Pi.
  • Change the hostname of your Raspberry Pi to something more meaningful by logging on to the device as user “pi” and issuing the command:

    sudo raspi-config

  • Select “Advanced Options” and set the following:
    • Hostname = pi-piwigo (or whatever you want to call it).
  • Select Finish
  • Reboot the Raspberry Pi

Install PHP, MySql, the Nginx web server, and other support packages

  • Create a user for the Nginx web server with the command:

    sudo groupadd www-data ; sudo usermod -a -G www-data www-data

  • Install the necessary packages with the command:


    sudo apt-get install nginx php5-fpm php5 php5-mysql php5-pgsql php5-imap php-pear php5-sqlite php5-ldap php5-gd php5-imagick php5-curl php-apc openssl ssl-cert php5-mcrypt php5-pspell php5-xmlrpc php5-xsl php5-cgi php-auth php-auth-sasl php-net-smtp mysql-server

  • Create your SSL certificates (which will last for 2 years) with the commands:


    sudo openssl req $@ -new -x509 -days 730 -nodes -out /etc/nginx/cert.pem -keyout /etc/nginx/cert.key
    sudo chmod 600 /etc/nginx/cert.pem ; sudo chmod 600 /etc/nginx/cert.key

Configure the Nginx web server

  • Open the configuration file for editing:

    sudo vi /etc/nginx/nginx.conf

  • Because the Raspberyy Pi only has 2 cores, change


    worker_processes 4;
    …to:
    worker_processes 2;

  • Change the top of the http section to:


    http {

    ##
    # Basic Settings
    ##

    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    # keepalive_timeout 65;
    types_hash_max_size 2048;
    server_tokens off;

    client_header_timeout 10;
    client_body_timeout 10;
    keepalive_timeout 10 10;
    send_timeout 10;

  • Change the Gzip section from:


    ##
    # Gzip Settings
    ##

    gzip on;
    gzip_disable "msie6";

    # gzip_vary on;
    # gzip_proxied any;
    # gzip_comp_level 6;
    # gzip_buffers 16 8k;
    # gzip_http_version 1.1;
    # gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
    …to:
    ##
    # Gzip Settings
    ##

    gzip on;
    gzip_disable "msie6";

    gzip_min_length 1100;
    gzip_vary on;
    gzip_proxied any;
    gzip_comp_level 6;
    gzip_buffers 16 8k;
    gzip_http_version 1.1;
    gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;

  • Now do the following:


    sudo vi /etc/php5/fpm/pool.d/www.conf
    …and uncomment the following two lines::
    listen.owner = www-data
    listen.group = www-data

  • Enter the following command:

    sudo vi /etc/nginx/sites-available/default

  • …and replace the entire content of the file with the code below (modified from http://doc.owncloud.org/server/5.0/admin_manual/installation/installation_others.html#nginx-configuration). Note: You’ll have to replace two occurrences of 192.168.XXX.XXX with the local IP address of your Raspberry Pi.


    server {
    listen 80;
    server_name 192.168.XXX.XXX;
    return 301 https://$server_name$request_uri; # enforce https
    }

    server {
    listen 443 ssl;
    server_name 192.168.XXX.XXX;

    ssl_certificate /etc/nginx/cert.pem;
    ssl_certificate_key /etc/nginx/cert.key;

    # Path to the root of your installation
    root /var/www/piwigo;

    client_max_body_size 1000M; # set max upload size
    fastcgi_buffers 64 4K;

    index index.php;
    error_page 403 /core/templates/403.php;
    error_page 404 /core/templates/404.php;

    location = /robots.txt {
    allow all;
    log_not_found off;
    access_log off;
    }

    location ~ ^/(data|config|\.ht|db_structure\.xml|README) {
    deny all;
    }

    location / {
    # The following 2 rules are only needed with webfinger
    rewrite ^/.well-known/host-meta /public.php?service=host-meta last;
    rewrite ^/.well-known/host-meta.json /public.php?service=host-meta-json last;

    rewrite ^(/core/doc/[^\/]+/)$ $1/index.html;

    try_files $uri $uri/ index.php;
    }

    location ~ ^(.+?\.php)(/.*)?$ {
    try_files $1 = 404;

    include fastcgi_params;
    fastcgi_param SCRIPT_FILENAME $document_root$1;
    fastcgi_param PATH_INFO $2;
    fastcgi_param HTTPS on;
    fastcgi_pass 127.0.0.1:9000;
    # Or use unix-socket with 'fastcgi_pass unix:/var/run/php5-fpm.sock;'
    }

    # Optional: set long EXPIRES header on static assets
    location ~* ^.+\.(jpg|jpeg|gif|bmp|ico|png|css|js|swf)$ {
    expires 30d;
    # Optional: Don't log access to assets
    access_log off;
    }

    }

  • Enter the following command (if you don’t do this, you will get a 502 bad Gateway error when you later try to connect with your browser):


    sudo vi /etc/php5/fpm/pool.d/www.conf

    Change the following line from:


    listen = /var/run/php5-fpm.sock

    …to:


    listen = 127.0.0.1:9000

Test the web server

  • Create a directory for Piwigo with\;


    sudo mkdir -p /var/www/piwigo
    sudo chown -R www-data:www-data /var/www

  • Create a test file with:


    sudo vi /var/www/piwigo/test.php

    …and paste in the following:


    <?php
    phpinfo();
    ?>

    …then save and exit

  • Restart the web server and Php with the commands:


    sudo /etc/init.d/php5-fpm restart
    sudo /etc/init.d/nginx restart

  • Point your browser at the https://192.168.XXX.XXX/test.php page and check that it works.
  • If it does, you can delete the test page now:

    sudo rm -f /var/www/piwigo/test.php

Prepare for Piwigo installation

Create a database and user

  • Create a database and user with (obviously you need to change PASSWORD with the appropriate password):


    sudo mysql -u root -p
    create database piwigo; grant all on piwigo.* to 'gallery'@'localhost' identified by 'PASSWORD'; flush privileges; \q;

  • Improve the Mysql security by running the command:


    sudo mysql_secure_installation

    …and responding appropriately to the prompts.

Get the netinstall-file for Piwigo

  • Get the netinstall-file for Piwigo with:


    cd /var/www/piwigo
    sudo wget http://piwigo.org/download/dlcounter.php?code=netinstall -O netinstall.php
    sudo chown www-data:www-data netinstall.php

Configuration tweaks

  • Enter the following command to edit the file:


    sudo vi /etc/php5/fpm/php.ini

    …and set:


    upload_max_filesize = 1000M
    post_max_size = 1000M
    max_file_uploads = 20

    At the end of the file, add  the following:


    upload_tmp_dir = /tmp
    extension = apc.so
    apc.enabled = 1
    apc.include_once_override = 0
    apc.shm_size = 256

  • Enter the following command to edit the file:


    sudo vi /etc/nginx/nginx.conf

    …and set:


    client_max_body_size 20M;
    client_body_buffer_size 128k;

  • Restart the web server and Php with the commands:


    sudo /etc/init.d/php5-fpm restart
    sudo /etc/init.d/nginx restart

Install Piwigo

Optional steps

Enabling IPTC support in Piwigo, and copying XMP tags into IPTC

Before transferring my photographs across to Piwigo, I like to add tags/keywords to them using Windows Live Photo Gallery on my Windows PC. The problem with this, is that Live Photo Gallery saves the tags in XMP fields, and Piwigo can only handle the tags in the IPTC fields. Therefore, it’s necessary to copy the XMP tags into the IPTC fields for each photograph. For this, I use exiv2 on pi-piwigo.

  • Update the packages:

    sudo apt-get update

  • Install exiv2 on your Raspberry Pi with:

    sudo apt-get install exiv2

  • You now need to configure Piwigo to be able to use IPTC tags. To do this, sign in to http://192.168.XXX.XXX as your Admin user and install and activate the “LocalFiles Editor” plugin. Then go to:


    Administration > Plugins > LocalFiles Editor > Local config

    …and paste in the following:


    <?php
    $conf['use_iptc'] = true;

    $conf['use_iptc_mapping'] = array(
    'keywords' => '2#025',
    );
    ?>

Once this has been done, you can run the commands that will copy the XMP fields into the IPTC fields of your photographs. You can write a script to do this, but in the commands below we modify the photographs directory by directory.

  • Change to the directory containing the photographs:

    cd /var/www/piwigo/galleries/Digital/1999/199911

  • Make sure that the temporary script we create for each run doesn’t already exist:

    rm /tmp/updateTags.sh

  • Create a temporary script which contains all the information we need to modify the photographs in the current directory:


    for f in *.@([Jj][Pp][Gg]); do echo "exiv2 -M\"set Iptc.Application2.Keywords String " `exiv2 -PXt -gXmp.dc.subject $f 2>/dev/null | sed s/", "/";"/g` "\" $f" >> /tmp/updateTags.sh; done ; sudo chmod 777 /tmp/updateTags.sh

  • If you like, you can inspect the script to make sure everything looks correct before we run it:

    less /tmp/updateTags.sh

  • If you are happy with it, run the script:

    sudo /tmp/updateTags.sh

  • Make sure that all the files still have the correct ownership:

    sudo chown www-data:www-data *

  • Delete our temporary script:

    rm /tmp/updateTags.sh

To force Piwigo to read these tags, perform a metadata synchronization.

Sharing the photographs directory with a PC using samba

As well as being able to view the photographs on Piwigo, I like to be able to view them in Windows Live Photo Gallery too. For this reason, in the next set of instructions, we are going to install samba, which will allow us to share the relevant directory on pi-piwigo with other computers on the network. This directory will be configured as read only for Windows user “smith”, but user www-data will be able to write to it.

  • Update the packages:

    sudo apt-get update

  • Install samba on the Raspberry Pi:

    sudo apt-get install samba samba-common-bin

  • Change into the directory containg the samba configuration file:

    cd /etc/samba/

  • Backup the original configuration file:

    sudo mv smb.conf backsmb.conf

  • Edit the samba configuration file with:


    sudo vi smb.conf

    …and paste the following, changing the workgroup (XXX) as appropriate\;


    #
    [global]
    workgroup = XXX
    server string = My Samba Share %v
    security = user
    map to guest = bad user
    dns proxy = no
    hosts deny = ALL
    hosts allow = 192.168.0.
    #
    [SharePiwigo]
    comment = This is a folder containing photos
    path = /var/www/piwigo/galleries
    browsable = yes
    guest ok = no
    read only = yes
    writeable = no
    valid users = smith www-data

  • Restart samba with:

    sudo service samba restart

  • Create a user on the Raspberry Pi, with the same name as the Windows user you intend to connect with:

    sudo adduser smith
    You will be prompted for a password, which can be anything you like.

  • Create a user on the Raspberry Pi which will be used by samba. The name should be the same as the one you created in the last step, and match the name of the Windows user:

    sudo smbpasswd -a smith
    You will be prompted for a password, which should be the same as the one the user uses on Windows. This will prevent them having to sign in when they access the share.

  • Create another samba user, this time for www-data. If you connect to samba as this user, you will be able to write files to the share, because the filesystem below the mount point is owned by www-data:

    sudo smbpasswd -a www-data
    You will be prompted for a password.

  • Restart samba with:

    sudo service samba restart

You should now be able to browse to this share using your Windows PC.

Transferring the /var/www/piwigo filesystem to an external USB hard disk

Storing the photographs on the Raspberry Pi’s SD card is not an ideal choice, as we want to reduce writes to the SD card as much as possible (to prolong its life), and it is an expensive option in terms of cost per megabyte. It is relatively easy to add a USB hard disk to the Raspberry Pi. More detailed information can be found at…, but here is a summary of the steps I performed.

  • Connect the drive (which should be mains powered as the Raspberry Pi cannot provide it with enough power via USB) to the Raspberry Pi using a USB cable. To determine which device it is recognized as, enter:


    sudo fdisk -l

    In my case, it was recognized as /dev/sda1

  • Create a temporary mount point for the drive:

    sudo mkdir /mnt/USB

  • Change the ownership of the mount point to www-data:

    sudo chown -R www-data:www-data /mnt/USB

  • Completely clear out the disk and create a new ext4 filesystem on it. You will lose any existing data on the drive, and you will no longer be able to attach it to a Windows PC (unless you reformat it):


    sudo mkfs.ext4 /dev/sda1 -L pi-piwigo_USB

    Note that we have given it a label of “pi-piwigo-USB”

  • We can now mount the disk to our temporary mount point with:

  • To test that it is recognized, type in the following to check the disk usage on each drive:

    df -h

  • If all is well, we can copy the content of the /var/www/piwigo directory to the USB drive, and then delete it from the original location:


    cd /var/www/piwigo
    sudo cp -pR * /mnt/USB/
    sudo rm -Rf *

  • We sould configure the Raspberry Pi to automount the USB drive whenever it boots up. To avoid any risk of multiple USB drives being confused and switched over, we should identify the unique ID assigned to the drive and mount that. To get the ID, enter:


    sudo ls -laF /dev/disk/by-uuid/

    In my case, the drive ID is 76a9e7e1-1218-4ba2-9c9d-3bda2e78d610

  • To get the drive to automount, we need to edit the fstab file:


    sudo vi /etc/fstab

    ...and add the following (obviously changing the ID as appropriate):


    UUID=76a9e7e1-1218-4ba2-9c9d-3bda2e78d610 /mnt/USB ext4 rw,defaults 0 0

    Save and exit the file.

  • Reboot the Raspberry Pi with:

    sudo reboot

  • On rebooting, check that /var/www/piwigo contains the files, and that the ownership of the files is www-data:

    ls -l /var/www/piwigo

  • If all is well, delete the temporary mount point:

    sudo rmdir /mnt/USB

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s