Article

Table of Contents
Theme:
Was this article helpful?
Try Vultr Today with

$50 Free on Us!

Want to contribute?

You could earn up to $600 by adding new articles!

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

Author: Humphrey Mpairwe

Last Updated: Tue, Nov 16, 2021
CentOS Linux Guides MySQL and MariaDB PHP Web Servers

A LAMP stack is a collection of open-source software bundled and installed to enable a server to provide web resources. Consisting of Linux, Apache, MySQL, and PHP, the LAMP stack makes it easy to build dynamic websites and applications and host a wide range of web services.

One of the key advantages of LAMP is the flexibility to substitute services; for instance, MySQL can be substituted for MariaDB, PostgreSQL or SQLite, PHP for Perl, Python, or Ruby. Apache as well can be replaced with Nginx to form the LEMP stack. This makes the stack a reliable option for deploying all kinds of applications on your server.

In this guide, you will install the LAMP stack on a CentOS 7 cloud server.

Prerequisites

  • Deploy a CentOS 7 server on Vultr
  • A sudo user account (root privileges)

Step 1: Install Apache

On CentOS, Apache is identified by the httpd package. To install the web server, we need to update the system, then attempt to install httpd.

Update the server.

$ sudo yum update

Install Apache with the httpd package.

$ sudo yum install httpd -y

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

$ sudo systemctl enable httpd
$ sudo systemctl start httpd

If you have a firewall enabled, you must open the http and https transport ports for the web server to function.

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

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

$ sudo systemctl reload firewalld

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

Step 2: Install MySQL (MariaDB)

On CentOS 7, MySQL is substituted by MariaDB. The database management system is a community-developed fork of MySQL, and former members of the MySQL team developed it before it became an Oracle product. As a result, MariaDB uses the same commands and database queries with almost no difference from MySQL.

Let's install MariaDB.

$ sudo yum install mariadb-server

Next, enable and start the database server to run automatically at system boot.

$ sudo systemctl start mariadb
$ sudo systemctl enable mariadb

Now, we need to maximize security for the database server by running the secure installation script that will prompt you to set a root password for the server, and all insecure default settings will be disabled for your database server.

$ sudo mysql_secure_installation

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
  SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!

In order to log into MariaDB to secure it, we'll need the current
password for the root user.  If you've just installed MariaDB, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.

Enter current password for root (enter for none):

Once finished, enter the MariaDB console to confirm if the database server works.

$ mysql

Your output should 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)]> 

Now, let's create a sample database.

MariaDB [(none)]> CREATE DATABASE LAMP;

 Query OK, 1 row affected (0.00 sec)

Now, exit the console.

MariaDB [(none)]> exit

With the successful creation of the database, it means the server is up and running. To confirm the current status of MariaDB, you can always use the command below.

$ sudo service mariadb status

Step 3: Install PHP

After the successful installation of Apache to serve web content, MariaDB to store database tables, and manage website data, PHP is the last component to complete your LAMP server installation on CentOS 7.

To install PHP, run the command below.

$ sudo yum install php php-mysqlnd

The first argument installs PHP, and the second is an extension that connects PHP with the database server. Now, restart Apache to enable PHP and the installed extension.

$ sudo systemctl restart httpd

To check for the current PHP version, always use the command php -v or php -version in your server terminal.

$ php -v

PHP 5.4.16 (cli) (built: Apr  1 2020 04:07:17) 
Copyright (c) 1997-2013 The PHP Group
Zend Engine v2.4.0, Copyright (c) 1998-2013 Zend Technologies

Next, let's test if PHP is successfully installed and can communicate with the web server. To this, we need to set up a small PHP file; if it loads in the browser, then PHP is installed on the server.

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

Enter the following lines of code.

<?php

echo "PHP successfully works with Apache, It works";

phpinfo();

Save and close the file.

Now, load your server IP in a browser followed by /hello.php. The server displays the PHP test page.

Now, delete the hello.php file from your server since it poses a security risk by displaying sensitive information about your PHP environment.

Congratulations, you have successfully installed the LAMP stack on your CentOS 7 server instance. You can go ahead and install a content management system (CMS) such as WordPress to build dynamic websites on your server.

Want to contribute?

You could earn up to $600 by adding new articles