Chat Zalo Chat Messenger Phone Number Đăng nhập
How To Install Linux, Apache, MySQL, PHP (LAMP) stack On

How To Install Linux, Apache, MySQL, PHP (LAMP) stack On


A “LAMP” stack is a group of open source software that is typically installed together to allow a server to host dynamic websites and web applications. This term is an acronym that represents the Linux operating system, with the web server Apache. Site data is typically stored in an Mand SQL database and dynamic content is processed by PHP.

On most Linux systems, you can install MySQL by downloading the mysql-server package from your system’s default package management repositories. However, on CentOS 7, the mysql-server package will install MariaDB, a community-developed fork of the MySQL relational database management system that functions as a direct replacement for MySQL. Therefore, this tutorial will describe how to install a LAMP stack consisting of Linux, Apache, MariaDB and PHP on a CentOS 7 server.


Before you begin this guide, you must have a separate non-root user account configured on the server. You can learn how to do this by following our initial server setup tutorial for CentOS 7.

Step 1 — Installing the

Apache web server Apache is

a popular open source web server that is used to display web pages to visitors. You can configure it to serve PHP pages.

Install Apache using the CentOS package manager, yum. A package manager allows you to install most software from a repository maintained by CentOS.

Type this command in your terminal

to install the Apache httpd package:

  1. sudo yum install httpd

When prompted, type Y to confirm the Apache installation. Once the installation is complete, start your Apache server with this command:

  1. sudo systemctl start httpd

You can test if your server is running by entering

your public IP address or domain name in your web browser.

If you don’t have a domain name pointing to your server or don’t know your server’s public IP address, you can find it by running the following command:

  1. ip addr show eth0 | grep inet | awk ‘{ print $2; }’ | sed ‘s//.*$//’

This will print a few different addresses. You can try each of them in your web browser.

An alternative method is to use an external party to tell you how your server looks. You can do this by asking a specific server what its

IP address is with this command:

  1. curl

Whichever method you choose, type your IP address into your web browser to verify that your server is running.

http://your_server_IP_address You

will be presented with the default homepage of CentOS 7 Apache: Default home page of CentOS 7 Apache web server status after initial installation.

You can enable Apache to start at boot with:

  1. sudo systemctl enable httpd.service

