Tutorial

How To Install Linux, Apache, MariaDB, PHP (LAMP) Stack on CentOS 8 [Quickstart]

Published on April 16, 2020

Developer Advocate

English
How To Install Linux, Apache, MariaDB, PHP (LAMP) Stack on CentOS 8 [Quickstart]

Introduction

In this tutorial, you’ll install a LAMP stack on a CentOS 8 server. Although MySQL is available from the default repositories in CentOS 8, this guide will walk through the process of setting up a LAMP stack with MariaDB as the database management system.

For a more detailed version of this tutorial, with more explanations of each step, please refer to How To Install Linux, Apache, MySQL, PHP (LAMP) Stack on CentOS 8.

Prerequisites

To follow this guide, you’ll need access to a CentOS 8 server as a sudo user.

Step 1 — Install Apache

Install the httpd package with:

  1. sudo dnf install httpd

After the installation is finished, run the following command to enable and start the server:

  1. sudo systemctl start httpd

If firewalld is active, you’ll need to run the following command to allow external access on port 80 (HTTP):

  1. sudo firewall-cmd --permanent --add-service=http

Reload the firewall configuration so the changes take effect:

  1. sudo firewall-cmd --reload

With the new firewall rule added, you can test if the server is up and running by accessing your server’s public IP address or domain name from your web browser. You’ll see a page like this:

Default Apache Page CentOS 8

Step 2 — Install MariaDB

We’ll now install MariaDB, a community-developed fork of the original MySQL server by Oracle. To install this software, run:

  1. sudo dnf install mariadb-server

When the installation is finished, enable and start the MariaDB server with:

  1. sudo systemctl start mariadb

To improve the security of your database server, it’s recommended that you run a security script that comes pre-installed with MariaDB. Start the interactive script with:

  1. sudo mysql_secure_installation

The first prompt will ask you to enter the current database root password. Because you just installed MariaDB and haven’t made any configuration changes yet, this password will be blank, so just press ENTER at the prompt.

The next prompt asks you whether you’d like to set up a database root password. Because MariaDB uses a special authentication method for the root user that is typically safer than using a password, you don’t need to set this now. Type N and then press ENTER.

From there, you can press Y and then ENTER to accept the defaults for all the subsequent questions.

Step 3 — Install PHP

To install the php and php-mysqlnd packages using the dnf package manager, run:

sudo dnf install php php-mysqlnd

After the installation is finished, you’ll need to restart the Apache web server in order to enable the PHP module:

sudo systemctl restart httpd

Step 4 — Test PHP with Apache

The default Apache installation on CentOS 8 will create a document root located at /var/www/html. You don’t need to make any changes to Apache’s default settings in order for PHP to work correctly within your web server.

The only adjustment we’ll make is to change the default permission settings on your Apache document root folder. The following command will change the ownership of the default Apache document root to a user and group called sammy:

  1. sudo chown -R sammy.sammy /var/www/html/

We’ll now create a test PHP page to make sure the web server works as expected. First, you might want to install nano, a more user-friendly text editor, since that doesn’t come installed with CentOS 8 by default:

  1. sudo dnf install nano

Now, create a new PHP file called info.php at the /var/www/html directory:

  1. nano /var/www/html/info.php

The following PHP code will display information about the current PHP environment running on the server:

/var/www/html/info.php
<?php

phpinfo();

When you are finished, save and close the file.

To test whether our web server can correctly display content generated by a PHP script, go to your browser and access your server hostname or IP address, followed by /info.php:

http://server_host_or_IP/info.php

You’ll see a page similar to this:

CentOS 8 default PHP info Apache

Here are links to more detailed guides related to this tutorial:

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about our products

About the authors
Default avatar

Developer Advocate

Dev/Ops passionate about open source, PHP, and Linux.

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
Leave a comment


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Join the Tech Talk
Success! Thank you! Please check your email for further details.

Please complete your information!

Become a contributor for community

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

DigitalOcean Documentation

Full documentation for every DigitalOcean product.

Resources for startups and SMBs

The Wave has everything you need to know about building a business, from raising funding to marketing your product.

Get our newsletter

Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

New accounts only. By submitting your email you agree to our Privacy Policy

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.