DevOps Series Using Ansible to Deploy a Piwigo Photo Gallery

0
8159

Piwigo is Web based photo gallery software that is written in PHP. In this tenth article in our DevOps series, we will use Ansible to install and configure a Piwigo instance.

Piwigo requires a MySQL database for its back-end and has a number of extensions and plugins developed by the community. You can install it on any shared Web hosting service provider or install it on your own GNU/Linux server. It basically uses the (G)LAMP stack. In this article, we will use Ansible to install and configure a Piwigo instance, which is released under the GNU General Public License (GPL).

You can add photos using the Piwigo Web interface or use an FTP client to synchronise the photos with the server. Each photo is made available in nine sizes, ranging from XXS to XXL. A number of responsive UI themes are available that make use of these different photo sizes, depending on whether you are viewing the gallery on a phone, tablet or computer. The software also allows you to add a watermark to your photos, and you can create nested albums. You can also tag your photos, and Piwigo stores metadata about the photos too. You can even use access control to make photos and albums private. My Piwigo gallery is available at https://www.shakthimaan.in/gallery/.

Linux

The Piwigo installation will be on an Ubuntu 15.04 image running as a guest OS using KVM/QEMU. The host system is a Parabola GNU/Linux-libre x86_64 system. Ansible is installed on the host system using the distribution package manager. The version of Ansible used is:

$ ansible --version

ansible 2.4.1.0

config file = /etc/ansible/ansible.cfg

configured module search path = [u’/home/shakthi/.ansible/plugins/modules’, u’/usr/share/ansible/plugins/modules’]

ansible python module location = /usr/lib/python2.7/site-packages/ansible

executable location = /usr/bin/ansible

python version = 2.7.14 (default, Sep 20 2017, 01:25:59) [GCC 7.2.0]
Figure 1: Apache2 default index page

The /etc/hosts file should have an entry for the guest “ubuntu” VM as indicated below:

192.168.122.4 ubuntu

You should be able to issue commands from Ansible to the guest OS. For example:

$ ansible ubuntu -m ping

ubuntu | SUCCESS => {

“changed”: false,

“ping”: “pong”

}

On the host system, we will create a project directory structure to store the Ansible playbooks:

ansible/inventory/kvm/

/playbooks/configuration/

/playbooks/admin/

An ‘inventory’ file is created inside the inventory/kvm folder that contains the following:

ubuntu ansible_host=192.168.122.4 ansible_connection=ssh ansible_user=xetex ansible_password=pass

Apache

The Apache Web server needs to be installed first on the Ubuntu guest VM. The Ansible playbook for the same is as follows:

- name: Install Apache web server

hosts: ubuntu

become: yes

become_method: sudo

gather_facts: true

tags: [web]

tasks:

- name: Update the software package repository

apt:

update_cache: yes

- name: Install Apache

package:

name: “{{ item }}”

state: latest

with_items:

- apache2

- wait_for:

port: 80
Figure 2: Piwigo install page

The Ansible playbook updates the software package repository by running apt-get update and then proceeds to install the Apache2 package. The playbook waits for the server to start and listen on port 80. An execution of the playbook is shown below:

$ ansible-playbook -i inventory/kvm/inventory playbooks/configuration/piwigo.yml --tags web -K

SUDO password:

PLAY [Install Apache web server] ******************

TASK [setup] ***********************************

ok: [ubuntu]

TASK [Update the software package repository] *************

changed: [ubuntu]

TASK [Install Apache] *************************************** changed: [ubuntu] => (item=[u’apache2’])

TASK [wait_for] *********************************************

ok: [ubuntu]

PLAY RECAP **************************************************

ubuntu : ok=4 changed=2 unreachable=0 failed=0

The verbosity in the Ansible output can be achieved by passing ‘v’ multiple times in the invocation. The more number of times that ‘v’ is present, the greater is the verbosity level. The -K option will prompt for the sudo password for the xetex user. If you now open http://192.168.122.4, you should be able to see the default Apache2 index.html page as shown in Figure 1.

MySQL

Piwigo requires a MySQL database server for its back-end, and at least version 5.0. As the second step, you can install the same using the following Ansible playbook:

- name: Install MySQL database server

hosts: ubuntu

become: yes

become_method: sudo

gather_facts: true

tags: [database]

tasks:

- name: Update the software package repository

apt:

update_cache: yes

- name: Install MySQL

package:

name: “{{ item }}”

state: latest

with_items:

- mysql-server

- mysql-client

- python-mysqldb

- name: Start the server

service:

name: mysql

state: started

- wait_for:

port: 3306

- mysql_user:

name: guest

password: ‘*F7B659FE10CA9FAC576D358A16CC1BC646762FB2’

encrypted: yes

priv: ‘*.*:ALL,GRANT’

state: present

The APT software repository is updated first and the required MySQL packages are then installed. The database server is started, and the Ansible playbook waits for the server to listen on port 3306. For this example, a guest database user account with osfy as the password is chosen for the gallery Web application. In production, please use a stronger password. The hash for the password can be computed from the MySQL client as indicated below:

mysql> SELECT PASSWORD(‘osfy’);

+-------------------------------------------+

| PASSWORD(‘osfy’) |

+-------------------------------------------+

| *F7B659FE10CA9FAC576D358A16CC1BC646762FB2 |

+-------------------------------------------+

1 row in set (0.00 sec)

Also, the default MySQL root password is empty. You should change it after installation. The playbook can be invoked as follows:

