How to install LAMP on CentOS 7

Giteqa

Introduction

The LAMP stack is a package consisting of the Linux operating system, Apache server, MySQL database (MariaDB), and the PHP programming language. Each layer of the stack represents the open-source software needed to develop web applications.

What do you need to get started?

Access to user account with sudo privileges or root.
Terminal window or command line.
The yum and RPM package managers are enabled by default.

Step 1. Refresh the package repository cache

Before starting building the stack, be sure to update the packages on your CentOS 7 server using the command:

sudo yum update

Step 2. Install the webserver Apache

Since you already have a CentOS operating system, the first step in building a LAMP stack is to install a web server.
The easiest way to install Apache is to use CentOS's own package manager, yum.

1. Install Apache on Centos with:

sudo yum install httpd

When prompted, confirm that you are running the command with sudo privileges.

The output will show that the httpd package was installed successfully, as shown in the image below:

2. Then start Apache by running the following command:

sudo systemctl start httpd.service

3. Check if the service is running by going to the public IP address of your server, in my case, it is 185.163.45.1xx.
The browser should display the CentOS 7 Apache test web page:

4. Finally, configure 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.
It is a fork of the open-source MySQL database management system.

1. Install MariaDB using the command:

sudo yum install mariadb-server mariadb

When prompted for y/n, confirm with y.

2. Now start MariaDB with the command:

sudo systemctl start mariadb

Step 4. Run MySQL Security Script

MariaDB has no secure settings by default.
Hence, you need to tweak the settings, test the database, and delete the anonymous users.

1. Start by entering the command:

sudo mysql_secure_installation

2. You will be prompted for the root password for MariaDB (this is not the root password for your server).
Since you don't have a password yet, pressing Enter allows you to continue with the setup.

4. Then he will ask you a series of questions. To keep your database secure, please answer the following questions:
Set root password? [Y/n] Y
New password: enter the password you want to use.
Re-enter new password: enter the password from the previous field again.
Remove anonymous users? [Y/n] Y
Prevent remote root login? [Y/n] Y
Do you want to delete and access the test database? [Y/n] Y
Reload privilege tables now? [Y/n] Y

5. After answering the questions, the output will display a message that your system is being cleaned up and the installation should now be secure.

Finally, allow MariaDB to start at system boot:

sudo systemctl enable mariadb.service

Step 5. Install PHP

As a server-side scripting language, PHP is part of the LAMP group that processes code to render dynamic content.
Once connected to the MySQL database, PHP will retrieve the information and process it for display on the Apache webserver.

1. Install the MySQL extension along with PHP, again using the yum package installer, with the command:

sudo yum install php php-mysql

You should now receive a Y/n prompt, allowing you to confirm the installation by entering Y.

2. To make your Apache web server coexist with PHP, restart the server:

sudo systemctl restart httpd.service

Step 6. Test PHP processing

In order to find and serve a website, Apache must save the file in the webroot.
Apache places its default website in this directory: /var/www/html/

Using nano or vim editor, you can go to this directory and run a PHP test on CentOS 7 server.

1. To install vim editor use this command:

sudo yum install vim

2. Use a basic PHP script to create the info.php file with the command:

sudo vim /var/www/html/info.php

3. An empty text file will open, in which you have to press the I button, copy, and paste the following:

<?php
phpinfo ();
?>

4. Press ESC (to exit), SHIFT + Q and enter qw + ENTER (to save changes and close the file).

5. Check if PHP is working by opening the following address:

http://ip_address/info.php

ip_address this is the public IP address of your server that your ISP gave you, in my case it is Mivocloud.
If PHP is configured correctly, you will see this image in your browser:

6. Install the firewall using the following command:

sudo yum install iptables-services

After that, we execute the following commands responsible for startup, startup on startup and information about the firewall:

sudo systemctl start iptables
sudo systemctl enable iptables
sudo systemctl status iptables

6. If a firewall is enabled, you will need to open a route for HTTP traffic. Use the command:

sudo iptables -I INPUT 1 -p tcp -m tcp --dport 80 -m state --state NEW -j ACCEPT

Then run the command to open it for HTTPS traffic:

sudo iptables -I INPUT 1 -p tcp -m tcp --dport 443 -m state --state NEW -j ACCEPT

Finally, save the changes to the firewall and restart it to activate the new settings:

service iptables save
service iptables restart

Step 7. install PHP modules
To optimize PHP functionality, view the names and descriptions of additional modules using the command:

yum search php-

For detailed information in understandable language about what each module does, see the longer description with: yum info followed by a space and the name of the module.

Install the optional package with sudo yum install followed by a space and the module name.

Step 8. Restart Apache

Restart the Apache service for the changes to take effect:

sudo systemctl restart httpd

Conclusion

By following this tutorial, you've learned how to install each LAMP stack level on CentOS. You are now ready to explore all the innovations that the LAMP stack makes. if you want to start testing the LAMP stack, then you only need a VDS server, on equipment from Mivocloud everything works quickly and without interruption.