How to Set Up a LAMP Stack (Linux, Apache, MySQL, PHP) on Your VPS

A LAMP stack is a powerful framework for hosting web applications. This guide will help you install and configure Linux, Apache, MySQL, and PHP on your VPS.


Step 1: Update Your System

  • Log in to Your VPS:

    • Use SSH to connect to your VPS:
    bash
    ssh user@your_vps_ip
  • Update the Package Index:

    • Update your system’s package index to ensure you have the latest information on available packages:
    bash
    sudo apt update
  • Upgrade Installed Packages:

    • Upgrade any installed packages to their latest versions:
    bash
     
    sudo apt upgrade -y

Step 2: Install Apache

  • Install Apache:

    • Use the following command to install the Apache web server:
    bash
    sudo apt install apache2 -y
  • Start Apache Service:

    • Start the Apache service and enable it to run on boot:
    bash
    sudo systemctl start apache2
    sudo systemctl enable apache2
  • Verify Apache Installation:

    • Open your web browser and navigate to http://your_vps_ip. You should see the Apache2 default welcome page.

Step 3: Install MySQL

  • Install MySQL:

    • Install the MySQL server package:
    bash
    sudo apt install mysql-server -y
  • Secure MySQL Installation:

    • Run the security script to set up security options for MySQL:
    bash
    sudo mysql_secure_installation
    • Follow the prompts to configure your MySQL installation, including setting a root password and removing anonymous users.

Step 4: Install PHP

  • Install PHP:

    • Install PHP along with the necessary modules for Apache and MySQL:
    bash
    sudo apt install php libapache2-mod-php php-mysql -y
  • Restart Apache:

    • Restart the Apache service to load the new PHP module:
    bash
    sudo systemctl restart apache2

Step 5: Test PHP Processing

  • Create a PHP Info File:

    • Create a new PHP file in the web root directory:
    bash
    echo "<?php phpinfo(); ?>" | sudo tee /var/www/html/info.php
  • Access the PHP Info Page:

    • Open your web browser and navigate to http://your_vps_ip/info.php. You should see the PHP information page, confirming that PHP is working correctly.
  • Remove the Info File:

    • For security reasons, remove the info.php file after testing:
    bash
    sudo rm /var/www/html/info.php

Step 6: Configure Firewall

  • Allow Apache through the Firewall:

    • If you have a firewall enabled, allow Apache to receive traffic:
    bash
    sudo ufw allow 'Apache Full'
  • Check Firewall Status:

    • Verify that the firewall rules are correctly set:
    bash
    sudo ufw status

Step 7: Finalize and Secure Your LAMP Stack

  • Update MySQL User Privileges:

    • Log in to MySQL:
    bash
    sudo mysql -u root -p
    • Run the following commands to create a new user and grant privileges:
    sql
     
    CREATE USER 'newuser'@'localhost' IDENTIFIED BY 'password'; GRANT ALL PRIVILEGES ON *.* TO 'newuser'@'localhost' WITH GRANT OPTION; FLUSH PRIVILEGES; EXIT;
  • Secure Apache:

    • Consider enabling additional security features, such as disabling directory listing and setting up HTTPS using SSL certificates.

By following these steps, you have successfully set up a LAMP stack on your VPS. You can now start hosting your web applications and sites. If you encounter any issues, please refer to the documentation or seek assistance from support.

Was this answer helpful? 0 Users Found This Useful (0 Votes)