How to Install LAMP on Centos. Print

  • centos, lamp, webserver
  • 111

Here is a complete, step-by-step guide to installing a LAMP stack (Linux, Apache, MariaDB/MySQL, and PHP) on CentOS.

A quick note on CentOS before we begin: Because CentOS Linux 7 and 8 have reached their End of Life (EOL), this guide uses dnf commands, which are standard for modern RHEL-based systems like CentOS Stream 9, Rocky Linux, and AlmaLinux. If you are on an older CentOS 7 server, you can simply substitute yum for dnf in the commands below.

Prerequisites

  • A CentOS server (or equivalent RHEL clone).

  • A user account with sudo privileges.

  • An active internet connection.

Step 1: Update Your System

Before installing new software, it is always best practice to ensure your existing system packages are up to date.

  1. Open your terminal.

  2. Run the following command to update your package manager:

    Bash
     
    sudo dnf update -y
    

Step 2: Install Apache (The Web Server)

Apache is the software that will serve your web pages to visitors. In CentOS, the Apache package is called httpd.

  1. Install Apache:

    Bash
     
    sudo dnf install httpd -y
    
  2. Start the Apache service:

    Bash
     
    sudo systemctl start httpd
    
  3. Enable Apache to start automatically on boot:

    Bash
     
    sudo systemctl enable httpd
    
  4. Configure the Firewall:

    If you have firewalld running, you need to allow HTTP and HTTPS traffic so the outside world can access your server.

    Bash
     
    sudo firewall-cmd --permanent --zone=public --add-service=http
    sudo firewall-cmd --permanent --zone=public --add-service=https
    sudo firewall-cmd --reload
    

    You can verify Apache is working by typing your server's public IP address into a web browser. You should see the default CentOS Apache welcome page.

Step 3: Install MariaDB (The Database)

MariaDB is a community-developed fork of MySQL and is the default database system provided in CentOS repositories.

  1. Install MariaDB:

    Bash
     
    sudo dnf install mariadb-server mariadb -y
    
  2. Start the MariaDB service:

    Bash
     
    sudo systemctl start mariadb
    
  3. Enable MariaDB to start automatically on boot:

    Bash
     
    sudo systemctl enable mariadb
    
  4. Secure your database:

    MariaDB comes with a security script that removes dangerous defaults. Run it with:

    Bash
     
    sudo mysql_secure_installation
    
    • Note: You will be asked for the current root password. Since you just installed it, leave it blank and press Enter.

    • When asked to set a root password, press Y and create a strong password.

    • For the remaining prompts (removing anonymous users, disallowing remote root login, removing the test database, and reloading privilege tables), press Y and hit Enter for all of them.

Step 4: Install PHP (The Scripting Language)

PHP is the language that will process code to display dynamic content. We also need to install the PHP extension that allows it to communicate with MariaDB.

  1. Install PHP and the MySQL extension:

    Bash
     
    sudo dnf install php php-mysqlnd -y
    
  2. Restart Apache:

    For Apache to recognize the new PHP installation, you must restart the web server.

    Bash
     
    sudo systemctl restart httpd
    

Step 5: Test the LAMP Stack

To confirm that Apache and PHP are working together perfectly, you can create a basic PHP info script.

  1. Create the test file:

    Use the nano text editor (or vi) to create a file called info.php in your web root directory.

    Bash
     
    sudo nano /var/www/html/info.php
    
  2. Add the PHP code:

    Paste the following lines into the blank file:

    PHP
     
    <?php
    phpinfo();
    ?>
    
  3. Save and exit:

    If using nano, press CTRL + O to save, Enter to confirm, and CTRL + X to exit.

  4. View the page in your browser:

    Open your web browser and navigate to:

    http://your_server_ip/info.php

    (Replace your_server_ip with your actual server IP address). You should see a detailed web page displaying information about your PHP installation, including versions and enabled modules.

Important Security Step: Once you have verified that PHP is working, it is highly recommended that you delete the info.php file so unauthorized users cannot see your server's configuration details.

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

Your LAMP stack is now fully installed, configured, and ready to host dynamic web applications.


Was this answer helpful?

« Back

Powered by WHMCompleteSolution