Actions

User

User:Gold/PHP 8 Scratchpad/Add PHP8

From Mahara Wiki

< User:Gold‎ | PHP 8 Scratchpad

Check available versions

gold@shipnet:~/$ apt-cache search php | sort | grep fpm
php7.4-fpm - server-side, HTML-embedded scripting language (FPM-CGI binary)
php-fpm - server-side, HTML-embedded scripting language (FPM-CGI binary) (default)

If there are no php 8 options we need to add the repo and check again.

gold@shipnet:~/$ sudo add-apt-repository ppa:ondrej/php

You will likely be challenged to install ppa:ondrej/apache2. We are not using mod-php so there is no need to do that.

To check php8 is available rerun the apt-cache call above. You should see many more options.

Install PHP 7.4 and 8.1

Testing on PHP 7.4 and PHP 8.1 so install the related supporting packages that Mahara needs. There is no php8.1-json package. This is part of php8.1-common now.

gold@shipnet:~/$ sudo apt install \
  libapache2-mod-fcgid \
  php7.4-cli php7.4-common php7.4-curl php7.4-fpm php7.4-gd \
  php7.4-intl php7.4-json php7.4-ldap php7.4-mbstring php7.4-pgsql  \
  php7.4-xml php7.4-xmlrpc php7.4-zip \
  php8.1-cli php8.1-common php8.1-curl php8.1-fpm php8.1-gd \
  php8.1-intl php8.1-ldap php8.1-mbstring php8.1-pgsql php8.1-xml \
  php8.1-xmlrpc php8.1-zip

Set up Apache

PHP

We need to modify how apache2 works with PHP. This requires that we switch from mod_php (the likely default install) to php-fpm which lets us use multiple stand-alone php servers that we can select on a site by site basis.

Check which modules you have installed with sudo apache2ctl -M as we may need to refer to the php version on some of the mods. We will use php7.2 as the reference. Swap this out for the version you currently have.

  • Disable mod-php with sudo a2dismod php7.2
  • Enable Fast-CGI sudo a2enmod proxy_fcgi setenvif
  • Disable MPM-prefork with sudo a2dismod mpm_prefork
  • Enable MPM-event sudo a2enmod mpm_event
  • Restart the server: sudo systemctl restart apache2

Update php.ini for our new versions.

   sudo sh -c "echo 'post_max_size = 37M'       >> /etc/php/7.4/cli/php.ini"
   sudo sh -c "echo 'upload_max_filesize = 32M' >> /etc/php/7.4/cli/php.ini"
   sudo sh -c "echo 'post_max_size = 37M'       >> /etc/php/8.1/fpm/php.ini"
   sudo sh -c "echo 'upload_max_filesize = 32M' >> /etc/php/8.1/fpm/php.ini"

With PHP running as a service we need to restart this for new config to be seen now. You can use sudo systemctl | grep fpm to list the php-fpm services you have running. For each restart with:

sudo systemctl restart php7.4-fpm.service
sudo systemctl restart php8.1-fpm.service

SSL

Enable SSL via sudo a2enmod ssl

Set up the SSL certs. We use -days 365 as apparently some modern browsers will reject SSL certs that last longer than that.

sudo mkdir /etc/apache2/ssl && \
sudo openssl req -x509 -nodes -days 365 -newkey rsa:2048 \
  -keyout /etc/apache2/ssl/apache.key \
  -out /etc/apache2/ssl/apache.crt

Sites

I'm configuring 2 sites. One for PHP 7.4 and one for PHP 8.1. At this point I'm not supporting PHP 7.2 and 8.0 is not the default in Ubuntu 22.04 which is our target platform.

I have updated my local /etc/hosts file and added entries for https://php74.mahara.local/ and https://php81.mahara.local/. While I'm ignoring 7.2 and 8.0 I'm including the minor version for future-proofing. Just in case.

Next the following is added as mahara-php8.conf and a sudo a2ensite mahara-php8 && sudo systemctl reload apache2 to enable it.

/etc/apache2/sites-available/mahara-php8.conf
<VirtualHost *:80>
  ServerName php81.mahara.local
  DocumentRoot /home/gold/Mahara/php8/htdocs
 
  <Directory /home/gold/Mahara/codereview>
    Options Indexes FollowSymLinks MultiViews
    Require all granted
  </Directory>
 
  ErrorLog /var/log/apache2/error.log
  LogLevel debug
 
  CustomLog /var/log/apache2/access.log combined
  DirectoryIndex index.php index.html
</VirtualHost>

<IfModule mod_ssl.c>
    <VirtualHost _default_:443>
        ServerName php81.mahara.local
        DocumentRoot /home/gold/Mahara/php8/htdocs
        SSLEngine on
        SSLCertificateFile /etc/apache2/ssl/apache.crt
        SSLCertificateKeyFile /etc/apache2/ssl/apache.key

        <FilesMatch \.php$>
            SSLOptions +StdEnvVars
            SetHandler "proxy:unix:/run/php/php8.1-fpm.sock|fcgi://localhost"
        </FilesMatch>
        <Directory /home/gold/Mahara/php8>
            SSLOptions +StdEnvVars
            DirectoryIndex index.php
            Options Indexes FollowSymLinks MultiViews
            Require all granted
        </Directory>
        ErrorLog /var/log/apache2/error.log
        LogLevel info
 
        CustomLog /var/log/apache2/access.log combined
    </VirtualHost>
</IfModule>

<VirtualHost *:80>
  ServerName php74.mahara.local
  DocumentRoot /home/gold/Mahara/php8/htdocs
 
  <Directory /home/gold/Mahara/codereview>
    Options Indexes FollowSymLinks MultiViews
    Require all granted
  </Directory>
 
  ErrorLog /var/log/apache2/error.log
  LogLevel debug
 
  CustomLog /var/log/apache2/access.log combined
  DirectoryIndex index.php index.html
</VirtualHost>

<IfModule mod_ssl.c>
    <VirtualHost _default_:443>
        ServerName php74.mahara.local
        DocumentRoot /home/gold/Mahara/php8/htdocs
        SSLEngine on
        SSLCertificateFile /etc/apache2/ssl/apache.crt
        SSLCertificateKeyFile /etc/apache2/ssl/apache.key

        <FilesMatch \.php$>
            SSLOptions +StdEnvVars
            SetHandler "proxy:unix:/run/php/php7.4-fpm.sock|fcgi://localhost"
        </FilesMatch>
        <Directory /home/gold/Mahara/php8>
            SSLOptions +StdEnvVars
            DirectoryIndex index.php
            Options Indexes FollowSymLinks MultiViews
            Require all granted
        </Directory>
        ErrorLog /var/log/apache2/error.log
        LogLevel info
 
        CustomLog /var/log/apache2/access.log combined
    </VirtualHost>
</IfModule>