Skip to main content

Installation Guide for LAMP Stack on CentOS 7 Linux, Apache, MySQL, PHP

· 12 min read
Yashwin Shankar
Front End Developer @ Tech4Biz Solutions Pvt Ltd.
Introduction

A "LAMP" stack is a group of software that works together to enable your computer to host websites and web apps. The term "LAMP" represents Linux (the operating system), Apache (the web server), MariaDB (a place to store website data), and PHP (a language for making websites dynamic).

On most computers, when we ask for "MySQL," we get MariaDB instead, and that's okay because they're similar. This guide will help you set up the LAMP stack on your computer running CentOS 7. It's like putting together a team to make your websites run smoothly. Let's get started!
Before You Start
Before we dive into the exciting part, make sure you have a special account set up on your computer. We won't be using the root account for this - it's too powerful!

Don't worry if you haven't set up this special account yet. We have a guide that shows you how to do it. Just follow our step-by-step instructions for setting up your personal space on your CentOS 7 computer. Once you've got that sorted, we'll be all set for the next steps!
Setting Up Apache - Your Website Host
Apache is a friendly host for your website, making it visible to visitors and handling PHP pages.To get Apache, we'll use a special tool on CentOS called "yum." It helps bring things to your computer easily.

Now, imagine you're talking to your computer. Say, "Hey, computer, I want Apache!" and use this magical command in your terminal:
sudo yum install httpd

When asked, type Y to confirm the Apache installation. After the installation finishes, kick off your Apache server by using this command.
sudo systemctl start httpd

To check if your server is working, open your web browser and type in your public IP address or your website's name.
Note: If you picked Cloudtopiaa to manage your website's address, just follow the simple instructions in our guides. They'll show you how to create a new website name and connect it to your server hassle-free.
ip addr show eth0 | grep inet | awk '{ print $2; }' | sed 's//.*$//'

After running the command, you'll see a few addresses. Test each one in your web browser to see your server. Another way is to ask another server to tell you your IP address using this command:
curl http://icanhazip.com

Whatever way you pick, just enter your IP address into your web browser to make sure your server is working.
http://your_server_IP_address

When you check, you should see a basic web page – that's the default landing page for CentOS 7 Apache.
default_apache
To make sure Apache starts automatically when your computer starts up, use this command:
sudo systemctl enable httpd.service

Installing MariaDB for Your Website's Database Management
Now that your website foundation is set up, let's install MariaDB. It helps manage and store information for your site in organized databases.

To get MariaDB on your system, just run this command:
sudo yum install mariadb-server

After the installation finishes, kick off MariaDB with this command:
sudo systemctl start mariadb

To make sure MariaDB starts automatically when your computer boots up, use this command:
sudo systemctl enable mariadb.service

To enhance the security of your database server, it's a good idea to use a special script that's already installed with MariaDB. This script will fix some security issues and make sure your database is more protected.

To start the script, run:
sudo mysql_secure_installation

This script will ask you a few questions to make your MariaDB setup more secure. The first question is about the 'database root password.' It's not the same as your computer's root password. The database root is like a manager with full control over the database. Since you just installed MariaDB and didn't set up a password yet, leave it blank and press ENTER when asked.

The next question will ask if you want to create a password for the database root. Type N and press ENTER.

After that, just type Y and press ENTER for the following questions. This will get rid of any unnecessary users and the test database, stop remote root login, and apply these changes right away.

Once you're done, log in to the MariaDB console by typing:
sudo mysql

This action links you to the MariaDB server as the main database boss, also known as the 'root' user:
Output
Welcome to the MariaDB monitor.  Commands end with ; or g.
Your MariaDB connection id is 12
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. 

For better security, it's a good idea to have separate user accounts with limited access for each database, especially if you have more than one on your server.

To see how it works, let's make a database called 'example_database' and a user named 'example_user.' You can use different names if you want.

In your MariaDB console, type the following command to create a new database:
CREATE DATABASE example_database;

Let's make a new user and give them complete access to the special database you just made. The command below sets the password as 'password,' but make sure to use a strong and secure password of your choice:
GRANT ALL ON example_database.* TO 'example_user'@'localhost' IDENTIFIED BY 'password' WITH GRANT OPTION;

This command ensures that the 'example_user' can do everything with the 'example_database' but won't be able to mess with any other databases on your server.

After that, use the FLUSH statement to make sure the changes to 'example_user' are saved and take effect:
FLUSH PRIVILEGES;

To leave the MariaDB shell, just type:
exit

To check if the new user has the right permissions, log back into the MariaDB console, but this time use the 'example_user' details you set up earlier:
mysql -u example_user -p

Remember the '-p' flag in this command? It'll ask for the password you set when making the 'example_user.' Once you're in the MariaDB console, check if you can access the 'example_database' using this statement:
SHOW DATABASES;

If everything went well, you should see 'example_database' in the output:
Output
+--------------------+
| Database           |
+--------------------+
| example_database   |
| information_schema |
+--------------------+
2 rows in set (0.000 sec)

To leave the MariaDB shell, just type:
exit

