Cara Install PHP 8.0 di Ubuntu 20.04 Apache2 & NginX

Cara Install PHP 8.0 di Ubuntu 20.04 – Dalam tutorial kali ini, kita akan mempelajari cara install PHP 8 pada VPS, Cloud hosting, atau dedicated hosting. Dan mengkonfigurasi dengan Apache2 maupun Nginx.

Secara resmi dari php.net versi terbaru PHP adalah versi 8.0. Instalasi PHP ini di test menggunakan Ubuntu server 20.04 pada virtual machine AWS EC2. Dan ini akan bekerja pada semua server/cloud hosting berbasis Ubuntu.

Apa saja yang perlu kita lakukan untuk tutorial Cara Install PHP 8?

  1. Akses SSH ke server dan melakukan update
  2. Menambahkan PPA repository untuk PHP 8
  3. Install PHP 8 untuk Apache2 / NginX
  4. Install ekstensi PHP 8
  5. Mengkonfigurasi PHP 8 2
  6. Testing PHP 8.0

Apa saja yang dibutuhkan tutorial Cara Install PHP 8?

  1. VPS atau akun AWS. Baca disini cara membuat akun AWS
  2. Ubuntu 20.04. Baca disini cara membuat Instance/virtual machine dengan Ubuntu
  3. Apache2: Baca disini cara menginstall Apache2 di AWS EC2, atau Nginx: Baca disini cara Install NginX
  4. SSH & User dengan privilege perintah sudo

Step 1: Akses SSH ke server dan melalukan update

Seperti pada tutorial sebelumnya, kita membutuhkan akses ke server dengan SSH. Setelah login via SSH, biasakan selalu untuk melakukan update package.

sudo apt update -y && sudo sudo apt upgrade -y


Proses ini akan memperbarui package index dan memperbarui package yang diinstal ke versi terbaru.

Step 2: Menambahkan PPA repository untuk PHP 8

Tambahkan ondrej/php yang memiliki package PHP 8 dan ekstensi PHP lain yang diperlukan.

sudo apt install software-properties-common
sudo add-apt-repository ppa:ondrej/php

PPA Apache2

 sudo add-apt-repository ppa:ondrej/apache2

PPA NginX

 sudo add-apt-repository ppa:ondrej/nginx

Setelah menambahkan PPA ondrej, lakukan update:

sudo apt update

Setelah menambahkan PPA dan perform update, barulah kita menginstall PHP 8.

PPA adalah software repositories yang dirancang untuk pengguna Ubuntu dan lebih mudah dipasang daripada repositori pihak ketiga lainnya. PPA sering digunakan untuk mendistribusikan perangkat lunak pra-rilis sehingga dapat diuji.

Step 3: Install PHP 8 untuk Apache2 / Install PHP 8 FPM untuk NginX

Install PHP 8 Apache2

Jalankan perintah ini untuk menginstall PHP8.

sudo apt install php8.0

Setelah instalasi berhasil, kita verifikasi instalasi PHP 8 dengan perintah berikut:

php -v
PHP 8.0.10 (cli) (built: Aug 26 2021 15:50:07) ( NTS )
Copyright (c) The PHP Group
Zend Engine v4.0.10, Copyright (c) Zend Technologies
    with Zend OPcache v8.0.10, Copyright (c), by Zend Technologies

Install PHP 8 FPM Nginx

Untuk Nginx kita perlu menginstal FPM (FastCGI Process Manager) , jalankan perintah berikut untuk menginstal PHP 8 FPM:

sudo apt install php8.0-fpm

Setelah instalasi berhasil, kita verifikasi instalasi PHP 8 FPM dengan perintah berikut:

php-fpm8.0 -v
PHP 8.0.11 (fpm-fcgi) (built: Sep 23 2021 21:26:24)
Copyright (c) The PHP Group
Zend Engine v4.0.11, Copyright (c) Zend Technologies
    with Zend OPcache v8.0.11, Copyright (c), by Zend Technologies

Step 4: Install ekstensi PHP 8

Untuk menginstall ekstensi pada PHP 8 sangat mudah.

sudo apt install php8.0-nama_ekstensi_yang_akan_diinstall

Kemudian lanjut untuk menginstall ekstensi umum PHP.

sudo apt install php8.0-common php8.0-mysql php8.0-xml php8.0-curl php8.0-gd php8.0-imagick php8.0-cli php8.0-dev php8.0-imap php8.0-mbstring php8.0-opcache php8.0-soap php8.0-zip -y

Step 5: Mengkonfigurasi PHP 8

Konfigurasi PHP 8 untuk Apache2

Sekarang kita mengkonfigurasi PHP untuk webserver dengan mengubah beberapa nilai dalam file php.ini. Untuk PHP 8 dengan Apache, lokasi php.ini akan berada di direktori berikut.

sudo nano /etc/php/8.0/apache2/php.ini

Tekan F6 pada keyboard/virtual keyboard untuk melakukan pencarian nama konfigurasi dan mengubah nilai default konfigurasi tersebut agar webserver bekerja lebih baik dengan kata lain lebih optimal.

PicsArt 10 20 11.12.57
Tombol fungsi pada virtual keyboard
upload_max_filesize = 32M 
post_max_size = 48M 
memory_limit = 256M 
max_execution_time = 600 
max_input_vars = 3000 
max_input_time = 1000

Setelah melakukan perubahan simpan file php.ini (CTRL+O), kemudian enter dan keluar dari editor (CTRL+X). Agar konfigurasi terbaru dapat berjalan kita perlu melalukan restart service Apache2.

sudo service apache2 restart

