Install LAMP (Linux, Apache, MariaDB, PHP/PhpMyAdmin) in Centos/RHEL 8 Linux

by Jay


A “LAMP” stack is a group of open-source softwares like Linux Operating server, Apache web server, Mysql Database and PHP that is typically installed together to enable a server to host websites and web apps.

Although the database management system for this software stack is normally MySQL, certain Linux distributions, use MariaDB as a drop-in substitute for MySQL.

In this tutorial, We will use Rocky Linux 8.6 server to install and configure LAMP stack for our demonstration, with MariaDB serving as the database management system.

Rocky Linux is an open-source enterprise operating system designed to be 100% bug-for-bug compatible with Red Hat Enterprise Linux®. It is under intensive development by the community.

Advantages of using Lamp stack in web development.

  • LAMP is open source. The software’s source code is openly available for modification, allowing users to better the program’s functionality as a whole.
  • The LAMP stack’s modules can be customized.
  • The scale of the LAMP community makes support simple to find.
  • It is a well-developed stack that is simple to set up.
  • For security, Lamp Stack includes a robust structure and cutting-edge encryption.

Below step by step guide will assist you in installing and configuring the LAMP stack on CENTOS/RHEL, Rocky Linux, AlamaLinux 8.x, but it should work for all versions of CENTOS/RHEL, Rocky Linux, AlamaLinux 8.x etc.

Step 1: Let’s check the system configuration.

by running command to find the system hostname and currenly running OS versions and configured IP on primary interface. in your case network interface will br different just run command # ip -4 -br addr show to indentify the network interface.

[root@web ~]# ip -4 -br addr show enp0s3
enp0s3           UP   
[root@web ~]#
[root@web ~]# hostnamectl | grep -Ei "Operating|hostname"
   Static hostname:
  Operating System: Rocky Linux 8.6 (Green Obsidian)
[root@web ~]#

Step 2: Verify the currently configured repository on the server.

[root@web ~]# yum repolist
repo id                                            repo name
appstream                                          Rocky Linux 8 - AppStream
baseos                                             Rocky Linux 8 - BaseOS
extras                                             Rocky Linux 8 - Extras

Please Note: By default rocky linux came with above repo enabled, If you are using any other destro you can enable epel repository to install LAMP stack OR if you are configuring LAMP for your orgnization please check internally for repository if default repo not enabled or you have just spinup fresh OS.

Step 3: Installing apache/httpd on the server.

Use this command yum install httpd -y to install the apache/httpd service on server.

[root@web ~]#  yum install httpd -y

Last metadata expiration check: 0:01:57 ago on Mon 03 Apr 2023 05:53:33 AM EDT.
Dependencies resolved.
 Package                   Architecture   Version                                             Repository         Size
 httpd                     x86_64         2.4.37-51.module+el8.7.0+1155+5163394a.1            appstream         1.4 M
Installing dependencies:
 apr                       x86_64         1.6.3-12.el8                                        appstream         128 k
 apr-util                  x86_64         1.6.1-6.el8.1                                       appstream         104 k
 httpd-filesystem          noarch         2.4.37-51.module+el8.7.0+1155+5163394a.1            appstream          41 k
 httpd-tools               x86_64         2.4.37-51.module+el8.7.0+1155+5163394a.1            appstream         109 k
 mod_http2                 x86_64         1.15.7-5.module+el8.6.0+823+f143cee1                appstream         153 k
 rocky-logos-httpd         noarch         86.3-1.el8                                          baseos             24 k
Installing weak dependencies:
 apr-util-bdb              x86_64         1.6.1-6.el8.1                                       appstream          23 k
 apr-util-openssl          x86_64         1.6.1-6.el8.1                                       appstream          26 k

Transaction Summary
Install  9 Packages

[Output Altered...]



Use below commands to Start httpd service and enable it to start automatically after every reboot and check the status.

[root@web ~]# systemctl start httpd

[root@web ~]# systemctl enable httpd

Created symlink /etc/systemd/system/ → /usr/lib/systemd/system/httpd.service.
[root@web ~]#
[root@web ~]# systemctl status httpd

● httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled)
   Active: active (running) since Mon 2023-04-03 05:59:22 EDT; 48s ago
     Docs: man:httpd.service(8)
 Main PID: 2387 (httpd)
   Status: "Running, listening on: port 80"

Step 4: Add the firewall rule to allow http and https access from outside permanently.

[root@web ~]# firewall-cmd --add-service=http --permanent
[root@web ~]# firewall-cmd --add-service=https --permanent
[root@web ~]#firewall-cmd --reload

Please Note: Above command enabled access for http and https both but initially only http service access required. By default apache Listen on port 80 (http) and 443 (https). If you don’t want to enable services on permanently just remove the --permanent from the above command and do not use firewall-cmd --reload elese firewall reload will delete non-permanent rules

To list all information for the default firewall zone.

[root@web ~]# firewall-cmd --list-all
public (active)
  target: default
  icmp-block-inversion: no
  interfaces: enp0s3
  services: cockpit dhcpv6-client http https ssh
  forward: no
  masquerade: no
  rich rules:
[root@web ~]#

Step 5: (optional) Install mod_ssl to to access apache as secure port https.

Use below command to install mod_ssl

# yum install mod_ssl -y

More About mod_ssl: An optional module for the Apache HTTP Server is mod ssl. With the aid of the Open Source SSL/TLS toolkit OpenSSL, it offers robust cryptography for the Apache v1.3 and v2 webserver through the Secure Sockets Layer and Transport Layer Security cryptographic protocols.

