0% found this document useful (0 votes)
4 views5 pages

Install Apache and Connect To MySQL RDS

The document outlines the steps to set up an Apache web server on Amazon Linux, including installing necessary packages, configuring file permissions, and connecting to an RDS database. It provides instructions for creating a PHP script to manage employee data, including adding and displaying employee information from a MySQL database. The document also includes code snippets for database connection and table management functionalities.

Uploaded by

raj0000kaml
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views5 pages

Install Apache and Connect To MySQL RDS

The document outlines the steps to set up an Apache web server on Amazon Linux, including installing necessary packages, configuring file permissions, and connecting to an RDS database. It provides instructions for creating a PHP script to manage employee data, including adding and displaying employee information from a MySQL database. The document also includes code snippets for database connection and table management functionalities.

Uploaded by

raj0000kaml
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Use Amazon Linux

[ec2-user ~]$ sudo yum update

[ec2-user ~]$ sudo yum install -y httpd24 php56 php56-mysqlnd

[ec2-user ~]$ sudo service httpd start

[ec2-user ~]$ sudo chkconfig httpd on

To set file permissions for the Apache web server

[ec2-user ~]$ sudo groupadd www


[ec2-user ~]$ sudo usermod -a -G www ec2-user
[ec2-user ~]$ exit
[ec2-user ~]$ groups
ec2-user wheel www

[ec2-user ~]$ sudo chown -R root:www /var/www

[ec2-user ~]$ sudo chmod 2775 /var/www


[ec2-user ~]$ find /var/www -type d -exec sudo chmod 2775 {} +

[ec2-user ~]$ find /var/www -type f -exec sudo chmod 0664 {} +

Connect your Apache web server to your RDS DB instance

[ec2-user ~]$ cd /var/www


[ec2-user ~]$ mkdir inc
[ec2-user ~]$ cd inc

[ec2-user ~]$ >dbinfo.inc


[ec2-user ~]$ nano dbinfo.inc

<?php
define('DB_SERVER', 'endpoint');
define('DB_USERNAME', 'huzefa');
define('DB_PASSWORD', 'password');
define('DB_DATABASE', 'sansdb');

?>

Save and close the dbinfo.inc file.

Change the directory to /var/www/html:

[ec2-user ~]$ cd /var/www/html

Create a new file in the html directory named


SamplePage.php, and then edit the file by calling nano
(or the editor of your choice).

[ec2-user ~]$ >SamplePage.php


[ec2-user ~]$ nano SamplePage.php

<?php include "../inc/dbinfo.inc"; ?>


<html>
<body>
<h1>Sample page</h1>
<?php

/* Connect to MySQL and select the database. */


$connection = mysqli_connect(DB_SERVER, DB_USERNAME, DB_PASSWORD);

if (mysqli_connect_errno()) echo "Failed to connect to MySQL: " .


mysqli_connect_error();

$database = mysqli_select_db($connection, DB_DATABASE);

/* Ensure that the Employees table exists. */


VerifyEmployeesTable($connection, DB_DATABASE);

/* If input fields are populated, add a row to the Employees table. */


$employee_name = htmlentities($_POST['Name']);
$employee_address = htmlentities($_POST['Address']);

if (strlen($employee_name) || strlen($employee_address)) {
AddEmployee($connection, $employee_name, $employee_address);
}
?>

<!-- Input form -->


<form action="<?PHP echo $_SERVER['SCRIPT_NAME'] ?>" method="POST">
<table border="0">
<tr>
<td>Name</td>
<td>Address</td>
</tr>
<tr>
<td>
<input type="text" name="Name" maxlength="45" size="30" />
</td>
<td>
<input type="text" name="Address" maxlength="90" size="60" />
</td>
<td>
<input type="submit" value="Add Data" />
</td>
</tr>
</table>
</form>

<!-- Display table data. -->


<table border="1" cellpadding="2" cellspacing="2">
<tr>
<td>ID</td>
<td>Name</td>
<td>Address</td>
</tr>

<?php

$result = mysqli_query($connection, "SELECT * FROM Employees");

while($query_data = mysqli_fetch_row($result)) {
echo "<tr>";
echo "<td>",$query_data[0], "</td>",
"<td>",$query_data[1], "</td>",
"<td>",$query_data[2], "</td>";
echo "</tr>";
}
?>

</table>

<!-- Clean up. -->


<?php

mysqli_free_result($result);
mysqli_close($connection);

?>

</body>
</html>

<?php

/* Add an employee to the table. */


function AddEmployee($connection, $name, $address) {
$n = mysqli_real_escape_string($connection, $name);
$a = mysqli_real_escape_string($connection, $address);

$query = "INSERT INTO `Employees` (`Name`, `Address`) VALUES ('$n',


'$a');";

if(!mysqli_query($connection, $query)) echo("<p>Error adding employee


data.</p>");
}

/* Check whether the table exists and, if not, create it. */


function VerifyEmployeesTable($connection, $dbName) {
if(!TableExists("Employees", $connection, $dbName))
{
$query = "CREATE TABLE `Employees` (
`ID` int(11) NOT NULL AUTO_INCREMENT,
`Name` varchar(45) DEFAULT NULL,
`Address` varchar(90) DEFAULT NULL,
PRIMARY KEY (`ID`),
UNIQUE KEY `ID_UNIQUE` (`ID`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=latin1";

if(!mysqli_query($connection, $query)) echo("<p>Error creating


table.</p>");
}
}

/* Check for the existence of a table. */


function TableExists($tableName, $connection, $dbName) {
$t = mysqli_real_escape_string($connection, $tableName);
$d = mysqli_real_escape_string($connection, $dbName);

$checktable = mysqli_query($connection,
"SELECT TABLE_NAME FROM information_schema.TABLES WHERE TABLE_NAME
= '$t' AND TABLE_SCHEMA = '$d'");

if(mysqli_num_rows($checktable) > 0) return true;

return false;
}
?>

Save and close the SamplePage.php file.

select * from Employees

You might also like