Home Technical How to setting up a LAMP stack on your VPS

How to setting up a LAMP stack on your VPS

Last updated on Nov 18, 2026

Setting up a LAMP stack (Linux, Apache, MySQL/MariaDB, PHP) on a Virtual Private Server (VPS) is the classic foundation for hosting dynamic websites and applications like WordPress, Drupal, or custom PHP code.

Debian/Ubuntu

  1. Prerequisites:

  2. Update Package Lists:

    Before installing any software, update your server's package index to ensure you get the latest stable versions:

    sudo apt update && sudo apt upgrade -y

  3. Install the Apache Web Server:

    • Apache is one of the most reliable and widely used web servers in the world. Install it using the apt package manager:

      sudo apt install apache2 -y

    • Once installed, adjust your firewall settings to allow HTTP (port 80) and HTTPS (port 443) traffic through UFW:

      sudo ufw allow 'Apache Full'

    • Verify that Apache is running by checking its service status:

      sudo systemctl status apache2

    • Test it:

      Open your web browser and navigate to your server's public IP address (http://YOUR_SERVER_IP). You should see the default Ubuntu Apache welcome page.

  4. Install the MySQL Database Server:

    • Install MySQL (or MariaDB as a drop-in alternative) to handle your application's data:

      sudo apt install mysql-server -y

    • After installation, it is critical to run the built-in security script to remove unsafe default settings:

      sudo mysql_secure_installation

      This script will prompt you to configure the VALIDATE PASSWORD PLUGIN (recommended for strong security), set a root password, remove anonymous users, disallow root login remotely, and remove the test databases.

  5. Install PHP:

    • PHP is the scripting language that processes code to build dynamic web content. Alongside PHP, you will need several extensions so it can communicate with Apache and MySQL.

    • Install PHP and the most common modules with this command:

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

    • Check your installed PHP version to ensure success:

      php -v

  6. Configure Apache to Handle PHP Files (Optional Check):

    • By default, Apache serves index.html files before index.php. If you want to change this behavior so Apache prioritizes PHP files first, open the dir.conf configuration file:

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

    • It will look like this:

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

    • Save and close the file (Ctrl+O, Enter, then Ctrl+X), then restart Apache:

      sudo systemctl restart apache2

  7. Test PHP Processing:

    • To verify that your server is properly processing PHP scripts, create a test file inside your web root directory (/var/www/html/):

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

    • Add the following standard PHP code inside the file:

      <?php

      phpinfo();

      ?>

    • Save and close the file. Now, open your browser and navigate to:

      http://YOUR_SERVER_IP/info.php

    • You should see a detailed system page outlining your PHP configuration, version, and active modules.

    • 🔒 Security Note: Once you have verified everything works correctly, delete this file for security reasons, as it exposes sensitive server data:

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

  8. Conclusion:

    Congratulations! You now have a fully functional LAMP stack running on your VPS, ready for you to host applications, configure virtual hosts, or install an SSL certificate via Let's Encrypt.

RHEL/CentOS

  1. Prerequisites:

  2. Update System Packages:

    • Before installing any components, ensure your system repositories and packages are fully up to date:

      sudo dnf upgrade -y

  3. Install and Configure the Apache Web Server:

    Apache (known in the Red Hat ecosystem as httpd) is available directly from the standard DNF repositories.

    • Install Apache:

      sudo dnf install httpd -y

    • Start and Enable Apache:

      Ensure Apache starts automatically on system boot and is running right now:

      sudo systemctl enable --now httpd

    • Configure the Firewall:

      CentOS Stream comes with firewalld enabled by default. Allow HTTP and HTTPS traffic through the firewall:

      sudo firewall-cmd --permanent --add-service=http

      sudo firewall-cmd --permanent --add-service=https

      sudo firewall-cmd --reload

    • Test it:

      Open your web browser and navigate to your server's public IP address (http://YOUR_SERVER_IP). You will see the default CentOS Stream Apache test page.

  4. Install and Secure MariaDB (Database Server):

    CentOS Stream uses MariaDB as the default drop-in replacement for MySQL.

    • Install MariaDB Server:

      sudo dnf install mariadb-server -y

    • Start and Enable MariaDB:

      sudo systemctl enable --now mariadb

    • Run the Security Script:

      Secure your database installation by setting a root password, removing anonymous users, and disabling remote root logins:

      sudo mysql_secure_installation

      Follow the interactive prompts (pressing Y for most recommendations is standard).

  5. Install PHP:

    CentOS Stream provides modern PHP streams. You can check available modules or install the core package alongside common extensions needed for frameworks like WordPress or Drupal:

    • Install PHP and Common Extensions:

      sudo dnf install php php-mysqlnd php-gd php-xml php-mbstring -y

    • Verify PHP Version:

      Check that PHP installed correctly:

      php -v

    • Restart Apache to Load PHP:

      sudo systemctl restart httpd

  6. Test PHP Processing:

    To verify that Apache and PHP are communicating correctly, create a test script in the web root directory (/var/www/html/):

    • Create the test file:

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

    • Add the PHP code:

      PHP

      <?php

      phpinfo();

      ?>

      (Save and exit by pressing Ctrl+O, Enter, then Ctrl+X).

    • Test in your browser:

      Navigate to http://YOUR_SERVER_IP/info.php. You should see the official PHP configuration page detailing your build, modules, and environment settings.

    • 🔒 Security Note: Once verification is complete, remove this file to prevent exposing sensitive system diagnostics:

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

  7. Adjust SELinux Permissions (Important for CentOS):

    CentOS uses SELinux for enhanced security. If you plan to host web files outside the default /var/www/html/ directory or allow PHP to write to files (like uploads), ensure your SELinux contexts are properly configured:

    • To allow Apache to connect out to databases or networks:

      sudo setsebool -P httpd_can_network_connect_db on

  8. Conclusion:

    Your LAMP stack on CentOS Stream 10 is now completely operational, secure, and ready to host your dynamic web applications!