0% found this document useful (0 votes)
35 views38 pages

Ass1_SW lab-III

Uploaded by

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

Ass1_SW lab-III

Uploaded by

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

SOFTWARE LAB 3

ASSIGNMENT 1

• CREATE A WEBSITE USING LAMP STACK

• LOAD BALANCING IN APACHE

• MASTER / SLAVE CONFIGURATION OF MYSQL

• WRITING JAVA PROGRAM TO STORE AND FETCH STUDENTS DATA FROM DATABASE
TASK1-CREATE A WEBSITE USING LAMP
STACK
• “LAMP” stack is a group of open-source software that is typically installed
together to enable a server to host dynamic websites and web apps.
• This term is actually an acronym.
• LAMP Stack is just Linux OS, Apache server, MySQL database query language,
PHP web programming language.
WEBSITE USING LAMP STACK

• “Some popular
alternatives are MERN
Stack and MEAN Stack.
• MERN:
MongoDB +
Express Js +
( Angular / React JS )+
Node JS
APACHE WEB SERVER

• Apache is a popular open-source, cross-platform web server that is, by


the numbers, the most popular web server in existence. It’s actively
maintained by the Apache Software Foundation.
• Some high-profile companies using Apache include Cisco, IBM,
Salesforce, General Electric, Adobe, VMware, Xerox, LinkedIn,
Facebook, Hewlett-Packard, AT&T, Siemens, eBay, and many more (
source).
• the basic job of all web servers is to accept requests from clients (e.g. a
visitor’s web browser) and then send the response to that request (e.g.
the components of the page that a visitor wants to see).
SOME COMMON FEATURES SEEN IN APACHE INCLUDE:

• .htaccess
• IPv6
• FTP
• HTTP/2
• Perl, Lua, and PHP
• Bandwidth throttling
• WebDAV
• Load balancing
• URL rewriting
• Session tracking
• Geolocation based on IP address
DEPLOY WEBSITE ON LOCAL UBUNTU
MACHINE USING LAMP STACK COMMAND
• Step 1 : Installing Apache And Updating The Firewall Configuration
• Step 2 : Installing MySQL
• Step 3 : Installing PHP
• Step 4 : How To Setup Virtual Host In Apache
• Step 5 : Testing PHP Processing on your Web Server Configuration
• Step 6 : Testing Database Connection from PHP
STEP 1 : INSTALLING APACHE AND UPDATING THE
FIREWALL CONFIGURATION

• Install Apache using Ubuntu’s package manager, apt


• sudo apt update
• sudo apt install apache2

• Adjusting the Firewall to Allow Web Traffic


• sudo ufw app list

• sudo ufw app info "Apache Full"


• Allow incoming HTTP and HTTPS traffic for this profile:
• sudo ufw allow in "Apache Full“
• http://your_server_ip
STEP 2 : INSTALLING MYSQL

• sudo apt install mysql-server


• sudo mysql_secure_installation
• sudo mysql ##open the mysql console##
• SELECT user, authentication_string, plugin, host FROM mysql.user; ##Checking authentication##

• exit ##from database after confirmation of getting the following above output##
STEP 3 : INSTALLING PHP
PHP IS THE COMPONENT OF YOUR SETUP THAT WILL PROCESS CODE TO DISPLAY DYNAMIC CONTENT

• sudo apt install php libapache2-mod-php php-mysql ## PHP code can run under
the Apache server and talk to
your MySQL database

• php –v
• #Apache will first look for a file called index.html. We want to tell the web server to prefer PHP
files over others, so make Apache look for an index.php file first.
STEP 4 — CREATING A VIRTUAL HOST FOR YOUR WEBSITE

• Create a virtual host by navigating to ‘/etc/apache2/sites-available/’ and create a file


‘<filename>.conf’ and following content.
• <VirtualHost *:80>
• ServerName <your_domain>
• DocumentRoot /var/www/<your_domain>
• ErrorLog ${APACHE_LOG_DIR}/error.log
• CustomLog ${APACHE_LOG_DIR}/access.log combined
• </VirtualHost>

• Enable the config file : a2ensite <your_domain>.conf


• Disable the Apache’s default config: a2dissite 000-default.conf

• Restart Apache service : systemctl reload apache2


CREATING WEB PAGES
YOU CAN ADD YOU .PHP FILES AT THE DOCUMENTROOT SPECIFIED BEFORE (/YOUR_DOMAIN/
INDEX.HTML).

• <html> • Note :
• <head> • You need to tell apache to give index.php higher
• <title>your_domain website</title> precedence than index.html
• </head> • You can do that by opening
• ‘/etc/apache2/mods-enabled/dir.conf’ file and adding
<body>
index.php at the following location.
• <h1>Hello World!</h1>
• <IfModule mod_dir.c>
• <p>This is the landing page of • DirectoryIndex index.php index.html index.cgi index.pl
<strong>your_domain</strong>.</p>
index.xhtml index.htm
• </body> • </IfModule>
• </html> • And then reload Apache : systemctl reload apache2
STEP 5 — TESTING PHP PROCESSING ON YOUR WEB SERVER
NOW THAT YOU HAVE A CUSTOM LOCATION TO HOST YOUR WEBSITE’S FILES AND FOLDERS

