How to Install LAMP Stack with MariaDB on Ubuntu 25.04

A LAMP stack is a group of open-source software that is typically installed together to enable a server to host dynamic websites and web apps. This term is actually an acronym which represents the Linux operating system, with the Apache web server. The site data is stored in a MySQL or MariaDB database, and dynamic content is processed by PHP.

Update the System

First, check for any pending system upgrades by running the following command,

apt update
apt upgrade

Install Apache

To install Apache along with its utilities, run the following command,

apt install -y apache2 apache2-utils

Next, check the status of Apache by running,

systemctl status apache2

Output:

root@server:~# systemctl status apache2
● apache2.service - The Apache HTTP Server
     Loaded: loaded (/usr/lib/systemd/system/apache2.service; enabled; preset: enabled)
     Active: active (running) since Thu 2025-04-24 14:01:45 UTC; 19s ago
 Invocation: ecad99e69c7148f6b9bec2160178cc2c
       Docs: https://httpd.apache.org/docs/2.4/
   Main PID: 3350 (apache2)
      Tasks: 55 (limit: 3985)
     Memory: 5.3M (peak: 5.6M)
        CPU: 31ms
     CGroup: /system.slice/apache2.service
             ├─3350 /usr/sbin/apache2 -k start
             ├─3352 /usr/sbin/apache2 -k start
             └─3353 /usr/sbin/apache2 -k start

If Apache is not active, you can start it using the following command,

systemctl start apache2

Use the following command to enable Apache to start automatically at boot time,

systemctl enable apache2

You can confirm the Apache2 version with the following command,

apache2 -v

Output:

root@server:~# apache2 -v
Server version: Apache/2.4.63 (Ubuntu)
Server built:   2025-02-19T07:03:52

Enable Firewall

We will open the HTTP and HTTPS ports in the firewall so the Apache web server can be accessed.

Allow the HTTP and HTTPS ports by running the following command,

ufw allow http

ufw allow https

Output:

root@server:~# ufw allow http
ufw allow https
Rules updated
Rules updated (v6)
Rules updated
Rules updated (v6)

To verify if it's working, open your server's IP address in a browser. You should see the Apache default welcome page displayed.

http://ip-address

Node: Replace ip-address with actual IP Address

images

Install MariaDB Server

Install MariaDB server, run the below command:

apt install mariadb-server mariadb-client mariadb-client-compat -y

Once the installation is complete, check the status of MariaDB by running,

systemctl status mariadb

Output:

root@server:~# systemctl status mariadb
● mariadb.service - MariaDB 11.4.5 database server
     Loaded: loaded (/usr/lib/systemd/system/mariadb.service; enabled; preset: enabled)
     Active: active (running) since Thu 2025-04-24 14:03:51 UTC; 4s ago
 Invocation: 4b52e9174438494baddddeccc69cd0d2
       Docs: man:mariadbd(8)
             https://mariadb.com/kb/en/library/systemd/
   Main PID: 4538 (mariadbd)
     Status: "Taking your SQL requests now..."
      Tasks: 13 (limit: 26301)
     Memory: 166.5M (peak: 256M)
        CPU: 3.653s
     CGroup: /system.slice/mariadb.service
             └─4538 /usr/sbin/mariadbd

If MariaDB is not active, you can start it using the following command,

systemctl start mariadb

Use the following command to enable MariaDB to start automatically at boot time,

systemctl enable mariadb

Next, MariaDB Security Setup,

mysql_secure_installation
  • Set Root Password: If not already set, it asks to create a password for the root user.

  • Remove Anonymous Users: Asks if you want to remove any anonymous users. (Recommended: Yes)

  • Disallow Remote Root Login: Asks if you want to disable remote root login. (Recommended: Yes)

  • Remove Test Database: Asks if you want to remove the test database. (Recommended: Yes)

  • Reload Privilege Tables: Applies changes made. (Recommended: Yes)

Output:

root@server:~# mysql_secure_installation
/usr/bin/mysql_secure_installation: Deprecated program name. It will be removed in a future release, use 'mariadb-secure-installation' instead

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!

In order to log into MariaDB to secure it, we'll need the current
password for the root user. If you've just installed MariaDB, and
haven't set the root password yet, you should just press enter here.

Enter current password for root (enter for none): 
OK, successfully used password, moving on...

Setting the root password or using the unix_socket ensures that nobody
can log into the MariaDB root user without the proper authorisation.

You already have your root account protected, so you can safely answer 'n'.

Switch to unix_socket authentication [Y/n] y
Enabled successfully!
Reloading privilege tables..
 ... Success!

You already have your root account protected, so you can safely answer 'n'.

Change the root password? [Y/n] n
 ... skipping.

By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them.  This is intended only for testing, and to make the installation
go a bit smoother.  You should remove them before moving into a
production environment.

Remove anonymous users? [Y/n] y
 ... Success!

