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 lamp.bikinin.website (sesuaikan dengan domain anda)
mkdir -p /var/www/lamp.bikinin.website
nano /var/www/lamp.bikinin.website/index.html
  • File index.html
<!DOCTYPE html>
<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">
    <title>sys-ops.id</title>
  </head>
  <body>
   <h1><center>Virtual Host lamp.bikinin.website - Ubuntu 22.04</h1>
   <h3><center>Sys-Ops.id</h3>
   <p>
   <li>install apache2</li>
   <li>install mariadb 10.8</li>
   <li>install php 8.1</li>
  </body>
</html>
  • Buat file config virtual host untuk domain lamp.bikinin.website
nano /etc/apache2/sites-available/lamp.bikinin.website.conf
<VirtualHost *:80>
     ServerAdmin [email protected]
     ServerName lamp.bikinin.website
     ServerAlias www.lamp.bikinin.website
     DocumentRoot /var/www/lamp.bikinin.website

     <Directory /var/www/lamp.bikinin.website>
        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
     </Directory>

     ErrorLog ${APACHE_LOG_DIR}/error.log
     CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
  • Enable rewrite module, headers module, dan konfigurasi domain lamp.bikinin.website.conf
a2enmod rewrite
a2enmod headers
a2ensite lamp.bikinin.website.conf
a2enmod http2
a2dissite 000-default
systemctl restart apache2
  • Akses domain lewat browser: http://lamp.bikinin.website

  • Install MariaDB 10.8
curl -LsS https://downloads.mariadb.com/MariaDB/mariadb_repo_setup | 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
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 mariadb.org 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/lamp.bikinin.website/info.php

<?php phpinfo();

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

<?php
// 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;
    exit;
}
echo "Sukses: Koneksi ke database berhasil terhubung." . PHP_EOL;
echo "Informasi Host: " . mysqli_get_host_info($link) . PHP_EOL;
mysqli_close($link);
?>
  • 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 lamp.bikinin.website
  • 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

herdiana3389

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