$ ansible-playbook -i inventory/kvm/inventory playbooks/configuration/piwigo.yml --tags database -K
Figure 3: Piwigo install success page

PHP

Piwigo is written using PHP (PHP Hypertext Preprocessor), and it requires at least version 5.0 or later. The documentation website recommends version 5.2. The Ansible playbook to install PHP is given below:

- name: Install PHP

hosts: ubuntu

become: yes

become_method: sudo

gather_facts: true

tags: [php]

tasks:

- name: Update the software package repository

apt:

update_cache: yes

- name: Install PHP

package:

name: “{{ item }}”

state: latest

with_items:

- php5

- php5-mysql

Update the software package repository, and install PHP5 and the php5-mysql database connectivity package. The Ansible playbook for this can be invoked as follows:

$ ansible-playbook -i inventory/kvm/inventory playbooks/configuration/piwigo.yml --tags php -K

Piwigo

The final step is to download, install and configure Piwigo. The playbook for this is given below:

- name: Setup Piwigo

hosts: ubuntu

become: yes

become_method: sudo

gather_facts: true

tags: [piwigo]

vars:

piwigo_dest: “/var/www/html”

tasks:

- name: Update the software package repository

apt:

update_cache: yes

- name: Create a database for piwigo

mysql_db:

name: piwigo

state: present

- name: Create target directory

file:

path: “{{ piwigo_dest }}/gallery”

state: directory

- name: Download latest piwigo

get_url:

url: http://piwigo.org/download/dlcounter.php?code=latest

dest: “{{ piwigo_dest }}/piwigo.zip”

- name: Extract to /var/www/html/gallery

unarchive:

src: “{{ piwigo_dest }}/piwigo.zip”

dest: “{{ piwigo_dest }}/gallery”

remote_src: True

- name: Restart apache2 server

service:

name: apache2

state: restarted

The piwigo_dest variable stores the location of the default Apache hosting directory. The APT software package repository is then updated. Next, an exclusive MySQL database is created for this Piwigo installation. A target folder gallery is then created under /var/www/html to store the Piwigo PHP files. Next, the latest version of Piwigo is downloaded (2.9.2, as on date) and extracted under the gallery folder. The Apache Web server is then restarted.

You can invoke the above playbook as follows:

$ ansible-playbook -i inventory/kvm/inventory playbooks/configuration/piwigo.yml --tags piwigo -K

If you open the URL http://192.168.122.4/gallery in a browser on the host system, you will see the screenshot given in Figure 2 to start the installation of Piwigo.

After entering the database credentials and creating an admin user account, you should see the ‘success’ page, as shown in Figure 3.

You can then go to http://192.168.122.4/gallery to see the home page of Piwigo, as shown in Figure 4.

Backup

The Piwigo data is present in both the installation folder and in the MySQL database. It is thus important to periodically make backups, so that you can use these archive files to restore data, if required. The following Ansible playbook creates a target backup directory, makes a tarball of the installation folder, and dumps the database contents to a .sql file. The epoch timestamp is used in the filename. The backup folder can be rsynced to a different system or to secondary backup.

- name: Backup Piwigo

hosts: ubuntu

become: yes

become_method: sudo

gather_facts: true

tags: [backup]

vars:

piwigo_dest: “/var/www/html”

tasks:

- name: Create target directory

file:

path: “{{ piwigo_dest }}/gallery/backup”

state: directory

- name: Backup folder

archive:

path: “{{ piwigo_dest }}/gallery/piwigo”

dest: “{{ piwigo_dest }}/gallery/backup/piwigo-backup-{{ ansible_date_time.epoch }}.tar.bz2”

- name: Dump database

mysql_db:

name: piwigo

state: dump

target: “{{ piwigo_dest }}/gallery/backup/piwigo-{{ ansible_date_time.epoch }}.sql”

The above playbook can be invoked as follows:

$ ansible-playbook -i inventory/kvm/inventory playbooks/configuration/piwigo.yml --tags backup -K

Two backup files that were created from executing the above playbook are piwigo-1510053932.sql and piwigo-backup-1510053932.tar.bz2.

Cleaning up

You can uninstall the entire Piwigo installation using an Ansible playbook. This has to happen in the reverse order. You have to remove Piwigo first, followed by PHP, MySQL and Apache. A playbook to do this is included in the playbooks/admin folder and given below for reference:

---

- name: Uninstall Piwigo

hosts: ubuntu

become: yes

become_method: sudo

gather_facts: true

tags: [uninstall]

vars:

piwigo_dest: “/var/www/html”

tasks:

- name: Delete piwigo folder

file:

path: “{{ piwigo_dest }}/gallery”

state: absent

- name: Drop database

mysql_db:

name: piwigo

state: absent

- name: Uninstall PHP packages

package:

name: “{{ item }}”

state: absent

with_items:

- php5-mysql

- php5

- name: Stop the database server

service:

name: mysql

state: stopped

- name: Uninstall MySQL packages

package:

name: “{{ item }}”

state: absent

with_items:

- python-mysqldb

- mysql-client

- mysql-server

- name: Stop the web server

service:

name: apache2

state: stopped

- name: Uninstall apache2

package:

name: “{{ item }}”

state: absent

with_items:

- apache2

The above playbook can be invoked as follows:

$ ansible-playbook -i inventory/kvm/inventory playbooks/admin/uninstall-piwigo.yml -K

You can visit http://piwigo.org/ for more documentation.

LEAVE A REPLY

Please enter your comment!
Please enter your name here