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

Updated on November 21, 2023
How to Install Linux, Apache, MySQL, and PHP (LAMP Stack) on CentOS 8 header image

LAMP stack is a group of open-source software bundled and installed together to enable a server to host modern dynamic websites and web applications written in server-side languages like PHP. LAMP is an acronym for Linux, Apache, MySQL, and PHP/Perl that makes it possible to host dynamic web applications on a server.

You'll install the LAMP Stack on a CentOS 8 cloud server. To get started, you need SSH access to your server and an existing Vultr server instance.

Prerequisites

  • Deploy a CentOS 8 server on Vultr
  • A non-root user account with sudo privileges

Install Apache

To install Apache web server, we need to update the system and install httpd.

Update the server.

$ sudo dnf update

Install Apache, identified by the httpd package.

$ sudo dnf install httpd -y

Once installed, start the Apache daemon and enable it to start during system boot.

$ sudo systemctl enable httpd
$ sudo systemctl start httpd

By default, the firewall is enabled on CentOS 8, so we must allow connections to Apache by opening http and https for the web server to function well.

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

Verify that the firewall rule was applied with the command.

$ sudo firewall-cmd --permanent --list-all

Your output will be similar to:

public
  target: default
  icmp-block-inversion: no
  interfaces:
  sources:
  services: cockpit dhcpv6-client http https ssh
  ports:
  protocols:
  masquerade: no
  forward-ports:
  source-ports:
  icmp-blocks:
  rich rules:

Reload firewall for the new rules to take effect.

$ sudo firewall-cmd --reload

Now, test the web server installation by visiting your server's public IP Address in a browser. You should see the Apache test page.

Install MySQL

With the web server up and running, we need to install a database server to allow storage and management of data for dynamic websites. But since the base MySQL is not available in CentOS repositories, we can substitute it with MariaDB, which uses the same commands and table structures.

To install MySQL run the command:

$ sudo dnf install mariadb-server

Once installation is done, enable and start MySQL to run during system boot.

$ sudo systemctl enable mariadb

$ sudo systemctl start mariadb

Next, we need to secure the database server by removing insecure defaults and setting a new root password through the secure installation script.

$ sudo mysql_secure_installation

Enter a new root password different from your server's password and verify the installation by logging in to MySQL.

$ mysql

Your output will be similar to:

Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 13
Server version: 5.5.68-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> 

The output shows that MariaDB is working perfectly well, and you can create databases and tables through the console.

Install PHP

Run the command below to install PHP and the MySQL module

$ sudo dnf install php php-mysqlnd

After installation is complete, restart Apache to enable PHP and the MySQL module for your server.

$ sudo systemctl restart httpd

Test PHP Functionality

To verify PHP functionality with Apache, we need to create a sample PHP file to confirm if the server can run the script.

$ sudo nano /var/www/html/test.php

Insert the following line of code into the file.

<?php

phpinfo();

Save and close the file.

Now, go to your web browser and visit the server's public IP address followed by /test.php. You should see the PHP test file.

Congratulations, you have successfully installed LAMP on your CentOS 8 server; you can now install any content management system (CMS) to build dynamic websites and build modern applications for your web visitors.