How to build a LAMP stack (Linux, Apache, MySQL, PHP) on CentOS 7

  • 31 January 2017
  • ADM


How to build a LAMP stack (Linux, Apache, MySQL, PHP) on CentOS 7 - images/logos/centos.jpg


This article will present how to build a LAMP. LAMP is just an acronym for the software the stack uses.

  • Linux - Operating System.
  • Apache - HTTP server
  • MySQL/MariaDB - database.
  • PHP - programming language.

The article assumes that the Linux is already installed, locally or a small VM in the cloud.

Step 1 - Install Apache

  1. Before you begin any installation, make sure that your software is up to date. To do that run the following command:

    $ sudo yum update
  2. Install Apache:

    $ sudo yum install httpd
  3. Start Apache:

    $ sudo systemctl start httpd.service
  4. Set Apache to start on server boot:

    $ sudo systemctl enable httpd.service
  5. Verify that Apache is installed by going to using any web browser:

    http://the server's IP address.

    If Apache is installed, the Apache Test Page displays like in the image bellow.

    How to build a LAMP stack (Linux, Apache, MySQL, PHP) on CentOS 7 - /images/apacheWelcomeScreen.png

  6. Open the 80 port

    If the page is not displayed most probably the port 80 is not open for public access (from outside of the server). To open the port follow the next steps:

    • Run the command to add the port 80 to the firewall

      $ firewall-cmd --zone=public --add-port=2888/tcp --permanent
    • Run the command to reload the new rule added

      $ firewall-cmd --zone=public --add-port=2888/tcp --permanent

Install MySQL

  1. Install MariaDB, which is a community-developed fork of MySQL:

     $sudo yum install mariadb-server mariadb
  2. Start the service:

    $ sudo systemctl start mariadb
  3. Set MySQL to start on server boot:

    $ sudo systemctl enable mariadb.service
  4. Run this command to finish setting up the installation:

    $ sudo mysql_secure_installation

    You will be asked for the root password. Because you didn't set it earlier, press Enter to set a password now.

    • Type "Y" to set the root password.
    • Enter and confirm the new password.
    • You will be asked more questions as part of the security configuration. It is a best practice to respond "Y" to these system prompts because will disable the remote access for root user, will delete test database and will remove the anonymous user.

Install PHP

  1. Install PHP:

    $ sudo yum install php php-mysql

    Enter "Y" + Enter key to install.

  2. Restart Apache to read the new settings:
    $ sudo systemctl restart httpd.service

Install PHP modules

If your applications require any PHP modules, you can install them using the following steps:

  1. View available PHP modules:

    $ yum search php-

    This command will help you to see the exact module name.

  2. The list displays the available PHP modules. To view the description of a specific package, use the following command:

    $ yum info <the name of the package>
  3. Install the package you want:

     $sudo yum install <the name of the package>

    Note: don't forget to restart apache after the module installation.

Test PHP processing on Apache

  1. Create a new PHP file under the /var/www/html directory:

    $ sudo vi /var/www/html/info.php
  2. When the file opens type i (or Insert) key to start the insert mode, then type in the following code:

  3. Press Esc to exit the insert mode and then type:


    to save the changes.

  4. To verify it worked, type this URL into your browser:

    http://the server's IP address/info.php

A page displays with the PHP version, extensions, build date, and other information.

How to build a LAMP stack (Linux, Apache, MySQL, PHP) on CentOS 7 - /images/apachePHPTestScreen.png

Now you are ready to deploy dynamic web application.