joomla

Install Joomla 4 with PHP 8.1 on Apache

published on

I have already written a guide on how to install Joomla using Apache but have chosen to add a new, updated, guide which will leave you with the latest version of Joomla (at the time of writing this article) along with PHP 8.1 and Redis cache.

Start by installing some prerequisites

sudo apt install lsb-release ca-certificates apt-transport-https software-properties-common language-pack-en-base
sudo locale-gen en_US.UTF-8

Now install PHP 8.1

sudo add-apt-repository ppa:ondrej/php
sudo apt-get update
sudo apt install openssl php8.1-{common, cli, bz2, zip, curl, intl, mysql, snmp, memcached, imagick, gd, fileinfo, imap, ldap, soap, tidy, xml, gd, gmp, pspell, mbstring, opcache, fpm, ssh2, imap, redis, apcu, mcrypt}

Once PHP 8.1 is installed you need to make a few changes to a configuration file. You can do this by running sudo nano /etc/php/8.1/fpm/php.ini and updating the relevant lines to read like below.

output_buffering = Off
expose_php = off
max_execution_time = 30
max_input_time = 1000
max_input_vars = 3000
memory_limit = 512M       
display_errors = Off
display_startup_errors = Off
log_errors = On
html_errors = Off
post_max_size = 128M
upload_max_filesize = 128M
date.timezone = Europe/London
opcache.enable=1
opcache.memory_consumption=128
opcache.max_accelerated_files=10000
opcache.revalidate_freq=200

Now run sudo nano /etc/php/8.1/fpm/pool.d/www.conf and make sure the file contains the lines below and they are not commented out.

listen = /run/php/php8.1-fpm.sock
listen.owner = www-data
listen.mode = 0660

Now install Redis cache

The Redis cache can be installed by running sudo apt-get install redis-server php-redis.

You will then need to make a few small changes to the Redis configuration file by running sudo nano /etc/redis/redis.conf and updating or adding the following four lines.

supervised systemd
maxmemory 256mb
maxmemory-policy allkeys-lru
bind 127.0.0.1 ::1

Now start Redis and PHP 8.1

sudo systemctl restart redis-server.service
sudo systemctl enable redis-server.service
sudo systemctl restart php8.1-fpm

Next, install MariaDB

MariaDB can be installed by running sudo apt-get install mariadb-server. It is important to secure MariaDB by now running sudo mysql_secure_installation.

Once this complete you can create a database for Joomla by running sudo mysql -u root -p and entering the following commands at the MariaDB prompt.

CREATE DATABASE joomla charset=utf8mb4;
CREATE USER 'joomlauser'@'localhost' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON joomla.* TO 'joomlauser'@'localhost';
FLUSH PRIVILEGES;
exit

Remember that where you see joomla you should replace this with your own database name, where you see joomlauser you should replace this with your own user name for the Joomla database and where you see password you should replace this with a strong password.

Next, install the Apache web server

Run the below commands in order to install Apache along with the necessary tools to make sure that PHP 8.1 works using the mpm_event module.

sudo add-apt-repository ppa:ondrej/apache2
sudo apt-get update
sudo apt-get install libapache2-mod-fcgid apache2 apache2-utils
sudo ufw allow 80
sudo ufw allow 443
sudo ufw enable
sudo a2enmod mime http2 rewrite deflate expires headers ssl 
sudo a2enmod proxy_fcgi setenvif
sudo a2enconf php8.1-fpm
sudo systemctl restart php8.1-fpm
sudo systemctl reload apache2

Set up the Apache virtual host

First, disable the default site with sudo a2dissite 000-default.conf then make a new directory for your site by running sudo mkdir /var/www/[domain] where [domain] is your domain name.

Now create the virtual host by running sudo nano /etc/apache2/sites-available/[domain].conf and adding the following lines

<VirtualHost *:80>
    ServerAdmin [email address]
    ServerName [domain]
    ServerAlias www.[domain]
    DocumentRoot /var/www/[domain]/joomla
    Protocols h2 http/1.1
    DirectoryIndex index.php index.html

    <Directory /var/www/[domain]/joomla>
       Options +SymLinksIfOwnerMatch -MultiViews -Indexes -Includes -ExecCGI
       AllowOverride All
       Require all granted
    </Directory>

    <FilesMatch "\.php$">
       SetHandler "proxy:unix:/run/php/php8.1-fpm.sock|fcgi://localhost"
    </FilesMatch>      

    <IfModule mod_deflate.c>
        AddOutputFilterByType DEFLATE text/html text/css text/javascript application/javascript
    </IfModule>

    SetEnv TZ Europe/London
    AddDefaultCharset UTF-8
    DefaultLanguage en
    FileETag none

    Header set X-XSS-Protection "1; mode=block"
    Header set X-Content-Type-Options nosniff
    Header set X-Frame-Options SAMEORIGIN
    Header set Referrer-Policy: no-referrer-when-downgrade

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Lastly, run the following commands to activate your site configuration

sudo a2ensite [domain].conf
sudo systemctl restart apache2

Now install Joomla 4

Run the following commands to install Joomla into the web root

cd /var/www/[domain]
sudo mkdir joomla
sudo wget https://downloads.joomla.org/cms/joomla4/4-1-5/Joomla_4-1-5-Stable-Full_Package.zip
sudo unzip Joomla_4-1-5-Stable-Full_Package.zip -d /var/www/[domain]/joomla

Set up file ownership and permissions

chown -R www-data:www-data /var/www/[domain]/joomla
sudo find /var/www/[domain]/joomla/ -type d -exec chmod 755 {} \;
sudo find /var/www/[domain]/joomla/ -type f -exec chmod 644 {} \;

Secure your site with a SSL / TLS certificate

sudo apt install snapd
sudo snap install core; sudo snap refresh core
sudo snap install --classic certbot
sudo ln -s /snap/bin/certbot /usr/bin/certbot
sudo certbot -d [domain] -d www.[domain] --apache

Setup Joomla 4

To finish setting up Joomla 4 visit your chosen domain name in your browser and follow the setup screens that appear. At the end you can remove the installation folder if it doesn't do it for you and, back on the server, you need to run the following command to setup some extra rules.

sudo cp /var/www/[domain]/joomla/htaccess.txt /var/www/[domain]/joomla/.htaccess

The Redis cache will need some configuration now so visit the Joomla admin panel and select System, then Global Configuration, then system then cache settings and set the options you see to the following before clicking on Save.

Cache-Handler: Redis
Persistent Redis: Yes
Redis Server Host: localhost
Redis Server Port: 6379
Redis Server Authentication: leave empty
Redis Database: 0
Cache Time: 30
Platform Specific Caching: No
System Cache: On – conservative Caching

Final thoughts

You have now finished installing Joomla 4 with PHP 8.1 and Redis cache on Apache. You may want to add further configuration changes but you will be able to create your new site and make available to the world now.