Install PHP 8.1 and Setting Nginx PHP-FPM on Ubuntu 22.04

Install PHP 8.1 dan Setting Nginx PHP-FPM pada Ubuntu 22.04


  • Tambahkan repository ondrej PHP pada ubuntu 22.04
root@ubuntu-2204:~# apt install software-properties-common -y
root@ubuntu-2204:~# add-apt-repository ppa:ondrej/php
root@ubuntu-2204:~# apt update
  • Install PHP 8.1
root@ubuntu-2204:~# apt install php8.1 -y
  • Install module php 8.1, sudah termasuk php-fpm
root@ubuntu-2204:~# apt install -y php8.1-{common,cli,gd,curl,mysql,xml,mbstring,zip,ldap,xmlrpc,curl,intl,fpm,imagick,dev,imap,opcache,soap,memcached,redis}
  • Cek versi php
root@ubuntu-2204:~# php -v
PHP 8.1.5 (cli) (built: Apr 21 2022 10:32:13) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.1.5, Copyright (c) Zend Technologies
    with Zend OPcache v8.1.5, Copyright (c), by Zend Technologies

Setting Nginx dengan PHP-FPM

  • Cek status serveri php-fpm, pastikan running
root@ubuntu-2204:~# systemctl enable --now php8.1-fpm
root@ubuntu-2204:~# systemctl status php8.1-fpm
● php8.1-fpm.service - The PHP 8.1 FastCGI Process Manager
     Loaded: loaded (/lib/systemd/system/php8.1-fpm.service; enabled; vendor preset: enabled)
     Active: active (running) since Wed 2022-04-27 00:54:10 UTC; 20min ago
       Docs: man:php-fpm8.1(8)
   Main PID: 9781 (php-fpm8.1)
     Status: "Processes active: 0, idle: 2, Requests: 1, slow: 0, Traffic: 0req/sec"
      Tasks: 3 (limit: 2327)
     Memory: 22.5M
        CPU: 349ms
  • Setting fpm php.ini
root@ubuntu-2204:~# nano /etc/php/8.1/fpm/php.ini
upload_max_filesize = 100M
post_max_size = 100M
memory_limit = 256M
max_execution_time = 300
max_input_time = 300
date.timezone = Asia/Jakarta
zlib.output_compression = On
zlib.output_compression_level = 6
root@ubuntu-2204:~# systemctl restart php8.1-fpm
  • Pastika pada server block nginx sudah di tambahakan baris berikut untuk mengaktifkan php-fpm
   location ~ \.php$ {
      include /etc/nginx/fastcgi_params;
      fastcgi_pass unix:/run/php/php8.1-fpm.sock;
      fastcgi_index index.php;
      fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
      include fastcgi_params;
      fastcgi_read_timeout 300;
      proxy_read_timeout 600;
   }

Verifikasi PHP-FPM

  • Buat file info.php untuk cek konfigurasi php
root@ubuntu-2204:~# nano /var/www/nginx.rainui.cloud/info.php

<?php phpinfo();
  • Cek pada browser, pastikan pada Server API sudah menggunakan FPM/FastCGI

herdiana3389

A system administrator with skills in system administration, virtualization, linux, windows, networking, cloud computing, container, etc.