Home Cloud Serevrs Install and Configure a LAMP Stack on Your VPS (Linux, Apache, MySQL, PHP)

Install and Configure a LAMP Stack on Your VPS (Linux, Apache, MySQL, PHP)

Last updated on Feb 13, 2025

A LAMP stack (Linux, Apache, MySQL, and PHP) is a popular open-source web server environment used to host websites and applications. This guide will walk you through installing and configuring a LAMP stack on your Linux VPS (Ubuntu or Debian).


📌 Prerequisites

Before you begin, make sure you have:

  • A VPS with Ubuntu or Debian installed.

  • Root or sudo user access.

  • An SSH client (like PuTTY for Windows or Terminal for macOS/Linux).


1️⃣ Update Your VPS

Before installing any software, update your package list to ensure you have the latest versions.

sudo apt update && sudo apt upgrade -y

2️⃣ Install Apache Web Server

Apache is the most widely used web server.

Step 1: Install Apache

sudo apt install apache2 -y

Step 2: Start and Enable Apache

sudo systemctl start apache2 sudo systemctl enable apache2

Step 3: Allow Web Traffic Through the Firewall

sudo ufw allow 'Apache Full'

Step 4: Verify Apache is Running

Open your browser and enter your VPS IP address (http://your-vps-ip).
You should see the Apache default page.


3️⃣ Install MySQL Database Server

MySQL is used to store website data and manage databases.

Step 1: Install MySQL

sudo apt install mysql-server -y

Step 2: Secure MySQL Installation

Run the MySQL security script to improve security settings:

sudo mysql_secure_installation

Follow the prompts:

  • Set a strong root password.

  • Remove anonymous users.

  • Disable remote root login.

  • Remove test database.

  • Reload privilege tables.

Step 3: Verify MySQL Installation

sudo systemctl status mysql

4️⃣ Install PHP

PHP is a scripting language used to run dynamic web applications.

Step 1: Install PHP

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

Step 2: Check Installed PHP Version

php -v

5️⃣ Configure Apache to Use PHP

By default, Apache serves index.html first. You need to prioritize PHP files.

Step 1: Edit Apache Configuration

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

Find the following line:

DirectoryIndex index.html index.cgi index.pl index.php index.xhtml index.htm

Move index.php to the front:

DirectoryIndex index.php index.html index.cgi index.pl index.xhtml index.htm

Step 2: Restart Apache

sudo systemctl restart apache2

6️⃣ Test Your LAMP Stack

To verify that PHP is working correctly, create a test file.

Step 1: Create a PHP Info Page

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

Add the following content:

<?php phpinfo(); ?>

Step 2: Save and Exit

Press Ctrl + X, then Y, then Enter to save.

Step 3: Open in a Browser

Visit http://your-vps-ip/info.php in your browser.
You should see a PHP information page.


7️⃣ Secure Your Apache and MySQL Installation

To enhance security, follow these best practices.

Step 1: Disable Directory Listing

Edit the default Apache configuration:

sudo nano /etc/apache2/apache2.conf

Find:

<Directory /var/www/> Options Indexes FollowSymLinks AllowOverride None Require all granted </Directory>

Change Indexes to -Indexes:

<Directory /var/www/> Options -Indexes FollowSymLinks AllowOverride None Require all granted </Directory>

Save and restart Apache:

sudo systemctl restart apache2

Step 2: Set Up a Firewall for MySQL

By default, MySQL listens on port 3306. If you are not running remote databases, block external access:

sudo ufw deny 3306

Step 3: Remove the Test PHP Info File

Once you've verified PHP is working, delete the test file:

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

8️⃣ Final Verification

To confirm everything is working, you can:

  • Restart all services:

    sudo systemctl restart apache2 mysql
    
  • Check their status:

    sudo systemctl status apache2 mysql
    
  • Upload a sample PHP website to /var/www/html/ and test.


🎉 Conclusion

You have successfully installed and configured a LAMP stack on your Linux VPS! You are now ready to host websites, WordPress, or other web applications. 🚀

For any further assistance, contact VolticHost Support.