Piwigo

Updated 18 March 2020

Piwigo

Introduction

Piwigo is an open source CMS for creating picture galleries With free extensions, it is easily customizable for specific tasks. Piwigo is written in PHP and designed for using MySQL or MariaDB.

Configuring a LXC container

We recommend installing Piwigo in a container, configured according to the manual.

Installing and configuring MariaDB

Install and configure MariaDB as explained in the manual. Replace dbtest with the piwigo database, and test with piwigouser.

Installing and configuring Nginx

Install and configure the Nginx web server and configure PHP-FPM as described in the manual.

Installing Piwigo

Here is how to install Piwigo from source.

Downloading and extracting Piwigo

Install ImageMagick to handle different image formats:

emerge -a media-gfx/imagemagick

Warning

Make sure you do not have dev-php/pecl-imagick installed on your system, as it may result in failing to create thumbnail images.

You will need unzip to extract the Piwigo files. Install it if necessary:

emerge -a app-arch/unzip

Now you can download and unpack Piwigo. Then create the necessary paths:

mkdir /var/calculate/www

cd /var/calculate/www

curl -o piwigo.zip http://piwigo.org/download/dlcounter.php?code=latest

unzip piwigo.zip

rm piwigo.zip

Set the privileges:

chown -R nginx. piwigo

chmod o-w -R piwigo

Configuring Nginx for Piwigo

Set up Nginx service for domain piwigo.example.org:

/etc/nginx/sites-enabled/piwigo.example.org.conf
server {
    listen        80;

    server_name piwigo.example.org;

    # It is best to place the root of the server block at the server level, and not the location level
    # any location block path will be relative to this root.

    root /var/calculate/www/piwigo;

    # It's always good to set logs, note however you cannot turn off the error log
    # setting error_log off; will simply create a file called 'off'.
    access_log /var/log/nginx/piwigo.access_log;
    error_log /var/log/nginx/piwigo.error_log;

    # This can also go in the http { } level
    index index.html index.htm index.php;

    location / {
            # if you're just using wordpress and don't want extra rewrites
            # then replace the word @rewrites with /index.php
            try_files $uri $uri/ @rewrites;
    }

    location @rewrites {
            # Can put some of your own rewrite rules in here
            # for example rewrite ^/~(.*)/(.*)/? /users/$1/$2 last;
            # If nothing matches we'll just send it to /index.php
            rewrite ^ /index.php last;
    }

    # This block will catch static file requests, such as images, css, js
    # The ?: prefix is a 'non-capturing' mark, meaning we do not require
    # the pattern to be captured into $1 which should help improve performance
    location ~* \.(?:ico|css|js|gif|jpe?g|png)$ {
            # Some basic cache-control for static files to be sent to the browser
            expires max;
            add_header Pragma public;
            add_header Cache-Control "public, must-revalidate, proxy-revalidate";
    }

    # remove the robots line if you want to use wordpress' virtual robots.txt
    location = /robots.txt { access_log off; log_not_found off; }
    location = /favicon.ico { access_log off; log_not_found off; }

    # this prevents hidden files (beginning with a period) from being served
    location ~ /\. { access_log off; log_not_found off; deny all; }

    location ~ \.php {
            fastcgi_param        SCRIPT_NAME $fastcgi_script_name;
            fastcgi_param        SCRIPT_FILENAME $document_root$fastcgi_script_name;
            fastcgi_index        index.php;
            fastcgi_pass         unix:/run/php-fpm.socket;
            include fastcgi_params;
    }
}

Configuring PHP-FPM

Set the PHP-FPM environment variables:

/etc/php/fpm-php7.3/fpm.d/www.conf
; Pass environment variables like LD_LIBRARY_PATH. All $VARIABLEs are taken from
; the current environment.
; Default Value: clean env
env[HOSTNAME] = $HOSTNAME
env[PATH] = /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/opt/bin
env[TMP] = /tmp
env[TMPDIR] = /tmp
env[TEMP] = /tmp

Configure file download settings for PHP-FPM:

/etc/php/fpm-php7.3/php.ini
; Temporary directory for HTTP uploaded files (will use system default if not
; specified).
; http://php.net/upload-tmp-dir
upload_tmp_dir = /var/calculate/tmp

; Maximum allowed size for uploaded files.
; http://php.net/upload-max-filesize
upload_max_filesize = 100M

