How to Install LAMP Stack on CentOS 8

Install LAMP Stack on CentOS 8

In this article, we will have explained the necessary steps to install and configure LAMP Stack on CentOS 8. Before continuing with this tutorial, make sure you are logged in as a user with sudo privileges. All the commands in this tutorial should be run as a non-root user.

LAMP is short for Linux, Apache, MySQL or PHP which is an open source web development platform. It uses Linux as it’s operating system while Apache and MySQL are used as a web & database servers, the PHP is also used as an object oriented scripting language to work as a complete web stack, which is used to prepare servers for hosting web content.

Install LAMP Stack on CentOS

Step 1. The first command will update the package lists to ensure you get the latest version and dependencies.

sudo dnf install epel-release
sudo dnf update

Step 2. Install Apache.

Apache http server can be installed on CentOS 8 as easily as running the command below:

sudo dnf install httpd

Once the installation is done, you can start and enable Apache to run on system reboot by executing:

systemctl enable --now  httpd

Check Apache version:

httpd -v

Step 3. Install MariaDB.

Run the following command as root or user with sudo privileges to install MariaDB on CentOS 8:

sudo dnf install @mariadb

Once MariaDB server is installed, start and enable the service with:

sudo systemctl start mariadb.service
sudo systemctl enable mariadb.service

MariaDB server package comes with a script called mysql_secure_installation performs several security-related operations, and sets the root password:

sudo mysql_secure_installation

Secure-MariaDB-Linux-Server

Step 4. Installing PHP.

Add remi repository in CentOS so we can install latest PHP 7.3, typing following command:

sudo dnf install dnf-utils http://rpms.remirepo.net/enterprise/remi-release-8.rpm

The default PHP module is set to PHP 7.3. To install a newer PHP release, enable the appropriate version:

sudo dnf module reset php
sudo dnf module enable php:remi-7.3

Then, that we have Remi repository enabled, we can install PHP and several most common PHP modules with:

sudo dnf install php php-opcache php-gd php-curl php-mysqlnd

Step 5. Configure FirewallD.

By default, CentOS 8 forbids public access to port 80. To allow other computers to access the web page, we need to open port 80 in firewalld, the dynamic firewall manager on CentOS. Run the following command to open port 80:

firewall-cmd --permanent --zone=public --add-service=http
firewall-cmd --permanent --zone=public --add-service=https
systemctl reload firewalld

Step 6. Confirm the LAMP server installation.

Create a file called info.php within the /var/www/html/ directory with the following content:

<?php phpinfo(); ?>

Now open your browser to the http://localhost/info.php, You will see the PHP information which indicates you have installed and configured Apache, MariaDB and PHP on your CentOS 8 server.

That’s all you need to do to Install LAMP Stack on CentOS 8. I hope you find this quick tip helpful. If you have questions or suggestions, feel free to leave a comment below.