Your database is all set, and now you can move on to installing PHP.
Adding PHP for Interactive Website Content
Now that you have Apache for showing your website and MariaDB for storing data, let's add PHP to make your site interactive. PHP processes code to show dynamic content. You'll also need 'php-mysql' to make PHP talk to MySQL databases. No worries about the core PHP packages; they'll be installed automatically.
Just use this command to get both 'php' and 'php-mysql' packages with yum:
sudo yum install php php-mysql

After installing PHP, restart the Apache web server so that it recognizes the new PHP module you just added:
sudo systemctl restart httpd.service

Your server is now set up with everything needed for your LAMP stack application. The next thing to do is to check if everything is working well together.
Checking PHP on Your Apache Web Server
The initial setup of Apache on CentOS 7 puts the main folder for your website at /var/www/html. You don't have to tweak any Apache settings for PHP to work correctly.

But, if you want to make things easier, you can change the permission settings on that folder. This way, you can create and edit files in that directory without needing to use 'sudo' every time.

Run the following command to change the owner of the default Apache folder to a user and group named 'sammy.' Just replace 'sammy' with your own username and group:
sudo chown -R sammy.sammy /var/www/html/

To check if your web server is working correctly, let's create a PHP test file. You can use any text editor you like. In the examples below, we'll use the default 'vi' text editor in CentOS 7.

Make a new PHP file called 'info.php' in the 'var/www/html' directory:
vi /var/www/html/info.php

This will open an empty PHP file in the 'var/www/html' directory. Press 'I' to start typing and making changes. Now, enter the following PHP code:
<?php phpinfo(); ?>

This PHP code shows details about the PHP setup on your server. After editing, press 'ESC' to exit insert mode in vi. Then type ':x' to save and close the file

To check if your web server can display PHP content correctly, go to your server's public IP address followed by '/info.php' in a web browser
http://your_server_IP_address/info.php

When you open your web browser and go to your server's public IP address followed by '/info.php', you'll see a page that looks like the one below:

default_php
This page shows information about your server from the PHP perspective. It's helpful for solving problems and making sure your settings are correct. When you're finished, it's a good idea to delete this file because it contains sensitive information about your PHP and CentOS setup.

To get rid of the file, you can use 'rm':
rm /var/www/html/info.php

You can save this page so you can come back to it later if you need the information again. Next, you can check if the database is working properly using PHP.

Checking Database Connection with PHP (Optional)
To see if PHP is correctly connecting to MariaDB and performing database operations, you can create a test table with some example data. Afterward, you can ask PHP to retrieve and display the contents of that table.

Begin by accessing the MariaDB console using the username and password you set up earlier:
mysql -u example_user -p

In the MariaDB console, enter the following command to create a table called "todo_list" in your "example_database":
CREATE TABLE example_database.todo_list (
item_id INT AUTO_INCREMENT,
content VARCHAR(255),
PRIMARY KEY(item_id)
);

The MariaDB console will let you know about any changes made to your table after each update:
Query OK, 0 rows affected (0.00 sec)

Add some information into the test table. You can use the following command multiple times, changing the values each time, to fill up your test table:
INSERT INTO example_database.todo_list (content) VALUES ("My first important item");

To make sure that the information was saved correctly in your table, execute the following command:
SELECT * FROM example_database.todo_list;

Here's what an example of the result might look like:
Output
+---------+--------------------------+
| item_id | content                  |
+---------+--------------------------+
|       1 | My first important item  |
|       2 | My second important item |
|       3 | My third important item  |
|       4 | and this one more thing  |
+---------+--------------------------+
4 rows in set (0.000 sec)

Once you've checked that your test table has the right information, you can leave the MariaDB console:
exit

Now, let's make a PHP script to connect to MariaDB and get your data. Open a new PHP file in your web folder using your favorite text editor. If you like, you can use vi as shown in this example:
vi /var/www/html/todo_list.php

To add the following content, follow these steps in the vi text editor:
  1. Press the "I" key to enter insert mode.
  2. Replace "example_user" and "password" with your actual database username and password.
After making these changes, the content will be added to the file.
<?php
$user = "example_user";
$password = "password";
$database = "example_database";
$table = "todo_list";

try {
$db = new PDO("mysql:host=localhost;dbname=$database", $user, $password);
echo "<h2>TODO</h2><ol>"; 
foreach($db->query("SELECT content FROM $table") as $row) {
  echo "<li>" . $row['content'] . "</li>";
}
echo "</ol>";
} catch (PDOException $e) {
  print "Error!: " . $e->getMessage() . "<br/>";
  die();
}

To save and exit the file in vi:Press the "Esc" key to exit insert mode. Type ":x" (without quotes) and press Enter.
Once you've saved and closed the file, you can view this page in your web browser by going to your server's hostname or public IP address, followed by "/todo_list.php".
http://server_host_or_IP/todo_list.php

Here's what the web page might look like, showing the information you added to your test table:

todo_list
Conclusion:
In this guide, you've created a strong base for hosting PHP websites and apps for your visitors, using Apache as your web server. You've configured Apache to work with PHP and set up a MariaDB database to store your website's information.

Ready to start building your own website? Let's get started!