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.

If you read this far, tweet to the author to show them you care. Tweet a thanks
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.

31 thoughts on “How to Install LAMP Server on RHEL, CentOS, Rocky & AlmaLinux”

  1. Typo ‘LocationMach’ please change ‘LocationMatch’
    and ‘nano /etc/httpd/conf.d/phpmyadmin.conf’ change to ‘nano /etc/httpd/conf.d/phpMyAdmin.conf’

    New Version

    Reply
    • @Lester,
      Do this way…

      # wget http://pkgs.repoforge.org/rpmforge-release/rpmforge-release-0.5.3-1.el7.rf.x86_64.rpm
      # rpm -Uvh rpmforge-release-0.5.3-1.el7.rf.x86_64.rpm
      
      Reply
    • It’s really late to answer this but I think I would let you know.

      # yum install http://ftp.tu-chemnitz.de/pub/linux/dag/redhat/el7/en/x86_64/rpmforge/RPMS/rpmforge-release-0.5.3-1.el7.rf.x86_64.rpm

      You can get past right this way.

      Reply
  2. @sahadev: Check /etc/httpd/conf.d/phpmyadmin.conf file and verify your IP/netmask
    ##############

    Order Deny,Allow
    Deny from all
    Allow from 10.0.0.0/24 ## Use your IP/Network CDIR
    Require all granted

    Reply
      • @Israrul,

        How to configure PhpMyAdmin to allow access to specific IP address.

        Open /etc/httpd/conf.d/phpMyAdmin.conf file and edit Require ip& Allow from lines as shown in the following example.

        Here is what phpMyAdmin.conf will look like with the added access rules.

        <Directory /usr/share/phpMyAdmin/>
        
        <IfModule mod_authz_core.c>
        # Apache 2.4
        <RequireAny>
        Require ip 127.0.0.1
        Require ip ::1
        # Example office IP
        Require ip 192.168.0.100
        </RequireAny>
        
        </IfModule>
        <IfModule !mod_authz_core.c>
        # Apache 2.2
        Order Deny,Allow
        Deny from All
        Allow from 127.0.0.1
        Allow from ::1
        # Example home IP
        Allow from 192.168.0.100
        </IfModule>
        
        </Directory>
        <Directory /usr/share/phpMyAdmin/setup/>
        <IfModule mod_authz_core.c>
        # Apache 2.4
        <RequireAny>
        Require ip 127.0.0.1
        Require ip ::1
        # Example home IP
        Require ip 192.168.0.100
        </RequireAny>
        
        </IfModule>
        <IfModule !mod_authz_core.c>
        # Apache 2.2
        Order Deny,Allow
        Deny from All
        Allow from 127.0.0.1
        Allow from ::1
        # Example home IP
        Allow from 192.168.0.100
        </IfModule>
        </Directory>
        
        Reply

Got something to say? Join the discussion.

Have a question or suggestion? Please leave a comment to start the discussion. Please keep in mind that all comments are moderated and your email address will NOT be published.