In this article, you will learn about How to install LAMP (Linux, Apache, MySQL, PHP) stack installation on CentOS 7/8 and RHEL 7/8
The LAMP is a combination of open-source software applications that are used together to make a server ready to host the dynamic websites. There are four main components, a Linux operating system on which the Apache server will be installed and the data of the websites will be stored in a MySQL database, here we are using MariaDB and the websites will be dynamic with the help of PHP programming language so before starting the LAMP stack installation, look for prerequisites below.
Preconditions for LAMP server
- A VPS or a local server on which CentOS 7/8 or RHEL 7/8 is running.
- You must log-in with a root user or a non-root user with sudo privilege.
- You should be familiar with the basics of the firewall.
Now Let’s start the LAMP stack installation.
Step 1: Update CentOS Server
This is the good practice to check the server for any pending update. Always update the system with the latest packages. Use the below command to confirm it.
sudo yum update
Step 2: Installation of Apache Server
Apache is available in default software repositories, to install it, we are going to use the yum package manager. The name of the Apache software package is httpd. Use the below command to install it.
sudo yum install httpd
If it is asking your user password, enter the password and it will prompt to confirm the installation, press y and hit the enter key.
Note: Below are some information for default locations,
httpd binary: /sbin/httpd
Apache configuration file: /etc/httpd/conf/httpd.conf
Website files: /var/www/html/
Apache logs: /var/log/httpd/
Step 3: Starting Apache service
After the successful Apache installation, we are going to start the apache service. the Apache service name is httpd. Use the below command to start the service.
sudo systemctl start httpd
Step 4: Check the status of Apache service
This is the time to confirm the status of the httpd service if it is running or not. use the below command to verify that.
sudo systemctl status httpd
Step 5: Enable Apache service
If you want to service start automatically after every restart or start of the server, tell this to the server to start it automatically. Use the below command for this purpose.
sudo systemctl enable httpd
Step 6: Configuring the firewall for Apache service
As you have installed Apache successfully, and you have also started the service of Apache. Now you have to configure the firewall to access the Apache web service outside. So you have to allow service http and https. Or you can allow the standard port number that is 80 for http and 443 for https. Use the below command to configure the firewall for service.
sudo firewall-cmd --permanent --add-service http
sudo firewall-cmd --permanent --add-service https
Now reload the firewall service,
sudo firewall-cmd --reload
Step 7: Time to check the website on the browser
Now we are going to use the browser to check the default Apache server page, To check it, you can use your public IP If you have configured on VPS or If you have a public IP otherwise if it local computer and you have a private IP then type that computer IP on browser and press enter.
For example in my case, http://192.168.43.228
You will get the page as below.
Step 8: Install MariaDB
MariaDB is a relational database management system. It is similar to MySQL and it has been developed by the same developer of MySQL. This tool helps us to store and manage the database. So first we are going to install it.
This is available by default in yum repositories but the default version is 5.5. But If you want to install the latest version of MariaDB then follow the link How to install MariaDB Server on RHEL 8 / CentOS 8.
The software package name is to install MariaDB-server MariaDB-client. Use the below command to install old mariadb version 5.5.
sudo yum install MariaDB-server MariaDB-client
It may prompt for the password for your user, enter the password. It will ask your confirmation to install the mariadb software package, press y and press enter to install it.
Note: Below are some information for default locations related to MariaDB
MariaDB binary: /bin/mysql
MariaDB Configuration file: /etc/my.cnf
Database location: /var/lib/mysql
MariaDB logs: /var/log/mariadb/mariadb.log
Step 9: Starting the Mariadb service and enabling for startup
The software package installation has been done. Now you have to start the MariaDB service. The service name of MariaDB is mariadb. So let’s start the service using the below command.
sudo systemctl enable --now mariadb
Step 10: Check the MariaDB service status
Use the below command to check the MariaDB service status.
sudo systemctl status mariadb
You will get the output like in the below picture.
Step 11: Securing MariaDB
MariaDB has been install and the service is running. Now you have to make it secure, like deleting the anonymous user and deleting the test database and disallowing root remotely log in and configuring other settings. for this reason, you have to use the below command.
After pressing enter, you will get some series of questions like below
Set a root password? [Y/n] Y
New password: Enter your password here
Re-enter new password: repeat your password
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
After this step, it will give the successful message. that’s it. If you want to read more about MariaDB use this link How to manage MySQL or MariaDB databases and users?.
Step 12: Installing EPEL and Remi repository
There is no latest PHP package available in CentOS 7 official repository but you can find for CentOS 8 officially, for the latest installation you have to add EPEL and Remi repositories then you can use yum to install it. Use the below command to install the EPEL repository.
Follow the link for latest PHP installation, How to install PHP 7.4 on CentOS/RHEL 8
Note: You have to follow this step for CentOS 7 and RHEL 7
sudo yum install epel-release
Now we will install Remi repository, for that, use the below command.
sudo rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-7.rpm
Now we have to install yum-utils this helps yum to manage a collection of repository and packages.
sudo yum install yum-utils
Step 13: Install PHP
PHP is a general-purpose server-side scripting language. PHP works together with HTML to display dynamic content on your website. It also has the correct drivers for connecting to the MariaDB database management system that we have installed above.
Now we shall use yum-config-manager to enable Remi repository by default repository to install different PHP versions. you can use the below command to install a different version as you want.
Note: To install it you have to use the root account.
To install PHP version 7.0 then use the below command.
yum-config-manager --enable remi-php70 && yum install php
Now if you want to install PHP version 7.1 then use the below command.
yum-config-manager --enable remi-php71 && yum install php
If you want to install PHP version 7.2 then use the below command.
yum-config-manager --enable remi-php72 && yum install php
If you want to install PHP version 7.3 then use the below command.
yum-config-manager --enable remi-php73 && yum install php
Confirm the installation pressing y and hitting enter key.
Now, we are going to install php-mysqlnd modules, you can use the below command for that.
yum --enablerepo=remi install php-mysqlnd
If you want to install any other package, you just have to put yum –enablerepo=remi install package_name
As there is no service for PHP so work with Apache, you have to restart the Apache service.
Step 14: Restart the Apache service
sudo systemctl restart httpd
Step 15: Test the PHP
For testing the PHP, you have to create a php info page. I’m going to use vi editors but you can use it as per your choice. The location to create a file will be the ‘webroot‘ directory. the location is /var/www/html/
To create the file using the below description
sudo vi /var/www/html/info.php
and then press enter key and type in that file the below tag
<?php phpinfo(); ?>
After finishing the typing the tag save the file pressing Esc key and then typing:wq
Now you can check If PHP is working or not. To check it use below way, the ip_address replace with you CentOS public IP to check it publically or your system private IP to check it to locally.
You will get output like the below picture,
In this tutorial, we learn the LAMP stack installation. And you can see that the LAMP stack installation is pretty easy. It is the combination of Linux, Apache, MySQL, and PHP. I hope, you understand but if you have any questions, you can ask in the comment section.