Cantech Knowledge Base

Your Go-To Hosting Resource

How to Install Apache, MySQL, PHP (LAMP Stack) on Ubuntu 24.04?

Introduction

The LAMP Stack (Linux,Apache,MySQL,and PHP) are the most widely used open-source development platforms. Apache is a powerful web server, which efficiently handles client requests to deliver web pages. MySQL is a relational database management system, which also stores and manages structured data by SQL queries. PHP on the other hand, is a server-side scripting language which enables the creation of web pages and seamlessly integrates with MySQL and Apache to create interactive, database based websites. Running on the LAMP offers the advantage of stable, secure, and scalable environment for web app development

This blog explains how to install Apache, MySQL and PHP on Ubuntu 24.04

Prerequisites

Verify the following prerequisites before beginning the installation process:

  • Ubuntu 24. 04 Installed – An initial or ongoing setup phase of Ubuntu 24. 04.
  • A user that has sudo administrator privileges.
  • Updated server

Apache Installation

Updated Apache version is available in the default APT repositories on Ubuntu 24.04. The below steps show how to update the default package index and install Apache web server package.

  • Update the server’s package index
sudo apt update
  • Install Apache
sudo apt install apache2 -y
  • Starting Apache Service.
sudo systemctl start apache2
  • Enable apache to start automatically at boot time.
sudo systemctl enable apache2
  • Check if Apache server is running correctly
sudo systemctl status apache2

Output:

apache2.service - The Apache HTTP Server
    Loaded: loaded (/usr/lib/systemd/system/apache2.service; enabled; preset: enabled)
    Active: active (running) since Wed 2025-03-11 06:50:21 UTC; 3 days ago
      Docs: https://httpd.apache.org/docs/2.4/
   Process: 164186 ExecReload=/usr/sbin/apachectl graceful (code=exited, status=0/SUCCESS)
  Main PID: 124430 (apache2)
     Tasks: 8 (limit: 4389)
    Memory: 18.3M (peak: 44.1M)
       CPU: 27.485s
    CGroup: /system.slice/apache2.service
      ├─124430 /usr/sbin/apache2 -k start
      ├─164195 /usr/sbin/apache2 -k start
      ├─164196 /usr/sbin/apache2 -k start
      ├─164199 /usr/sbin/apache2 -k start
      ├─164201 /usr/sbin/apache2 -k start
  • Enable the connections to HTTP port 80 by default firewall configuration.
sudo ufw allow 80/tcp
  • Access your domain or IP using a browser like Chrome and check if the default Apache web page displays.
http://SERVER-IP
Apache Default Page

MySQL Installation

To install the new MySQL version on the server using the default APT package manager, follow the steps below:

  • Install the MySQL package.
sudo apt install -y mysql-server
  • Allow MySQL service to start automatically at boot time.
sudo systemctl enable mysql
  • Start the MySQL service.
sudo systemctl start mysql
  • View the MySQL status and check if it is running.
sudo systemctl status mysql

Output:

mysql.service - MySQL Community Server
Loaded: loaded (/usr/lib/systemd/system/mysql.service; enabled; preset: enabled)
Active: active (running) since Thu 2025-03-11 06:54:13 UTC; 2 days ago
Process: 144075 ExecStartPre=/usr/share/mysql/mysql-systemd-start pre (code=exited, status=0/SUCCESS)
Main PID: 144083 (mysqld)
Status: "Server is operational"
Tasks: 38 (limit: 4389)
Memory: 369.2M (peak: 382.6M)
CPU: 36min 21.658s
CGroup: /system.slice/mysql.service
     └─144083 /usr/sbin/mysqld

The running command in the output shows that MySQL server is active on the server.

  • Execute MySQL secure installation to disable insecure defaults and allow authentication on your database server.
sudo mysql_secure_installation

Type the following keys when MySQL database server options are prompted:

  • For Validate Password prompt: Type Y to allow password checks on the database server.
  • For Password strength policy prompt: Type 2 to enable multi-character password usage on the server.
  • For Remove anonymous users prompt: Type Y to remove anonymous users from the database server.
  • For Disallow root login remotely prompt: Type Y to remove remote access to the root database user.
  • For the Remove test database prompt: Type Y to delete the default MySQL test database on your server.
  • For Reload privileges tables now prompt: Type Y to reload the MySQL privilege tables and add your configuration changes.

Once you enter the above output to prompts, your output should be:

