Installing LAMP Stack on CentOS 7

2017-07-22 By Karthik 2305 Views linux development web servers apache mysql php php script
0 reviews

The LAMP stack stands for Linux, Apache, MySQL and PHP - a technology stack that is needed for building dynamic websites and applications. However, on a CentOS 7 installation, this software is not installed by default.

In this tutorial, we will walk you through the steps to set up LAMP on CentOS 7.

Prerequisites :

1) A non-root sudo user with access to CentOS 7 installation

Getting Started with Apache

Once we have logged in to our server with the user, we will first install Apache. Apache is a popular web server, that is used by almost 60% of websites and hence is a great choice to get started with.

While we can download the software from Apache’s official site, CentOS 7’s default package manager yum comes with packages for Apache. Let’s install it by executing the following command

sudo yum install httpd

This will install the Apache server on CentOS 7. To start our server, let’s execute

sudo systemctl start httpd

This will start your server. You can now navigate to your IP address in a browser, and see this test page from Apache. This will confirm that the server is working now.

You can stop the server by

sudo systemctl stop httpd

Getting Started with PHP

PHP is a widely popular programming language that allows you to build scripts that can take users’ information, save it to a database and many more. Popular sites like Facebook, Amazon leverage the power of PHP to build their websites.

You can install PHP from the official site, but since it is popular, CentOS yum again provides packages for PHP. Install the following command

sudo yum install php php-mysql

Once you are done, type

php -v

And you will see

PHP 5.4.16 (cli) (built: Nov  6 2016 00:29:02)
Copyright (c) 1997-2013 The PHP Group
Zend Engine v2.4.0, Copyright (c) 1998-2013 Zend Technologies

This will indicate that PHP has been installed successfully.

To work with Apache though, we will need to restart Apache - which can be done by

sudo systemctl restart httpd

Installing MySQL

We will now install MySQL, which is a relational database management system. Instead of the default MySQL packages, we will be installing MariaDB, which is a community maintained fork of MySQL

Install it by executing

sudo yum install mariadb-server mariadb

Once the installation is complete, start MariaDB by typing the following command

sudo systemctl start mariadb

Note : While we have installed MariaDB, by default the credentials for your root user are blank. It is important to secure your installation.

Run

sudo mysql_secure_installation

and it will run an interactive process that will ask you to

1) Set Root Passwords - Set your passwords and note them down in a password manager. 2) Remove anonymous users - Yes. 3) Disallow root login remotely - Recommended. 4) Remote test database and access to it - Yes

The process will also give you the reason behind each and every step and why it is recommended to do so.

Creating your first PHP Script

Now that we are done with installing all the scripts, let’s test if everything is properly configured.

We will create a file in the root of Apache’s web server, which is located at var/www/html

Create a file using either nano or vim editor and name it index.php

The content of the file should have the following code. Save the file when done.

<!--?php phpinfo();
?-->

When you now navigate to the IP address, instead of Apache’s test page, you should see a page with PHP’s installation information.

Note : Since this page shows entire information about your PHP installation. It is recommended to delete this file after you have confirmed the installation is working properly.

And with that, we have successfully installed the LAMP stack on CentOS 7 machine. We hope you found this tutorial helpful, please let us know your feedback in the comments section.

What do you think about this article?

Rate this article
Need assistance?

Try this guide to receive free bundled services at signup on a new free account.

Sign Up

Your Feedback Is Important

We hope you’ll give the new products and updates a try. If you have an idea for improving our products or want to vote on other user ideas so they get prioritized, please submit your feedback on our Community platform. And if you have any questions, please feel free to ask in the Community or contact our Technical Support team.