How to Install LAMP Stack on CentOS 9 Stream
Introduction
The LAMP stack is a bundle consisting of a Linux operating system, an Apache server, a MySQL (MariaDB) database, and the PHP programming language. Each layer of the stack represents open-source software required for developing web applications.
In this tutorial, you will learn how to install the LAMP stack on CentOS 9.
Prerequisites
- Access to a user account with sudo or root privileges
- A terminal window or command line
- The yum and RPM package managers, included by default
Step 1: Update Package Repository Cache
Before you start building the stack, be sure to update the packages on your CentOS 9 server using the command:
# yum update
Step 2: Install the Apache Web Server
As you already have a CentOS operating system running, the first step of assembling the LAMP stack is to install the web server. The simplest way to install Apache is through CentOS’s native package manager, yum.
1. Install Apache on Centos with:
# yum install httpd
When prompted, confirm that you are executing the command with sudo privileges. The output will show the package httpd package was installed as in the image below:
2. Start and enable Apache service.
# systemctl start httpd.service
# systemctl enable httpd.service
3. Check the status of Apache to see if it is running
# systemctl status httpd
4. To access via a browser, we should allow HTTP/S traffic across the firewall.
# firewall-cmd --permanent --zone=public --add-service=http # firewall-cmd --permanent --zone=public --add-service=https
Reload the firewall to apply the changes
# firewall-cmd --reload
Then proceed to your browser on http://your-ip-address
Step 3: Install MySQL (MariaDB) and Create a Database
To organize and store data for your dynamic website, you need MariaDB. This is an open-source fork of the MySQL database management system. It is a backward-compatible and binary drop-in replacement for the original MySQL.
1.Install MySQL
To install MySQL, use the following command.
# yum install mysql-server -y
Start and enable the service
# systemctl start mysqld # systemctl enable mysqld
Check the status of the MySQL service.
# systemctl status mysqld
2. Install MariaDB
To install MariaDB use the following command.
# yum remove mysql-server -y # yum rm -rf /var/lib/mysql # yum install mariadb-server -y
Start and aenbale MariaDB service.
# systemctl start mariadb # systemctl enable mariadb
Check the status of the service with the following command.
# systemctl status mariadb
Step 4: Run MySQL Security Script
Secure MariaDB/MySQL using the MySQL installation script. You can configure it by picking the default options by pressing Enter.
# mysql_secure_installation
- Set root password? [y/n] Y
- New password: Type in a password you would like to use
- Re-enter new password: Retype the password from the previous field
- Remove anonymous users? [y/n] Y
- Disallow root login remotely? [y/n] Y
- Remove the test database and access it. [y/n] Y
- Reload privilege tables now? [y/n] Y
To login to MariaDB/MySQL, use the following command.
# mysql -u root -p
To logout use the following command
> exit
Step 5: Install PHP and required modules
To install PHP, use the following command.
# yum install php -y
The syntax for installing additional PHP modules that assess different needs is shown below.
# yum install php-<package name>
In this tutorial, we will install a few additional modules using the following command.
# yum nstall php-mysqlnd php-gd php-curl php-ftp php-fpm -y
Check the PHP version using the following command.
# php -v
Reload the webserver to accept PHP requests
# systemctl restart httpd
Step 6:Test PHP installation
Edit the PHP configuration file using the following command.
# nano /var/www/html/info.php
Add the following code
<?php phpinfo (); ?>
Save and exit the file.
Go to your browser and access the following web address http://our-ip-address/info.php
Conclusion
By following this guide, you learned how to install each layer of the LAMP stack on CentOS 9. Now you are ready to explore all the innovations the LAMP stack makes possible.