How to Install the LAMP Stack on CentOS 7

Introduction

How to Install the LAMP Stack on CentOS 7

1. Install Apache

First, update the packages list:

sudo yum update

Then, install the Apache web server:

sudo yum install httpd

Once the installation is complete, start the Apache service and enable it to start on boot:

sudo systemctl start httpd.service

sudo systemctl enable httpd.service

2. Install MariaDB

Install the MariaDB server and client packages:

sudo yum install mariadb-server mariadb

Once the installation is complete, start the MariaDB service and enable it to start on boot:

sudo systemctl start mariadb

sudo systemctl enable mariadb

Run the mysql_secure_installation script to improve the security of your MariaDB installation:

sudo mysql_secure_installation

3. Install PHP

Install the PHP packages:

sudo yum install php php-mysql

Once the installation is complete, restart the Apache service to apply the changes:

sudo systemctl restart httpd.service

You can now verify that the LAMP stack is working by creating a info.php file in the /var/www/html directory:

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

Add the following content to the file:

Save and close the file.

Now, you can access the info.php file in your web browser using the server domain name or IP address:

http://your_server_ip_or_domain/info.php

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.

how to install lamp stack on centos

Prerequisites

  • Access to a user account with sudo or root privileges
  • A terminal window or command line
  • The yum and RPM package managers, included by default

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

Step 2: Install the Apache Web Server

As you already have a CentOS operating system running, the first step of assembling the LAMP stack is to install the web server. The simplest way to install Apache is through CentOS’s native package manager, yum.

1. Install Apache on Centos with:

sudo yum install httpd

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

The output will show the package httpd package was installed as in the image below:

CentOS running output httpd packages

2. Next, start Apache by running the following command:

sudo systemctl start httpd.service

3. 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:

apache http welcome page

4. 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.

1. Install MariaDB with the command:

sudo yum install mariadb-server mariadb

When a y/n prompt appears, confirm with y.

2. 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.

1. Begin by typing the command:

sudo mysql_secure_installation

2. 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.

3. 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

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

mysql successful installation cleaning up

5. Lastly, enable MariaDB to start up when you boot the system:

sudo systemctl enable mariadb.service

Step 5: Install PHP

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.

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

sudo yum install php php-mysql

Now you should get a Y/n prompt allowing you to confirm the installation, by entering Y.

2. 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/

By using the nano editor, you can go into this directory and run a test of PHP on the CentOs 7 server.

1. To install the editor, use this command:

sudo yum install nano

2. Use a basic PHP script to make an info.php file, with the command:

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

3. This opens a blank text file in which you should copy and paste the following:

<?php

phpinfo ();

?>

4. Hold CTRL+X (to exit) and Y and Enter (to save changes, and close the file).

5. Check whether PHP is working by visiting the following URL:

http://ip_address/info.php

The ip_address should be the public IP address of your server. If PHP is set up correctly you will see this image on the browser:

php running version 5.4

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

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

Following with the command to open it for HTTPS traffic:

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

Finally, restart the firewall to enable the new settings:

sudo firewall-cmd --reload

Step 7: Install PHP Modules

To optimize PHP’s capabilities, look at the names and descriptions of optional modules with the command:

yum search php-

To get detailed, plain-language information about what each module does, view a longer description with: yum info followed by a space and the module name.

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

Step 8: Restart Apache

In order for the changes to take effect, restart the Apache service with the command:

sudo systemctl restart apache2

Conclusion

By following this guide, you learned how to install each layer of the LAMP stack on CentOS. Now you are ready to explore all the innovations the LAMP stack makes possible.

How to Install the LAMP Stack on CentOS 7

The LAMP stack is a popular open source web platform commonly used to run dynamic web sites and servers. It is composed of four components: Linux, Apache, MySQL, and PHP. In this tutorial, we will show you how to install the LAMP stack on a CentOS 7 server.

Prerequisites

Before you begin, you will need:

  • A CentOS 7 server with a non-root user with sudo privileges.
  • A domain name pointed to your server’s public IP address.

Step 1 — Installing Apache

The first component of the LAMP stack we will install is Apache. Apache is a popular open source web server that can be used to serve both static and dynamic content.

We can install Apache using the yum package manager. First, update the package index:

sudo yum update

Once the package index is updated, we can install Apache:

sudo yum install httpd

Once the installation is complete, start the Apache service and enable it to start on boot:

sudo systemctl start httpd
sudo systemctl enable httpd

You can now verify that Apache is running by visiting your server’s public IP address in your web browser. You should see the Apache test page, which looks like this:

Apache Test Page

Now that Apache is installed, we can move on to the next step.

Step 2 — Installing MySQL

The next component of the LAMP stack we will install is MySQL. MySQL is a popular open source relational database management system.

We can install MySQL using the yum package manager. First, update the package index:

sudo yum update

Once the package index is updated, we can install MySQL:

sudo yum install mysql-server

Once the installation is complete, start the MySQL service and enable it to start on boot:

sudo systemctl start mysqld
sudo systemctl enable mysqld

Now that MySQL is installed, we can move on to the next step.

Step 3 — Installing PHP

The final component of the LAMP stack we will install is PHP. PHP is a popular open source scripting language used for creating dynamic web pages.

We can install PHP using the yum package manager. First, update the package index:

sudo yum update

Once the package index is updated, we can install PHP:

sudo yum install php php-mysql

Once the installation is complete, restart the Apache service:

sudo systemctl restart httpd

Now that PHP is installed, we can move on to the next step.

Step 4 — Testing the LAMP Stack

Now that the LAMP stack is installed, we can test it to make sure it is working properly. We can do this by creating a test PHP file in the Apache document root.

First, open the Apache document root in your text editor:

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

Add the following lines to the file:

<?php
phpinfo();
?>

Save and close the file. Now, we can test the LAMP stack by visiting this file in our web browser. Visit http://your_server_ip/info.php, replacing your_server_ip with your server’s public IP address. You should see a page that looks like this:

PHP Info Page

If you see this page, then the LAMP stack is working properly. You can now start using it to serve dynamic web content.

Conclusion

In this tutorial, we have shown you how to install the LAMP stack on a CentOS 7 server. Now that the LAMP stack is installed, you can start using it to serve dynamic web content.

Jaspreet Singh Ghuman

Jaspreet Singh Ghuman

Jassweb.com/

Passionate Professional Blogger, Freelancer, WordPress Enthusiast, Digital Marketer, Web Developer, Server Operator, Networking Expert. Empowering online presence with diverse skills.

jassweb logo

Jassweb always keeps its services up-to-date with the latest trends in the market, providing its customers all over the world with high-end and easily extensible internet, intranet, and extranet products.

Contact
San Vito Al Tagliamento 33078
Pordenone Italy
Item added to cart.
0 items - 0.00
Open chat
Scan the code
Hello 👋
Can we help you?