Install LAMP Stack on Rocky Linux 8
LAMP stands for Linux, Apache, MySQL/MariaDB, and PHP. This is a stack that is normally used together to build powerful websites and web applications.
Linux is the operating system where this stack runs. Apache is the webserver that runs on the server, MySQL/MariaDB is the database server and PHP is the web scripting language that is used for the backend server-database communication in web application development.
This guide shall cover how to set up the LAMP stack on Rocky Linux 8. Rocky Linux 8 is the new operating system that was designed by the community to replace the now ending CentOS. This is after the news that CentOS will be moved to CentOS stream. This means that CentOS won't be receiving stable releases and shall mainly be used for testing purposes.
The steps below can be followed to install and configure LAMP stack on Rocky Linux 8.
Prerequisites
Before you can proceed with the steps to install lamp stack, make sure your system has at least the following minimal requirements:
- 4GB RAM
- 4 vCPUs
- 50GB disk
- Normal User with sudo permissions
We have already installed Rocky Linux 8 on our Oracle Virtual Box environment.
Step 1. Install Apache Web server
Install Apache webserver available from Rocky Linux Appstream repositories.
[root@rockylinux ~]# yum install @httpd
...
Installed:
httpd-2.4.37-39.module+el8.4.0+571+fd70afb1.x86_64 httpd-filesystem-2.4.37-39.module+el8.4.0+571+fd70afb1.noarch
mod_http2-1.15.7-3.module+el8.4.0+553+7a69454b.x86_64 mod_ssl-1:2.4.37-39.module+el8.4.0+571+fd70afb1.x86_64
rocky-logos-httpd-84.5-8.el8.noarch
sscg-2.3.3-14.el8.x86_64
Complete!
Step 2. Configure Firewall
Allow HTTP and HTTPD service through the firewall
sudo firewall-cmd --permanent --add-service={http,https} sudo firewall-cmd --reload
Visit the the server's web page on http://server-ip or http://FQDN
Step 3. Install MariaDB database
LAMP stack can be run on either MariaDB or MySQL for the database server. This guide shall cover how to set up MariaDB on Rocky Linux 8. We have already covered a different tutorial to Install MariaDB on Rocky Linux 8.4 [Step-by-Step].
Download and install MariaDB server from the Rocky Linux repositories
[root@rockylinux ~]# sudo dnf -y install @mariadb
Rocky Linux 8 - AppStream 3.2 kB/s | 4.8 kB 00:01
Rocky Linux 8 - BaseOS 1.4 kB/s | 4.3 kB 00:03
Rocky Linux 8 - BaseOS 909 kB/s | 5.6 MB 00:06
Rocky Linux 8 - Extras 882 B/s | 3.5 kB 00:04
ELRepo.org Community Enterprise Linux Repository - el8 1.3 kB/s | 3.0 kB 00:02
Extra Packages for Enterprise Linux Modular 8 - x86_64 31 kB/s | 61 kB 00:01
Extra Packages for Enterprise Linux 8 - x86_64 36 kB/s | 53 kB 00:01
...
Installed:
mariadb-backup-3:10.3.28-1.module+el8.4.0+427+adf35707.x86_64 mariadb-errmsg-3:10.3.28-1.module+el8.4.0+427+adf35707.x86_64
mariadb-gssapi-server-3:10.3.28-1.module+el8.4.0+427+adf35707.x86_64 mariadb-server-3:10.3.28-1.module+el8.4.0+427+adf35707.x86_64
mariadb-server-utils-3:10.3.28-1.module+el8.4.0+427+adf35707.x86_64 perl-DBD-MySQL-4.046-3.module+el8.4.0+577+b8fe2d92.x86_64
perl-DBI-1.641-3.module+el8.4.0+509+59a8d9b3.x86_64 perl-Math-BigInt-1:1.9998.11-7.el8.noarch
perl-Math-Complex-1.59-419.el8_4.1.noarch psmisc-23.1-5.el8.x86_64
Complete!
Start and enable MariaDB service
sudo systemctl enable --now mariadb
Step 4. Execute MySQL Hardening Script
Run the hardening script to harden your MariaDB server. You will be prompted for following input:
Enter current password for root (enter for none):
Change the root password? [Y/n] y
Remove anonymous users? [Y/n] y
Disallow root login remotely? [Y/n] y
Remove test database and access to it? [Y/n] y
Reload privilege tables now? [Y/n] y
Sample Output:
[root@rockylinux ~]# mysql_secure_installation NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY! In order to log into MariaDB to secure it, we'll need the current password for the root user. If you've just installed MariaDB, and you haven't set the root password yet, the password will be blank, so you should just press enter here. Enter current password for root (enter for none): OK, successfully used password, moving on... Setting the root password ensures that nobody can log into the MariaDB root user without the proper authorisation. Set root password? [Y/n] y New password: Re-enter new password: Password updated successfully! Reloading privilege tables.. ... Success! By default, a MariaDB installation has an anonymous user, allowing anyone to log into MariaDB without having to have a user account created for them. This is intended only for testing, and to make the installation go a bit smoother. You should remove them before moving into a production environment. Remove anonymous users? [Y/n] Y ... Success! Normally, root should only be allowed to connect from 'localhost'. This ensures that someone cannot guess at the root password from the network. Disallow root login remotely? [Y/n] Y ... Success! By default, MariaDB comes with a database named 'test' that anyone can access. This is also intended only for testing, and should be removed before moving into a production environment. Remove test database and access to it? [Y/n] Y - Dropping test database... ... Success! - Removing privileges on test database... ... Success! Reloading the privilege tables will ensure that all changes made so far will take effect immediately. Reload privilege tables now? [Y/n] Y ... Success! Cleaning up... All done! If you've completed all of the above steps, your MariaDB installation should now be secure. Thanks for using MariaDB!
Connect to the database instance using the credentials for the root user set in the previous step.
[root@rockylinux ~]# mysql -u root -p
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 16
Server version: 10.3.28-MariaDB MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]>
Step 5. Install and Configure PHP
The next step is to install PHP and some of the necessary PHP extensions on Rocky Linux 8
By default, the Rocky Linux Appstream repo has PHP versions from PHP 7.2 to PHP 7.4. You can verify this by running the command below:
[root@rockylinux ~]# dnf module list php
Last metadata expiration check: 0:28:50 ago on Sun 22 Aug 2021 08:00:27 PM EAT.
Rocky Linux 8 - AppStream
Name Stream Profiles Summary
php 7.2 [d][e] common [d], devel, minimal PHP scripting language
php 7.3 common [d], devel, minimal PHP scripting language
php 7.4 common [d], devel, minimal PHP scripting language
Hint: [d]efault, [e]nabled, [x]disabled, [i]nstalled
To install PHP on Rocky Linux, run the command below for select versions
# PHP 7.4 sudo dnf -y module install php:7.4 # PHP 7.3 sudo dnf -y module install php:7.3 # PHP 7.2 sudo dnf -y module install php:7.2
Sample Output:
[root@rockylinux ~]# sudo dnf -y module install php:7.4
Last metadata expiration check: 0:31:02 ago on Sun 22 Aug 2021 08:00:27 PM EAT.
Dependencies resolved.
...
Installed:
nginx-filesystem-1:1.14.1-9.module+el8.4.0+542+81547229.noarch oniguruma-6.8.2-2.el8.x86_64
php-cli-7.4.6-4.module+el8.4.0+415+e936cba3.x86_64 php-common-7.4.6-4.module+el8.4.0+415+e936cba3.x86_64
php-fpm-7.4.6-4.module+el8.4.0+415+e936cba3.x86_64 php-json-7.4.6-4.module+el8.4.0+415+e936cba3.x86_64
php-mbstring-7.4.6-4.module+el8.4.0+415+e936cba3.x86_64 php-xml-7.4.6-4.module+el8.4.0+415+e936cba3.x86_64
Complete!
Verify the version of PHP installed to your system
[root@rockylinux ~]# php -v
PHP 7.4.6 (cli) (built: May 12 2020 08:09:15) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
Step 6. Install PHP Modules
PHP itself contains several features that can be individually installed on the Linux system. PHP is generally distributed for Linux in a number of packages, allowing you some choice over what is installed on your server.
Default packages often have no database-specific extensions installed, so they would not be able to immediately communicate with a PostgreSQL or MySQL or MariaDB database. You must install the PHP MySQL module to enable your PHP installation to communicate with a MariaDB database
Install PHP modules.
sudo dnf -y install php php-fpm php-mysqlnd php-opcache php-gd php-xml php-mbstring php-zip
Start and enable PHP-FPM for parsing PHP web content
sudo systemctl enable --now php-fpm
Create a test PHP index file to display the version, module and general information about the LAMP stack.
Add the file in /var/www/html.
[root@rockylinux ~]# cat /var/www/html/info.php
<?php
phpinfo();
?>
Accessing this file through a web browser should show a page containing information about PHP.
Step 7. Restart Apache Web Server
Next we will restart the apache service to activate the changes:
sudo systemctl restart httpd
You should also enable the httpd service to automatically start the service after reboot:
sudo systemctl enable httpd
Step 8. Access LAMP Stack Server
Open the LAMP stack server on the web browser at http://server-ip/info.php
This verifies that our LAMP stack installation is complete and every item has been successfully installed.
Conclusion
In this tutorial we discussed about the the popular Linux-Apache-MySQL-PHP (LAMP) web programming environment. These components can be combined to produce a complete open source web server, database and web page programming solution. While each of the components is an individual open source project, they are easily incorporated into a single server environment.
That's it for setting up LAMP stack on Rocky Linux 8.4. You can now start building your web applications and websites using this stack. You can also secure your server using SSL if you have a fully qualified domain name, FQDN.