In this post we will perform the installation of LAMP (Apache, PHP, and MariaDB) on Ubuntu 24.04 – this is Apache, MariaDB, and PHP on Linux(Ubuntu server). Linux is at the heart of 99% of web hosting service offerings. The main reason for the choice is in Linux stability, security, robustness, and it eliminates the need for licensing fees since it’s open-source. In this article we will perform the installation, configuration and basic usage of LAMP Stack on Ubuntu 24.04 (Noble Numbat).

We can have a short explanation of the components that is in LAMP Stack before diving into actual installations.

  • Linux: This is our Ubuntu 24.04 Linux system.
  • Apache: Apache is a web server that sits in between the application and the web browser.
  • MariaDB: This is a relational database management system used to store and manage dynamic content of your web app.
  • PHP: This is a scripting language that is used in creating dynamic pages on the internet. Its code can be embedded within HTML and is able to connect to database for data storage and retrieval.

The installations performed in this article will only be directly applicable for any PHP based web application. If you’re planning to host any other web application, for example an application written in Django or Node.js skip the PHP part and find the right article in our website. But the Apache and MariaDB parts remain constant if your app depends on them.

Installing LAMP on Ubuntu 24.04 (Noble Numbat)

Refer to the next steps to install and configure Apache, MariaDB and PHP on Ubuntu 24.04 (Noble Numbat).

Step 1: Install Apache web server

Apache httpd server package is available in Ubuntu APT repositories as apache2. Install the web server on the system using the commands below.

sudo apt update && sudo apt install apache2

The service is started automatically after the installation.

$ systemctl status apache2
 apache2.service - The Apache HTTP Server
     Loaded: loaded (/usr/lib/systemd/system/apache2.service; enabled; preset: enabled)
     Active: active (running) since Mon 2024-04-15 20:43:53 UTC; 2min 52s ago
       Docs: https://httpd.apache.org/docs/2.4/
   Main PID: 1862 (apache2)
      Tasks: 55 (limit: 4614)
     Memory: 5.3M (peak: 5.6M)
        CPU: 33ms
     CGroup: /system.slice/apache2.service
             ├─1862 /usr/sbin/apache2 -k start
             ├─1865 /usr/sbin/apache2 -k start
             └─1866 /usr/sbin/apache2 -k start

Apr 15 20:43:53 ubuntu2204-server systemd[1]: Starting apache2.service - The Apache HTTP Server...
Apr 15 20:43:53 ubuntu2204-server apachectl[1861]: AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1. Set the 'ServerName' directive globally >
Apr 15 20:43:53 ubuntu2204-server systemd[1]: Started apache2.service - The Apache HTTP Server.

A default page is located in /var/www/html/index.html and is served on the server IP over HTTP. Hit your server IP address in your browser to check the page.

Apache ubuntu 01

Step 2: Install PHP and Extensions

PHP works on the server-side in executing the scripts and giving response to the web server. We can install the default version of PHP available in the OS upstream repositories with the following command:

sudo apt install php

Check PHP version after installation.

$ php --version
PHP 8.3.4 (cli) (built: Mar 31 2024 08:14:14) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.3.4, Copyright (c) Zend Technologies
    with Zend OPcache v8.3.4, Copyright (c), by Zend Technologies

The libapache2-mod-php PHP extension is required for serving the pages on Apache web server.

sudo apt install libapache2-mod-php

Installing PHP extensions

PHP extensions are libraries created to enhance PHP functionalities. A PHP extension is loaded at runtime and it provides classes, functions, and other features usable in your PHP scripts.

Install some recommended and commonly used modules.

sudo apt install php-{cli,curl,json,mysql,zip,pear,gd,mbstring,xml,bcmath}

To check loaded PHP modules use:

$ php -m
[PHP Modules]
bcmath
calendar
Core
ctype
curl
date
dom
exif
FFI
fileinfo
filter
ftp
gd
gettext
hash
iconv
json
libxml
mbstring
mysqli
mysqlnd
...

Step 3: Install MariaDB Database Server

MariaDB is a RDMS forked from MySQL but has matured to be one of the most databases used in the world. Install MariaDB with the following commands:

sudo apt install mariadb-server

