How to Deploy a LAMP Stack on Ubuntu 24.04: A Comprehensive Guide

Linux, Apache, MySQL/MariaDB, and PHP are reliable and efficient web platforms platforms for hosting dynamic websites and web applications. This simple-to-follow tutorial will outline installing an LAMP stack on an Ubuntu 24. 04 platform. It is easy to install and has a reliable, updated, and highly flexible OS; Ubuntu is the most preferred Linux distribution in web hosting. What is more, using a LAMP stack, you will have all the tools you need to create, run and test your projects.

What is a LAMP Stack?

LAMP stands for Linux, Apache, MySQL, and PHP, all open-source software services enabling and facilitating web hosting. LAMP’s components all have the right job to do:

  • Linux is the lowest layer in the stack because it is the operating system on which the other layers are built. Linux is reputed for its stability, security, and free-source nature, which has made it the most used operating system in most web servers.
  •  Apache: A popular HTTP server that accepts and fulfils requests from clients, which are primarily browsers, and serves the required web content. It is considered highly flexible, feature-rich, and backed up by a broad community.
  •  MySQL/MariaDB: A Database management system that stores data for your website or application in a relational manner. MySQL has always been the most popular one; however, MariaDB is a Branch of MySQL that is becoming increasingly popular because of better performance and additional features.
  •  PHP: A language used on a web server to generate content on a web page that is different for each user. Requests are understood, and the interaction with the database is done in order to output the web page to the client in PHP.
Why Choose LAMP?

LAMP is used because of its ease and flexibility. It supports every website, from a small personal blog to a large-scale application worldwide. The stack’s modularity allows developers to replace some parts of the existing stack, such as MySQL with PostgreSQL or PHP with Python. Furthermore, the whole stack is open source, lowering costs and encouraging people to contribute to developing the services.

Prerequisites

But before thinking about the installation process, the following prerequisites must be fulfilled:

  • Ubuntu 24.04 installed: This can be on a physical machine, virtual machine (VM), cloud server, or local machine for development, among other things.
  • Administrative access is necessary for users with sudo privileges to install and configure software.
  • Basic knowledge of the Linux command line: Familiarity with basic commands will make the process smoother.

Also, before installing new software, it is recommended that one update the Ubuntu system.

Upgrade the packages using the command line by typing

sudo apt update & sudo apt upgrade

 This ensures that your system is up to date with the latest patches and software releases available on the market.

Step 1: Installing Apache

Apache is the web server component of the LAMP stack. It listens to client requests (browsers) and responds with website content.

Update Your Package Repository

As mentioned earlier, always start by updating the package list to ensure you’re downloading the latest version of the software:

sudo apt update
Install Apache

To install Apache, run:

sudo apt install apache2

The package manager will handle the installation process, including setting up necessary dependencies.

Verify Apache Installation

Once installed, verify that Apache is running correctly:

sudo systemctl status apache2

You should see a green “active” status. If not, troubleshoot by checking logs using:

sudo journalctl -xe
Adjust Firewall Settings

If you have enabled UFW (Uncomplicated Firewall), you must allow HTTP and HTTPS traffic. Use the following commands:

sudo ufw allow 'Apache Full'
sudo ufw enable

Now, open a browser and visit http://your_server_ip. If you see the Apache default page, the installation is successful.

Additional Apache Configuration Tips

  • Enabling/Disabling Modules: Apache supports many modules that enhance its features, such as SSL or PHP support. It is also possible to turn on a module using the terminal by typing ‘sudo a2enmod module_name’ and to turn it off by typing ‘sudo a2dismod module_name’.
  •  Log Management: Apache writes logs of errors and access attempts, which are useful for diagnostics and gaining an idea of its usage. Initially, Apache error logs are in /var/log/apache2/error. Log and access logs are in /var/log/apache2/access. Log.

Step 2: Installing MySQL or MariaDB

Storing your website’s data requires a relational database management system or RDBMS. Some examples of RDBMS used for this purpose include MySQL and MariaDB.

