How to Setup a LAMP Stack on Ubuntu for Web Development

Introduction

LAMP stands for Linux, Apache, MySQL/MariaDB, and PHP/Perl/Python, a popular open-source software stack used for creating web servers and deploying web applications.

The LAMP stack is a robust framework for web development, combining these four key components:

  • Linux: The operating system.
  • Apache: The web server software.
  • MySQL/MariaDB: The database management system.
  • PHP/Perl/Python: The programming languages used for server-side scripting.

This guide will walk you through setting up a LAMP stack on Ubuntu, a popular Linux distribution.

Prerequisites

  • A fresh installation of Ubuntu
  • Basic knowledge of Linux command-line operations

Steps to Set Up LAMP on Ubuntu

Step 1: Installing Linux

If you haven’t already, start by installing a Linux distribution like Ubuntu, CentOS, or Debian. For this guide, we’ll use Ubuntu, a popular choice for its ease of use and wide support.

Step 2: Update Your Package Manager

  • Open a terminal window on your Linux system.
  • To ensure you have the latest software, update your package manager by running the following command:
sudo apt update
  • Upgrade installed packages:
sudo apt upgrade
  • This will update the list of available packages and upgrade the installed packages to their latest versions.

Steps to install LAMP stack components

 Step 1: Installing Apache

Apache is required for site visitors to see web pages. Apache is a highly accepted web server with a large active user base, that will be installed on Ubuntu using the apt package manager.

  • Open a terminal window by pressing Ctrl + Alt + T.
  • Install Apache by running the following command:
sudo apt install apache2
Install Apache by running the following command. ‘sudo apt install apache2’
Install Apache by running the following command. ‘sudo apt install apache2’
  • Once the installation is complete, enable Apache to start at boot and start the service by executing the following commands:
sudo systemctl enable apache2 
sudo systemctl start apache2
Command to enable Apache to start at boot and start the service on Ubuntu- sudo ‘systemctl enable apache2’ and ‘sudo systemctl start apache’
Command to enable Apache to start at boot and start the service on Ubuntu- sudo ‘systemctl enable apache2’ and ‘sudo systemctl start apache’
  • To verify that Apache is working properly, open a web browser and enter your server’s IP address or “http://localhost” if you’re setting this up on your local machine. You should see the default Apache welcome page.

Step 2: Install MySQL

With a web server set up successfully, a database system needs to be installed for storing and managing site data. MySQL is one such popular and effective database management system used alongside the PHP ecosystem.

  • Install MySQL by executing the following command:
sudo apt install mysql-server
Command to install MySQL server on Ubuntu using apt package manager- ‘sudo apt install mysql-server'
Command to install MySQL server on Ubuntu using apt package manager- ‘sudo apt install mysql-server’
  • During the installation, you will be prompted to set a root password for MySQL. Choose a strong password and remember it.
  • After installation, secure your MySQL installation by executing the following command and following the prompts:
sudo mysql_secure_installation
Secure MySQL installation command on Ubuntu- ‘sudo mysql_secure_installation’ 
Secure MySQL installation command on Ubuntu- ‘sudo mysql_secure_installation’ 

Step 3: Install PHP

With Apache and MySQL installed to host, store, and manage the data for your site, PHP is to be installed to serve the code and generate dynamic content for the web server.

  • Install PHP and some common extensions by executing the following command:
sudo apt install php libapache2-mod-php php-mysql
Install PHP and extensions on Ubuntu- ‘sudo apt install php libapache2-mod-php php-mysql' 
Install PHP and extensions on Ubuntu- ‘sudo apt install php libapache2-mod-php php-mysql’ 
  • Once PHP is installed, you can also test it by creating a PHP info file. To do this, create a new file in the web server’s root directory.
sudo nano /var/www/html/phpinfo.php
Command to create a PHP info file in the web server root directory on Ubuntu- sudo nano /var/www/html/phpinfo.php
Command to create a PHP info file in the web server root directory on Ubuntu- sudo nano /var/www/html/phpinfo.php 

Note: Add the following content to the file and save it:

             <?php    phpinfo();
  • Restart Apache to apply the changes by executing the following command.
sudo systemctl restart apache2
  • In your web browser, navigate to http://localhost/phpinfo.php (or use your server’s IP address if it’s remote). You should see the PHP information page displayed.

Step 4: Testing MySQL and PHP

You can create a simple PHP script to test the connection to MySQL by following these steps. 

  • Create a test PHP file by executing the following command:
sudo nano /var/www/html/test-mysql.php

Add the following PHP code to the file and save it.

```php    
<?php $connection = mysqli_connect("localhost", "root", "your_mysql_password");

if (!$connection) {
    die("Database connection failed: " . mysqli_connect_error());
}

echo "Connected to MySQL successfully!";

mysqli_close($connection); ?>
``` 
Create a test PHP file to check MySQL connection on Ubuntu using nano, including sample PHP code for testing 
Create a test PHP file to check MySQL connection on Ubuntu using nano, including sample PHP code for testing 
  • Replace ”your_mysql_password” with the root password you set during the MySQL installation in the PHP code.
  • Open your web browser and navigate to http://localhost/test-mysql.php (or your server’s IP address). You should see a message indicating a successful MySQL connection displayed in the browser.

Note: You’ve now successfully installed a LAMP stack on your Ubuntu system. You are now ready to start building and hosting web applications on your server.

Conclusion

Congratulations! You have successfully set up a LAMP stack on Ubuntu. Your system is now ready for web development and hosting applications.

Q1. What are the applications of LAMP? 

Answer: Developers use the LAMP stack because it provides the operating system, web server, database management software, and programming language they need to build web applications such as Content Management Systems (CMS), Ecommerce platforms, dynamic websites, and data-driven applications. 

Q2. What are the LAMP stack components? 

Answer: Linux, Apache, MySQL, and PHP are the components that stack together. 

  • Linux is the operating system layer and the backbone of the LAMP stack. 
  • Apache HTTP Server is a popular web server software that runs on top of the Linux operating system in the LAMP stack. 
  • MySQL is a database management system renowned for its support of SQL and relational tables, making it an excellent choice for creating and maintaining enterprise-level databases. 
  • PHP (Hypertext Preprocessor) is a programming language within the LAMP stack used for developing the server-side logic of websites and web applications. 

Q3. What are the benefits of LAMP? 

Answer: Developers who build and deploy web applications use LAMP because it is: 

  • Open source. 
  • Customizable.  
  • Well supported 
  • A mature stack.  
  • Easy to use.  

Q4. How to stop LAMP in Ubuntu? 

Answer: “Stopping LAMP” is not possible. You can stop 2 components of a LAMP stack: Apache and MySQL. will show if it is running. Linux and PHP are not services, so they do not need to be stopped. 

Q5. What are the prerequisites for installing LAMP on Ubuntu? 

Answer: To install LAMP on Ubuntu, you need a server running Ubuntu, a non-root user with sudo privileges, and an active security system. 

Looking for professional writing solutions? The Scribe specializes in delivering high-quality technical and business writing services tailored to your needs. From user manuals and API guides to engaging web content and technical proposals, our expert writers are dedicated to enhancing your business communications. Reach out to us today to discover how we can help you achieve your documentation goals efficiently!

Komal

Komal

Table of Contents

Read More

Scroll to Top