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.
![PHP 8.1 on Ubuntu 22.04](https://arriveddev.com/wp-content/uploads/2023/03/how-to-install-and-configure-php-8-1-on-ubuntu-22-1-1024x576.jpg)
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
![Add PPA Repository and install PHP 8.1 Ubuntu](https://arriveddev.com/wp-content/uploads/2023/03/how-to-install-and-configure-php-8-1-on-ubuntu-22-1-1.jpg)
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
![Install php 8.1 on Ubuntu 22.04 (Jammy Jellyfish) Install PHP 8.1 on Ubuntu 22.04 LTS](https://arriveddev.com/wp-content/uploads/2023/03/how-to-install-and-configure-php-8-1-on-ubuntu-22-2-2.jpg)
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
![Configure php8.1-fpm on Ubuntu 22.04](https://arriveddev.com/wp-content/uploads/2023/03/how-to-install-and-configure-php-8-1-on-ubuntu-22-2.jpg)
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
![Install PHP 8.1 extensions command on Ubuntu 22.04](https://arriveddev.com/wp-content/uploads/2023/03/how-to-install-and-configure-php-8-1-on-ubuntu-22-3.jpg)
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
Step 4: Optimize php.ini
Next, you will need to config and optimize the php.ini file according to your specific requirements. Navigate to the configuration values and make the necessary changes. You will need to edit both files as shown below and modify the appropriate values.
sudo nano /etc/php/8.1/cli/php.ini
sudo nano /etc/php/8.1/fpm/php.ini
![Optimize php.ini on Ubuntu 22.04 Optimize php on Ubuntu 22.04](https://arriveddev.com/wp-content/uploads/2023/03/how-to-install-and-configure-php-8-1-on-ubuntu-22-4.jpg)
The following values are the ones that should be optimize in php.ini. Note that these are only reference values and you may increase or decrease the values to fit your computer or server configuration.
date.timezone = Asia/Ho_Chi_Minh #https://www.php.net/manual/en/timezones.php
max_execution_time = 18000
max_input_time = 6000
max_input_vars = 10000
memory_limit= 2048M
post_max_size = 64M
upload_max_filesize = 64M
zlib.output_compression = On #Off if using wordpress
opcache.enable=1
opcache.enable_cli=1
opcache.memory_consumption=512
opcache.max_accelerated_files=100000
opcache.consistency_checks=0
To make the configure PHP 8.1 on Ubuntu 22.04 effective, you need to save the file and restart php8.1-fpm.
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
![Configure mutiple version php on ubuntu Set mutiple version php on ubuntu](https://arriveddev.com/wp-content/uploads/2023/03/how-to-install-and-configure-php-8-1-on-ubuntu-22-5.jpg)
With PHP 8.1 successfully installed and optimized, 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.
– Advertising –