How to Install LAMP Stack on CentOS 8?

Introduction
The LAMP stack is a widely-used set of open-source software that forms the backbone of many web applications. It includes Linux, Apache, MySQL (or MariaDB), & PHP. Together, these components create a powerful environment for developing and operating dynamic websites as well as apps. This guide will walk you through how to install the LAMP stack on CentOS 8, step-by-step, ensuring a smooth and robust setup.
With Apache serving as the web server, MySQL (or MariaDB) managing your database, and PHP handling the server-side scripting, the LAMP stack enables easy interaction between your applications and data. Setting up this stack on CentOS 8 provides a stable, reliable foundation for web hosting and application deployment. Additionally, for users who may be working on CentOS 7 or looking to install the LAMP stack on similar Linux systems, this guide will provide useful insights and variations where needed.
In this tutorial, you’ll learn the essentials of installing the LAMP stack on CentOS 8: from updating software and configuring Apache, MySQL, and PHP to setting up the firewall and testing your PHP installation. Whether you’re installing LAMP on CentOS 8 for personal projects, testing, or production, this guide ensures a clear path to a complete and secure LAMP stack setup.
Also Read: How to Check CentOS Version?
What is LAMP stack?

The LAMP stack is a prevalent open-source software suite that provides a robust platform for web development and deployment. The name “LAMP” stands for Linux, Apache, MySQL (or MariaDB), and PHP, each component playing a critical role in powering dynamic websites and applications.
- Linux: The foundation of the LAMP stack, Linux is the operating system that underlies the entire setup. It provides the stability and flexibility needed to run applications on both CentOS 8 and CentOS 7. For this guide, we’ll focus on installing the LAMP stack on CentOS 8, though similar steps apply if you’re using CentOS 7.
- Apache: Apache implies the web server in the LAMP stack. It administers HTTP requests as well as serves web content to users. Apache is known for its reliability, scalability, and flexibility, making it a favorite for developers working with LAMP on CentOS 8. By configuring Apache properly, you ensure that web traffic is managed efficiently and that your web content is always available to users.
- MySQL or MariaDB: MySQL (or MariaDB, a compatible alternative) is the database management system in the LAMP stack. This system is responsible for storing and managing data, making it essential for interactive websites and data-driven applications. By installing MySQL or MariaDB with the LAMP stack on CentOS 8, you create a powerful system for managing data securely and efficiently.
- PHP: PHP is the server-side scripting language that boosts dynamic content in web applications. It allows for interaction between the web server and the database, making applications more responsive and user-friendly. When you install PHP with the LAMP stack on CentOS 8, you enable your server to execute scripts that deliver content dynamically based on user interactions.
Installing the LAMP stack on CentOS 8 (or CentOS 7) sets up a reliable and flexible environment for developing, testing, and hosting web applications. This setup allows developers to run, scale, and optimize applications efficiently on a CentOS 8 LAMP server. Additionally, this setup supports lamp php configurations, enabling smooth operation and connectivity between the different software layers.
Installing the LAMP stack on CentOS 8 creates a stable environment to deploy applications or host websites. Let’s dive into how to install the LAMP stack on CentOS 8 and get it running.
Step 1: Update System Software Packages
Before you install the LAMP stack on CentOS 8, it’s essential to update the system software packages. This step ensures you’re working with the latest, most secure versions of all required software, providing better performance and compatibility. Running a system update also reduces potential security vulnerabilities in the LAMP stack setup.
To update your system, use the following command:
sudo dnf update -y
This command updates all installed packages, ensuring your CentOS 8 LAMP install will run on a stable and secure foundation. Regular updates are recommended, even after installing LAMP, to keep your server’s software up-to-date.
Step 2: Install Apache
Apache is a resilient and largely-employed web server, forming the core of the LAMP stack. To install Apache on CentOS 8, use the given below command:
sudo dnf install httpd -y
Once the installation is finished, you are required to permit Apache to begin on boot and then start the service. This ensures that Apache will run automatically every time the server is restarted:
sudo systemctl enable httpd sudo systemctl start httpd
To verify that Apache was installed successfully, open a web browser and input the IP address of your server. You should see the Apache test page, confirming that Apache is running. Successfully configuring Apache on CentOS 8 is an important step in setting up a LAMP stack install that is accessible to users.
Step 3: Install MySQL
MySQL (or MariaDB) is essential for managing databases, which are the backbone of any dynamic website. In this guide, we’ll use MySQL for the database component of the LAMP stack on CentOS 8. To install MySQL, use the command:
sudo dnf install mysql-server -y
Once installed, enable and begin the MySQL service so it runs every time the server starts:
sudo systemctl enable mysqld sudo systemctl start mysqld
After completing this step, MySQL is active on your CentOS 8 server, ready to support your data-driven applications. The database management system is crucial for LAMP PHP applications that depend on stored data, like e-commerce sites or content administration systems.
Step 4: Configure MySQL Security
It’s essential to secure your MySQL installation to prevent unauthorized access and protect your data. MySQL provides a simple security configuration tool for this purpose. To begin the configuration wizard, use the following command:
sudo mysql_secure_installation
Follow the prompts to set a strong root password, discard anonymous users, disable remote root login, and eliminate test databases. These actions help secure your MySQL installation, making your CentOS 8 LAMP install more secure and resilient to threats. Securing MySQL is a best practice for any LAMP stack environment, especially in production servers.
Step 5: Install PHP and Supporting Modules
PHP is the programming language that allows your server to generate dynamic content, working alongside Apache to display content based on user interactions or database data. To install PHP on CentOS 8 along with essential PHP modules, use the following command:
sudo dnf install php php-mysqlnd php-fpm -y
After PHP is installed, restart Apache to enable it to process PHP files:
sudo systemctl restart httpd
Installing PHP completes the LAMP stack, enabling your CentOS 8 server to handle dynamic web applications and support interaction with MySQL databases. This is especially useful for LAMP PHP setups where data processing and retrieval are needed.
Step 6: Adjust the Firewall
Configuring your firewall is crucial to allow users to access your server. To enable HTTP and HTTPS connections, which are standard for web traffic, you need to adjust the firewall settings. Use the following commands:
sudo firewall-cmd --permanent --add-service=http sudo firewall-cmd --permanent --add-service=https sudo firewall-cmd --reload
These commands open the HTTP and HTTPS ports, allowing users to connect to your LAMP stack on CentOS 8. Remember, firewall configurations are vital for web hosting, as they help secure your server while still enabling accessibility for authorized traffic.
Step 7: Test PHP with Apache
The final step is to test if PHP is correctly configured with Apache. To do this, create a test PHP file in the Apache web root directory using the following command:
sudo echo "<?php phpinfo(); ?>" > /var/www/html/info.php
This command creates a PHP file that displays detailed information about your PHP installation. Open a web browser and go to http://your_server_ip/info.php. In case PHP is configured aptly, you would watch the PHP data page, confirming that your LAMP install on CentOS 8 is fully functional.
For security reasons, it’s best to remove this test file afterward, as it contains sensitive server information:
sudo rm /var/www/html/info.php
Also Read: CentOS vs. Ubuntu: Features Comparison and Uses
Conclusion
Congratulations! You’ve now successfully installed the LAMP stack on CentOS 8, setting up a reliable environment for hosting and developing web applications. Apache is now configured to handle HTTP requests, MySQL manages your data, and PHP supports dynamic content—ensuring seamless interaction between your applications and data.
With your CentOS 8 server fully set up with LAMP, you’re ready to move on to developing and deploying web applications or sites. This step-by-step guide on how to install the LAMP stack on CentOS 8 not only covers the basics but also gives you practical insights into securing and configuring each component efficiently. Now, whether you’re using CentOS 7 or CentOS 8, this guide provides a foundation for building high-performance, data-driven websites.
For ongoing management, remember to keep Apache, MySQL, and PHP updated, monitor server performance, and regularly back up your data. Following these practices will ensure your LAMP stack on CentOS 8 remains secure, optimized, and ready to handle web traffic reliably.