Child pages
  • Configuring a Basic LAMP Stack on CentOS 7
Skip to end of metadata
Go to start of metadata

Introduction

This how-to will guide you through the steps of installing and configuring a basic web server on a basic default CentOS 7 installation. Fortunately, this process is quite straightforward on a CentOS 7 machine, and can be done is as little as 5 minutes.

A basic LAMP stack consists of four primary components: A Linux OS, Apache Web Server, a MySQL relational database and a set of PHP libraries. Together these 4 components form the basic working structure of your web server, and handle a large majority of the usual tasks that you will likely be handling on your web server.

If you’re already starting out with a base CentOS 7 installation then you already have the first component out of the way: Linux. We will begin with the next component, the Apache installation.

Step one: Apache

Apache is the most widely adopted HTTP (web) server, and is used to power a large majority of the websites on the internet. It is lightweight, so it doesn’t use many of your server’s resources, but still has enough features to offer a robust solution for your web server needs.

Fortunately, installing Apache on your CentOS 7 machine is easy: simply run the following commands in your terminal (or SSH client, such as PUTTY or KITTY):

sudo yum -y install httpd

It really is that easy! This will download all of the necessary packages for Apache, and will start up the Apache service without issue.

 

However, if you browse to your server’s IP address in a web browser you will not see anything. You will need to allow http and https traffic in the server’s firewall. Running the following commands will allow http/s traffic through:

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

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

sudo firewall-cmd --reload

Now if you browse to your server’s IP address, you should be presented with the Apache testing page.

Step two: MySQL

While the “M” in LAMP often refers to MySQL, it can be substituted for any other similar relation database package. MySQL is simply the most common. Today we will be installing a MySQL alternative -- MariaDB -- as it already comes pre-packaged with CentOS 7. In order to install MariaDB, run the following commands:

sudo yum -y install mariadb-server

The above command will install MariaDB. Remember to start the service using the following command:

systemctl start mariadb

However, you won’t want to deploy a web server with just a basic installation. You’ll want to harden and secure that installation using the following command:

sudo mysql_secure_installation

The above command runs through several sequences asking you some basic questions, such as setting a root MySQL password, removing remote root access, deleting pre-installed databases, etc. For our purposes, you can just enter “Yes” for all of them. Once that has been done, restart MariaDB to ensure that all of the changes take effect:

sudo systemctl restart mariadb

You are now running MariaDB. There is only one more step left before completing the setup of your web server.

Step three: PHP

As mentioned earlier, the last component of a LAMP stack is PHP. Once again this is made extremely easy on CentOS 7. Simply run the following command:

sudo yum -y install php php-mysql

You now have PHP installed! You’ll need to restart Apache in order for PHP to begin working. Run the following command:

sudo systemctl restart httpd

If you want to test to make sure that PHP is working properly on your server, you’ll need to create a simple PHP file. Run the following commands:

cd /var/www/html
touch phpinfo.php

That will create an empty file named phpinfo.php. Open the file by running the following command:

vi phpinfo.php

Once you have it open, you’ll need to enter the following code:

 

<?php
phpinfo();
?>

 

Save the file, then browse to http://your_ip_here/phpinfo.php

You should now see a basic PHP page that shows information about your installation of PHP.

Conclusion

Now that you have Apache, MySQL and PHP installed, you simply need to run a few commands to ensure they run by default whenever the server is rebooted. Simply run the following commands:

sudo systemctl enable httpd
sudo systemctl enable mysqld

Congratulations, you have successfully configured a basic Web server with Linux, Apache, MySQL and PHP!