How to Install LAMP Server on RHEL, CentOS, Rocky & AlmaLinux

A LAMP stack is a collection of four different software (Linux, Apache, MySQL, and PHP) that programmers or web developers use to create and deploy websites or applications.

This tutorial will concentrate on how to install and configure the famous LAMP stack with PhpMyAdmin on the last release of RHEL and RHEL-based distributions such as CentOS, Oracle Linux, Rocky, and AlmaLinux.

Requirements

Depending on the used distribution, use the following links to perform a minimal system installation, using a static IP Address for network configuration.

Step 1: Install Apache Web Server

1. After performing a minimal system installation and configuring your server network interface with a Static IP Address, go ahead and install Apache (httpd) service binary package provided from official repositories using the following yum command.

# yum install httpd
Install Apache in Linux
Install Apache on Linux

2. After finishing the Apache installation, use the following commands to manage Apache daemon since RHEL and CentOS both migrated their init scripts from SysV to SystemdWhy was init Replaced with Systemd in Linux?.

# systemctl start httpd
# systemctl enable httpd
# systemctl status httpd
Manage Apache in Linux
Manage Apache in Linux

3. On the next step, make sure to allow access to Apache by opening ports 80 and 443 on Firewall using firewall-cmd, which is the default command to manage Firewalld through daemon.

# firewall-cmd --add-service=http
# firewall-cmd --add-service=https
# systemctl restart firewalld
NOTE: Make notice that using this rule will lose its effect after a system reboot or firewalld service restart because it opens on-fly rules, which are not applied permanently.

To apply consistency iptables rules on the firewall using the --permanent option and restart the firewalld service to take effect.

# firewall-cmd --permanent --add-service=http
# firewall-cmd --permanent --add-service=https
# systemctl restart firewalld
Open Apache Ports in Firewalld
Open Apache Ports in Firewalld

Some important Firewalld commands to manage the firewall as presented below:

# firewall-cmd --state
# firewall-cmd --list-all
# firewall-cmd --list-interfaces
# firewall-cmd --get-service
# firewall-cmd --query-service service_name
# firewall-cmd --add-port=80/tcp

4. To verify Apache functionality open a remote browser and type your server IP Address using HTTP protocol on the URL, and a default page should appear like in the screenshot below.

http://server_IP
Verify Apache in Linux
Verify Apache in Linux

5. For now, the Apache DocumentRoot path it’s set to /var/www/html system path, which by default doesn’t provide any index file. If you want to see a directory list of your DocumentRoot path.

Open the Apache welcome configuration file and set the Indexes statement from to + on LocationMatch directive, using the below screenshot as an example.

# vi /etc/httpd/conf.d/welcome.conf
Configure Apache Welcome Page
Configure Apache Welcome Page

6. After making changes, close the file, restart the Apache service to reflect changes, and reload your browser page to see the final result.

# systemctl restart httpd
Apache Directory Listing
Apache Directory Listing

Step 2: Install PHP Support for Apache

7. Before installing PHP dynamic language support for Apache, get a full list of available PHP modules and extensions using the following command.

# yum search php
List PHP Modules
List PHP Modules

8. Depending on what type of applications you want to use, install the required PHP modules from the above list, but for a basic MySQL/MariaDB support in PHP and PhpMyAdmin you need to install the following modules.

# yum install php php-mysql php-pdo php-gd php-mbstring
Install PHP Modules
Install PHP Modules

9. To get a full information list on PHP from your browser, create an info.php file on Apache Document Root using the following command, restart the httpd service, and direct your browser to the http://server_IP/info.php address.

# echo "<?php phpinfo(); ?>" > /var/www/html/info.php
# systemctl restart httpd
List PHP Information
List PHP Information

10. If you get an error on PHP Date and Timezone, open the php.ini configuration file, search, and uncomment date.timezone statement, append your physical location, and restart the Apache daemon.

# vi /etc/php.ini

Locate and change date.timezone line to look like this, using PHP Supported Timezones list.

date.timezone = Continent/City
Set PHP Timezone
Set PHP Timezone