Konfigurasi PHP 8 FPM untuk Nginx

Untuk PHP 8 FPM dengan Nginx, lokasi php.ini akan berada di direktori berikut:

sudo nano /etc/php/8.0/fpm/php.ini

Tekan F6 pada keyboard/virtual keyboard untuk melakukan pencarian nama konfigurasi dan mengubah nilai default konfigurasi tersebut agar webserver bekerja lebih baik dengan kata lain lebih optimal.

upload_max_filesize = 32M 
post_max_size = 48M 
memory_limit = 256M 
max_execution_time = 600 
max_input_vars = 3000 
max_input_time = 1000

Simpan file php.ini tersebut dan restart PHP FPM agar bekerja dengan konfigurasi yang baru kita buat:

sudo php-fpm8.0 -t 
sudo service php8.0-fpm restart

Step 6: Testing PHP 8 / PHP 8 FPM

Akses direktori webserver, dan kita membuat file php di dalam direktori tersebut.

cd /var/www/html
~/var/www/html$ sudo nano testphpinfo.php

Buat file testphpinfo.php dengan kode seperti dibawah ini:

<?php 
	echo phpinfo(); 
?>

Simpan file testphpinfo.php dan keluar editor. Kemudian buka web browser dan akses file testphpinfo.php.

Cara Install PHP 8

php8fpmnginx
PHP 8 FPM phpinfo()

PHP 8 maupun PHP 8 FPM bekerja dan berfungsi dengan baik. Demi keamanan segera hapus file testphpinfo.php setelah kita akses via web browser.

sudo rm /var/www/html/testphpinfo.php 

Atau

sudo unlink /var/www/html/testphpinfo.php

Cara Mengatasi Error “403 Forbidden NginX”

Kebanyakan saat pertama kali menginstall PHP 8 FPM pada NginX dan menjalankan file php akan terjadi error. Pertama-tama kita identifikasi dulu penyebab errornya dengan melihat error log. Dengan error log tersebut kita akan terbantu untuk menemukan solusi.

Check Error Log pada NginX

Masih dalam terminal konsol jalankan perintah berikut:

cat /var/log/nginx/error.log

Atau:

tail -n 1000 /var/log/nginx/error.log
2021/10/20 16:38:22 [error] 39366#39366: *3 directory index of "/var/www/html/" is forbidden, client: xxx.xxx.xxx.xxx, server: _, request: "GET / HTTP/1.1", host: "xxx.xxx.xxx.xxx"
2021/10/20 16:47:33 [notice] 40843#40843: using inherited sockets from "6;7;"

Error log tersebut mengindikasikan bahwa directory index “/var/www/html/” forbidden.

Fix Error 403 Forbidden

# You should look at the following URL's in order to grasp a solid understanding
# of Nginx configuration files in order to fully unleash the power of Nginx.
# http://wiki.nginx.org/Pitfalls
# http://wiki.nginx.org/QuickStart
# http://wiki.nginx.org/Configuration
#
# Generally, you will want to move this file somewhere, and start with a clean
# file but keep this around for reference. Or just disable in sites-enabled.
#
# Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples.
##

# Default server configuration
#
server {
        listen 80 default_server;
        listen [::]:80 default_server;

        # SSL configuration
        #
        # listen 443 ssl default_server;
        # listen [::]:443 ssl default_server;
        #
        # Note: You should disable gzip for SSL traffic.
        # See: https://bugs.debian.org/773332
        #
        # Read up on ssl_ciphers to ensure a secure configuration.
        # See: https://bugs.debian.org/765782
        #
        # Self signed certs generated by the ssl-cert package
        # Don't use them in a production server!
        #
        # include snippets/snakeoil.conf;

        root /var/www/html;

        # Add index.php to the list if you are using PHP
        index index.html index.htm index.nginx-debian.html;

        server_name _;

        location / {
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                try_files $uri $uri/ =404;
	        # proxy_pass http://localhost:8080;
        	# proxy_http_version 1.1;
	        # proxy_set_header Upgrade $http_upgrade;
        	# proxy_set_header Connection 'upgrade';
	        # proxy_set_header Host $host;
        	# proxy_cache_bypass $http_upgrade;
        }

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #       include snippets/fastcgi-php.conf;
        #
        #       # With php7.0-cgi alone:
        #       fastcgi_pass 127.0.0.1:9000;
        #       # With php7.0-fpm:
        #       fastcgi_pass unix:/run/php/php7.0-fpm.sock;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #       deny all;
        #}
}


# Virtual Host configuration for example.com
#
# You can move that to a different file under sites-available/ and symlink that
# to sites-enabled/ to enable it.
#
#server {
#       listen 80;
#       listen [::]:80;
#
#       server_name example.com;
#
#       root /var/www/example.com;
#       index index.html;
#
#       location / {
#               try_files $uri $uri/ =404;
#       }
#}

Pada baris 38 tambahan index.php pada list tersebut, contoh:

index index.php index.html index.htm index.nginx-debian.html;

Dan hilangkan mark # pada baris 56 dengan pembuka “{” sampai penutup “}” menjadi:

location ~ \.php$ {
               include snippets/fastcgi-php.conf;
               fastcgi_pass unix:/run/php/php8.0-fpm.sock;
        }

Lakukan pengecheckan syntax pada NginX:

sudo nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

Simpan file default konfigurasi tersebut dan restart NginX:

sudo service nginx restart

Test kembali pada web browser untuk menjalankan/membuka file berekstensi php.

Demikian Tutorial Cara Install PHP 8.0 di Ubuntu 20.04 Apache2 & NginX.

Semoga bermanfaat.

Tinggalkan komentar