Secure MariaDB database server after installation by running the mariadb-secure-installation script.

sudo mariadb-secure-installation

The script executed will give you straightforward prompts on improving the security of your MariaDB server. When running the script make sure you set root user password, disable remote root user logins, remove anonymous users, and deleting test database.

Use the mysql CLI client if you want to initiate a connection to the MariaDB server.

$ sudo mysql -u root -p
Enter password:
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 31
Server version: 10.11.7-MariaDB-2ubuntu2 Ubuntu 24.04

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)]>

Step 4: Hosting PHP app using MariaDB and Apache

You can use Apache virtual host feature to host more than one websites on a single server. We can test our setup with the installation of WordPress CMS.

Open MariaDB console and create database with user.

$ sudo mysql
CREATE DATABASE wordpress;
GRANT ALL ON wordpress.* TO wordpress@localhost IDENTIFIED BY "StrongDBPassw0rd";
FLUSH PRIVILEGES;
QUIT

Download and extract latest wordpress archive.

wget wordpress.org/latest.tar.gz
tar xvf latest.tar.gz

Move the folder into /var/wwwdirectory.

sudo mv wordpress /var/www

Create wordpress configuration file from sample provided during installation.

sudo mv /var/www/wordpress/wp-config-sample.php /var/www/wordpress/wp-config.php

Edit the file and set database connection details.

$ sudo vim /var/www/wordpress/wp-config.php
// ** Database settings - You can get this info from your web host ** //
/** The name of the database for WordPress */
define( 'DB_NAME', 'wordpress' );

/** Database username */
define( 'DB_USER', 'wordpress' );

/** Database password */
define( 'DB_PASSWORD', 'StrongDBPassw0rd' );

/** Database hostname */
define( 'DB_HOST', 'localhost' );

/** Database charset to use in creating database tables. */
define( 'DB_CHARSET', 'utf8' );

Disable default web page.

sudo a2dissite 000-default.conf

Create VirtualHost file for wordpress.

$ sudo vim /etc/apache2/sites-available/wordpress.conf
<VirtualHost *:80>
    ServerAdmin [email protected]
    ServerName wordpress.example.com
    DocumentRoot /var/www/wordpress
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Enable the website we just created.

sudo a2ensite wordpress.conf

Restart apache2 web server.

sudo systemctl restart apache2

To access the website, open /etc/hosts file in your system and map the domain name and IP address.

  • Linux: /etc/hosts
  • Windows: C:\Windows\System32\drivers\etc\hosts
192.168.1.201 wordpress.example.com

Load the web page in your browser. If successful you should get a page similar to below.

Apache PHP MariaDB ubuntu 01
Apache PHP MariaDB ubuntu 02

Step 5: Enabling SSL / TLS on Apache

We can also configure SSL/TLS setting in our Apache for secure encrypt HTTPS connection.

<VirtualHost *:80>
    ServerAdmin [email protected]
    ServerName wordpress.example.com
    DocumentRoot /var/www/wordpress
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

    # Enable SSL
    RewriteEngine On
    RewriteCond %{HTTPS} off
    RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
</VirtualHost>

<VirtualHost *:80>
        ServerAdmin [email protected]
        ServerName wordpress.example.com
        DocumentRoot /var/www/wordpress
        RewriteEngine On
        RewriteCond %{HTTPS} !=on
        RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R=301,L]
</virtualhost>
<VirtualHost *:443>
        ServerAdmin [email protected]
        ServerName wordpress.example.com
        DocumentRoot /var/www/wordpress
        ErrorLog /var/log/apache2/error.log
        CustomLog /var/log/apache2/access.log combined
        SSLEngine on
        SSLCertificateFile /etc/ssl/fullchain.pem
        SSLCertificateKeyFile /etc/ssl/privkey.pem
</VirtualHost>

Substitute paths to SSL key and Certificate with your correct file paths.

Conclusion

That’s it!. We’ve been able to setup a functional web server using Apache httpd server with PHP and MariaDB database server. We hope our article was helpful in how to install LAMP stack on Ubuntu 24.04 (Apache, PHP, and MariaDB) system.

LEAVE A REPLY

Please enter your comment!
Please enter your name here