CMS

Install Joomla 4 with PHP 8.1 on NGinx

published on

I have written guides on how to install Joomla on APache and so have added this guide for all of you who prefer to use NGinx. After finishing this guide you will have installed 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.

Install NGinx

First of all you need to add the repositories to install NGinx from. This makes sure that you will install the latest versions.

To add the repositories run sudo add-apt-repository ppa:ondrej/nginx-mainline then run sudo nano /etc/apt/sources.list.d/ondrej-ubuntu-nginx-mainline-*.list and uncomment the deb-src line.

Now run the following two commands

sudo apt-get update
sudo apt-get install nginx-core nginx-common nginx nginx-full libgeoip-dev libpcre3-dev

Set up NGinx for Joomla

Create an NGinx server block by running sudo nano /etc/nginx/sites-available/joomla.conf and adding the following code. This will tell NGinx how to handle your site.

server {
    listen 80;
    listen [::]:80;
    root /var/www/[domain]/joomla;
    index index.php index.html index.htm;
    server_name example.com www.example.com;

    client_max_body_size 100M;
    autoindex off;
    
    location / {
        try_files $uri $uri/ /index.php?$args;
    }

    # deny running scripts inside writable directories
    location ~* /(images|cache|media|logs|tmp)/.*.(php|pl|py|jsp|asp|sh|cgi)$ {
      return 403;
      error_page 403 /403_error.html;
    }

    location ~ .php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php8.1-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
  }

And finally run the following two commands to activate the server block above.

sudo ln -s /etc/nginx/sites-available/[domain].conf /etc/nginx/sites-enabled/
sudo systemctl restart nginx

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] --nginx

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 .

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 NGinx. 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.