; Maximum number of files that can be uploaded via a single request
max_file_uploads = 20

; Maximum size of POST data that PHP will accept.
; Its value may be 0 to disable the limit. It is ignored if POST data reading
; is disabled through enable_post_data_reading.
; http://php.net/post-max-size
post_max_size = 100M

Restart Nginx and PHP-FPM to validate:

nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
/etc/init.d/nginx reload
nginx | * Checking nginx configuration ...                                    [ ok ]
nginx | * Refreshing nginx configuration ...                                  [ ok ]
/etc/init.d/php-fpm restart
php-fpm | * Stopping PHP FastCGI Process Manager ...                          [ ok ]
php-fpm | * Testing PHP FastCGI Process Manager config ...                    [ ok ]
php-fpm | * Starting PHP FastCGI Process Manager ...                          [ ok ]

Configuring Piwigo

Add the 192.168.0.1 computer belonging to piwigo.example.orgto the DNS server on the local network, or edit the following line:

/etc/hosts
192.168.0.1 piwigo.example.org

Open http://piwigo.example.org in your browser to complete Piwigo configuration.

Configuring Piwigo

HTTPS configuration

Getting Let's Encrypt certificate

Obtain the Nginx domain certificate for piwigo.example.org, as described in the manual.

HTTPS support configuration in Nginx

Configure Nginx for HTTPS support, as described in the manual.

HTTPS configuration for Nextcloud

Set up Nginx service for domain piwigo.example.org:

/etc/nginx/sites-enabled/piwigo.example.org.conf
server {
    listen        80;
    server_name piwigo.example.org;
    rewrite ^ https://$server_name$request_uri? permanent;
}

server {
        listen        443 default ssl;

        server_name piwigo.example.org;

        ssl_certificate        /etc/letsencrypt/live/piwigo.example.org/fullchain.pem;
        ssl_certificate_key        /etc/letsencrypt/live/piwigo.example.org/privkey.pem;

        # It is best to place the root of the server block at the server level, and not the location level

        # any location block path will be relative to this root.
        root /var/calculate/www/piwigo;

        # It's always good to set logs, note however you cannot turn off the error log
        # setting error_log off; will simply create a file called 'off'.

        access_log /var/log/nginx/piwigo.access_log;
        error_log /var/log/nginx/piwigo.error_log;

        # This can also go in the http { } level
        index index.html index.htm index.php;

        location / {
                # if you're just using wordpress and don't want extra rewrites
                # then replace the word @rewrites with /index.php
                try_files $uri $uri/ @rewrites;
        }

        location @rewrites {
                # Can put some of your own rewrite rules in here
                # for example rewrite ^/~(.*)/(.*)/? /users/$1/$2 last;
                # If nothing matches we'll just send it to /index.php
                rewrite ^ /index.php last;
        }

        # This block will catch static file requests, such as images, css, js
        # The ?: prefix is a 'non-capturing' mark, meaning we do not require
        # the pattern to be captured into $1 which should help improve performance
        location ~* \.(?:ico|css|js|gif|jpe?g|png)$ {
                # Some basic cache-control for static files to be sent to the browser
                expires max;
                add_header Pragma public;
                add_header Cache-Control "public, must-revalidate, proxy-revalidate";
        }

        # remove the robots line if you want to use wordpress' virtual robots.txt
        location = /robots.txt { access_log off; log_not_found off; }
        location = /favicon.ico { access_log off; log_not_found off; }

        # this prevents hidden files (beginning with a period) from being served
        location ~ /\. { access_log off; log_not_found off; deny all; }

        location ~ \.php {
                fastcgi_param SCRIPT_NAME $fastcgi_script_name;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                fastcgi_index index.php;
                fastcgi_pass unix:/run/php-fpm.socket;
                include fastcgi_params;
        }
}

Apply the new Nginx setting

/etc/init.d/nginx reload
nginx | * Checking nginx configuration ...                                    [ ok ]
nginx | * Refreshing nginx configuration ...                                  [ ok ]

Conclusion

Congratulations, Piwigo is up and running! But this is only the beginning. Now you can create albums and put pictures in them, add tags, configure plugins... The Piwigo CMS should be able to handle these tasks perfectly.

Note

Share your Piwigo experience in our Mastodon group @piwigo@calculate.social.