Step 3: Install and Configure MariaDB Database

11. The RHEL distribution switched from MySQL to MariaDB for its default database management system. To install the MariaDB database use the following command.

# yum install mariadb-server mariadb
Install MariaDB in Linux
Install MariaDB on Linux

12. After the MariaDB package is installed, start the database daemon and use the mysql_secure_installation script to secure the database (set a root password, disable remote login from the root, remove the test database, and remove anonymous users).

# systemctl start mariadb
# systemctl enable mariadb
# systemctl status mariadb
# mysql_secure_installation
Manage MariaDB Service
Manage MariaDB Service
Secure MariaDB in Linux
Secure MariaDB in Linux

13. To test database functionality login to MariaDB using its root account and exit using a quit statement.

mysql -u root -p
MariaDB > show databases;
MariaDB > quit
Connect MariaDB in Linux
Connect MariaDB to Linux

Step 4: Install PhpMyAdmin to Manage MySQL

14. By default official repositories don’t provide any binary package for PhpMyAdmin Web Interface. If you are uncomfortable using the MySQL command line to manage your database you can install the PhpMyAdmin package by enabling the remi repository using the following command.

# yum install -y https://rpms.remirepo.net/enterprise/remi-release-9.rpm  [On CentOS/RHEL 8]
# yum install -y https://rpms.remirepo.net/enterprise/remi-release-8.rpm  [On CentOS/RHEL 8]
# yum install -y https://rpms.remirepo.net/enterprise/remi-release-7.rpm  [On CentOS/RHEL 7]

After enabling the remi repository, next install PhpMyAdmin.

# yum install phpmyadmin
Install PhpMyAdmin in Linux
Install PhpMyAdmin on Linux

15. Next configure PhpMyAdmin to allow connections from remote hosts by editing phpmyadmin.conf file, located on the Apache conf.d directory, commenting on the following lines.

# vi /etc/httpd/conf.d/phpMyAdmin.conf

Add the “Require all granted” line under the “Require local” line as shown.

<Directory /usr/share/phpMyAdmin/>
   AddDefaultCharset UTF-8
   Require local
   Require all granted
</Directory>
Allow Remote PhpMyAdmin Access
Allow Remote PhpMyAdmin Access

16. To be able to log in to the PhpMyAdmin Web interface, restart the Apache Web service and direct your browser to the URL address.

# systemctl restart httpd

http://server_IP/phpmyadmin/
PhpMyAdmin Dashboard
PhpMyAdmin Dashboard

Step 5: Enable LAMP System-Wide in Linux

17. If you need MariaDB and Apache services to be automatically started after reboot issue the following commands to enable them system-wide.

# systemctl enable mariadb
# systemctl enable httpd

That’s all it takes for a basic LAMP installation on RHEL-based distributions. The next series of articles related to the LAMP stack will discuss how to create Virtual Hosts, generate SSL Certificates and Keys, and add SSL transaction support for Apache HTTP Server.

Hey TecMint readers,

Exciting news! Every month, our top blog commenters will have the chance to win fantastic rewards, like free Linux eBooks such as RHCE, RHCSA, LFCS, Learn Linux, and Awk, each worth $20!

Learn more about the contest and stand a chance to win by sharing your thoughts below!

Matei Cezar
I'am a computer addicted guy, a fan of open source and linux based system software, have about 4 years experience with Linux distributions desktop, servers and bash scripting.

Each tutorial at TecMint is created by a team of experienced Linux system administrators so that it meets our high-quality standards.

Join the TecMint Weekly Newsletter (More Than 156,129 Linux Enthusiasts Have Subscribed)
Was this article helpful? Please add a comment or buy me a coffee to show your appreciation.

Got Something to Say? Join the Discussion...

Thank you for taking the time to share your thoughts with us. We appreciate your decision to leave a comment and value your contribution to the discussion. It's important to note that we moderate all comments in accordance with our comment policy to ensure a respectful and constructive conversation.

Rest assured that your email address will remain private and will not be published or shared with anyone. We prioritize the privacy and security of our users.