Wednesday, 22 February 2017

Create a php project which retrieve records from AWS RDS Database instances running on AWS EC2 instances

EC2 Instance RDS Instance ---------------------------------------------------------------- Instance type t2.micro (Free tier) db.t2.micro (Free tier) Availability zone us-east-1 us-east-1 Security group DemoSG DemoSG IAM role RDSfullaccess-role ---------- Root volume 8 GB SSD 8GB SSD Public IP address Yes Yes VPC ID and Subnet Default Default -------------------------------------------------------------- Security Group (DemoSG): -------------------------------------------------------------- inbound : open http (80), https (443), ssh (22) and MySQL/Aurora (3306) Outbound: All traffic IAM role: Attach AmazonRDSReadOnlyAccess policy -------------------------------------------------------------- Test the RDS Database connection from the Linux/Ubuntu Terminal --------------------------------------------------------------- Install the NETCAT software $sudo yum install nc Test the RDS connection $nc -zv <DB-instance-endpoint> <port> (Syntax) --------------------------------------------------------------- Steps to install Apache Webserver on Centos 7: --------------------------------------------------------------- step1: Connect to your EC2 instance and install the Apache web server $ sudo yum -y install httpd step2: About service. $ sudo systemctl start httpd [start the apache server] $ sudo systemctl enable httpd [apche start at boot] $ sudo systemctl status httpd [check the status of httpd] $ sudo systemctl stop httpd [stop the appache] step3: Create a sample html file [index.html] $ vi /var/www/html/index.html <html> <title>Demo Page </title> <body> <h1>Welcome to Apace Webserver! </h1></body> </html> step4: Open a browser window and enter the URL to access the file (It is the public DNS name of the EC2 instance followed by the file name) http://EC2-instance-public-DNS Step5: Add the www group to your instance $sudo group add www step7: Add your user (in our case centos) to the www group sudo usermod -a -G www centos Logout and connect the instance onces again step8:Change the group ownership of /var/www and connects to the www group sudo chown -R root:www /var/www step9: Recursively change the directory permissions of /var/www and its subdirectories to add group Write permissions to set the group ID on future subdirectories sudo chmod 2775 /var/www $find /var/www -type d -exec sudo chmod 2775 {}\; step10: Recursively change the file permissions of /var/www and its subdirectories to add group write Permissions $find /var/www -type f -exec sudo chmod 0664 {}\; ---------------------------------------------------------------------- Install the PHP, extensions and configure the php.ini ----------------------------------------------------------------------- step1: Connect to the EC2 Instance and install the PHP $sudo yum install php Step2: Restart the httpd service $sudo systemctl restart httpd.service Step3: Install the PHP extensions or Modules $sudo yum search php- (display all the php packages) $sudo yum install php-fpm (PHP FastCGI Process Manager) $sudo yum install php-dba (A database abstraction layer module for PHP applications) $sudo yum install php-mysqlnd . (A module for PHP applications that use MySQL databases) $sudo yum install php-pdo (A database access abstraction module for PHP applications) $sudo yum install php-pgsql (A PostgreSQL database module for PHP) Step4: Add the extension in php.ini in same order and restart httpd service (/etc/php.ini) extension pdo.so extension mysqlnd.so extension pdo_sqlite.so extension pdo_mysqlnd.so extension pdo_sqlite.so extension mysqlnd_mysql.so step5: Create files in /var/www/html ------------------------------------------------------------------------- index.html ------------------------------------------------------------------------- <html> <body> <h1>Connect to the DataBase! </h1> <form action="script.php" method="post"> <input type="submit" value=&#8221;connect&#8221;/> </form> </body> </html> ----------------------------------------------------------------- dbinfo.inc ------------------------------------------------------------------ <!--php define('DB_SERVER', 'RDS_EndPoint'); define('DB_USERNAME', 'RDS_masterUsername'); define('DB_PASSWORD', 'RDS_master_password'); define('DB_DATABASE', 'RDS_database_name'); ?--> ----------------------------------------------------------------- script.php ----------------------------------------------------------------- <html> <body> <h1>DATABASE TABLE </h1> <!--php include "dbinfo.inc"; $db=DB_DATABASE; /* Connect to MySQL and select the database. */ $con = mysqli_connect(DB_SERVER, DB_USERNAME, DB_PASSWORD); if (mysqli_connect_errno()) echo "Failed to connect to MySQL: " . mysqli_connect_error(); $statement="SELECT * FROM $db.Employee"; $result=mysqli_query($con,$statement); if(!$result){ die("SQL Error:".mysqli_error($con)); } // Fetch one and one row while ($row = mysqli_fetch_array($result)) { echo $row['name'],"<br-->"; } // Free result set mysqli_free_result($result); mysqli_close($con); ?> </body> </html> Step6: To bind the address form our httpd web server with RDS instances, we need to run the following Commands: $ausearch -c 'httpd' --raw | audit2allow -M my-httpd $semodule -X 300 -i my-httpd.pp ------------------------------------------------------------------------- Logs and Important files used for debugging ------------------------------------------------------------------------ httpd: /var/log/httpd/access_log /var/log/httpd/error_log /etc/httpd/conf/httpd.conf /etc/httpd/modules PHP modules: /usr/lib64/php/modules /etc/php.ini Datadir /var/lib/mysql Socket /var/lib/mysql/mysql.sock MySQL file /etc/my.cnf ------------------------------------------------------------------------ RDS Database SQL Quieres: ------------------------------------------------------------------------- Step1: MySQL workbench download url: http://dev.mysql.com/downloads/workbench/ Step2: Install the MSI installer [windowns] $sudo apt-get install mysql-server [ubuntu] $sudo yum install mysql-server [linux] Step3: >mysql -h yourDatabaseEndpoint -P 3306 -u USERNAME -p >show databases; [Show the Databases] >use database_name; [use the following database] >Create table Employee( ID integer(3),name varchar(20),desg varchar(20),sal integer(6)); >desc Employee; Step4: >insert into Employee(ID,name,desg,sal)values(101,'fasi','CEO',80000); >insert into Employee(ID,name,desg,sal)values(102,'Raj','SSE',50000); >insert into Employee(ID,name,desg,sal)values(103,'suraj','SE',30650); >insert into Employee(ID,name,desg,sal)values(104,'prem','HW',18800); Step5: > select * from Employee; ---------------------------------------------------------------------------------- IF you want to create Docker container Convert your PHP application into Image by using Docker file below: Note: copy the /etc/php.ini file to your /var/www/html/ where your Dockerfile is there Dockerfile: ------------ FROM centos/httpd RUN yum install -y php RUN yum install -y php-mysqlnd php-fpm php-dba COPY . /var/www/html/ COPY php.ini /etc/php.ini EXPOSE 80 Run these commands: --------------------- docker build -t fasi/httpd . docker run -d -p 80:80 fasi/httpd docker exec -it <contianerID> bash Test in the browser: http://yourEC2_public_IP_addresss

No comments:

Post a Comment