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-basesudo locale-gen en_US.UTF-8
Now install PHP 8.1
sudo add-apt-repository ppa:ondrej/phpsudo apt-get updatesudo 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 = Offexpose_php = offmax_execution_time = 30max_input_time = 1000max_input_vars = 3000memory_limit = 512M display_errors = Offdisplay_startup_errors = Offlog_errors = Onhtml_errors = Offpost_max_size = 128Mupload_max_filesize = 128Mdate.timezone = Europe/Londonopcache.enable=1opcache.memory_consumption=128opcache.max_accelerated_files=10000opcache.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.socklisten.owner = www-datalisten.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 systemdmaxmemory 256mbmaxmemory-policy allkeys-lrubind 127.0.0.1 ::1
Now start Redis and PHP 8.1
sudo systemctl restart redis-server.servicesudo systemctl enable redis-server.servicesudo 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 updatesudo 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 joomlasudo wget https://downloads.joomla.org/cms/joomla4/4-1-5/Joomla_4-1-5-Stable-Full_Package.zipsudo 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]/joomlasudo 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 snapdsudo snap install core; sudo snap refresh coresudo snap install --classic certbotsudo ln -s /snap/bin/certbot /usr/bin/certbotsudo 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: RedisPersistent Redis: YesRedis Server Host: localhostRedis Server Port: 6379Redis Server Authentication: leave emptyRedis Database: 0Cache Time: 30Platform Specific Caching: NoSystem 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.
