DEV Community

Sangam Poudel
Sangam Poudel

Posted on

Setting Up a LAMP Stack on Linux for Developers

Hello Developers,

Starting your journey as a software developer requires both hunger and enthusiasm for learning. As you embark on this path, maintaining the same pace and passion is crucial—especially when it comes to your development environment. You don’t need the latest hardware to set up a powerful development setup; even an older laptop can be transformed into a robust environment by setting up a LAMP stack. This setup not only provides an excellent learning platform but also familiarizes you with essential technologies like deployment, automation, server configuration, and optimization.

Why Choose LAMP Stack?

A LAMP stack—comprising Linux, Apache, MySQL, and PHP—is a time-tested environment for web development. It’s versatile, reliable, and widely used, making it an excellent choice for learning and development. Using Linux for development offers several benefits, regardless of your technology stack, including enhanced control, performance, and security.

In This Guide

Today, I'll walk you through setting up a LAMP stack on your Ubuntu machine. We'll also use multiple PHP versions managed via the ondrej/php repository, ensuring you can work with different PHP versions as needed.

Step-by-Step Guide to Setting Up LAMP Stack on Ubuntu

1. Update Your System
Before starting, make sure your system is up to date:

sudo apt update
sudo apt upgrade
Enter fullscreen mode Exit fullscreen mode

2. Install Apache
Apache is the web server that will serve your applications. Install it with:
sudo apt install apache2

After installation, start Apache and enable it to run on boot:

sudo systemctl start apache2
sudo systemctl enable apache2
Enter fullscreen mode Exit fullscreen mode

You can verify that Apache is working by visiting http://localhost in your web browser.

3. Install MySQL
MySQL is the database server for your applications. Install it with:
sudo apt install mysql-server

Run the security script to improve MySQL security:
sudo mysql_secure_installation

Follow the prompts to set up a root password and secure your installation.

4. Install PHP
We’ll use the ondrej/php repository to manage multiple PHP versions. First, add the repository:

sudo add-apt-repository ppa:ondrej/php
sudo apt update
Enter fullscreen mode Exit fullscreen mode

Now, install PHP and the necessary modules. For example, to install PHP 8.1:
sudo apt install php8.1 libapache2-mod-php8.1 php8.1-mysql

You can also install additional PHP versions as needed, such as PHP 8.3:
sudo apt install php8.3 libapache2-mod-php8.3 php8.3-mysql

To switch between PHP versions, use:

sudo a2dismod php8.1
sudo a2enmod php8.3
sudo systemctl restart apache2
Enter fullscreen mode Exit fullscreen mode

5. Restart Apache and check the installation.
Once all the setup is completed, restart apache service using
sudo systemctl restart apache2

Create a PHP info file to test your setup:
sudo nano /var/www/html/info.php

Add the following content:

<?php
phpinfo();
?>
Enter fullscreen mode Exit fullscreen mode

Save and exit. Visit http://localhost/info.php in your browser to see the PHP info page.

6. (Optional) Install Additional Tools
Consider installing phpmyadmin for managing MySQL databases through a web interface:
sudo apt install phpmyadmin

Follow the prompts to configure it with Apache and MySQL.

Conclusion
Setting up a LAMP stack on your Ubuntu machine provides a powerful development environment, even on older hardware. By using multiple PHP versions and familiarizing yourself with various technologies, you’ll gain invaluable skills that will aid in your software development journey.

Feel free to reach out with any questions or share your experiences with setting up your development environment.
Happy coding!

Top comments (0)