Currently, in the year 2024, PHP has released version 8.3, however, PHP 8.1 is still essential for many web applications like WordPress. If you’re looking to install PHP 8.1 on Ubuntu 22.04, we will guide you through a simple process to save time. Additionally, we will also share tips to optimize PHP 8.1’s performance for the best results.
Step 1: Add PPA Repository and install PHP 8.1
Ubuntu systems usually rely on repositories to manage applications and tools. To install PHP 8.1 on Ubuntu 22.04, access the terminal and run the command below to add the repository.
sudo apt install lsb-release ca-certificates apt-transport-https software-properties-common -y
After adding the repository, you can update the system and install PHP 8.1 on Ubuntu by entering the corresponding command. To confirm, type Y
and press Enter.
sudo add-apt-repository ppa:ondrej/php sudo apt update sudo apt install php8.1
So, you have completed the install PHP 8.1 on Ubuntu 22.04. You can check the current version by running the command “php -v”. Now, you need to install the necessary PHP extensions for your project.
Step 2: Install php8.1-fpm and extensions
To install PHP 8 extensions on Ubuntu 22.04, use the command:
sudo apt install php8.1-<extension>
And here is the list of extensions for PHP 8.1:
sudo apt install php8.1-* php8.1-amqp php8.1-common php8.1-gd php8.1-ldap php8.1-apcu php8.1-curl php8.1-gmp php8.1-mailparse php8.1-ast php8.1-dba php8.1-igbinary php8.1-mbstring php8.1-bcmath php8.1-dev php8.1-imagick php8.1-memcached php8.1-bz2 php8.1-ds php8.1-imap php8.1-msgpack php8.1-cgi php8.1-enchant php8.1-interbase php8.1-mysql php8.1-readline php8.1-sqlite3 php8.1-xsl php8.1-xml php8.1-redis php8.1-sybase php8.1-yac php8.1-raphf php8.1-rrd php8.1-tidy php8.1-yaml php8.1-solr php8.1-smbclient php8.1-uuid php8.1-zip php8.1-psr php8.1-snmp php8.1-xdebug php8.1-zmq php8.1-cli php8.1-odbc php8.1-opcache php8.1-pgsql php8.1-phpdbg php8.1-pspell php8.1-soap php8.1-oauth php8.1-psr php8.1-fpm php8.1-intl php8.1-xhprof
For example, if you have only installed php8.1-fpm and a few necessary extensions required for your project, the installation command may look like this:
sudo apt install php8.1-common php8.1-mysql php8.1-xml php8.1-xmlrpc php8.1-curl php8.1-gd php8.1-imagick php8.1-cli php8.1-dev php8.1-imap php8.1-mbstring php8.1-opcache php8.1-soap php8.1-zip php8.1-intl php8.1-fpm php8.1-zip php8.1-bcmath php8.1-mcrypt
After completing the install PHP 8.1 on your Ubuntu system, enable php8.1-fpm and configure it to start automatically with the system.
sudo systemctl enable php8.1-fpm sudo service php8.1-fpm restart
Step 3: Setting Up Permissions and Security for PHP
By default, the user
and listen.owner
are set to www-data
, which serves as a security measure and limits certain permissions to ensure the safety of your web application. However, for certain reasons, www-data is often not recommended as it is similar to a common user/group.
Based on my experience (ArrivedDEV), I would replace user = www-data with user = arriveddev. arriveddev
here is my Ubuntu username, and by making this change, the web directory will only have permissions for arriveddev, which will ensure more security compared to www-data.
Of course, whether to make this change or keep the default setting is up to your choice.
After making the changes, you’ll need to restart php8.1-fpm to apply the updates.
sudo service php8.1-fpm restart
Note: You can install multiple PHP versions such as 7.4, 8.0, 8.1, or 8.2 on Ubuntu and switch default version using command line:
sudo update-alternatives --config php
With PHP 8.1 successfully installed, you’re now equipped to build and deploy high-performance web applications on your Ubuntu 22.04 system. Leverage the latest advancements in PHP to enhance your website’s speed, security, and overall user experience.