Install PHP 8.1 on Ubuntu

Install PHP 8.1 on Ubuntu

Install Apache

To run a web server, you need to install Apache first.

sudo su -
apt -y install apache2

Apache rewrite

Apache rewrite allow you setup the routing which is good for SEO.

a2enmod rewrite
vi /etc/apache2/sites-available/000-default.conf


<VirtualHost *:80>
    <Directory /var/www/html>
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted

Restart Apache

systemctl restart apache2

Add a new .htaccess file.

vi /var/www/html/.htaccess
RewriteEngine on

PHP 8.1

Ubuntu 20.04 comes with PHP 7.4. If you want to install PHP 8.1, you need to use the repository from Ondřej Surý, the lead developer on PHP and Debian.

apt -y install software-properties-common && sudo add-apt-repository ppa:ondrej/php


Install PHP-FPM module.

apt -y install php8.1-fpm libapache2-mod-fcgid
a2enmod proxy_fcgi setenvif && sudo a2enconf php8.1-fpm

Restart Apache server

sudo systemctl restart apache2

Check PHP version

php --version

Change PHP version

update-alternatives --set php /usr/bin/php8.1