Setting up LAMP ( Linux,Apache,MariaDB,PHP) stack on Redhat 7 Linux

0
354


This tutorial will show how to setup a basing LAMP environment on RHEL7. This guide is logically split into three parts:

  • Installation of Apache Webserver
  • Installation of PHP support
  • Installation of MariaDB database

Each section will guide you step by step and will also provide simple tests to test each above step.
NOTES:
The default database MySQL was on replaced by MariaDB fork. It uses same php-mysql module to talk to MariaDB database from php script.

 

1. Installation of Apache Webserver

First we need to install Apache webserver. The package and service is called httpd:

[root@rhel7 ~]# yum install httpd

Start httpd service:

[root@rhel7 ~]# service httpd start
Redirecting to /bin/systemctl start  httpd.service

Enable firewall to allow http port 80 access:

[root@rhel7 ~]# firewall-cmd --zone=public --add-port=80/tcp --permanent
[root@rhel7 ~]# firewall-cmd --reload

Make sure that our webserver, that is, httpd daemon starts after the reboot:

[root@rhel7 ~]# systemctl enable httpd

Create a simple html page to test whether all the above steps went according to our plan:

[root@rhel7 ~]# echo "APACHE HTML TEST" > /var/www/html/index.html

Navigate your browser to the hostname or IP address of your web server. The result should like like the image below:

index

 

2. Installation of PHP support

Once we get the basic HTML stuff working we can add PHP support. To do that simply install php package and restart our httpd daemon:

[root@rhel7 ~]# yum install php
[root@rhel7 ~]# service httpd restart

To test your PHP installation create a file /var/www/html/index.php with the content below:

<?php
    phpinfo();
?>

Once again navigate your browser to the hostname or IP address of your web server and point it to your new php file. If all went well, you should see page similar to the one below:

php

 

 

 

 

3. Installation of MariaDB database

As a last step we will install the last part of our RHEL 7 LAMP stack which is MariaDB database. We also need to talk to database from our php scripts so from this reason we also need to install php-mysql module:

[root@rhel7 ~]# yum install php-mysql mariadb-server
[root@rhel7 ~]# service httpd restart

Start MariaDB database and enable its start after reboot:

[root@rhel7 ~]# service mariadb start
[root@rhel7 ~]# systemctl enable mariadb

All is ready to perform our first LAMP stack test. Create a following file /var/www/html/connect.php with the following content:

<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "lamp";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
    die("Database connection failed: " . $conn->connect_error);
}
echo "Success. Connected to database";
?>

Navigate your browser to your webserver and to the new connect.php file. The result should look like the one below:

connect

 

The reason why the connection to the database failed is because we have not created any database yet. Let’s create database called lamp and retry our test:

[root@rhel7 ~]# mysql -u root -e "create database lamp"

NOTE: If this is your fresh MariaDB installation you can access your database without password.

When you now refresh your browser your page should look like the one below which confirms that our RHEL 7 LAMP stack is ready: connectok

 

 

4. Troubleshooting

If you get a following error:

Database connection failed: Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)

make sure that you have started your MariaDB server:

[root@rhel7 ~]# service mariadb start

Furthermore, the below error message:

Sep 04 17:20:12 rhel7 httpd[2234]: AH00557: httpd: apr_sockaddr_info_get() failed for rhel7
Sep 04 17:20:12 rhel7 httpd[2234]: AH00558: httpd: Could not reliably determine 
the server's fully qualified domain name, using 127.0.0.1. Set the 'Server...his message

The fix to the above error message is:

[root@rhel7 ~]# echo 'ServerName 127.0.0.1' >> /etc/httpd/conf/httpd.conf
[root@rhel7 ~]# service httpd restart

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.