How to Install LAMP Stack on CentOS 7

· >

In this post we are going to learn how to install LAMP stack on CentOS 7. LAMP stands for (Linux, Apache, MySQL/MariaDB, PHP).

  • Linux which is Operating System
  • Apache which is Web Server
  • MySQL/MariaDB which is Database
  • PHP is scripting language to serve dynamic content

All of these are open source and free to use. Each piece of the stack is mandatory for creating fully functional dynamic website.


  • CentOS Server running
  • SSH Access with Root Privileges

Step 1 – Update Packages

First of all, Login with your CentOS 7 Server & escalate the privileges to root. Before installation of LAMP stack, update your repository & software packages by running below commands on your CentOS 7. In CentOS default package manage is yum, so we shall use it to update & upgrade our repos.

#yum update && yum upgrade   

When prompted, press y to continue upgradation process.

Step 2 – LAMP Stack Install Apache

Now that your repository & software packages are upgraded, now install apache by entering below commands in your CentOS 7 server terminal.

#yum install httpd

When prompted, press y to continue installation process.  In CentOS, services when installed are not started & enabled by-default, you have to manually enable & start the service by executing below commands.

#systemctl enable httpd
#systemctl start httpd

Apache service should be active & enabled, like so.

Install LAMP Stack on CentOS 7

Step 3 – Update Firewall Rules

Firewalld is the defualt CentOS 7 firewall, we need to enable Apache2 access through firewall. Here is our turtoial on UFW Firewall to learn more about it.

Firewalld is enabled by-default in CentOS & you will need to open port for HTTP traffic by using the command:

Below command will open port for HTTP & HTTPS traffic respectively:

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

Finally, restart the firewall to enable the new settings:

#sudo systemctl restart firewalld

Now if you visit your server IP Address, Apache CentOS 7 Default Page should appear.

Step 4 – LAMP Stack MySQL Installation

To install MySQL, type following command in your terminal using sudo rights.

#sudo yum install mysql-server

Now check mysql service status, it should be enabled & Active. If it is not Active & enabled, copy below commands & paste in your terminal.

#sudo systemctl start mysql
#sudo systemctl enable mysql

Step 5 – LAMP Stack MySQL Secure Configuration

Next to secure the MySQL installation, execute the following command in your terminal & Answer as follows:

LAMP Stack MySQL Secure Installation
  • Set root password? [y/n] y
  • New password: Type in secure password for your database
  • Re-enter new password: Retype the above password
  • Remove anonymous users? [Y/n] y or Press Enter
  • Disallow root login remotely? [Y/n] y or Press Enter
  • Remove test database and access to it? [y/n] y or Press Enter
  • Reload privilege tables now? [Y/n] y or Press Enter
LAMP Stack MySQL Secure Installation

Articles related to Install LAMP Stack on CentOS 7

Step 6 – LAMP Stack PHP Installation

Next, to install PHP execute below command in your terminal. Currently PHP 7.4 is the latest release, however if using CentOS default repos it shall install version 5.4.16. You can view our guide to upgrade to latest version of PHP here on our blog post.

#yum install php php-mysql php-fpm

Press y when prompted. Once it is installed, verify the PHP version as follows.


Next, php-fpm service is disabled & stopped bydefault, to enable & start the service perform below steps.

#systemctl enable php-fpm
#systemctl start php-fpm
#systemctl status php-fpm
Install LAMP Stack on CentOS 7

Step 7 – Test PHP Integration with HTTPD

To test that php is serving contect via apache2 server, for this pupose we shall create php script in web root directory (/var/www/html/info.php) by entering “vim /var/www/html/info.php” in our terminal and pasting below code & save the file.

<?php phpinfo (); ?>

Now if you visit your server IP address/info.php, you will get below screen. which shows that PHP has been installed properly along with apache2 web server.

Install LAMP Stack on CentOS 7

In this post, we followed these step by step guide to install LAMP stack on CentOS 7. If you are interested in setting up LEMP stack on CentOS 7, visit our post Learn to Install LEMP stack on CentOS 7 here on techacad.net.

Notify of
Inline Feedbacks
View all comments