Upgrade Apache To PHP 8.1 with Ansible on Debian Upgrade Apache To PHP 8.1 with Ansible on Debian

October 12, 2022

ansible debian devops laravel londinium

My local Debian machine has Apache and PHP working and I have set it up with this Ansible playbook github.com/allotmentandy/ansible-debian-install-desktop

Today as part of the upgrade process for my laravel site, I wanted to update this Ansible Playbook to use PHP 8.1.

The older versions of laravel are now all out-of-date, see laravel.com/docs/9.x/releases#support-policy so it is time to upgrade to the latest and greatest version of php 8.1. (I know php 8.2 is out, but that isnt mentioned on the laravel php versions, so I will leave that for another day)

The first step was to add the install to roles/php/tasks/main.yml like so:

# install php8.1

- name: install php 8.1 packages
  tags: php8.1
  apt:
    name:
      - php8.1
      - php-mysql
      - libapache2-mod-php
      - php8.1-mysql
      - php8.1-cli
      - php8.1-common
      - php8.1-snmp
      - php8.1-ldap
      - php8.1-curl
      - php8.1-mbstring
      - php8.1-zip
      - php8.1-tidy
      - php8.1-opcache
      - php8.1-xml
      - php8.1-fpm 
      - libapache2-mod-php8.1
    state: present
    cache_valid_time: 3600
  become: True

I added a tag for php8.1 to the Ansible playbook to allow me to just run this

 ansible-playbook -i hosts workstation.yml --ask-become-pass --ask-pass --tags php8.1

Althought this worked, the command line version of php was 8.1, the apache webserver was still 8.0. After hunting around, i found this was required to totally stop the php8.0 version and have 8.1 on the webserver.

sudo systemctl stop php8.0-fpm
sudo systemctl disable php8.0-fpm
sudo a2disconf php8.0-fpm

sudo systemctl start php8.1-fpm
sudo systemctl enable php8.1-fpm

This translates into Ansible playbook as thus:

- name: Stop service php8.0-fpm on debian, if running
  tags: php8.1
  ansible.builtin.systemd:
    name: php8.0-fpm
    state: stopped

- name: Enable service php8.0-fpm and ensure it is not masked
  tags: php8.1  
  ansible.builtin.systemd:
    name: php8.0-fpm
    enabled: no

- name: Disables the Apache2 module php8.0-fpm
  tags: php8.1  
  community.general.apache2_module:
    state: absent
    name: php8.0-fpm

- name: Make sure php8.1-fpm is running
  tags: php8.1
  ansible.builtin.systemd:
    state: started
    name: php8.1-fpm

- name: Enable service php8.1-fpm and ensure it is not masked
  tags: php8.1
  ansible.builtin.systemd:
    name: php8.1-fpm
    enabled: yes

Now the apache server and command line are both using PHP 8.1.

This has been pushed to the github ansible playbook repo, hope it helps you.


If you would like to contact me with this form on londinium.com, ilminster.net or via Twitter @andylondon