Install PHP 8.1 on Ubuntu

feature image

If you do not have a server, you may check out Setup EC2 with Instance Connect (

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

Add the highlight code below.

  • Press “i” to change to insert mode.
  • Copy and paste the highlighted code.
  • Press “Esc” to switch back to command mode.
  • Type “:wq” to save and quit.
<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

Save and quit.

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 if needed.

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


Set up WordPress on Single instance EC2 in Ubuntu (