Normally, root should only be allowed to connect from 'localhost'.  This
ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n] y
 ... Success!

By default, MariaDB comes with a database named 'test' that anyone can
access.  This is also intended only for testing, and should be removed
before moving into a production environment.

Remove test database and access to it? [Y/n] y
 - Dropping test database...
 ... Success!
 - Removing privileges on test database...
 ... Success!

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

Reload privilege tables now? [Y/n] y
 ... Success!

Cleaning up...

All done!  If you've completed all of the above steps, your MariaDB
installation should now be secure.

Thanks for using MariaDB!

Log in to MariaDB, use the following command,

mariadb -u root -p

To exit from MariaDB.

exit

Check the MariaDB version, run the following command:

mariadb --version

Output:

root@server:~# mariadb --version
mariadb from 11.4.5-MariaDB, client 15.2 for debian-linux-gnu (x86_64) using  EditLine wrapper

Install PHP

PHP 8.4 is the default version of PHP that would be installed on Ubuntu 25.04.

Install PHP and Required Extensions using below command,

apt install php libapache2-mod-php php-mysql php-common php-cli php-common php-json php-opcache php-readline php-mbstring php-gd php-dom php-zip php-curl

Enable the Apache PHP module and restart the Apache web server with the following commands,

a2enmod php8.4
systemctl restart apache2

Output:

root@server:~# a2enmod php8.4
Considering dependency mpm_prefork for php8.4:
Considering conflict mpm_event for mpm_prefork:
Considering conflict mpm_worker for mpm_prefork:
Module mpm_prefork already enabled
Considering conflict php5 for php8.4:
Module php8.4 already enabled

Check the PHP version with the following command,

php --version

Output:

root@server:~# php --version
PHP 8.4.5 (cli) (built: Mar 17 2025 20:35:32) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.4.5, Copyright (c) Zend Technologies
    with Zend OPcache v8.4.5, Copyright (c), by Zend Technologies

To test PHP scripts, create an info.php file in the document root directory with the following content,

nano /var/www/html/info.php

Add the following to the file.

<?php phpinfo(); ?>

To verify, enter the following link in a web browser,

http://ip-address/info.php

Note: Replace ip-address with actual IP Address

images

Once verified, remove the info.php file to avoid any security risks:

rm /var/www/html/info.php

Run PHP-FPM with Apache [Optional]

FPM (FastCGI Process Manager) is an alternative PHP FastCGI implementation with additional features for heavy-loaded sites.

NOTE: This step is optional. PHP 8.4 is used to run PHP code by default, but if you want to run PHP code with PHP-FPM, follow the steps below.

Next, install PHP-FPM:

apt install php8.4-fpm

Enable proxy_fcgi and setenvif module.

a2enmod proxy_fcgi setenvif

Output:

root@server:~# a2enmod proxy_fcgi setenvif
Considering dependency proxy for proxy_fcgi:
Enabling module proxy.
Enabling module proxy_fcgi.
Module setenvif already enabled
To activate the new configuration, you need to run:
  systemctl restart apache2

Now, enable the Apache php8.4-fpm file,

a2enconf php8.4-fpm

Output:

root@server:~# a2enconf php8.4-fpm
Enabling conf php8.4-fpm.
To activate the new configuration, you need to run:
  systemctl reload apache2

Restart Apache with the following command,

systemctl restart apache2

Enable php-fpm.

systemctl enable php8.4-fpm

Follow command below to start php-fpm.

systemctl start php8.4-fpm

To check the status of php-fpm.

systemctl status php8.4-fpm

Output:

root@server:~# systemctl status php8.4-fpm
● php8.4-fpm.service - The PHP 8.4 FastCGI Process Manager
     Loaded: loaded (/usr/lib/systemd/system/php8.4-fpm.service; enabled; preset: enabled)
     Active: active (running) since Thu 2025-04-24 14:08:40 UTC; 39s ago
 Invocation: 91da589a478c4a23a950b59b2db4568b
       Docs: man:php-fpm8.4(8)
   Main PID: 17246 (php-fpm8.4)
     Status: "Processes active: 0, idle: 2, Requests: 0, slow: 0, Traffic: 0.00req/sec"
      Tasks: 3 (limit: 3985)
     Memory: 11.1M (peak: 12.9M)
        CPU: 48ms
     CGroup: /system.slice/php8.4-fpm.service
             ├─17246 "php-fpm: master process (/etc/php/8.4/fpm/php-fpm.conf)"
             ├─17247 "php-fpm: pool www"
             └─17248 "php-fpm: pool www"

Now, you have successfully installed the LAMP stack (Apache, MariaDB, and PHP 8.4) on Ubuntu 25.04.


CrownCloud - Get a SSD powered KVM VPS at $4.5/month!
Use the code WELCOME for 10% off!

1 GB RAM / 25 GB SSD / 1 CPU Core / 1 TB Bandwidth per month

Available Locations: LAX | MIA | ATL | FRA | AMS