Success.
All Done!
  • Next, you need to Log in to the MySQL console as the root user.
sudo mysql
  • Make an alteration to the root database user to use a new password. Replace Strong@@678password with your new password.
mysql > alter user 'root'@'localhost' IDENTIFIED BY 'Strong@@678password';

Replace the old password with a new strong password.

  • Flush MySQL privileges table to add the new user updates.
mysql> FLUSH PRIVILEGES;
  • Exit the MySQL console.
mysql > EXIT
  • Log in to the MySQL console again as the root user and enter the password you set earlier when prompted.
sudo mysql -u root -p
  • Then, create a new sample database content_database.
mysql > CREATE database content_database;
  • Run the below command, to view all the databases and verify that the new database is available.
mysql > SHOW DATABASES;

Output:

+--------------------+
| Database           |
+--------------------+
| information_schema |
| content_database   |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.01 sec)
  • Create a new MySQL user such as dbadmin with a strong password. Replace Strong@@678password with your new password.
mysql > CREATE USER 'dbadmin'@'localhost' IDENTIFIED BY 'Strong@@678password';
  • Allow full user privileges to your new sample database content_database.
mysql > GRANT ALL PRIVILEGES ON content_database.* TO 'dbadmin'@'localhost';
  • Reload the MySQL privilege table to apply the new changes.
mysql > FLUSH PRIVILEGES;
  • Finally, you can exit the MySQL shell.
mysql > EXIT

PHP Installation

PHP helps your websites run dynamic content and connect with databases such as MySQL. With PHP-FPM, your server can handle multiple PHP requests smoothly and faster. Here, you’ll see how to install PHP and set up PHP-FPM for better performance on your server.

Install the PHP and PHP-FPM module.

sudo apt install -y php php-fpm

Next, Install the common PHP extensions on the server.

sudo apt install -y php-mysql php-opcache php-cli libapache2-mod-php

These command installs the following PHP modules:

  • php-mysql: Allows PHP to connect to and interact with the MySQL database server.
  • libapache2-mod-php: Enables Apache to process and execute PHP scripts.
  • php-opcache: Caches precompiled PHP scripts in memory for faster execution.
  • php-cli: Provides access to PHP via the server terminal.

Check the Installed PHP version on your server.

php -v

Sample Output:

PHP 8.3.6 (cli) (built: Mar 11 2025 15:23:20) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.3.6, Copyright (c) Zend Technologies
with Zend OPcache v8.3.6, Copyright (c), by Zend Technologies

Start PHP-FPM service, that is on the installed PHP version of the server. For example, PHP 8.3.

sudo systemctl start php8.3-fpm

Enable PHP-FPM to start at boot time.

sudo systemctl enable php8.3-fpm

Check the service status of PHP-FPM and verify that it’s running.

sudo systemctl status php8.3-fpm

Sample Output:

php8.3-fpm.service - The PHP 8.3 FastCGI Process Manager
 Loaded: loaded (/usr/lib/systemd/system/php8.3-fpm.service; enabled; preset: enabled)
 Active: active (running) since Wed 2025-03-11 06:50:21 UTC; 3 days ago
   Docs: man:php-fpm8.3(8)
Main PID: 124416 (php-fpm8.3)
 Status: "Processes active: 0, idle: 2, Requests: 15, slow: 0, Traffic: 0req/sec"
  Tasks: 3 (limit: 4389)
 Memory: 12.0M (peak: 12.4M)
    CPU: 31.204s
 CGroup: /system.slice/php8.3-fpm.service
         ├─124416 "php-fpm: master process (/etc/php/8.3/fpm/php-fpm.conf)"
         ├─124419 "php-fpm: pool www"
         └─124420 "php-fpm: pool www"

How to Configure PHP – FPM

PHP-FPM delivers maximum  performance for PHP applications on your server by utilizing  the pools based on the server memory. The below steps allows you to configure PHP-FPM to work with the Apache web server and alter the default pool configurations to allow proper resource management.

  • Activate the necessary Apache modules.
sudo a2enmod proxy_fcgi setenvif

proxy_fcgi lets Apache to work as a proxy with PHP-FPM and setenvif facilitates environment variables to allow connections between Apache and PHP-FPM.

  • Add the default PHP-FPM configuration.
sudo a2enconf php8.3-fpm
  • Restart the Apache web server to update the changes.
sudo systemctl restart apache2
  • Switch to the PHP-FPM pool configurations directory.
