Although PHP 7.4 is not the latest version, it remains a mandatory requirement for several popular web frameworks and applications like Magento 2.4. In this guide, you’ll learn how to install PHP 7.4 on Ubuntu 22.04 or 20.04 LTS, ensuring compatibility and optimal performance for your projects.
PHP 7.4: Installation and Performance Optimization
Step 1: Add PPA repository and install PHP
The following dependent packages will need to be installed, most of which are already on Ubuntu 20.04 | 22.04, but running the command below can help ensure they are installed.
sudo apt install software-properties-common
sudo add-apt-repository ppa:ondrej/php
Next, update the system and install PHP 7.4 on ubuntu 22.04 | 20.04
sudo apt-get update
sudo apt install php7.4 -y
Change php7.4 if you want to install another version, for example, php7.3, php7.2
Step 2: Install php7.4-fpm
You can also install modules based on your application requirements. Use the following command to search for Ubuntu PHP 7.4 modules available in the package repository.
sudo apt-cache search php7*
You can install php7.4 on ubuntu Ubuntu 20.04 | 22.04 the necessary modules on your system. Just change the PHP version with the package name according to your requirement:
sudo apt install php7.4-common php7.4-mysql php7.4-xml php7.4-xmlrpc php7.4-curl php7.4-gd php7.4-imagick php7.4-cli php7.4-dev php7.4-imap php7.4-mbstring php7.4-opcache php7.4-soap php7.4-zip php7.4-intl php7.4-fpm php7.4-json php7.4-zip php7.4-bcmath php7.4-mcrypt
Enable php-fpm and allow php-fpm to start with the system:
sudo systemctl enable php7.4-fpm
sudo service php7.4-fpm restart
Step 3: Configure php7.4-fpm
Open the directory under the path /etc/php/7.4/fpm/pool.d and edit the file www.conf
.
- user = arriveddev
- listen.owner = arriveddev
+ arriveddev is the username on your operating system that allows it to manage PHP. The default is www-data but you should change it. Save the file and reset php-fpm ( change your PHP version number)
+ listen is the communication port between PHP and Nginx, you need to check that the value of listening is correctly installed in Nginx with separate configs.
sudo service php7.4-fpm restart
Step 4: Optimize php7.4-fpm
Next, come the optimization and configuration of php.ini, navigate to the variables and edit the values to suit your needs. You need to edit both files as below, then find the values and change them.
sudo gedit /etc/php/7.4/cli/php.ini sudo gedit /etc/php/7.4/fpm/php.ini
Save the file and reset php-fpm for the config to take effect, don’t forget to change your PHP version number.
sudo service php7.4-fpm restart
Step 5. Config multiple PHP versions on Ubuntu / Linux
You can also install and use multiple PHP versions for each website (project). The command changes the default PHP version, just enter the version number.
sudo update-alternatives --config php
This article guide you on how to install php7.4 on Ubuntu 22.04 | 20.04, if you want to install multiple versions just change the version number. For example php7.4, php7.3, etc.
Thank admin, bài viết rất chi tiết