--- slug: Installation Guide for LAMP Stack on CentOS 7 title: Installation Guide for LAMP Stack on CentOS 7 Linux, Apache, MySQL, PHP authors: [slorber, yangshun] tags: [hola, docusaurus] --- import CodeBlock from '@site/src/components/CodeBloack'; import apache from '@site/static/img/default_apache.png'; import php from '@site/static/img/default_php.png'; import todo from '@site/static/img/todo_list.png';
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:

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

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.

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:

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

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:

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:

After the installation finishes, kick off MariaDB with this command:

To make sure MariaDB starts automatically when your computer boots up, use this command:

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:

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:

This action links you to the MariaDB server as the main database boss, also known as the 'root' user:

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:

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:

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:

To leave the MariaDB shell, just type:

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:

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:

If everything went well, you should see 'example_database' in the output:

To leave the MariaDB shell, just type:

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:

After installing PHP, restart the Apache web server so that it recognizes the new PHP module you just added:

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:

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:

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:
`} />
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

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':

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:

In the MariaDB console, enter the following command to create a table called "todo_list" in your "example_database":

The MariaDB console will let you know about any changes made to your table after each update:

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:

To make sure that the information was saved correctly in your table, execute the following command:

Here's what an example of the result might look like:

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

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:

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.
TODO
    "; foreach($db->query("SELECT content FROM $table") as $row) { echo "
  1. " . $row['content'] . "
  2. "; } echo "
"; } catch (PDOException $e) { print "Error!: " . $e->getMessage() . "
"; 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".

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!