How to install PHP 7.4 in Ubuntu 20.04?

The PHP 7.4 was released on November 2019, adding new features like: type properties, arrow functions, mumeric literal separator, weak references and more.

PHP-FPM is a FastCGI process manager useful for heavy-loaded sites.

Today we’re going to install PHP 7.4 and PHP-FPM in Ubuntu 20.04

This post is part of: Install LAMPP Stack in Ubuntu 20.04

Related content

Install PHP and PHP-FPM

PHP-FPM is used together with a web server like Apache or NGINX, PHP-FPM serves dynamic content, while the web server serve static content and act like a reverse proxy in front of PHP-FPM.

PHP-FPM introduces the concept of pools, each pool can receive connections on a TPC/IP (IP:Port) socket or on a UNIX socket, and can run under a different user and group. Each pool has its configuration file, by default in Ubuntu/Debian the configuration file can be found in:

/etc/php/7.4/fpm/pool.d/www.conf

Some directives

  • listen: Address in which the PHP-FPM service accepts requests. Valid syntax are: ‘ip.add.re.ss: port’, ‘port’, ‘/path/to/unix/socket’, by default it has the value of /run/php/php7.4-fpm.sock for the www pool, note that if the PHP-FPM service is running in a different server than the web server then you must set this directive to ‘ip.add.re.ss:port’ or ‘port’, if you only specify the ‘port ‘value then the service receives connections in any IP address, otherwise only at the specified IP address. This option is mandatory.
  • user: Usuario Unix bajo el cual se ejecutan los procesos FPM para un pool determinado. Esta opción es obligatoria.
  • group: Grupo Unix bajo el cual se ejecutan los procesos FPM para un pool determinado. Si no es establecido, el grupo del usuario por defecto será usado.
  • listen.allowed_clients: List of IPv4 addresses of FastCGI clients which are allowed to connect. Makes sense only with a TCP/IP listening socket. Each address must be separated by a comma. If this value is left blank, connections will be accepted from any ip address. Default value: any. IPv6 addresses are allowed since PHP 5.5.20 and 5.6.4.
  • request_terminate_timeout: The timeout for serving a single request after which the worker process will be killed. This option should be used when the ‘max_execution_time’ ini option does not stop script execution for some reason. A value of ‘0’ means ‘Off’. Available units: s(econds)(default), m(inutes), h(ours), or d(ays). Default value: 0.

For more details about PHP-FPM configuration check PHP-FPM Configuration

In order to install PHP and PHP-FPM type the following command:

$ sudo apt install php-fpm
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following additional packages will be installed:
  php-common php7.4-cli php7.4-common php7.4-fpm php7.4-json php7.4-opcache php7.4-readline
Suggested packages:
  php-pear
The following NEW packages will be installed:
  php-common php-fpm php7.4-cli php7.4-common php7.4-fpm php7.4-json php7.4-opcache php7.4-readline
0 upgraded, 8 newly installed, 0 to remove and 0 not upgraded.
Need to get 4,079 kB of archives.
After this operation, 18.1 MB of additional disk space will be used.
Do you want to continue? [Y/n] 

After the installation process finish, you can find the configuration files under this directory structure.

/etc/php/7.4/
├── cli
│   └── conf.d
├── fpm
│   ├── conf.d
│   └── pool.d
└── mods-available

Manage the PHP-FPM service

In this part we will learn how to start, stop or restart the fpm service, for this we will use the systemd initialization system (systemd is a replacement for the SysV initialization system also a configuration and service management suite for the GNU/Linux operating system)

Comprobar estado

In order to check the service status type the following command:

