LAMP configuration on rhel5

Satish Tiwary's picture
LAMP

LAMP Stands for Linux Apache Mysql and PHP

L- Linux

A- Apache

M- Mysql

P-PHP(Personal Home Page) or Hypertext Preprocessor

LAMP is useful for all those who wants to design,develope,host website or web based application on LINUX Platform. Keeping PHP Programmers in mind who are new to Linux this article has been written.This article will help PHP Developer as well as Designer to work smoothly on Linux Platform just by understanding the concept of building and configuring Apache in Linux for PHP and PHP Mysql connectivity.So here we learn how to configure Apache Mysql to build a web site or web application using PHP.

slashroot.in has configured and tested LAMP on RHEL5.4/Centos5

So What We Learn in this Article?

1.LAMP Package installation.

2.LAMP configuration.(Apache Mysql Configuration for PHP on Linux)

3.How to run PHP Scripts on Linux.

4.All Possible way to write a PHP Script.

5.Mysql Server Configuration and Sql Query.

6.MYSQL-PHP Connectivity.

7.How to write PHP+HTML code in Linux.

8.How to write HTML Code inside PHP.

9.How to write PHP HTML Combined Coding.

10.How to install PHP.

11.How to configure PHP.

 

Stepwise explanation of LAMP Configuration:

 

Step1- Login to a Linux system here that system is RHEL5.4

Step2- configure yum server for package installation

Let's see  yum server configuration in brief here

copy Server folder/directory from your RHEL5 DVD/ IMAGE you have any where in your linux machine. I am going to copy it on Desktop of root.

now make a file satish.repo inside /etc/yum.repos.d/  directory.

#vim /etc/yum.repos.d/satish.repo
[satish]
baseurl=file:///root/Desktop/Server
gpgcheck=0

now save file and exit

:wq!

and now yum server is ready for use.

You can Read here YUM SERVER TUTORIAL.

Step3- Install and Configure Apache Server

Since PHP is a server side scripting language, we need a web server to run php programs or scripts.So to make it possible on a Linux Machine here we are going to configure Web Server(Apache).Basic configuration of apache server become quite easy once you configure yum server properly.In below steps you will learn how to configure apache server in few simple steps.Which packages to install and which service to start and what is it's configuration file and where to make change.

 

#yum install httpd*       ====>> this will install required packages for apache.

#service httpd restart    ====>> this will restart the apache service temporarily.

#chkconfig httpd on       =====>>this will on httpd service permanently.

Step4- Install and Configure Mysql Server

#yum install mysql*             ===========>> This will install mysql server required packages with dependencies.

#service mysqld restart         ===========>> This will restart mysql service.

#chkconfig mysqld on            ===========>> This will enable mysql services for permanent use.

(A)Now we need to change mysql password for user root.

#mysql

>USE mysql;

>UPDATE user SET password=PASSWORD('redhat123456') WHERE user='root';

>FLUSH PRIVILEGES;

>quit

note: in above command root is user and mysql password for root user is redhat123456.

(B)Now check ur above configuration by logging to mysql

#mysql -u root -p

password:redhat123456

if you get login then it's working and everything is fine.

 

Step5- Now install and configure PHP with apache server.

#yum install php*       ========>>this will install all php packages with dependency.

 

Configure Apache for running PHP Script.

This is the configuration file of http where we need to make changes so that bydefault our web server read those pages which we tells them to read and we also tells them to read files from a given directory only.We can assign site name here and decide where to store log files.

Go to last line of apache configuration file /etc/httpd/conf/httpd.conf and add the following line.

 

<VirtualHost 192.168.1.1:80>
#    ServerAdmin webmaster@dummy-host.example.com
    DocumentRoot /var/www/html
    DirectoryIndex  lamp.php
    ServerName satish.com
#    ErrorLog logs/dummy-host.example.com-error_log
#    CustomLog logs/dummy-host.example.com-access_log common
</VirtualHost>

 

Then Go to your Document root Directory and create your php script there.

#cd /var/www/html    ========>>go to apache web directory where we create php pages.

#vim lamp.php          =========>> php scripts with name lamp.php

<?php

phpinfo();

?>

save the file and exit

phpinfo is a PHP utility function which  display php configuration.

Step6- Now LAMP is configured in your system.

To check it open your browser

and type following inside the URL  http://localhost

or type  http://ipaddress of your linux system  

like http://192.168.1.1   or http://satish.com

 

Note:the php script you have run above or any PHP Script are never get affected by the Browsers.As we all know PHP is a Server Side Scripting Language so it is always affected by the type of Server not Browser.We always need a Server to run a PHP script.

 

           PHP Installation and Configuration:

How to install PHP in Linux?

[root@satish ~]# yum install php*


 

How to Configure PHP?

Configuration file for PHP in linux is /etc/php.ini.Now we can do many changes as per our requirement in php configuration file.Every changes for php is made inside this php configuration file.

1.To increase the memory limit.

memory_limit = 256M

2.to increase maximum execution time for a php program.