Step 6: Test the default Apache page in Browser and Terminal.

To confirm Apache operation, open browser and enter your server’s IP address using the HTTP protocol on the URL (http://server IP); a default page should then display, as seen in the picture below.

HTTP Test Page

As of now apache is showing a test page from the default configuration. For more detailed configuration of apache please follow this article How to configure an Apache server in Linux

Step 7: Install and configure MySQL or Mariadb Server.

By default Rocky Linux have both MySQL and mariadb packages on the appstream repo.

[root@web ~]# yum list available | grep -Ei "mariadb-server|mysql-server"
mariadb-server.x86_64                3:10.3.35-1.module+el8.6.0+1005+cdf19c22        appstream
mysql-server.x86_64                  8.0.30-1.module+el8.6.0+1057+4d6a1721           appstream

You can choose any of the Database server based on your prefrance and your application need. Some distro default repos do not have both the options but still you have flexibility to install any DB on any distro by adding their repos on your server.

For our demonstration purpose I will install widly used MySQL server. Please follow below step to install MySQL server.

Run below command to install MySQL server

# yum install mysql-server mysql -y

You can check the installed packages by running below command

[root@web ~]# yum list installed mysql mysql-server

Installed Packages
mysql.x86_64                     8.0.30-1.module+el8.6.0+1057+4d6a1721         @appstream
mysql-server.x86_64              8.0.30-1.module+el8.6.0+1057+4d6a1721         @appstream
[root@web ~]#

Once installation completed start mysql and enable it to auto start at reboot and check the runnin status.

# systemctl start mysqld

# systemctl enable mysqld

Created symlink /etc/systemd/system/ → /usr/lib/systemd/system/mysqld.service. 

# systemctl status mysqld

● mysqld.service - MySQL 8.0 database server
   Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
   Active: active (running) since Mon 2023-04-03 07:01:10 EDT; 31s ago
 Main PID: 2718 (mysqld)
   Status: "Server is operational"
    Tasks: 39 (limit: 23504)
   Memory: 453.5M
   CGroup: /system.slice/mysqld.service
           └─2718 /usr/libexec/mysqld --basedir=/usr

[..........Output altered...............]

Step 7.1: Secure MySQL or Mariadb Server.

It’s time to do a secure MySQL. With this, you may stop root logins on localhost’s backyard, change the password for the MySQL root user, delete anonymous user accounts, erase test databases, and reload privileges. The command listed below can be used to carry out this:

Note: You will be asked for your MySQL root password when you run the command below. Since you haven’t yet chosen a password, just press Enter.

Please be aware that your server’s root password should prevent any confusion regarding this. After doing so, you will then be given a number of questions. We have supplied the answers below for ease of use. We advise you to select “YES” for the following choices:

# mysql_secure_installation
Secure mysql

Step 7.2: Restart MySQL.

# systemctl restart mysqld

MySQL installation is completed now Lets move to next steps to install PHP.

Step 8: Installing PHP.

What is PHP ?

  • A server-side programming language called PHP is used to create either static or dynamic webpages or online apps. PHP, which formerly stood for Personal Home Pages, is now known as Hypertext Pre-processor.
  • Only servers with PHP installed can interpret PHP scripts.
  • Only a web browser is needed on the client machines to view the PHP scripts.
  • A PHP file has the “.php” suffix and contains PHP tags.

The PHP version totally depends on the type of application you are going to deploy. Before installing PHP, please check your application’s compatibility.

Let’s check which PHP version is currently available on the default repository.

# yum list available php
Last metadata expiration check: 0:47:58 ago on Mon 03 Apr 2023 06:57:37 AM EDT.
Available Packages
php.x86_64                           7.2.24-1.module+el8.4.0+413+c9202dda                           appstream
[root@web ~]

As per above command output, currently PHP7.2 is available in default appstream repo. But you can install php 7.4 or 8.x by enabling/installing remi repo or epel repo or by upgrading your OS.

For this demnstation purpose we will install default available PHP version.

Use below command to install PHP

# yum install php php-mysqlnd -y

Once PHP and its necessary modules have been installed, restart the Apache service so that PHP can start working by entering the following command.

# systemctl restart httpd

Finally, completed the LAMP stack configuration successfully. Let’s examine the command-line versions of each component of the LAMP stack, including PHP, MySQL, and Apache.

Step 8.1: Check LAMP stack version in Command Line.

Check Apache Version

[root@web ~]# httpd -v

Check MySQL Version

[root@web ~]# mysql -V

Check PHP Version

[root@web ~]# php -v

Below image show above commands results.

MySQL Apache PHP Version check

Step 8.2: Let’s check the full PHP Process list via web (apache) server.

Create the info.php file on the Apache Document Root using the following command from the root account, restart the httpd service, then point your browser to the http://server IP/info.php address to get a complete list of information about PHP from your browser.

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

The web page for PHP information is exactly like the image below. From a PHP perspective, this page essentially provides information about server. This can help with troubleshooting and making sure the settings are applied properly.

PHP Info

After this examination, it is advisable to delete this file because it should undoubtedly give information about your server to unauthorised users.

Please follow this article to install and configure PhpMyAdmin.

Was this article of use to you? Post your insightful thoughts or recommendations in the comments section if you don’t find this article to be helpful or if you see any outdated information, a problem, or a typo to help this article better.

Related Posts

Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.