A LAMP stack is the standard foundation upon which websites are built. It is made up of four components that grant its name - Linux, Apache, MySQL (in this case, MariaDB), and PHP. Linux is the operating system, Apache is the web server, MySQL is the database, and PHP interprets code to produce dynamic content. In this article, we will walk you through installing and configuring a basic LAMP stack.
- A Server with CentOS 7 Installed
- How to Secure Your SSH Connection
Note: We will assume that a non-root user has been configured for the purposes of this guide. If it has not, you can run all commands without sudo privileges. However, we recommend only using the root user when there is no other option. To configure a new sudo user, please check out the Creating a New Sudo User section of our How to Secure Your SSH Connection in CentOS 7 article.
Before we install Apache, we first want to ensure that our operating system is fully up to date. We can do this with the following command:
$ sudo yum update
Now that the OS is up to date, we can proceed to install Apache. Do so by entering the following command:
$ sudo yum install httpd
Note: If you have a firewall enabled, you will need to ensure that ports 80 and 443 are listening.
Finally, we need to start the Apache daemon.
$ sudo systemctl start httpd
Now type the IP address of your server into a browser to confirm that Apache has installed properly. If it has, you will see the following page:
Note: CentOS 7's default repository will install MariaDB even if you type the command to install MySQL. MariaDB is a MySQL-based database developed as a response to Oracle's acquisition of MySQL. The two databases function very similarly.
The third portion of our LAMP stack is MariaDB. To install MariaDB, enter the following command:
$ sudo yum install mariadb-server mariadb
Next, start MariaDB.
$ sudo systemctl start mariadb
Now enter the MySQL installation module using the following command:
$ sudo mysql_secure_installation
You will be met with the following prompt:
NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY!
In order to log into MariaDB to secure it, we'll need the current
password for the root user. If you've just installed MariaDB, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.
Enter current password for root (enter for none):
Since we have just installed MariaDB, we have yet to set a root password so just press the
ENTER key to proceed.
Next, you will be prompted to create a new password for your MySQL root user. Choose any password you wish and then confirm the password at the following prompt.
Set root password? [Y/n]
ENTER for each of the rest of the prompts. When you are finished you will get the following output:
All done! If you've completed all of the above steps, your MariaDB
installation should now be secure.
Thanks for using MariaDB!
In this section, we will be installing and configuring PHP. First, we need to install PHP along with its packages for Apache and MySQL using the following command:
$ sudo yum install php php-mysql
Note: If you wish to download other PHP packages, you can append them to the end of this command.
Now we need to restart Apache so that it works with PHP. Do so using the following command:
$ sudo systemctl restart httpd
Finally, we will create a test file to ensure that PHP has been installed and configured correctly. Create a new file called
info.php by opening it in your preferred text editor using the following command:
$ sudo vi /var/www/html/info.php
Enter the following text into the file:
Now navigate to the following website, replacing the x's with your server's IP address or domain name:
You should see the following page.
If you see the above page, you have correctly deployed PHP and you are done deploying your LAMP stack. Since this page gives information about your server that could be used to exploit your security, delete this file using the following command:
$ sudo rm /var/www/html/info.php
A properly deployed LAMP stack is the backbone of many websites. Having read this guide, you should now know how Apache, MySQL, and PHP are used as well as how to perform a basic installation of a LAMP stack.