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

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

Install LAMP Stack (Apache, MariaDB 10.8, 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 mysql 8.0</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 MySQL 8.0
apt install mysql-server mysql-client -y
  • Cek versi mysql
mysql --version
   mysql  Ver 8.0.30-0ubuntu0.22.04.1 for Linux on x86_64 ((Ubuntu))
  • mysql secure installation
mysql_secure_installation
   Press y|Y for Yes, any other key for No: y
   Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG: 1
   New password:
   Re-enter new password:
   Estimated strength of the password: 100
   Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : y
   Remove anonymous users? (Press y|Y for Yes, any other key for No) : y
   Disallow root login remotely? (Press y|Y for Yes, any other key for No) : y
   Remove test database and access to it? (Press y|Y for Yes, any other key for No) : y
   Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y
  • Jika terjadi error saat menjalankan mysql_secure_installation: Failed! Error: SET PASSWORD has no significance for user ‘root’@’localhost’ as the authentication method used doesn’t store authentication data in the MySQL server. Please consider using ALTER USER instead if you want to change authentication parameters. Solusi >
sudo mysql
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password by 'P@ssw0rd!';
exit
mysql_secure_installation
  • Login ke dalam mysql
mysql -u root -p
   Enter password:
   Welcome to the MySQL monitor.  Commands end with ; or \g.
   Your MySQL connection id is 22
   Server version: 8.0.30-0ubuntu0.22.04.1 (Ubuntu)

   Copyright (c) 2000, 2022, Oracle and/or its affiliates.
   Oracle is a registered trademark of Oracle Corporation and/or its
   affiliates. Other names may be trademarks of their respective owners.

   Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

   mysql>

  • 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';
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.