Introduction
The LAMP stack is a bundle consisting of a Linux operating system, an Apache server, a MySQL (MariaDB) database, and the PHP programming language. Each layer of the stack represents an open-source software required for developing web applications.
In this tutorial you will learn how to install the LAMP stack on CentOS 7.
Prerequisites
- Deploy a Cybree Instance with CentOS 7 image.
- Connect to your instance via Cybree VNC terminal, or use other terminal (such as Putty SSH) to connect your instance
- Login to account with sudo or root privileges. The default Centos administrator login name is "root"
Step 1: Update Package Repository Cache
- Before you start building the stack, be sure to update the packages on your CentOS 7 server using the command:
sudo yum update -y
- Wait a few minute to download and process until it prompts "Complete!"
Step 2: Install the Apache Web Server
The first step of assembling the LAMP stack is to install the Apache web server through CentOS's native package manager, yum.
- Install Apache on Centos with:
sudo yum install httpd
- Enter "y" when you are prompted to confirm the installation. The output will show the package httpd package was installed as in the image below:
- Next, start Apache by running the following command:
sudo systemctl start httpd.service
- Check whether the service is running by going to your server's public IP address. The browser should display the test CentOS 7 Apache web page:
- Finally, set up Apache to start at boot:
sudo systemctl enable httpd.service
Step 3: Install MySQL (MariaDB) and Create a Database
To organize and store data for your dynamic website, you need MariaDB. This is an open-source fork of the MySQL database management system. It is a backward compatible and binary drop-in replacement for the original MySQL.
- Install MariaDB with the command:
sudo yum install mariadb-server mariadb
-
When a y/n prompt appears, confirm with y.
-
Now start MariaDB using the command:
sudo systemctl start mariadb
Step 4: Run MySQL Security Script
MariaDB does not have secure settings by default. Therefore, you need to configure settings, test the database, and remove anonymous users.
- Begin by typing the command:
sudo mysql_secure_installation
-
You will be prompted to provide your MariaDB root password (this is not the root password for your server). As you do not have a password yet, pressing Enter allows you to continue configuration.
-
Next, it will ask you a series of queries. To ensure your database is protected, answer the questions as follows:
- Set root password? [y/n] Y
- New password: Type in a password you would like to use
- Re-enter new password: Retype the password from the previous field
- Remove anonymous users? [y/n] Y
- Disallow root login remotely? [y/n] Y
- Remove test database and access to it? [y/n] Y
- Reload privilege tables now? [y/n] Y
- After answering the questions, the output will display a message that your system is cleaning up, and the installation should now be secure.
- Lastly, enable MariaDB to start up when you boot the system:
sudo systemctl enable mariadb.service
Step 5: Install PHP 5.6
As a server-side scripting language, PHP is the part of the LAMP grouping that processes the code for showing dynamic content. Once it is connected with the MySQL database, PHP will be retrieving information and processing it for the Apache webserver to display.
- First, you must add the Webtatic EL yum repository information corresponding to your CentOS version to yum:
rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
- Install the MySQL extension along with PHP, again using the yum package installer, with the command:
sudo yum install php56w php56w-mysql
-
Enter "y" when you are prompted to confirm the installation.
-
To have your Apache webserver start co-working with PHP, restart the server:
sudo systemctl restart httpd.service
Step 6: Test PHP Processing
To locate and serve the website, Apache must save the file to the web root. Apache places its default website in this directory: /var/www/html/
- In case nano editor is not installed yet, use this command to install nano
sudo yum install nano
- Use a basic PHP script to make an info.php file, with the command:
sudo nano /var/www/html/info.php
- This opens a blank text file in which you should copy and paste the following:
<?php
phpinfo ();
?>
-
Hold CTRL+X (to exit) and Y and Enter (to save changes, and close the file).
-
In order for the changes to take effect, restart the Apache service with the command:
sudo systemctl restart httpd.service
- Check whether PHP is working by visiting the following URL on your browser: http://[ip_address]/info.php
The [ip_address] should be the public IP address of your Cybree instance. You can look it up on your Cybree console. If PHP is set up correctly you will see this image on the browser:
Conclusion
By following this guide, you learned how to install each layer of the LAMP stack on CentOS 7. Now you are ready to explore all the innovations the LAMP stack makes possible.