How to install Apache, php8.3 and MySQL ( LAMP ) on Ubuntu 24.04

LAMP on Ubuntu 24.04

How to install Apache, php8.3 and MySQL ( LAMP ) on Ubuntu 24.04

A LAMP stack is a collection of open-source software used to host dynamic websites and web applications. LAMP stands for Linux, Apache, MariaDB/MySQL and PHP.


In this guide, we will install and configure a complete LAMP stack on Ubuntu 24.04 using Apache, MariaDB and PHP 8.3.


Update Package Index


Before installing any new packages, update the local package index and upgrade the currently installed packages:

sudo apt update && sudo apt upgrade -y


Install and Configure Apache


Install Apache: Install the Apache web server and the required utilities:

sudo apt install apache2 apache2-utils -y


Verify Apache is running:

sudo systemctl status apache2


If Apache is not running, start it with:

sudo systemctl start apache2


Enable Apache to start automatically when the server boots:

sudo systemctl enable apache2


Check the installed Apache version:

apache2 -v


Configure UFW Firewall


If UFW is enabled on your server, allow incoming HTTP and HTTPS connections to Apache:


sudo ufw allow 'Apache Full'


Alternatively, HTTP and HTTPS can be allowed separately:


sudo ufw allow 80/tcp
sudo ufw allow 443/tcp


Check the current firewall configuration:


sudo ufw status


Apache should now be accessible by entering the server IP address in a web browser:

http://SERVER_IP


Install MariaDB Server


Ubuntu 24.04 includes MariaDB in its official repositories. Install the MariaDB server and client packages:

sudo apt install mariadb-server mariadb-client -y


Verify MariaDB is running:


sudo systemctl status mariadb


If MariaDB is not running, start it:

sudo systemctl start mariadb


Enable MariaDB to start automatically during system boot:

sudo systemctl enable mariadb


Check the installed MariaDB version:

mariadb --version


Secure MariaDB Installation


MariaDB includes a security configuration utility that can remove insecure default settings and restrict access to the database server.

sudo mariadb-secure-installation


During the configuration process, you can remove anonymous users, disable remote root login, remove the test database and reload the privilege tables.


Login to MariaDB:

sudo mariadb


To exit from the MariaDB console:

exit


Install PHP 8.3 and Core Extensions


PHP 8.3 is the default PHP version included with Ubuntu 24.04. Install PHP together with the Apache PHP module and commonly used PHP extensions:

sudo apt install php8.3 libapache2-mod-php8.3 php8.3-cli php8.3-mysql php8.3-curl php8.3-gd php8.3-mbstring php8.3-xml php8.3-zip php8.3-intl php8.3-opcache php8.3-bcmath php8.3-soap php8.3-imagick -y


Check the installed PHP version:

php -v


Configure Apache to Process PHP


Enable the PHP 8.3 and Apache rewrite modules:

sudo a2enmod php8.3 rewrite


Restart Apache to apply the changes:

sudo systemctl restart apache2


Test PHP Processing


To confirm that Apache can correctly process PHP files, create a temporary PHP information page:


sudo nano /var/www/html/info.php


Add the following PHP code:


<?php phpinfo(); ?>


Save the file and open the following URL in your web browser:


http://SERVER_IP/info.php


If PHP is configured correctly, the PHP information page will display the current PHP configuration and loaded extensions.


Important: Remove the test file immediately after verifying PHP because it exposes detailed information about the server configuration:


sudo rm /var/www/html/info.php


Run PHP-FPM with Apache (Optional)


PHP-FPM (FastCGI Process Manager) is an alternative way of processing PHP requests and is often preferred for higher-load websites and more flexible PHP process management.


This section is optional. If you want to continue using the standard Apache PHP module installed above, you can skip it.


Install PHP 8.3 FPM:

sudo apt install php8.3-fpm -y


Disable the Apache PHP module and the prefork MPM:

sudo a2dismod php8.3
sudo a2dismod mpm_prefork


Enable the event MPM and the modules required for PHP-FPM:

sudo a2enmod mpm_event proxy_fcgi setenvif


Enable the PHP 8.3 FPM Apache configuration:

sudo a2enconf php8.3-fpm


Enable and start the PHP-FPM service:

sudo systemctl enable --now php8.3-fpm


Restart Apache:

sudo systemctl restart apache2


Verify that PHP-FPM is running:

sudo systemctl status php8.3-fpm


Verify Installed Services


Finally, verify that Apache and MariaDB are running correctly:

sudo systemctl status apache2
sudo systemctl status mariadb


If PHP-FPM is being used, check it as well:

sudo systemctl status php8.3-fpm


Conclusion


You have successfully installed a LAMP stack on Ubuntu 24.04 with Apache, MariaDB and PHP 8.3.

Your server is now ready to host PHP-based websites and applications such as WordPress, WooCommerce and other PHP web applications.