sudo cd /etc/php/8.3/fpm/pool.d/
  • Open the default www.conf PHP-FPM pool configuration.
sudo nano /etc/php/8.3/fpm/pool.d/www.conf

Verify that the default PHP-FPM pool name www.

[www]

Verify if the below directives are set to www-data to enable PHP-FPM to use the default web server user profile.

user = www-data
group = www-data
listen.owner = www-data
listen.group = www-data

Save and close the file.

  • Restart the PHP-FPM service to apply your configuration changes.
sudo systemctl restart php8.3-fpm

How to Configure Apache with PHP-FPM

In the below steps, set up a new Apache virtual host configuration and connect to the PHP-FPM service using the UNIX socket.

  • Remove the default Apache virtual host configuration files.
sudo rm -rf /etc/apache2/sites-enabled/000-default.conf && sudo rm -rf /etc/apache2/sites-available/000-default.conf
  • Create a new Apache virtual host configuration file.
sudo nano /etc/apache2/sites-available/app.example.com.conf
  • Apply the below configurations to the file. Replace app.example.com with your actual domain.
<VirtualHost *:80>
ServerAdmin [email protected]
ServerName wapp.example.com
DocumentRoot /var/www/app.example.com

<Directory /var/www/app.example.com>
    Options Indexes FollowSymLinks
    AllowOverride All
    Require all granted
</Directory>

<FilesMatch \.php$>
   SetHandler "proxy:unix:/var/run/php/php8.3-fpm.sock|fcgi://localhost/"
</FilesMatch>

ErrorLog ${APACHE_LOG_DIR}/app.example.com_error.log
CustomLog ${APACHE_LOG_DIR}/app.example.com_access.log combined
</VirtualHost>

Save it and close the file.

This adds a new Apache virtual host that listens for incoming connections on the default HTTP port 80 and serves web contents using your app.example.com domain. Later, the PHP file requests are sent to the PHP-FPM process using the /var/run/php/php8.3-fpm.sock UNIX socket.

➞ <Directory /var/www/app.example.com>: Assists the web root directory to deliver web application files.

<VirtualHost *:80>: Allows virtual host profiles to listen for connections on port 80.

ErrorLog, CustomLog: Allows custom paths to save the virtual host error and access logs.

<FilesMatch \.php$>: Sends PHP file requests to the PHP-FPM socket /var/run/php/php8.3-fpm.sock through FastCGI protocol.

  • Allow new Apache virtual host configuration.
sudo a2ensite app.example.com.conf
  • Test for syntax errors.
sudo apache2ctl configtest

Output:

Syntax OK
  • Create the virtual host web root directory /var/www/app.example.com defined in your configuration.
sudo mkdir -p /var/www/app.example.com
  • Create a sample PHP file named info.php.
sudo nano /var/www/html/info.php
  • Insert the content below into the file.
Copy
<?php
phpinfo();
?>

Save and close the file.

The code above shows details about your PHP version and installed modules when you open the file in a web browser.

  • Restart Apache to add your configuration changes.
sudo systemctl restart apache2
  • Access your domain using a web browser such as Chrome and append the /info.php path to verify that your PHP application information displays.
http://app.example.com/info.php

PHP FPM

Secure the Server

The below sections lets you configure the default firewall to allow connections to the default web server port 80 and set up trusted SSL certificates to enable HTTPS connections on port 443.

Configure the Firewall

  • Check the current firewall status and make sure it’s enabled.
sudo ufw status

Output:

Status: active
  • Show the available UFW application profiles and check if the Apache profile is available.
sudo ufw app list

Output:

Apache
Apache Full
Apache Secure
OpenSSH
  • Permit the Apache Full profile to open both HTTP and HTTPS access on your server.
sudo ufw allow "Apache Full"
  • Restart the firewall settings to apply the new changes.
sudo ufw reload
  • Check the UFW status to verify the Apache connection rules are that are available in the firewall table.
sudo ufw status

Output:

To                         Action      From
--                         ------      ----
1022/tcp                   ALLOW       Anywhere
Apache Full                ALLOW       Anywhere
1022/tcp (v6)              ALLOW       Anywhere (v6)
Apache Full (v6)           ALLOW       Anywhere (v6)

Create Let’s Encrypt SSL Certificates

  • Download the Certbot Let’s Encrypt client tool by using Snap.
sudo snap install certbot --classic
  • Request a new SSL certificate for your domain. Replace app.example.com with your domain and [email protected] with your email.
