What is LAMP stack and how to install it on Ubuntu

Updated: January 20, 2024 By: Guest Contributor Post a comment

Introduction

LAMP is an acronym that stands for Linux, Apache, MySQL, and PHP. These four stack components are traditionally used for hosting web content. As an open-source web development platform, LAMP uses Linux as the operating system, Apache as the Web server, MySQL as the relational database management system, and PHP as the object-oriented scripting language.

In this tutorial, we will discuss the LAMP stack in detail and provide step-by-step guidance on how to install each of these components on Ubuntu, which can be highly beneficial for developers aiming to deploy and host their web applications.

Before proceeding, ensure that you have a system running Ubuntu OS with sudo privileges.

Step-by-Step Instructions

Step 1: Install Apache

The Apache HTTP Server is one of the most widely used web servers. It’s known for its robustness, security, and flexibility. Below you’ll find the command to install Apache on Ubuntu: <code> sudo apt update sudo apt install apache2 </code>

Once the installation is complete, ensure that apache2 is running by checking the status of the service:

sudo systemctl status apache2

The output should indicate that the service is active and running. You can also verify the installation by accessing your server’s IP address in a web browser. You should see the Apache2 default page.

Step 2: Install MySQL

MySQL is a widely-used database server. This relational database is used numerous web applications to store data. To install MySQL, use the following commands:

sudo apt install mysql-server

After the installation, run the security script that comes pre-installed with MySQL:

sudo mysql_secure_installation

This program will set the root password, remove anonymous users, and strengthen the security of your MySQL server. It is strongly advised to follow the prompted steps and customize your configurations as needed.

Step 3: Install PHP

PHP is a widely-used open source general-purpose scripting language that is especially suited for web development and can be embedded into HTML. To install PHP along with some common PHP modules, use these commands:

sudo apt install php libapache2-mod-php php-mysql

After you have installed PHP, create a simple PHP script to test that PHP is correctly working with your web server.

echo '<?php phpinfo(); ?>' | sudo tee /var/www/html/info.php

Now you can visit your server’s domain followed by /info.php to see a page showing information regarding the PHP installation on the server.

Troubleshooting: Apache and PHP

If your PHP file is downloading instead of executing, you may need to make Apache aware of the PHP module. You can do this by editing the Apache configuration file:

sudo nano /etc/apache2/mods-enabled/dir.conf

Add the ‘index.php’ file as the first file to be looked for when a directory is accessed:

DirectoryIndex index.php index.html

Then, restart the Apache server for changes to take effect: <code> sudo systemctl restart apache2 </code>

Step 4: Installing Additional PHP Extensions

To enhance the functionality of PHP, you can install additional PHP extensions depending on your requirement. Use the following command to search for available PHP extensions:

apt search php-

Install the necessary extensions with:

sudo apt install php-[extension]

Replace ‘[extension]’ with the PHP extension you wish to install.

Step 5: Testing the LAMP stack

To fully test the LAMP stack, you can write a simple PHP script that connects to the MySQL database. Below is a basic example of a PHP script that connects to a MySQL database and lists the versions:

<?php
$link = mysqli_connect('localhost', 'root', 'your_password');
if (!$link) {
    die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
$query = 'SELECT VERSION()';
$result = mysqli_query($link, $query);
$row = mysqli_fetch_array($result);
echo 'MySQL version: ' . $row['VERSION()'];
mysqli_close($link);
?>

Replace ‘your_password’ with the password for the MySQL root or the user account you created. Save the above code in a file named testdb.php and then navigate to it in your web browser using your server domain followed by /testdb.php.

Conclusion

At this point, your Ubuntu system should have a fully functioning LAMP stack installed. Continue to develop and deploy your web applications securely, confirming that you test any changes to the environment progressively. As you become more familiar with managing your LAMP stack, you can explore additional configurations that enhance its performance, security, and flexibility.