The LAMP stack is a set of open-source software used for web application development. It consists of a Linux operating system, an Apache HTTP server, the MySQL database management system, and PHP programming language.
In this tutorial, learn how to install the LAMP stack on CentOS 8.
- A server running CentOS 8 Linux
- A terminal window/command line (Search > terminal)
- A user account with sudo or root privileges
- The yum and RPM package managers, included by default
Follow the installation steps outlined below and install the required software in the specified order. As this guide is for CentOS 8 users, it skips the first layer of the stack (the Linux operating system), presuming it has already been installed.
Step 1: Update System Software Packages
Open a terminal window and update the package repository before installing new software:
sudo yum update
Step 2: Install Apache
1. Install Apache Web Services with the command:
sudo yum –y install httpd
2. Then, start the Apache service by running:
sudo systemctl start httpd.service
3. To verify Apache is running, open a web browser and navigate to the server’s public IP address. It should display the Apache Test Page, as in the image below.
Note: For additional details, refer to our article on how to install Apache on CentOS 8.
Step 3: Install MySQL
The third layer of the LAMP stack is MySQL or MariaDB. Both are open-source database management systems used for storing and managing data on your website.
In this example, the tutorial includes a MySQL installation. Alternatively, you can install MariaDB.
1. Start by adding the MySQL repository:
rpm –ivh https://dev.mysql.com/get/mysql80-community-release-el8-1.noarch.rpm
2. Check the repository by typing:
sudo yum repolist all |grep mysql | grep enabled
The output should display a list of the different MySQL options available.
3. Now, install MySQL:
sudo yum ––disablerepo=AppStream install –y mysql-community-server
4. Then, enable the MySQL service and set it to start on boot:
sudo systemctl start mysqld
sudo systemctl enable mysqld
5. Check whether the service is running properly with the command:
sudo systemctl status mysql
The output should display the service is active (running).
Note: To easily manage MySQL databases, admins often opt to install PHPMyAdmin.
Step 4: Configure MySQL Security
The next step is to configure the basic MySQL security settings.
1. Start by displaying the temporary MySQL root password:
cat /var/log/mysqld.log | grep –i ‘temporary password’
The system displays the temporary MySQL password.
2. Next, run the command:
Provide the MySQL root password received in the previous step.
3. Then, enter and re-enter a new password, making sure it meets the standards for password strength.
4. After updating the password, you are prompted again to change the root password. To do so, type
y and hit Enter.
5. The output displays the estimated strength of the password. Press
y to continue with the password provided.
Answer the rest of the questions with
y and Enter:
- Remove anonymous users? (y)
- Disallow root login remotely? (y)
- Remove test database and access to it? (y)
- Reload privilege tables now? (y)
With this, MySQL is secured.
Note: For an in-depth guide and additional instructions, please refer to How to Install MySQL on CentOS 8.
Step 5: Install PHP and Supporting Modules
Set up the final layer by installing the PHP programming language and the supporting modules for phpMyAdmin:
sudo yum –y install php php-pdo php-pecl-zip php-json php-common php-fpm php-mbstring php-cli php-mysqlnd wget upzip
Then, enable the PHP module to work with Apache by restarting the webserver:
sudo systemctl restart httpd.service
Step 6: Adjust the Firewall
If you have firewalld set up on CentOS, you need to adjust the configuration to allow Apache connections.
1. Open the firewall for HTTP traffic with the command:
sudo firewall-cmd --permanent --zone=public --add-service=http
2. Next, adjust it to allow HTTPS traffic as well:
sudo firewall-cmd --permanent --zone=public --add-service=https
3. Restart the firewall for the changes to take place:
sudo firewall-cmd --reload
4. Check to verify HTTP and HTTPS routes are now permanently open by running:
sudo firewall-cmd --permanent --list-all
The output should display http and https in the list of services.
Step 7: Test PHP with Apache
Apache creates a web root file in the default website directory /var/www/html/. To check whether PHP is set up properly by running a test from this directory.
1. Create a info.php file inside the directory:
sudo vim /var/www/html/info.php
2. Then, add the following content:
<?php phpinfo (); ?>
3. Save and exit the file.
4. Now, verify if PHP is working correctly. Open a web browser and navigate to the URL (replacing
ip_address with the public IP of the server):
The browser should display the content, as in the image below.
Note: Want more details about this open-source software bundle? Refer to What is a LAMP stack?