How to Install LAMP Stack on Ubuntu

Why Use a LAMP Stack?

The LAMP stack is open-source software typically installed to enable a server to host dynamic websites or web apps. This stack is commonly used in web development, and it includes:

  1. Linux: It is the core operating system running on the server. Linux is open-source, reliable, and widely supported, making it a popular server choice.
  2. Apache: This web server software serves web pages in response to requests from web browsers.
  3. MySQL: This is a database management system. It stores information that can be queried by your web applications to generate dynamic page content or store user data.
  4. PHP: This server-side scripting language works with Apache to make your website dynamic.

As a web developer, a LAMP stack gives you a full suite of tools to build dynamic websites. It is also open-source, so it’s free to use and supported by a robust community.

Installation Guide

This tutorial assumes that you have Ubuntu 20.04 LTS (or later versions) installed on your machine.

If you don’t already have Ubuntu Desktop or Ubuntu installed, check out the following videos to get started:

Step 1: Update Your System

Always start with an updated system. From your terminal window, enter:

sudo apt update && sudo apt upgrade -y

Step 2: Install Apache

To install Apache, type in the following command:

sudo apt install apache2

After the installation, you can confirm Apache is running by typing:

sudo systemctl status apache2

You can also check this by opening a web browser and navigating to http://your_server_IP_address/, you should see the default Apache2 Ubuntu page.

Step 3: Install MySQL

To install MySQL, type in the following command:

sudo apt install mysql-server mysql-client

After the installation, log in with sudo to get started by creating a database and user.

sudo mysql

Step 4: Install PHP

To install PHP and related modules, use the following command:

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

You can verify the installation and check the PHP version with:

php -v

Step 5: Configuring Apache to Use PHP

By default, Apache delivers an HTML file on requests. We need to tell Apache to prefer PHP files. Open the dir. conf file with this command:

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

Ensure the index.php file is first in the DirectoryIndex list:

<IfModule mod_dir.c>
    DirectoryIndex index.php index.html index.cgi index.xhtml index.htm

Press CTRL+X, then Y, then ENTER to save the changes. Now restart Apache to apply the changes:

sudo systemctl restart apache2

Step 6: Test PHP Processing on your Web Server

To test PHP, we will create a fundamental PHP script. Open a new file in the Apache document root:

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

Add the following content to the file:


Again, CTRL+X, then Y, then ENTER to save and close the file. If you navigate to http://your_server_IP_address/info.php, you should see a PHP information page.

Congratulations, you have successfully installed a LAMP stack on your Ubuntu server.

This LAMP stack will provide a robust platform for web development. You can now serve web pages, store and manage data in an SQL database, and execute dynamic PHP scripts. Now, you can start developing your dynamic web applications.

Spread the love

Related Posts