Project

General

Profile

install_pdf_php.sh

Roger Borrello, 11/24/2025 06:21 PM

Download (3 KB)

 
1
#!/usr/bin/env bash
2
set -euo pipefail
3
#set -x
4

    
5
# Detect Ubuntu codename (jammy, noble, etc)
6
CODENAME=$(lsb_release -sc)
7
CODENAME="jammy"
8

    
9
echo "Detected Ubuntu codename: $CODENAME"
10

    
11
# Map codenames to wkhtmltopdf packages
12
case "$CODENAME" in
13
    jammy)
14
        WK_DEB="wkhtmltox_0.12.6.1-2.jammy_amd64.deb"
15
        ;;
16
    noble)
17
        WK_DEB="wkhtmltox_0.12.6.1-1.noble_amd64.deb"
18
        ;;
19
    *)
20
        echo "ERROR: This script does not support Ubuntu codename '$CODENAME'"
21
        echo "Supported: jammy (22.04), noble (24.04)"
22
        exit 1
23
        ;;
24
esac
25

    
26
# Download wkhtmltopdf package if needed
27
if [[ ! -f "$WK_DEB" ]]; then
28
    echo "Downloading wkhtmltopdf for Ubuntu $CODENAME..."
29
    # Try primary URL, if that fails try fallback, otherwise fail with message
30
    if ! wget -O "$WK_DEB" "https://github.com/wkhtmltopdf/packaging/releases/download/0.12.6.1-2/${WK_DEB}"; then
31
        echo "Primary download failed, trying fallback..."
32
        if ! wget -O "$WK_DEB" "https://github.com/wkhtmltopdf/packaging/releases/latest/download/${WK_DEB}"; then
33
            echo "ERROR: failed to download $WK_DEB" >&2
34
            rm -f "$WK_DEB"
35
            exit 1
36
        fi
37
    fi
38
fi
39

    
40
if [[ ! -s "$WK_DEB" ]]; then
41
    echo "ERROR: downloaded $WK_DEB is empty" >&2
42
    rm -f "$WK_DEB"
43
    exit 1
44
fi
45
echo "Installing $WK_DEB..."
46
sudo apt install -y "./$WK_DEB"
47

    
48
# Install PHP from PPA (works on 22.04 and 24.04)
49
if ! grep -q "ondrej/php" /etc/apt/sources.list /etc/apt/sources.list.d/* 2>/dev/null; then
50
    echo "Adding PHP PPA..."
51
    sudo apt update -y
52
    sudo add-apt-repository -y ppa:ondrej/php
53
fi
54

    
55
echo "Updating apt package lists..."
56
sudo apt update -y
57

    
58
echo "Installing PHP 8.3 and extensions..."
59
sudo apt install -y \
60
    php8.3 \
61
    php8.3-cli \
62
    php8.3-common \
63
    php8.3-fpm \
64
    php8.3-mysql \
65
    php8.3-zip \
66
    php8.3-gd \
67
    php8.3-mbstring \
68
    php8.3-curl \
69
    php8.3-xml \
70
    php8.3-bcmath
71

    
72
# Fix up php.ini
73
PHPINI="/etc/php/8.3/cli/php.ini"
74

    
75
# -----------------------------
76
# Determine TIMEZONE value
77
# -----------------------------
78
if [[ -f /etc/timezone ]]; then
79
    TZVAL="$(cat /etc/timezone)"
80
else
81
    TZVAL="America/Chicago"
82
fi
83

    
84
# Escape for sed (slashes and &)
85
TZ_ESCAPED=$(printf '%s' "$TZVAL" | sed 's/[\/&]/\\&/g')
86

    
87
# -----------------------------
88
# Update include_path
89
# Only if it matches the default:
90
#     ;include_path = ".:/usr/share/php"
91
# -----------------------------
92
sudo sed -i \
93
  -e 's#^\s*;\?\s*include_path\s*=\s*"\.:/usr/share/php"#include_path = "/php:/php/classes"#' \
94
  "$PHPINI"
95

    
96
# -----------------------------
97
# Update date.timezone
98
# Matches either:
99
#   ;date.timezone =
100
#   date.timezone =
101
#   date.timezone = "something"
102
# -----------------------------
103
sudo sed -i \
104
  -e "s#^\s*;\?\s*date.timezone\s*=.*#date.timezone = \"$TZ_ESCAPED\"#" \
105
  "$PHPINI"
106

    
107
# -----------------------------
108
# Update memory_limit
109
# Only if it matches the default:
110
#     memory_limit = -1
111
# -----------------------------
112
sudo sed -i \
113
  -e 's#^memory_limit\s*=\s*-1#memory_limit = 512M#' \
114
  "$PHPINI"
115

    
116
echo "Done!"