Step 2 — Installing MySQL (


With your web server up and running, you can install MariaDB. It will organize and provide access to databases where your site can store information.

To install the MariaDB

software package

, run: sudo

  1. yum install mariadb-server

When the installation is complete, start MariaDB: sudo systemctl start mariadb You can enable MariaDB to

start at boot with this command:

  1. sudo systemctl enable mariadb.service

to improve database server security it is recommended to run a security script that comes preinstalled with


tag. This script will remove some insecure default settings and block access to your database system.

Start the interactive script by running:

  1. sudo mysql_secure_installation

This script will guide you through a series of prompts where you can make some changes to your MariaDB configuration. The first message will ask you to enter the root password of the current database. This should not be confused with the root user of the system. The root user of the database is an administrative user with full privileges on the database system. Because you have just installed MariaDB and have not made any configuration changes, this password will be blank. Press ENTER when you are prompted.

The following message asks if you want to configure a database root password. Type N, and then press ENTER.

From there, you can press Y and then ENTER, to accept the defaults for all subsequent questions. This will remove anonymous users and the test database, disable remote root login, and load these new rules so that the server immediately respects the changes made.

When you’re done, log in

to the MariaDB console by entering:

  1. sudo mysql

This connects you to the MariaDB

server as the user root of the administrative database: OutputWelcome 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.

To increase security, it is best to have dedicated user accounts with less expansive privileges configured for each database. This is especially important if you plan to have multiple databases hosted on your server.

To demonstrate this configuration, create a database named example_database and a user named example_user. You can replace these names with different values.

Run the following command from the MariaDB console to

create a new database:

  1. CREATE DATABASE example_database;

You can create a new user and grant them full privileges on the custom database you just created. The following command sets this user’s password as a password, but you must replace this value with a strong password:

  1. GRANT ALL ON example_database.* TO ‘example_user’@’localhost’ IDENTIFIED BY ‘PASSWORD’ WITH GRANT OPTION;

This command gives the user full privileges to example_user database example_database, while preventing this user from creating or modifying other databases on the server.

Use the FLUSH

statement to reload and save the privileges you just granted to




MariaDB shell:

  1. exit

You can test whether the new user has the appropriate permissions by logging back into the MariaDB console, but using the example_user credentials you created earlier:

  1. mysql -u example_user-p

Note the -p flag in this command, which will prompt you for the password you chose when creating the example_user user. After logging into the MariaDB console, confirm that you have access to the example_database database with this declaration



Your example_database should appear in the output:

Output+-+ | Database | +-+ | example_database | | information_schema | +-+ 2 rows together (0.000 sec)

To exit the MariaDB shell, type:

  1. exit

Your database system is configured and you can move on to installing PHPs


Step 3 — PHP Installation

You have Apache installed to serve your content and MariaDB to store and manage your data. PHP will process the code to display dynamic content to the user. In addition to the php package, you will need php-mysql, a PHP module that allows PHP to communicate with MySQL-based databases. Core PHP packages will be automatically installed as dependencies.

Use this command

to install the php and php-mysql packages with yum: sudo

  1. yum install php php-mysql

Restart the Apache web server to enable the PHP module you installed:

  1. sudo systemctl restart httpd.service

Your server is now configured with all the necessary components for your LAMP stack application. The next step is to test your setup to make sure everything works harmoniously.

Step 4 — Test PHP on your web server


The default installation of Apache on CentOS 7 will create a document root located in /var/www/html. You don’t need to make any changes to the default Apache settings for PHP to work properly within your web server.

However, you can make an adjustment to change the default permission settings on the root folder of the Apache document. This allows you to create and modify files in that directory with your regular system user without the need to prefix each command with sudo.

The following command will change the default root property of the Apache document to a user and group named sammy, so be sure to replace the username and group highlighted in this command to reflect your system’s username and group:

  1. sudo chown -R sammy.sammy /var/www/html/

You can create a PHP test file to make sure the web server works as expected. Use your preferred text editor to create this file. The following examples use the default vi text editor on CentOS 7.

Create a PHP file named info.php in the var/www/html directory: vi /var/www/html/info.php This opens

a blank PHP file in the /var


  1. www/html


. Press I to enter INSERT mode in the vi editor. This allows you to write and make changes within the text editor. Type the following

PHP code: <?php phpinfo(); ?>

This PHP code displays information about the PHP environment running on your server. When you have finished making changes to this file, press the ESC key to exit INSERT mode in vi. Type 😡 – a semicolon and the letter x in lowercase – to save and close the file.

You can test whether your web server correctly displays PHP content by going to your server’s public IP address, followed by

/info.php: http://your_server_IP_address/info.php

A web page, similar to the following, will be displayed in your browser: CentOS 7 default PHP information webpage containing information about current PHP settings

This page gives you information about your server from a PHP perspective. It is useful for debugging and making sure that settings are applied correctly. After checking the relevant information about your PHP server, it is best to delete this file as it contains sensitive information about your PHP environment and your CentOS server.

You can use rm to delete this file: rm


  1. var/www/html/info.php

You can always re-create this page if you need to access the information again later. You can then test the database connection using PHP.

Step 5 – Test the database connection

with PHP (optional)

You can test whether PHP connects to MariaDB and executes database queries by creating a test table with some test data. You can query its content from a PHP script.

First, connect to the MariaDB console

with the database user you created in Step 2 of this guide


  1. mysql -u example_user-p

From the MariaDB console, run the following statement to create a table named todo_list within your example_database:

  1. CREATE TABLE example_database.todo_list
  2. ( item_id

  4. content VARCHAR(255),
  5. PRIMARY KEY(item_id)
  6. );

The MariaDB console will notify you about changes to your table after each edit.

Query successful, 0 rows affected (0.00 sec)

Insert some rows of content into the test table. You can repeat the following command several times, using different values, to populate

the test table: INSERT INTO example_database.todo_list

  1. (content) VALUES (“My first important item”);


confirm that the data was successfully saved in the table, run


  1. SELECT * FROM example_database.todo_list;

Here is an example of the output:

Output+-+-+ | item_id | Contents | +-+-+ | 1 | My first important article | | 2 | My second important topic | | 3 | My third important topic | | 4 | and this more thing | +-+-+ 4 rows together (0.000 sec) After confirming that you

have valid data in your test table,

you can exit the MariaDB console:

  1. Exit

Now you can create the PHP script that will connect to MariaDB and query its contents. Create a new PHP file in your custom web root directory using your preferred editor. This example uses

vi: vi /

  1. var/www/html/todo_list.php

Add the following content by pressing I in the vi text editor, remembering to replace the example_user and password with your own:

<?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/>”; dying(); }

Save and close the file when you are finished editing by pressing ESC, followed by typing 😡 in vi.

You can now access this page in your web browser by visiting your server’s hostname or public IP address, followed by

/todo_list.php: http://server_host_or_IP/todo_list.php

Below is an example of the web page, revealing the content you have inserted into your test table: Example of PHP web page generating the data that was inserted into the test table in MariaDB


In this guide, you have created a flexible foundation for serving PHP websites and applications to your visitors, using Apache as your web server. You’ve set up Apache to handle PHP requests and set up a MariaDB database to store your website data.

Contact US