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-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.
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/apache2sudo apt-get updatesudo apt-get install libapache2-mod-fcgid apache2 apache2-utilssudo ufw allow 80sudo ufw allow 443sudo ufw enablesudo a2enmod mime http2 rewrite deflate expires headers ssl sudo a2enmod proxy_fcgi setenvifsudo a2enconf php8.1-fpmsudo systemctl restart php8.1-fpmsudo 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].confsudo systemctl restart apache2
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] --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: 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 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.
