Install LAMP Stack (Apache, MariaDB 10.8, PHP 8.1) on Ubuntu 22.04

Install LAMP (Apache, MariaDB 10.8, PHP 8.1) di Ubuntu 22.04

Install LAMP Stack (Apache, MySQL 8.0, PHP 8.1) on Ubuntu 22.04

  • Update repository ubuntu
apt update && apt upgrade -y

  • Install Apache
apt install apache2 -y
  • Akses web server apache lewat browser: http://ip_address_ubuntu

  • Setting Virtual Host
  • Buat directory dan file index html untuk domain (sesuaikan dengan domain anda)
mkdir -p /var/www/
nano /var/www/
  • File index.html
<!DOCTYPE html>
<html lang="en" dir="ltr">
    <meta charset="utf-8">
   <h1><center>Virtual Host - Ubuntu 22.04</h1>
   <li>install apache2</li>
   <li>install mariadb 10.8</li>
   <li>install php 8.1</li>
  • Buat file config virtual host untuk domain
nano /etc/apache2/sites-available/
<VirtualHost *:80>
     ServerAdmin [email protected]
     DocumentRoot /var/www/

     <Directory /var/www/>
        Options Indexes FollowSymLinks
        Require all granted
        DirectoryIndex index.php index.html
        AllowOverride All
        Order allow,deny
        Allow from all

        Header set Access-Control-Allow-Headers "ORIGIN, X-REQUESTED-WITH, CONTENT-TYPE"
        Header set Access-Control-Allow-Methods "POST, GET, OPTIONS, PUT, DELETE"
        Header set Access-Control-Allow-Origin "*"
        Header set Access-Control-Allow-Credentials true
        Header set X-XSS-Protection 1;mode=block
        Header set X-Frame-Options SAMEORIGIN
        Header set X-Content-Type-Options nosniff
        Header set Strict-Transport-Security "max-age=15552000; includeSubDomains;preload"
        Header set Referrer-Policy strict-origin-when-cross-origin
        Header set Access-Control-Max-Age 60000

     ErrorLog ${APACHE_LOG_DIR}/error.log
     CustomLog ${APACHE_LOG_DIR}/access.log combined
  • Enable rewrite module, headers module, dan konfigurasi domain
a2enmod rewrite
a2enmod headers
a2enmod http2
a2dissite 000-default
systemctl restart apache2
  • Akses domain lewat browser:

  • Install MariaDB 10.8
curl -LsS | sudo bash -s -- --mariadb-server-version=10.8
apt install mariadb-server mariadb-client -y
  • Cek versi mariadb
mysql --version
   mysql  Ver 15.1 Distrib 10.8.5-MariaDB, for debian-linux-gnu (x86_64) using  EditLine wrapper
  • mysql secure installation
   Enter current password for root (enter for none):  <enter>
   Switch to unix_socket authentication [Y/n] n
   Change the root password? [Y/n] Y
   Remove anonymous users? [Y/n] Y
   Disallow root login remotely? [Y/n] Y
   Remove test database and access to it? [Y/n] Y
   Reload privilege tables now? [Y/n] Y
  • Login ke dalam mysql
mysql -u root -p
   Enter password:
   Welcome to the MariaDB monitor.  Commands end with ; or \g.
   Your MariaDB connection id is 39
   Server version: 10.8.5-MariaDB-1:10.8.5+maria~ubu2204 binary distribution

   Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
   Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

   MariaDB [(none)]>

  • Install PHP 8.1
apt install software-properties-common -y
add-apt-repository ppa:ondrej/php && apt update
apt install php8.1 -y
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
php --version
   PHP 8.1.11 (cli) (built: Sep 29 2022 22:29:14) (NTS)
   Copyright (c) The PHP Group
   Zend Engine v4.1.11, Copyright (c) Zend Technologies
   with Zend OPcache v8.1.11, Copyright (c), by Zend Technologies
  • Setting PHP-FPM
systemctl enable --now php8.1-fpm
a2dismod php8.1
a2dismod mpm_prefork
a2enmod mpm_event proxy_fcgi setenvif
a2enconf php8.1-fpm
systemctl restart apache2
  • Setting php.ini
nano /etc/php/8.1/fpm/php.ini

upload_max_filesize = 100M
post_max_size = 100M
memory_limit = 512M
max_execution_time = 300
max_input_time = 300
date.timezone = Asia/Jakarta
zlib.output_compression = On
zlib.output_compression_level = 6
  • Restart service apache dan php-fpm
systemctl restart php8.1-fpm
systemctl restart apache2
  • Verifikasi php-fpm
nano /var/www/

<?php phpinfo();

  • Testing koneksi database dengan php
  • Buat database baru
CREATE USER 'user_ops'@'localhost' IDENTIFIED BY 'P@ssw0rd!';
GRANT ALL PRIVILEGES ON sys_ops_id.* TO 'user_ops'@'localhost' IDENTIFIED BY 'P@ssw0rd!';
  • Buat file koneksi.php
nano /var/www/

// Koneksi Database
$konek = mysqli_connect("localhost", "user_ops", "P@ssw0rd!", "sys_ops_id");

if (!$konek) {
    echo "Error: Tidak dapat terhubung ke database." . PHP_EOL;
    echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL;
    echo "Debugging error: " . mysqli_connect_error() . PHP_EOL;
echo "Sukses: Koneksi ke database berhasil terhubung." . PHP_EOL;
echo "Informasi Host: " . mysqli_get_host_info($link) . PHP_EOL;
  • Verifikasi lewat browser

  • Opsional
  • Install Let’s Encrypt (Free SSL)
apt install certbot python3-certbot-apache -y
certbot --apache --agree-tos --redirect -m [email protected] -d
  • Verifikasi lewat browser
  • Re-new ssl
certbot renew --dry-run
  • Aktifkan renew ssl pada corn job
crontab -e
   * */12 * * * root /usr/bin/certbot renew >/dev/null 2>&1

systemctl restart cron


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