How to install and configure PHP 8.1 on Ubuntu 22.04, including installation of full extensions for php8.1-fpm and configuration for multiple PHP versions on Ubuntu or Debian systems.
4 Step to Install and Configure PHP 8.1 on Ubuntu 22.04 or 20.04 LTS
On November 25, 2021, PHP 8.1 was officially released with a plethora of new features and performance improvements. Among the significant new feature sets that come with PHP are the JIT compiler, named arguments, union types, and attributes. For more information, please refer to the following link.
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 PHP 8.1 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 extensions for your project.
Step 2: Configure php8.1-fpm on Ubuntu 22.04
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: PHP8.1-FPM Security Configuration Permissions
PHP-FPM (PHP FastCGI Process Manager) is a process manager for FastCGI that handles PHP. There are two values that need to be configured, namely user and listen, which allow you to specify the user used to execute PHP processes and how requests are passed from the web server to PHP-FPM.
Furthermore, user and listen in PHP-FPM also have security and permission implications. If you use the default user www-data or listen on a socket on all IPs, this can make your PHP application vulnerable to attacks. Configure PHP 8.1 on Ubuntu for user and listen to be appropriate and limiting their access can help strengthen the security of your website. Ensure that only the designated user can execute PHP processes on the server.
To configure php8.1-fpm on Ubuntu 22.04, access the directory “/etc/php/8.1/fpm/pool.d” and edit the file “www.conf”. Find and change the values for the following lines:
user = www-data | user = itsmeit |
listen.owner = www-data | listen.owner = itsmeit |
In which, user and listen.owner need to be set to the username that you want php8.1-fpm to manage. For example, on my Ubuntu, the username is “itsmeit” so I will set it to “itsmeit”. After making the changes, you need to restart php8.1-fpm to apply the updates.
sudo service php8.1-fpm restart
Step 4: Optimize php.ini on Ubuntu 22.04
Next, you will need to optimize and configure 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
The following values are the ones that should be configured 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 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 with 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
Above is a comprehensive guide on how to install and utilize configure PHP 8.1 on Ubuntu 22.04 operating system. Additionally, you can expand your knowledge of PHP permissions to enhance the performance of your application or website by referring to this resource.
– Advertising –