max_execution_time = 210

3.To increase maximum upload size of a php script.

max_upload_size = 100M

In this way you can do so many changes inside your php configuration file according to your need.

 

ALL THE POSSIBLE WAY TO WRITE  A PHP SCRIPT.

here i have mentioned all the possible ways to write a php program.Now it depend on you guys to choose which method you want to go with.

method1:

<?

your PHP Code here.

?>

 

method2:

 

<?php

OUR PHP CODE

?>

method3:

 

<?php

PHP CODE


php?>

method4:

 

<script language="php">

then write code here.

</script>

HOW to write PHP HTML both codes together in a File?

Now if someone wants to write both html and php in a single file he/she can do that.You can see below script and learn how to write both php and html codes together in a single file.

#vim satish.php

<html>
<head>
<title>HTML PHP COMBINE CODE</title>
</head>
<body>
<h1>this code will show you how to write html and php code in a single file</h>
</body>
</html>

<?php
print "hello";
<?

HOW to Write HTML code inside PHP Script?

Now here is another method to write these two(html and php)together.Here we learn how to write html codes inside a php script.Below is a simple example of making you learn writing html codes inside php program. see it, then write your own and test it.

#vim satish.php

<?php

echo "<html>";

echo "<body>";

echo "<title> Learn how to write html inside php. </title>";

echo "<h1> HTML inside PHP </h1>";

echo"</body>";

echo "</html>";

?>

PHP HTML Combined Coding:

This is another method to write combined coding of html and php and is very Easy to learn and implement.

#vim tiwary.php

<html>

<head>

<title>php html combined codes</title>

</head>

<body>

<?php

here we write PHP Codes

?>

</body>

</html>

                                      MYSQL-PHP

Lets see the easiest way to installl,configure and run mysql on Linux.

[root@satish ~]# yum install mysql*

[root@satish ~]# service mysqld restart

[root@satish ~]# mysql_secure_installation




NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MySQL
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!


In order to log into MySQL to secure it, we'll need the current
password for the root user.  If you've just installed MySQL, 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):
OK, successfully used password, moving on...

Setting the root password ensures that nobody can log into the MySQL
root user without the proper authorisation.

Set root password? [Y/n] y
New password:
Re-enter new password:
Password updated successfully!
Reloading privilege tables..
 ... Success!


By default, a MySQL installation has an anonymous user, allowing anyone
to log into MySQL without having to have a user account created for
them.  This is intended only for testing, and to make the installation
go a bit smoother.  You should remove them before moving into a
production environment.

Remove anonymous users? [Y/n] y
 ... Success!

Normally, root should only be allowed to connect from 'localhost'.  This
ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n] y
 ... Success!

By default, MySQL comes with a database named 'test' that anyone can
access.  This is also intended only for testing, and should be removed
before moving into a production environment.

Remove test database and access to it? [Y/n] y
 - Dropping test database...
 ... Success!
 - Removing privileges on test database...
 ... Success!

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

Reload privilege tables now? [Y/n] y
 ... Success!

Cleaning up...



All done!  If you've completed all of the above steps, your MySQL
installation should now be secure.

Thanks for using MySQL!



Now Login to mysql server by user root and the password we have assigned to root user above.

[root@satish ~]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.0.77 Source distribution

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| tiwary             |
+--------------------+
3 rows in set (0.01 sec)

mysql> quit
Bye
[root@satish ~]#

Now Learn How to Create and Use database in MYSQL.

mysql> create database linux;
Query OK, 1 row affected (0.01 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| linux              |
| mysql              |
| tiwary             |
+--------------------+
4 rows in set (0.00 sec)

mysql> use linux;
Database changed
mysql> show tables;
Empty set (0.00 sec)

mysql> exit
Bye
[root@satish ~]#


 

We have created a database named linux in above code.

 PHP connectivity with mysql

Now in LAMP or in web developing field php connectivity with your database is one of most important task.here we learn how to connect php script with our database.Here in LAMP our database in use is Mysql.

[root@satish html]# vim index.php

<html>
<head>
<title>Checking database Connection  with PHP</title>
</head>
<body>


<?php
$dbhost='localhost:3306';
$dbuser='root';
$dbpass='redhat';


$conn=mysql_connect($dbhost,$dbuser,$dbpass);
if(! $conn)


{
die('not possible to connect:' . mysql_error());
}


echo 'Successfully connected with database<br/>;


?>


</body>
</html>

              

 

now to check you can go to your browser and type http://localhost in  URL.

 

Explanation of the above Code:

$dbhost='localhost:3306';  ===>>here localhost is hostname and 3306 is mysql port.

$dbuser='root';            ===>>here root is the username of mysql database.
$dbpass='redhat';          ===>>here redhat is the password of user root.
Rate this article: 
Average: 5 (14 votes)

Add new comment

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Type the characters you see in this picture. (verify using audio)
Type the characters you see in the picture above; if you can't read them, submit the form and a new image will be generated. Not case sensitive.