This guide will teach you how to install and configure the LAMP stack (Linux – Apache – MariaDB – PHP) on CentOS 7.
- Update and refresh all the repositories on your system by using the following command:
sudo apt update
Install & Configure Apache Service on the System
Apache is a commonly used web server that can be installed on the CentOS Linux Operating System. Install the Apache service using the appropriate package manager for your system. Once Apache is installed, start the service & configure it according to your needs.
1.1 Install Apache Web Server on CentOS
Run the following command on your terminal to install the Apache server on your system.
yum install httpd -y
1.2 Start & Enable Apache Service
Use the following commands to start and enable the apache service to start automatically at boot time.
systemctl start httpd
systemctl enable httpd
1.3 Check the Status of the Apache Service
Ensure the apache service is running on your system. Check the status by using the following command:
sudo systemctl status httpd
1.4 Test Apache
Test the Apache through the IP of your server.
1.4.a Check the IP of the Server
Check your server’s IP address from the server’s control panel or by using the following command on the terminal.
1.4.b Access Through IP Address:
Open any browser and test the apache web interface using your server’s IP.
Install & Configure MariaDB Database Server
Now install the Database module of the LAMP stack by following the procedure stated below.
2.1 Install MariaDB Server
To install MariaDB Server with all dependencies, use the following command on the terminal.
yum install mariadb-server mariadb -y
2.2 Start & Enable the MariaDB Service
Use the following commands to start and enable the MariaDB service. Enable it to start automatically at boot time.
sudo systemctl start mariadb
sudo systemctl enable mariadb
2.3 Check the Status of the MariaDB Service
Ensure the MariaDB service is running on your system. Check the status by using the following command:
sudo systemctl status mariadb
2.4 Secure the MariaDB Server on CentOS
It is always recommended to secure MariaDB Service before using it for production. Use the following command to secure the installation. Go through all the prompts on the terminal and remove all possible vulnerabilities. This will prompt you to set the root password, remove anonymous users, disallow root login remotely, and remove test databases.
Now install the language module of the LAMP stack. Install the PHP language package and MySQL extension by using the following command:
yum install php php-mysql
After the installation, test the PHP package. Move to the following directory:
Now, create a new testphp.php file in the above directory for testing. Use the following command to create a PHP file.
Now edit the newly created PHP file using this command:
Write the following code in the PHP file and save the file.
Now restart the apache service by running the following command.
systemctl restart httpd
Now open any browser and access the web interface of the PHP file using the following address.