Diferència entre revisions de la pàgina «AWS: Amazon Web Services. Cloud»
| Línia 69: | Línia 69: | ||
''llista_langtrainer.php'': | ''llista_langtrainer.php'': | ||
<pre> | <pre> | ||
| + | <?php | ||
| + | header('Content-type: text/html; charset=utf-8'); | ||
| + | $servername = "database-2.cdlwhdawd8sb.us-east-1.rds.amazonaws.com"; | ||
| + | $username = "admin"; | ||
| + | $password = "keiL2lai"; | ||
| + | $dbname = "langtrainer"; | ||
| + | |||
| + | // Create connection | ||
| + | $conn = mysqli_connect($servername, $username, $password, $dbname); | ||
| + | // Check connection | ||
| + | if (!$conn) { | ||
| + | die("Connection failed: " . mysqli_connect_error()); | ||
| + | } | ||
| + | |||
| + | mysqli_query($conn, "SET NAMES 'utf8'"); | ||
| + | |||
| + | $sql = "select word, translation from word w, translation t where w.id_word=t.id_word limit 10"; | ||
| + | |||
| + | $resultset = mysqli_query($conn,$sql); | ||
| + | |||
| + | if (php_sapi_name()==='cli') { //executem des de la comanda | ||
| + | echo "VOCABULARI\n============\n"; | ||
| + | while($row = mysqli_fetch_array($resultset)) { | ||
| + | echo $row['word'].": ".$row['translation']."\n"; | ||
| + | } | ||
| + | } else { //executem des de la web | ||
| + | echo "<h2>VOCABULARI</h2>"; | ||
| + | echo "<ul>"; | ||
| + | while($row = mysqli_fetch_array($resultset)) { | ||
| + | echo "<li>".$row['word'].": ".$row['translation']."</li>"; | ||
| + | } | ||
| + | echo "</ul>"; | ||
| + | } | ||
| + | mysqli_close($conn); | ||
| + | |||
| + | echo php_sapi_name(); | ||
| + | |||
| + | ?> | ||
</pre> | </pre> | ||
Revisió del 20:33, 26 gen 2021
Introducció
Entro a la consola de AWS Management Console
Creo una màquina EC2: All services > Compute > EC2
Les màquines EC2 significa Elastic Computing, i forma part de la Capa gratuïta, 12 mesos gratis, 750 hores al mes (750 horas por mes de uso de instancias t2.micro con Linux, RHEL o SLES)
He de posar la informació de la target de crèdit per tal de poder escollir una instància.
Step 1: Choose an Amazon Machine Image (AMI) Escullo: Ubuntu Server 20.04 LTS (HVM), SSD Volume Type - ami-0885b1f6bd170450c (64-bit x86) / ami-054e49cb26c2fd312 (64-bit Arm) (tier elegible) Step 2: Choose an Instance Type Step 7: Review Instance Launch create a new key pair (joan.pem) Your instances are now launching Name: -- Instance ID: i-069719a8c85b6e2bd Instance state: Pending -> Running Instance type: t2.micro Status check Alarm status Availability Zone: us-east-1d Public IPv4 DNS: ec2-54-172-172-75.compute-1.amazonaws.com Public IPv4 address: 54.172.172.75
Ara ja m'he de poder connectar via ssh. El public DNS és:
- ec2-52-207-224-88.compute-1.amazonaws.com
$ chmod 400 joan2.pem $ ssh -i "joan.pem" ubuntu@ec2-54-172-172-75.compute-1.amazonaws.com
i ja puc entrar. Puc utilitzar scp per enviar fitxers al servidor. Recordar que quan acabi l'he d'apagar.
Base de dades langtrainer a AWS
Vull tenir un mysql en el cloud. Escullo Database RDS (RDS significa Amazon Relational Database Service)
De moment no tinc res:
- DB Instances (0/40)
create database. Engine options: escullo Standard create, MySQL
- Template: escullo Free Tier
- DB instance identifier: database-2
- username: admin
- password: S*******
Miro quines opcions té el stardard create, i entre elles hi ha el public: no
Finalment: create database
- end point: database-2.cdlwhdawd8sb.us-east-1.rds.amazonaws.com
(TBD). Havia de fer de manera d'obrir el port 3306 (inbound rule)
mysql -h langtrainer.cyr40ydu5rcj.us-east-1.rds.amazonaws.com -P 3306 -u admin -p mysql -h langtrainer.cyr40ydu5rcj.us-east-1.rds.amazonaws.com -u admin -p langtrainer < /home/joan/copia_langtrainer_200902.dmp
Des del meu portàtil puc accedir a la base de dades remota:
php llista_langtrainer.php
llista_langtrainer.php:
<?php
header('Content-type: text/html; charset=utf-8');
$servername = "database-2.cdlwhdawd8sb.us-east-1.rds.amazonaws.com";
$username = "admin";
$password = "keiL2lai";
$dbname = "langtrainer";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
mysqli_query($conn, "SET NAMES 'utf8'");
$sql = "select word, translation from word w, translation t where w.id_word=t.id_word limit 10";
$resultset = mysqli_query($conn,$sql);
if (php_sapi_name()==='cli') { //executem des de la comanda
echo "VOCABULARI\n============\n";
while($row = mysqli_fetch_array($resultset)) {
echo $row['word'].": ".$row['translation']."\n";
}
} else { //executem des de la web
echo "<h2>VOCABULARI</h2>";
echo "<ul>";
while($row = mysqli_fetch_array($resultset)) {
echo "<li>".$row['word'].": ".$row['translation']."</li>";
}
echo "</ul>";
}
mysqli_close($conn);
echo php_sapi_name();
?>
Màquina EC2 a AWS. LAMP
ssh -i /home/joan/Baixades/prova1_ec2.pem ec2-user@ec2-52-91-66-204.compute-1.amazonaws.com sudo chmod 400 /home/joan/Baixades/prova1_ec2.pem ssh -i /home/joan/Baixades/prova1_ec2.pem ec2-user@ec2-52-91-66-204.compute-1.amazonaws.com
Ja estic a la màquina remota.
Instal·lo el Apache i el LAMP (tutorial que vaig trobar, TBD)
També puc executar el script llista_langtrainer en la màquina remota: (la base de dades langtrainer està en una altra màquina de AWS)
Des de la línia de comandes:
php llista_langtrainer.php
i també la puc accedir online:
script lista_langtrainer.php:
<?php
header('Content-type: text/html; charset=utf-8');
$servername = "langtrainer.cyr40ydu5rcj.us-east-1.rds.amazonaws.com";
$username = "admin";
$password = "She4aiVa";
$dbname = "langtrainer";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
mysqli_query($conn, "SET NAMES 'utf8'");
$sql = "select word, translation from WORD W, TRANSLATION T where W.id_word=T.id_word limit 10";
$resultset = mysqli_query($conn,$sql);
while($row = mysqli_fetch_array($resultset)) {
\techo $row['word'].": ".$row['translation']."\
";
}
mysqli_close($conn);
?>
creat per Joan Quintana Compte, setembre 2020