sudo certbot --apache -d app.example.com -m [email protected] --agree-tos

Output:

Requesting a certificate for app.example.com
Successfully received certificate.
Certificate is saved at: /etc/letsencrypt/live/app.example.com/fullchain.pem
Key is saved at:         /etc/letsencrypt/live/app.example.com/privkey.pem
This certificate expires on 2024-10-14.
These files will be updated when the certificate renews.
Certbot has set up a scheduled task to automatically renew this certificate in the background.

Deploying certificate
Successfully deployed certificate for app.example.com to /etc/apache2/sites-available/000-default-le-ssl.conf
Congratulations! You have successfully enabled HTTPS on https://app.example.com
  • Check the automatic SSL certificate renewal with Certbot to confirm it functions correctly.
sudo certbot renew --dry-run
  • Restart the Apache web server to add your SSL configuration changes.
sudo systemctl restart apache2

Test the LAMP Stack Installation

The below steps are used to set up a new sample table in your existing content_database MySQL database to connect with your PHP application and display the message when accessed in a web browser.

  • Access the MySQL console with the database user dbadmin that you created earlier.
mysql -u dbadmin -p

Add the dbadmin user password when prompted to access the MySQL console.

  • Switch to the sample database content_database.
mysql> USE content_database;
  • Create a new sample table of messages to store your data.
mysql > CREATE TABLE IF NOT EXISTS messages (
       content_id INT AUTO_INCREMENT PRIMARY KEY,
       content VARCHAR(250) NOT NULL
      );

➞ This query creates new table using:

  1. content_id: consists of numbers and automatically increments new data on new row.
  2. content: Has mixed content data with up to 250 characters.
  • Insert new data to the messages table. For example, add a new Hello World! Greetings from Cantech
mysql > INSERT INTO messages (content) VALUES ('Hello World! Greetings from Cantech');
  • View all table data to verify that the new string is added to the column.
mysql > SELECT * from messages;

Output:

+----+---------------------------------------------+
| content_id | content                             |
+------------+-------------------------------------+
|  1         | Hello World! Greetings from Cantech |
+------------+-------------------------------------+
1 row in set (0.00 sec)
  • Close the MySQL console.
mysql > EXIT
  • Make a new PHP file named setup.php inside your web root directory /var/www/html/app.example.com.
sudo nano /var/www/html/setup.php
<?php
$hostname = "localhost";
$username = "dbadmin";
$password = "Strong@@password456";
$dbname = "content_database";

// Establish Connection
$conn = new mysqli($hostname, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
    die("Connection Failed." . $conn->connect_error);
}

$sql = "SELECT content FROM messages";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
   $row = $result->fetch_assoc();
   echo ""<h2 style='color: blue; text-align: center; margin-bottom: 15px;'>" 
 . htmlspecialchars($row["content"]) . "</h2>";

} else {
    echo "<h1>No records found.</h1>";
}

$conn->close();

Save and close the file.

The PHP code connects to the content_database and shows data from the content column in the messages table. If no records exist, it shows No records found, or Connection Failed if the database connection doesn’t work.

  • Allow Apache user www-data full privileges to your web root directory.
chown -R www-data:www-data /var/www/html/app.example.com/
  • Open your domain in a web browser with the /setup.php path to confirm that your PHP application shows the Hello World! Greetings from Cantech message from the MySQL database.
https://app.example.com/setup.php

Output:

Hello World! Greetings from Cantech

Conclusion

Finally, you have successfully installed Apache, MySQL, and PHP (LAMP stack) on the Ubuntu 24.04 server by following all the above processes. For updated information. Please visit the following official documentation resources.

FAQ’s

What is PHP programming?

PHP stands for Hypertext Preprocessor. It is a widely used server-side scripting language primarily designed for web development. PHP is used by 78.1% of all websites such as high-traffic websites.

What is the latest version of PHP?

PHP 8.4 is the latest version to release under the new community support life cycle, which changed because of RFC passed last year.

What is the latest version of MySQL?

MySQL 9.0 is the latest version of MySQL. It offers access to the latest features and improvements in MySQL technologies.

What is Apache and MySQL?

Apache is the web server that processes various requests and delivers web assets and content through HTTP. MySQL is a relational database management system.

How to Install Lamp on Ubuntu 24.04?

You can install LAMP on Ubuntu 24.04 by setting up Apache, MySQL, and PHP step by step. Follow the guide to configure and verify each service properly.

Related:

October 15, 2025