CentOS

How to Install LAMP Stack on CentOS 8 Server

What is LAMP Stack?

The LAMP Stack is a bundle of an open-source software stack that powers both static and dynamic websites. It stands for Linux as an operating system, Apache as a web server, MySQL/MariaDB as a database server, and the PHP as a processing language.

In this tutorial, we will show you step by step install the LAMP Stack on CentOS 8 server.

Prerequisites

For this tutorial, ensure you have the CentOS 8 server installed. And make sure you have root privileges, it’s needed for doing things related to installing new packages, service management, and editing configuration files.

Before going any further, run the sudo command below to get the root privileges.

sudo su

Step 1 – Install Apache Web Server

First, we will install the Apache webserver on the CentOS 8 server. It’s available by default on the AppStream repository as a package called “httpd”.

Install the Apache “httpd” using the dnf command below.

dnf install httpd -y

Once the installation is complete, start the httpd service and add it to start automatically at system boot.

systemctl start httpd
systemctl enable httpd

Now check the httpd service.

systemctl status httpd

And the Apache httpd service is up and running.

Install Apache httpd on CentOS 8

Next, we need to add the http and https service to the firewall configuration.

By default, the firewalld service is blocking the access to both http and https protocols. We need to open both ports for access to the Apache httpd service.

Add http and https services to the firewalld configuration.

firewall-cmd --add-service={http,https} --permanent
firewall-cmd --reload

Open HTTP and HTTPS  port uisng firewalld

After that, go to the “/var/www/html” directory and create the default “index.html” file.

cd /var/www/html
echo "<h1><center>This is Index.html for Apache httpd web server</h1></center>" > index.html

Now open your web browser and type the server IP address on the address bar.

http://serverip/

You will get the page of “index.html” that we just created.

Install Apache httpd on CentOS 8

Step 2 – Install and Configure MySQL Database Server

After installing the Apache httpd web server, we will install the MariaDB database server.

Install the MariaDB database server from the official AppStream repository using the dnf command below.

dnf install mariadb mariadb-server

Once the installation is complete, start the MariaDB service and add it to start automatically at the system boot.

systemctl start mariadb
systemctl enable mariadb

Next, we will configure the MariaDB root password. And we will use the “mysql_secure_installation” command-line utility provided by MariaDB.

mysql_secure_installation

Type your strong password for the MariaDB “root” user and type “Y” for all questions.

Set a 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

And we have configured the root password for the MariaDB database server.

Now you can log in to the MariaDB shell using the “mysql” command below.

mysql -u root -p

Type your MySQL root password.

Once you’ve logged in, you can check databases and users using the queries below.

show databases;
select user,host,password from mysql.user;

You will get the list of databases and users of the MariaDB database system.

Install MariaDB on CentOS 8

Step 3 – Install and Configure PHP 7.2

By default, the CentOS AppStream repository is provided PHP 7.2 packages.

Install PHP 7.2 to the CentOS 8 server using the dnf command below.

dnf install php php-common php-pecl-apcu php-cli php-pear php-pdo php-mysqlnd php-gd php-mbstring php-xml

Once the installation is complete, edit the default PHP configuration file “/etc/php.ini”.

vim /etc/php.ini

Uncomment the “cgi.fix_pathinfo” option and change the value to “0”.

cgi.fix_pathinfo=0

Save and close.

Now restart the Apache httpd service.

systemctl restart httpd

And the PHP 7.2 has been installed to the CentOS 8 server.

Step 4 – Testing

To test the LAMP Stack installation on the CentOS 8 server, we will create a new simple PHP file on default document-root directory “/var/www/html”.

Go to the “/var/www/html” directory and create the PHP file “info.php”.

cd /var/www/html
echo "<?php phpinfo(); ?>" > info.php

Now open your web browser and type the server IP address following by the PHP file “info.php”.

http://serverip/info.php

Now you will get some information about your PHP installation.

Install LAMP Stack (Linux, Apache, MySQL, and PHP) on CentOS 8

As a result, we have installed the LAMP Stack on CentOS 8 Server.

Write A Comment