Install MySQL or MariaDB

You have the MySQL and MariaDB options, which have almost the same functionality. To install MySQL, run:

 sudo apt install MySQL-server

 For MariaDB, use:

 sudo apt install MariaDB-server
Secure Your Database Installation

After installation, secure your database by running:

sudo mysql_secure_installation

This command demands you to answer several security questions, such as prohibiting anonymous users, banning root users from login over the network, and eliminating the test database. Answering “Yes” to these questions is advised to enhance security.

Verify MySQL/MariaDB Installation

To ensure the database service is running, use:

sudo systemctl status mysql

This will display the current status of MySQL.

 sudo systemctl status mariadb

Advanced MySQL/MariaDB Configuration

  • Backup: The database should be backed up regularly, and it is more important for the production database. Some utilities employed to create DB backups include mysqldump.
  •  Replication: MySQL/MariaDB offers replication for making backup copies of a database, increasing the system’s dependability and size. Users can choose to use the Master-Slave or Master-Master replication type.

Step 3: Installing PHP

PHP is the other server-side scripting language that works with the database to produce dynamic web pages. Apache and MySQL/MariaDB are fully compatible with it.

Install PHP

To install PHP and the necessary modules for working with MySQL/MariaDB, run: To install PHP and the modules required for working with MySQL/MariaDB, run:

 In the terminal window, type

sudo apt install php libapache2-mod-php PHP-MySQL

 This command installs PHP, Apache module for PHP and PHP extension for MySQL/MariaDB.

Verify PHP Installation

To verify the PHP installation, create a simple PHP file:

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

Add the following code:

<?php

phpinfo();

?>

Save the file and navigate to HTTP. It will be like this: http://your_server_ip/info. PHP. You should get a PHP information page that assures you that PHP works.

Step 4: Testing the LAMP Stack

After installing Apache, MySQL/MariaDB, and PHP, you should check that the entire stack is working correctly.

 Navigate to http://your_server_ip/info. Php. If you have looked at the PHP information page, you will realize that everything is in order.

 For security reasons, it’s a good idea to delete the info. Php file after testing: For security reasons, deleting the info is a good idea. Php file after testing:

 sudo rm /var/www/html/info. php
Testing with a Database Connection

To give aggressive testing on the LAMP stack, you can create a PHP file that should link to MySQL/MariaDB. For example:

<?php
$servername = "localhost";
$username = "root";
$password = "";
$conn = new mysqli($servername, $username, $password);
if ($conn->connect_error) {

  die("Connection failed: " . $conn->connect_error);

}

echo "Connected successfully";

?>

Place this code in /var/www/html/testdb.php and navigate to http://your_server_ip/testdb.php to confirm that PHP can communicate with MySQL/MariaDB.

Step 5: Configuring MySQL/MariaDB

After installing MySQL/MariaDB, the next step is configuring the database.

Login to MySQL

To log in to the MySQL shell as the root user:

sudo MySQL
Create a New Database

Once logged in, you can create a new database for your website or application:

CREATE DATABASE your_database_name;
7.3 Create a Database User

Next, create a new user and assign them privileges to the database:

CREATE USER 'username'@'localhost' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON your_database_name.* TO 'username'@'localhost';
FLUSH PRIVILEGES;

Step 6: Setting Up Virtual Hosts for Apache

A virtual host allows you to run multiple websites on a single Apache server. Virtual hosts are essential for managing different websites on the same server.

Create a New Virtual Host File

To set up a virtual host, first create a new configuration file in the Apache sites-available directory:

sudo nano /etc/apache2/sites-available/your_domain.conf

Add the following configuration:



<VirtualHost *:80>

    ServerAdmin webmaster@your_domain

    ServerName your_domain

    ServerAlias www.your_domain

    DocumentRoot /var/www/your_domain

    ErrorLog ${APACHE_LOG_DIR}/error.log

    CustomLog ${APACHE_LOG_DIR}/access.log combined