$ sudo systemctl status php7.4-fpm.service
● php7.4-fpm.service - The PHP 7.4 FastCGI Process Manager
     Loaded: loaded (/lib/systemd/system/php7.4-fpm.service; enabled; vendor preset: enabled)
     Active: active (running) since Thu 2020-08-27 21:08:35 UTC; 1 weeks 1 days ago
       Docs: man:php-fpm7.4(8)
   Main PID: 707 (php-fpm7.4)
     Status: "Processes active: 0, idle: 2, Requests: 0, slow: 0, Traffic: 0req/sec"
      Tasks: 3 (limit: 1075)
     Memory: 12.2M
     CGroup: /system.slice/php7.4-fpm.service
             ├─707 php-fpm: master process (/etc/php/7.4/fpm/php-fpm.conf)
             ├─766 php-fpm: pool www
             └─767 php-fpm: pool www

Aug 27 21:08:34 ubuntu systemd[1]: Starting The PHP 7.4 FastCGI Process Manager...
Aug 27 21:08:35 ubuntu systemd[1]: Started The PHP 7.4 FastCGI Process Manager.

Start the service

If the above command had showed the following information (note the Active: inactive):

● php7.4-fpm.service - The PHP 7.4 FastCGI Process Manager
     Loaded: loaded (/lib/systemd/system/php7.4-fpm.service; enabled; vendor preset: enabled)
     Active: inactive (dead) since Sat 2020-09-05 01:36:38 UTC; 1s ago
       Docs: man:php-fpm7.4(8)
    Process: 707 ExecStart=/usr/sbin/php-fpm7.4 --nodaemonize --fpm-config /etc/php/7.4/fpm/php-fpm.conf (code=exited, status=0/SUCCESS)
    Process: 30653 ExecStopPost=/usr/lib/php/php-fpm-socket-helper remove /run/php/php-fpm.sock /etc/php/7.4/fpm/pool.d/www.conf 74 (code=exited, status=0/SUCCESS)
   Main PID: 707 (code=exited, status=0/SUCCESS)
     Status: "Processes active: 0, idle: 2, Requests: 0, slow: 0, Traffic: 0req/sec"

Aug 27 21:08:34 ubuntu systemd[1]: Starting The PHP 7.4 FastCGI Process Manager...
Aug 27 21:08:35 ubuntu systemd[1]: Started The PHP 7.4 FastCGI Process Manager.
Sep 05 01:36:38 ubuntu systemd[1]: Stopping The PHP 7.4 FastCGI Process Manager...
Sep 05 01:36:38 ubuntu systemd[1]: php7.4-fpm.service: Succeeded.
Sep 05 01:36:38 ubuntu systemd[1]: Stopped The PHP 7.4 FastCGI Process Manager.

then we can start the service with this command:

$ sudo systemctl start php7.4-fpm.service

Restart the php7.4-fpm

To apply any configuration file modification, you must restart the server with the following command:

$ sudo systemctl restart php7.4-fpm.service

Reload the configuration

It is better to use the following command to reload configuration changes since the service does not lose connections.

$ sudo systemctl reload php7.4-fpm.service

Stop the service

To stop the service execute the following command:

$ sudo systemctl stop php7.4-fpm.service

Start with the operating system

You can verify that the php7.4-fpm service will start after a system reboot by running the following command:

$ sudo systemctl is-enabled php7.4-fpm.service
enabled

The output of the previous command should be enabled or disabled, if it’s disabled execute:

$ sudo systemctl enable php7.4-fpm.service 
Synchronizing state of php7.4-fpm.service with SysV service script with /lib/systemd/systemd-sysv-install.
Executing: /lib/systemd/systemd-sysv-install enable php7.4-fpm
Created symlink /etc/systemd/system/multi-user.target.wants/php7.4-fpm.service → /lib/systemd/system/php7.4-fpm.service.

If you want to revert the above command execute:

$ sudo systemctl disable php7.4-fpm.service 
Synchronizing state of php7.4-fpm.service with SysV service script with /lib/systemd/systemd-sysv-install.
Executing: /lib/systemd/systemd-sysv-install disable php7.4-fpm
Removed /etc/systemd/system/multi-user.target.wants/php7.4-fpm.service.

YouTube video

Install LAMP Stack in Ubuntu 20.04, 1 (2)

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.