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
sudoprivileges. -
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.
-
Open your terminal.
-
Run the following command to update your package manager:
Bashsudo 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.
-
Install Apache:
Bashsudo dnf install httpd -y -
Start the Apache service:
Bashsudo systemctl start httpd -
Enable Apache to start automatically on boot:
Bashsudo systemctl enable httpd -
Configure the Firewall:
If you have
firewalldrunning, you need to allow HTTP and HTTPS traffic so the outside world can access your server.Bashsudo firewall-cmd --permanent --zone=public --add-service=http sudo firewall-cmd --permanent --zone=public --add-service=https sudo firewall-cmd --reloadYou 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.
-
Install MariaDB:
Bashsudo dnf install mariadb-server mariadb -y -
Start the MariaDB service:
Bashsudo systemctl start mariadb -
Enable MariaDB to start automatically on boot:
Bashsudo systemctl enable mariadb -
Secure your database:
MariaDB comes with a security script that removes dangerous defaults. Run it with:
Bashsudo 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.
-
Install PHP and the MySQL extension:
Bashsudo dnf install php php-mysqlnd -y -
Restart Apache:
For Apache to recognize the new PHP installation, you must restart the web server.
Bashsudo 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.
-
Create the test file:
Use the
nanotext editor (orvi) to create a file calledinfo.phpin your web root directory.Bashsudo nano /var/www/html/info.php -
Add the PHP code:
Paste the following lines into the blank file:
PHP<?php phpinfo(); ?> -
Save and exit:
If using
nano, pressCTRL + Oto save,Enterto confirm, andCTRL + Xto exit. -
View the page in your browser:
Open your web browser and navigate to:
http://your_server_ip/info.php(Replace
your_server_ipwith 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.
sudo rm /var/www/html/info.php
Your LAMP stack is now fully installed, configured, and ready to host dynamic web applications.