Home  »  ArticlesGuidesHow ToSoftwareTechnologyTools   »   How to Install Apache, MySQL, PHP (LAMP) Stack on Ubuntu 24.04 LTS

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

The LAMP stack, consisting of Linux, Apache, MySQL, and PHP, is a popular open-source web platform used to run dynamic websites and servers. Ubuntu 24.04 LTS, with its stability and support, makes an excellent base for setting up a LAMP stack. In this comprehensive guide, we’ll walk you through the steps to install the LAMP stack on Ubuntu 24.04 LTS.

Prerequisites

Before you begin, ensure you have:

  1. A system running Ubuntu 24.04 LTS.
  2. Sudo privileges to install and configure the software.

Step 1: Update Package Index

Start by updating your package index to ensure you have the latest information on available packages:

$ sudo apt update

Step 2: Install Apache

Apache is a widely used web server software. To install Apache, run the following command:

$ sudo apt install apache2 -y

Once installed, you can enable Apache to start on boot and start the service immediately:

$ sudo systemctl enable apache2
$ sudo systemctl start apache2

You can verify that Apache is running by visiting your server’s IP address in a web browser. You should see the default Apache welcome page.

Step 3: Install MySQL

MySQL is a powerful database management system. Install MySQL using the following command:

$ sudo apt install mysql-server -y

After the installation, run the security script to improve MySQL’s security:

$ sudo mysql_secure_installation

Follow the prompts to configure the security settings. This includes setting a root password, removing anonymous users, disallowing remote root login, and removing the test database.

Step 4: Install PHP

PHP is a server-side scripting language that is widely used for web development. To install PHP and the necessary modules, run:

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

To test if PHP is installed correctly, create a PHP info file:

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

Add the following content to the file:

<?php
phpinfo();
?>

Save and close the file. You can now visit http://your_server_ip/info.php in your web browser to see the PHP information page.

Step 5: Configure Apache to Prefer PHP Files

By default, Apache serves index.html files before index.php files. To change this behavior, edit the dir.conf file:

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

Move the index.php entry to the first position like this:

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

Save and close the file, then restart Apache to apply the changes:

$ sudo systemctl restart apache2

Step 6: Test Your LAMP Stack

To ensure that everything is working correctly, you can create a simple PHP script to test the database connection. Create a new PHP file:

$ sudo nano /var/www/html/testdb.php

Add the following content:

<?php
$servername = "localhost";
$username = "root";
$password = "your_root_password";

// Create connection
$conn = new mysqli($servername, $username, $password);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>

Replace "your_root_password" with the root password you set during the MySQL secure installation process. Save and close the file, then visit http://your_server_ip/testdb.php in your web browser. You should see a message saying “Connected successfully”.

Conclusion

Congratulations! You’ve successfully installed the LAMP stack on your Ubuntu 24.04 LTS system. You now have a powerful web server ready to host dynamic web applications. Make sure to remove the test scripts (info.php and testdb.php) once you’ve confirmed that everything is working to prevent exposing sensitive information.

Feel free to explore further configurations and optimizations to tailor your LAMP stack to your specific needs.

Found this article interesting? Follow Brightwhiz on Facebook, Twitter, and YouTube to read and watch more content we post.