• Create a new file named info.php inside your custom web root folder:
• /your_domain/info.php

• Add the following text, which is valid PHP code, inside the file\
• <?php
• phpinfo();

• To test this script, go to your web browser and access your server’s
domain name or IP address
• http://server_domain_or_IP/info.php
STEP 6 — TESTING DATABASE CONNECTION FROM
PHP
• $ sudo mysql #First, connect to the MySQL console using the root account:
• mysql> CREATE DATABASE example_database;
• mysql> CREATE USER 'example_user'@'%' IDENTIFIED WITH mysql_native_password BY
'password';
• mysql> GRANT ALL ON example_database.* TO 'example_user'@'%’; # user
permission
• mysql> exit
• mysql> SHOW DATABASES;
• mysql> CREATE TABLE example_database.todo_list (item_id INT AUTO_INCREMENT,
• content VARCHAR(255), PRIMARY KEY(item_id));
• mysql> INSERT INTO example_database.todo_list
(content) VALUES ("My first important item");
• mysql> SELECT * FROM
example_database.todo_list;
• mysql> exit
• Now you can create the PHP script that will connect
to MySQL : /your_domain/todo_list.php
• http://your_domain_or_IP/todo_list.php (web
browser)
RESOURCES- LAMP

• https://lakebrains.com/how-to-deploy-single-website-using-lamp-on-ubu
ntu-server/
• https://www.digitalocean.com/community/tutorials/how-to-install-linux-
apache-mysql-php-lamp-stack-on-ubuntu-20-04
TASK2- LOAD BALANCING IN APACHE
WHY WE NEED LOAD BALANCING IN APACHE

• Load balancing increases the network utilization, reliability, and


ultimately increases the user satisfaction.
• Load balancers improve application performance by increasing
response time and reducing network latency.
• They perform several critical tasks such as the following: Distribute the
load evenly between servers to improve application performance.
Redirect client requests to a geographically closer server to reduce
latency.
TASK2- LOAD BALANCING IN APACHE
TASK2- LOAD BALANCING IN APACHE

• Load balancing methods are algorithms or mechanisms used to efficiently distribute


an incoming server request or traffic among servers from the server pool.
• It is the distribution of inbound network and application traffic across multiple servers. This
helps prevent any one server from carrying too heavy a load and increases availability.
• Apache load balancer is open source and provides a server application traffic
distribution solution
• A load balancing algorithm is the logic that a load balancer uses to distribute
network traffic between servers (an algorithm is a set of predefined rules).
DIFFERENT TYPES OF LOAD BALANCING ALGORITHMS

Dynamic LBA’s Static LBA’s


(current state of each server and distribute traffic (distributes traffic without making these
accordingly) adjustments)
APACHE LOAD-BALANCER

• This server will handle all HTTP requests from site visitors. As you
might see, this means even though you run a load balanced system,
using only a single load balancer means you still have a SPOF (single
point of failure). It is also possible to configure an environment where
yet another server will act as the fail-over load-balancer if the first one
fails, but this is outside the scope of this guide.
• To set up our load-balancer, we use the Apache web-server and its
modules mod_proxy, mod_proxy_http and mod_proxy_balancer.
• These are part of most of the Apache web-server distributions.
CONFIGURE APACHE LOAD BALANCER WITH
MOD PROXY
CONFIGURE APACHE LOAD BALANCER WITH MOD PROXY

• The first step to take is to enable the loadbalancing modules. Run the following
commands Use the following commands:
• $ sudo a2enmod proxy
• $ sudo a2enmod proxy_http
• $ sudo a2enmod proxy_balancer
• $ sudo a2enmod lbmethod_byrequests
• $ sudo a2enmod headers

• Run all the above commands then restart Apache to obtain the effect of changes we have
made.
• $ sudo service apache2 restart
DEFINE APACHE LOAD-BALANCER

• Create a new virtual host and add the following lines.


• <VirtualHost *:80> • ProxySet lbmethod=byrequests
• </Proxy>
• ProxyRequests off #disable forward proxying
• #balance-manager is a tool that lets you
• ServerName beginlinux.com configure and tune Apache
• <Proxy balancer://cluster> • <Location /balancer-manager>
• SetHandler balancer-manager
• BalancerMember http://172.31.0.42:80
• #lock this down tightly
• BalancerMember http://172.31.0.44:80 • Order deny,allow
• #security • Allow from all

• Order Deny,Allow • </Location>


• #what to actually balance
• Deny from none
• #in this case, balance everything except
• Allow from all the manager
• #use round-robin balancing • ProxyPass /balancer-manager !
• ProxySet lbmethod=byrequests • ProxyPass / balancer://cluster/

• </VirtualHost>
RESOURCES - LOAD-BALANCING-ALGORITHMS

