Installing Apache, MySQL, PHP (LAMP stack) on CentOS 7

Installation Apache MySQL PHP CentOS LAMP


In this tutorial, we will be install Apache, PHP, MySQL (LAMP stack) on CentOS 7. LAMP stack is one of the most commonly used setups to host web applications. 

Installing Apache

We will be using yum to install apache, yum is a package repository maintained by CentOS to make installing packages safe and easy. 

Install apache (httpd) by using the following command: 

# sudo yum install httpd

If need apache to start on system boot, we need to enable it using the following command: 

# sudo systemctl enable httpd.service

Installing MySQL/MariaDB

MySQL has been replaced with MariaDB in CentOS 7. MariaDB is a popular drop-in replacement for MySQL. We can install MariaDB by using the following command:

# sudo yum install mariadb-server

Now we can add MariaDB to system boot and start up MariaDB service by using the commands below. 

# sudo systemctl enable mariadb.service
# sudo systemctl start mariadb.service

Now run the command below to secure your database:

# mysql_secure_installation

And when you are prompt to enter the MySQL password, leave it blank and press enter. Now continue with the installation, you will be asked to enter a root password for MySQL. 

Installing PHP

Now let's install PHP to complete our LAMP stack installation. PHP is also available on yum, we can run the following command to install PHP and php-mysql module. 

# sudo yum install php php-pear php-mysql

Finally, we need to restart apache using this command in order for PHP to work:

# sudo systemctl restart httpd.service

Now that we have everything installed, let's test and see if everything is working.

Let's create a test PHP file that will display PHP installation info by using the following command:

# echo "<?php phpinfo();" > /var/www/html/test.php

Now you can visit this URL. 

http://<your IP address or domain>/test.php

Your should now see a page that looks like this:

CentOS php test page


If you are unable to reach your website, then it's most likely you have a firewall setup and you do not have incoming HTTP/HTTPS traffic allowed. Use the following commands depending on which firewall you are using.


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

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

# sudo firewall-cmd --reload


# iptables -I INPUT -p tcp -m tcp --dport 80 -j ACCEPT
    No comments found for this tutorial, be the first to leave a comment!

Tutorial by

Last updated on
Jul 26, 2015