How To Install Linux, Apache, MySQL and PHP (LAMP) stack on CentOS 7

Published on  

6 min read

Looking for other version?

Choose a different version or distribution.

How To Install Linux, Apache, MySQL and PHP (LAMP) stack on CentOS 7

LAMP stack stands for Linux, Apache, MySQL, and PHP which is a popular open-source software stack that's used to create dynamic websites and web applications

A "LAMP" stack is a collection of open source software that is typically installed together to allow a server to host dynamic websites and PHP-based web apps. This term is an acronym for the Linux operating system and the Apache web server. The site's data is stored in a MySQL database, and PHP handles dynamic content.

In this tutorial, you will learn how to install Apache Server, MySQL and PHP separately.

Prerequisites

  • A server with CentOS 7 as OS
  • User privileges: root or non-root user with sudo privileges

Installing Apache, MySQL and PHP Separately On CentOS 7

To assist you comprehend Apache, MySQL, and PHP installations, we'll separate them into sections. However, before we get started, there are a few things you should be aware of.

Step 1: Update your System

Before you install any software, it's important to ensure that your CentOS 7 system is up to date. Open a terminal window and run the following command:

sudo yum clean all && sudo yum -y update

This command will update your package list and retrieve information on the latest versions of software packages available in CentOS's repositories.

Step 2: Install Apache

Apache is the most widely used web server software in the world. To install Apache on CentOS 7, run the following command:

sudo yum install httpd

This command will install the Apache web server and its dependencies.

Now let’s start Apache using the following command:

sudo systemctl start httpd.service

The last thing you will want to do is enable Apache to start on boot. Use the following command to do so:

sudo systemctl enable httpd.service

Step 3: Install MySQL (MariaDB)

Now that our web server is up and running, we can install MariaDB, a MySQL drop-in replacement. MariaDB is a fork of the MySQL relational database management system created by the community. Essentially, it will arrange and offer access to databases where our website's information might be stored.

We can utilize yum to buy and install packages once more. This time, we'll also install some "helper" packages to enable us get our components to communicate with one another:

sudo yum install mariadb-server mariadb

When the installation is complete, we need to start MariaDB with the following command:

sudo systemctl start mariadb

Now that our MySQL database is up and running, we'd like to run a quick security script that would remove certain risky defaults and restrict access to our database system. Begin the interactive script by typing:

sudo mysql_secure_installation

The prompt will ask you for your current root password. Since you just installed MySQL, you most likely won’t have one, so leave it blank by pressing enter. Then the prompt will ask you if you want to set a root password. Go ahead and enter Y, and follow the instructions.

The last thing you will want to do is enable MariaDB to start on boot. Use the following command to do so:

sudo systemctl enable mariadb.service

Step 4: Install PHP

PHP is a server-side scripting language that's used to create dynamic web content. To install PHP on CentOS 7, run the following command:

sudo yum install php php-mysql

This command will install PHP and the MySQL extension for PHP.

Configuring Firewall (Optional)

CentOS 7 by comes with firewalld, a complete firewall solution installed by default. So we’ll need to open port 80 on it to accept incoming connections to Apache. The easiest way to do this is by adding the http/https service rules to firewalld using the following commands.

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

How To Find your Server’s Public IP Address

There are several methods for determining your server's public IP address if you do not know it. This is usually the address you use to connect to your server via SSH.

There are several ways to accomplish this from the command line. To begin, you could use the iproute2 tools to obtain your IP address by entering the following:

ip addr show eth0 | grep inet | awk '{ print $2; }' | sed 's/\/.*$//'

This will return two or three lines. They are all valid addresses, but your computer may only be able to use one of them, so try them all.

Another option is to use the curl utility to contact an outside party and ask it how it sees your server. This is accomplished by requesting your IP address from a specific server:

curl http://icanhazip.com

Testing the Installation

To verify that the LAMP stack has been installed correctly, you can create a PHP file and place it in the Apache document root directory. To do this, open a text editor and create a file called info.php with the following content:

<?php
phpinfo();
?>

Save this file as info.php and copy it to the Apache document root directory:

sudo cp info.php /var/www/html/

Then, open a web browser and navigate to http://localhost/info.php or http://your_server_ip/infp.php. If the installation was successful, you should see a page with detailed information about the PHP configuration and modules.

Conclusion

In this guide, we've shown you how to install the LAMP stack on CentOS 7. By following these steps, you'll have a powerful web development environment that's ready to create dynamic websites and web applications. Whether you're a beginner or an experienced developer, the LAMP stack is a must-have tool for building modern web applications.

About the Author

Rexposed Staff

Was this helpful?