</VirtualHost>

Save and close the file.

Enable the Virtual Host

To enable your new virtual host, run the following:

sudo a2ensite your_domain.conf

Finally, restart Apache to apply the changes:

sudo systemctl restart apache2

Step 7: Securing Your LAMP Stack

Security is paramount, especially when running a public web server. A few essential steps can harden your LAMP stack.

Enable a Firewall

As mentioned earlier, ensure UFW is active and that only necessary services like HTTP, HTTPS, and SSH are allowed:

sudo ufw allow 'Apache Full'
sudo ufw allow OpenSSH
sudo ufw enable
Install SSL Certificates

SSL certificates are crucial for encrypting traffic between your server and clients. Let’s Encrypt offers free SSL certificates. Install the certbot package:

sudo apt install certbot python3-certbot-apache

Then, run:

sudo certbot --apache

This command automatically configures SSL for your Apache virtual hosts.

Step 8: Optimizing the LAMP Stack for Performance

Performance optimization ensures your server runs efficiently, mainly as your traffic grows.

Enabling Caching

One way to improve performance is by enabling caching in Apache. The mod_cache module caches responses to reduce server load. To enable it:

sudo a2enmod cache
sudo a2enmod cache_disk
sudo systemctl restart apache2
Optimizing MySQL/MariaDB

You can optimize MySQL by adjusting the configuration settings in /etc/mysql/my.cnf. The innodb_buffer_pool_size and query_cache_size parameters significantly impact performance.

Opcode Caching for PHP

You can install opcache to cache PHP scripts, reducing the need to recompile them on every request:

sudo apt install php-opcache

Troubleshooting Common Issues

Apache Won’t Start

If Apache won’t start, check for errors using:

sudo journalctl -xe

Common issues include misconfigured virtual hosts or other services using the same port.

MySQL/MariaDB Connection Issues

If you can’t connect to MySQL/MariaDB, ensure the service is running:

sudo systemctl status mysql  # or MariaDB

Check the MySQL logs for errors in /var/log/mysql/error.log.

Conclusion

It is easy to install the LAMP stack on Ubuntu 24. 04, though it is essential to comprehend every option to optimize each segment’s utility. With this in mind, you should now have a fully operational LAMP stack ready to serve dynamic websites or applications. With additional tuning and security enhancements, you can maximize the performance and security of your server. It is thus suitable for designing independent blogs, corporate sites, full-fledged application sites, etc.

References

Canonical. (2023). Apache Web Server on Ubuntu 24.04. Retrieved from https://ubuntu.com/tutorials/install-and-configure-apache#1-overview

MySQL Documentation Team. (2023). MySQL 8.0 Reference Manual. Retrieved from https://dev.mysql.com/doc/refman/8.0/en/

MariaDB Foundation. (2023). MariaDB Server Documentation. Retrieved from https://mariadb.com/kb/en/documentation/

The PHP Group. (2023). PHP Manual. Retrieved from https://www.php.net/manual/en/

Let’s Encrypt. (2023). Certbot Documentation. Retrieved from https://certbot.eff.org/docs/

UFW Documentation. (2023). Ubuntu Firewall. Retrieved from https://help.ubuntu.com/community/UFW

Open Web Application Security Project (OWASP). (2023). OWASP Secure Configuration. Retrieved from https://owasp.org/www-project-secure-configuration/

DigitalOcean. (2023). How to Install Linux, Apache, MySQL, PHP (LAMP) Stack on Ubuntu 24.04. Retrieved from https://www.digitalocean.com/community/tutorials/how-to-install-linux-apache-mysql-php-lamp-stack-ubuntu-24-04

Stack Overflow. (2023). Common Errors and Solutions for LAMP Stack on Ubuntu. Retrieved from https://stackoverflow.com/questions/tagged/lamp

Ubuntu Community Help Wiki. (2023). Virtual Hosting With Apache. Retrieved from https://help.ubuntu.com/community/ApacheVirtualHosts

Leave a Comment