Pulsar67 Logo
Setting Up a LAMP Stack (Linux, Apache, MySQL, PHP)

Setting Up a LAMP Stack (Linux, Apache, MySQL, PHP)

By Trenton Barrett on March 24, 2025 (Last updated: March 29, 2025)

Installing a LAMP Stack on Ubuntu

This comprehensive guide walks you through setting up a LAMP stack — Linux, Apache, MySQL, and PHP — on an Ubuntu server. This configuration is a common foundation for hosting dynamic web applications.

Step 1: Update Your System

Before installing anything, it’s important to make sure your system package index is up to date. This ensures you're getting the latest versions of all software.

sudo apt update
sudo apt upgrade

Step 2: Install Apache

Apache is a powerful and widely-used web server. Once installed, it will serve your website's content to users.

sudo apt install apache2

Check the service status to ensure it’s running properly:

sudo systemctl status apache2

If you’re seeing "active (running)," then Apache has started successfully.

Step 3: Install MySQL

MySQL is a popular database system used to store website and application data.

sudo apt install mysql-server

Important Warning (as of July 2022):

Running mysql_secure_installation immediately after installation may result in an error loop. This occurs because the MySQL root user, by default, is set to authenticate using the auth_socket plugin instead of a password.

To fix this, first change the authentication method before running the secure installation script:

sudo mysql

ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'your_secure_password';

exit

After this change, the secure installation script can be run without issues.

Now, run the security script to improve your MySQL installation:

sudo mysql_secure_installation

Note:

One of the prompts asks if you'd like to enable password validation. This feature rejects weak passwords that don’t meet strength criteria. While it’s optional, enabling it is a good practice for production environments. Regardless, always use strong, unique passwords for your databases.

During the script, you'll be prompted to:

  • Enable password validation (optional)
  • Set a root password (you've already done this)
  • Remove anonymous users
  • Disallow remote root login
  • Remove test databases
  • Reload privilege tables to apply changes

Step 4: Install PHP

PHP is the scripting language used for dynamic web content. You’ll also install modules to connect it with Apache and MySQL.

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

Check that PHP is installed:

php -v

Step 5: Test PHP with Apache

To verify PHP is working through Apache, create a test file:

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

Then open your browser and go to:

http://your_server_ip/info.php

You should see the PHP information page confirming everything is configured correctly.

Step 6: Adjust the Firewall (Optional)

If you're using UFW (Uncomplicated Firewall), you’ll need to allow Apache traffic:

sudo ufw allow in "Apache Full"

This ensures that HTTP and HTTPS traffic can reach your server.

Conclusion

You’ve successfully installed a full LAMP stack on Ubuntu. You now have Apache serving content, MySQL managing your data, and PHP rendering dynamic pages. You’re ready to start building and deploying modern web applications.