https://httpd.apache.org/docs/2.4/mod/mod_proxy_balancer.html
https://www.inmotionhosting.com/support/server/apache/apache-load-bal
ancer/
https://websistent.com/configure-apache-web-server-load-balancing/
https://kemptechnologies.com/load-balancer/load-balancing-algorithms-
techniques
WHAT DOES MYSQL SERVER DO?

• MySQL is a database management system.


• It may be anything from a simple shopping list to a picture gallery or
the vast amounts of information in a corporate network. To add, access,
and process data stored in a computer database, you need a database
management system such as MySQL Server.
• MySQL connections are load-balanced between available DB nodes. It is
possible to add or remove database nodes without any changes to the
applications.
TASK3- MASTER / SLAVE CONFIGURATION OF
MYSQL
TASK3- MASTER / SLAVE CONFIGURATION OF
MYSQL
• Master-slave replication enables data from one database server (the master) to be replicated to one
or more other database servers (the slaves).
• The master logs the updates, which then ripple through to the slaves
• Master-slave replication can be either synchronous or asynchronous. The difference is simply the
timing of propagation of changes.
• If the changes are made to the master and slave at the same time, it is synchronous. If changes are
queued up and written later, it is asynchronous
• Some Uses:
• 1) Scale-out solutions
• 2) Data security
• 3) Analytics
• 4) Security
STEPS TO CONFIGURE MASTER

• The default MySQL server configuration file is named mysqld.cnf and can
be found in the /etc/mysql/mysql.conf.d/ directory. Open this file on the
source server with your preferred text editor. Here, we’ll use nano:
• $ sudo nano /etc/mysql/mysql.conf.d/mysqld.cnf
• Replace the standard IP address with the IP address of server.
• bind-address = <server ip addr>

• Next, find the server-id directive,


• Uncomment this line by removing the pound sign (#).
• server-id = 1 (Master Server Id)
STEPS TO CONFIGURE MASTER

• find the log_bin directive #base name and location of


MySQL’s binary log file.
• find the commented-out binlog_do_db directive
• Uncomment binlog_do_db, and replace
inclide_database_name with newdatabase ( db)
• Note: If you want to replicate more than one database, you can
add another binlog_do_db
• Then restart the MySQL service
• $ sudo systemctl restart mysql
• Grant privileges to the slave
• mysql> GRANT REPLICATION SLAVE ON *.* TO
'slave_user'@'%' IDENTIFIED BY 'password’;
• mysql>FLUSH PRIVILEGES;
STEPS TO CONFIGURE SLAVE

• Dump the newly create database file using following command :


• mysqldump -u root -p --opt newdatabase > newdatabase.sql

• Create new database from slave server and import the above dump file :
• mysql -u root -p newdatabase < /path/to/newdatabase.sql

Now we need to configure the slave configuration in the same way as we did the master.
• Open MySQL config file : ‘ sudo nano /etc/mysql/my.cnf’
• Set server id as 2 : server-id = 2
• Make sure below parameters are set properly
• relay-log = /var/log/mysql/mysql-relay-bin.log
• log_bin = /var/log/mysql/mysql-bin.log
• binlog_do_db = newdatabase

Restart Slave and run following command:


• CHANGE MASTER TO MASTER_HOST='12.34.56.789’, MASTER_USER='slave_user’, MASTER_PASSWORD='password',
MASTER_LOG_FILE='mysql-bin.000001’, MASTER_LOG_POS= 107;
RESOURCES

https://www.digitalocean.com/community/tutorials/how-to-set-up-replicati
on-in-mysql
https://phoenixnap.com/kb/mysql-master-slave-replication
https://hevodata.com/learn/mysql-master-slave-replication
https://
wiki.logicaldoc.com/wiki/File:Apache-load-balancer-mod-proxy-http.png
TASK4-WRITING JAVA PROGRAM TO STORE AND
FETCH STUDENTS DATA FROM DATABASE
JAVA DATABASE CONNECTIVITY

• The syntax for importing package to deal with JDBC


operations:
• import java.sql.* ;
• The syntax for registering drivers after loading the
driver class:
• forName(com.mysql.jdbc.xyz) ;
EXECUTING SQL IN JAVA

• In Java, the mysql jdbc Class jar file needs to be imported into the project. This can be done using Class.forName()
Method.
• Class.forName("com.mysql.jdbc.Driver");
• The connection can be established using DriverManager.getConnection(connection_url, username, password);
• Object returned by above command can be used to create statement and run sql query using execute() method.

Example :
1. Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/dbname",“username","password");
2. Statement statement = con.createStatement();
3. statement.execute(query);
4. ResultSet = statement.executeQuery(query);
JAVA DATABASE CONNECTIVITY’S.
RESOURCES

• https://www.geeksforgeeks.org/student-record-system-using-java-swing
/
• https://www.geeksforgeeks.org/java-program-to-retrieve-contents-of-a-t
able-using-jdbc-connection/
• https://projectsgeek.com/2012/07/write-a-program-in-java-for-student-
details-roll-no-name-etc-to-access-as-a-database-and-write-the-
application-in-jdbc-awt-or-jfame.html

You might also like