How to Install LAMP Server on CentOS 8

LAMP, an acronym for Linux, Apache, MySQL, and PHP, is a popular free and open-source stack used by website administrators and developers alike to test and host dynamic websites.

The LAMP server comes with 4 core components: the Apache web server, MySQL or MariaDB database and PHP which is a popular scripting language that is used for creating dynamic web pages.

Related Article: How to Install LEMP Server on CentOS 8

The LAMP stack is a popular hosting stack for a vast majority of hosting companies in providing a hosting environment for users’ websites. In this article, you will learn how to install LAMP server on CentOS 8 Linux distribution.

Step 1: Update CentOS 8 Software Packages

As is always recommended, it’s a good idea to update software packages before embarking on any installation. So log in to your server and run the command below.

$ sudo dnf update
Update CentOS 8 Packages
Update CentOS 8 Packages

Step 2: Install Apache Web Server on CentOS 8

With the system packages up to date, the next step is installing the Apache Web Server and some crucial tools and utilities run the command.

$ sudo dnf install httpd httpd-tools 
Install Apache on CentOS 8
Install Apache on CentOS 8

Once the installation is complete, enable Apache to auto-start at system boot time using the command below.

$ sudo systemctl enable httpd

Next, start the Apache service by running the command.

$ sudo systemctl start httpd

To confirm is Apache web service is running, run the command.

$ sudo systemctl status httpd
Verify Apache Service Status
Verify Apache Service Status

After installing apache, update the firewall rules to allow requests to web server.

$ sudo firewall-cmd --permanent --zone=public --add-service=http
$ sudo firewall-cmd --permanent --zone=public --add-service=https
$ sudo firewall-cmd --reload

If you are a little curious, you can get the version of apache among other details related to Apache by running the rpm command.

$ sudo rpm -qi
Check Apache Version
Check Apache Version

Additionally, you can open your web browser and visit your server’s IP a shown.

http://server-IP
Verify Apache Web Page
Verify Apache Web Page

Step 3: Install MariaDB on CentOS 8

MariaDB is a fork of MySQL database . It was developed by a former team of MySQL who had concerns that Oracle may turn MySQL to a closed-source project. It ships with innovative and better features than MySQL that make it a better option than MySQL.

To install MariaDB, run the command.

$ dnf install mariadb-server mariadb -y
Install MariaDB in CentOS 8
Install MariaDB in CentOS 8

Next, start and enable MariaDB on startup, run the command.

$ systemctl start mariadb
$ systemctl enable mariadb

You can verify the status of MariaDB by running the command.

$ systemctl status mariadb
Verify MariaDB Service Status
Verify MariaDB Service Status

Lastly, we need to secure our MariaDB database engine by running.

$ mysql_secure_installation
Secure MariaDB Installation on CentOS 8
Secure MariaDB Installation on CentOS 8

You will be prompted to enter the root password ( if you already have a root password in place) or set it up. Thereafter, answer Y for every subsequent prompt.

Secure MariaDB Installation Summary
Secure MariaDB Installation Summary

Step 4: Install PHP 7 on CentOS 8

The last component in the LAMP stack we need to install is PHP, and as mentioned earlier, PHP is a scripting web programming language used for developing dynamic web pages.

We are going to install the latest version of PHP ( PHP 7.4 by the time of penning down this guide) using the Remi repository.

First, install the EPEL repository.

$ sudo dnf install https://dl.fedoraproject.org/pub/epel/epel-release-latest-8.noarch.rpm

Next, install yum utils and enable remi-repository using the command below.

$ sudo dnf install dnf-utils http://rpms.remirepo.net/enterprise/remi-release-8.rpm
Install Remi Repository on CentOS 8
Install Remi Repository on CentOS 8

After the successful installation of yum-utils and Remi-packages, search for the PHP modules which are available for download by running the command.

$ sudo dnf module list php

The output will include the available PHP modules, stream and installation profiles as shown below.

List PHP Module in CentOS 8
List PHP Module in CentOS 8

The output indicates that the currently installed version of PHP is PHP 7.2. To install the newer release, PHP 7.4, reset the PHP modules.

$ sudo dnf module reset php
Reset PHP Module in CentOS 8
Reset PHP Module in CentOS 8

Having reset the PHP modules, enable the PHP 7.4 module by running.

$ sudo dnf module enable php:remi-7.4
Enable PHP Module in CentOS 8
Enable PHP Module in CentOS 8

Finally, install PHP, PHP-FPM (FastCGI Process Manager) and associated PHP modules using the command.

$ sudo dnf install php php-opcache php-gd php-curl php-mysqlnd
Install PHP Modules in CentOS 8
Install PHP Modules in CentOS 8

To verify the version installed to run.

$ php -v 
Check PHP Version in CentOS 8
Check PHP Version in CentOS 8

Perfect! We now have PHP 7.4 installed. Equally important, we need to start and enable PHP-FPM on boot-up.

$ sudo systemctl start php-fpm
$ sudo systemctl enable php-fpm

To check its status execute the command.

$ sudo systemctl status php-fpm
Check PHP-FPM Status
Check PHP-FPM Status

To instruct SELinux to allow Apache to execute the PHP code via PHP-FPM run.

$ setsebool -P httpd_execmem 1

Finally, restart Apache web server for PHP to work with Apache web server.

$ sudo systemctl restart httpd

Step 5: Testing PHP Information

To test PHP with the web server, you’ll have to create an info.php file to the document root directory.

$ vi /var/www/html/info.php

Insert the PHP code below and save the file.

<?php
 phpinfo ();
?>

Then head out to your browser, and type the URL below. Remember to replace the server IP address with your server’s actual IP address.

http://server-ip-address/info.php
Check PHP Info in CentOS 8
Check PHP Info in CentOS 8

You should now be able to see information about PHP on your web browser.

Great! You have now installed Apache, PHP, and MariaDB on your CentOS 8 system. As good practice, ensure you delete the info.php file as it can pose a security risk if hackers are able to identify the PHP version you are running.

If you liked this article, then do subscribe to email alerts for Linux tutorials. If you have any questions or doubts? do ask for help in the comments section.

If You Appreciate What We Do Here On TecMint, You Should Consider:

TecMint is the fastest growing and most trusted community site for any kind of Linux Articles, Guides and Books on the web. Millions of people visit TecMint! to search or browse the thousands of published articles available FREELY to all.

If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.

Support Us

We are thankful for your never ending support.

11 thoughts on “How to Install LAMP Server on CentOS 8”

  1. After installing apache also the firewall rules must be updated with:

    $ sudo firewall-cmd --permanent --zone=public --add-service=http
    $ sudo firewall-cmd --permanent --zone=public --add-service=https
    $ sudo firewall-cmd --reload
    
    Reply

Got something to say? Join the discussion.

Have a question or suggestion? Please leave a comment to start the discussion. Please keep in mind that all comments are moderated and your email address will NOT be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.