LAMP is a popular stack used in development circles for hosting both static and dynamic web applications. It’s an acronym for Linux, Apache, MySQL (or MariaDB) & PHP. Notably, it comprises the Apache web server, MySQL or MariaDB database server, and PHP.
[ You might also like: How to Install LEMP Stack on Rocky Linux 8 ]
In this guide, we walk you through the installation of LAMP on Rocky Linux 8.
Before you get started, ensure you have the following:
Let’s get started…
Step 1: Install Apache on Rocky Linux
The first component that we are going to start installing is the Apache webserver. This is provided by the httpd software package. When installed, the httpd daemon runs in the background awaiting incoming HTTP requests from client devices.
To install Apache, launch your terminal and run the command.
$ sudo dnf install httpd
This installs the httpd package along with other dependencies.
Thereafter, enable the webserver to start at boot time.
$ sudo systemctl enable httpd
And then start the Apache httpd daemon as shown.
$ sudo systemctl start httpd
To verify that Apache is running on Rocky Linux 8, issue the command:
$ sudo systemctl status httpd
Another nifty way of verifying that Apache is active and running is by browsing the server’s IP address or Fully Qualified Domain Name (FQDN) as shown.
http://server-IP OR http://domain.com
This should give you the Apache HTTP test page, an indicator that all is well.
If you are getting an error on your browser when trying to access the page, then perhaps the firewall is blocking HTTP traffic. Run the commands below to allow HTTP traffic and reload the firewall.
$ sudo firewall-cmd --add-service=http --permanent $ sudo firewall-cmd --reload
Step 2: Install MariaDB on Rocky Linux
Next, we need to install a database server. Two options can suffice – MariaDB and MySQL. We will focus on MariaDB thanks to its numerous enhancements including faster and safer replication, numerous high-performance storage engines, backward compatibility with MySQL, and better overall performance when compared to MySQL.
The AppStream repository for Rocky Linux provides MariaDB 10.3 by the time of writing down this guide.
To install MariaDB, run the command:
$ sudo dnf install mariadb-server mariadb
When the installation is complete, enable MariaDB to start upon a reboot or during booting.
$ sudo systemctl enable --now mariadb
And finally, start the MariaDB service.
$ sudo systemctl start mariadb
To confirm that MariaDB daemon is running, run the command:
$ sudo systemctl status mariadb
MariaDB’s default settings are weak and pose a few vulnerabilities that can be leveraged by hackers to breach the database server. As such, we need to take additional steps to harden the database server.
To achieve this, run the script shown.
$ sudo mysql_secure_installation
The first step is to set the root password. So press ENTER since no Root password is configured by default, and then
‘Y’ to set a root password. Provide a strong password and confirm it.
‘Y’ for the remaining configurations. This will effectively purge or remove any anonymous users, block remote root login and remove the Test database which is not needed in a production environment.
The database server is now fully configured and secure.
Step 3: Install PHP on Rocky Linux
Finally, the last component to install will be PHP. PHP, a backronym for PHP Hypertext Preprocessor, is a scripting language used in the development of dynamic web pages.
[ You might also like: How to Install Latest PHP 8.0 on Rocky Linux 8 ]
Rocky Linux AppStream provides multiple versions of PHP. To check the available versions, run the command:
$ sudo dnf module list php
This provides a list of PHP modules and Streams.
The default PHP stream is PHP 7.2. To install the latest module Stream from the repository, reset the PHP streams.
$ sudo dnf module reset php
Then enable the preferred PHP stream. For example, to enable PHP 7.4, execute:
$ sudo dnf module install php:7.4
This installs PHP 7.4 and associated extensions.
Moreover, you can install additional PHP extensions. Here, we are installing php-curl, and php-zip extensions.
$ sudo dnf install php-curl php-zip
Once installed, confirm the version of PHP installed as shown.
$ php -v
Another way of testing the version of PHP installed is to create a test PHP file in the /var/www/html path.
$ sudo vim /var/www/html/info.php
Paste the configuration shown.
<?php phpinfo(); ?>
Save the changes and restart the webserver.
$ sudo systemctl restart httpd
Then head back to your browser and follow the URL shown
A page displaying the PHP version among other parameters such as details of PHP extensions enabled will be displayed.
You can now remove the test PHP file.
$ sudo rm -f /var/www/html/info.php
And there you have it. We have successfully installed the LAMP stack on Rocky Linux 8.4. You can proceed and host your web applications.