This repository has been archived by the owner on Jun 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
setting-up-lamp.html
175 lines (157 loc) · 11.5 KB
/
setting-up-lamp.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
<!DOCTYPE html>
<html data-bs-theme="dark">
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>2. LAMP configuration</title>
<link rel="stylesheet" href="lib/bootstrap-5.3.0-alpha3-dist/css/bootstrap.min.css">
<link rel="stylesheet" href="css/styles.css">
<link rel="stylesheet" href="css/sidebars.css">
<script src="js/https-redirector.js"></script>
</head>
<body>
<!-- This button is only shown when the sidebar is collapsed -->
<div id="show-sidebar-button" style="display: none;">
<img src="img/arrow-line-left-thin.svg" class="shadow-sm"></img>
</div>
<!-- SIDEBAR -->
<div class="main h-100">
<div id="collapsable-sidebar">
<div class="flex-shrink-0 p-3">
<div class="d-flex align-items-center justify-content-between pb-3 mb-3 gap-2 link-light text-decoration-none border-bottom">
<a class="p-0 m-0 text-decoration-none" href="index.html">
<span class="fs-4 fw-semibold text-light text-nowrap p-0 m-0"><span style="color:#53a3ff;">LAMP</span> on <span style="color:#f09641">AWS</span></span>
</a>
<button type="button" id="hide-sidebar-button" class="btn shadow-sm d-flex justify-content-center p-1 m-0 shadow-sm">
<img src="img/arrow-line-left-thin.svg" height="26px"></img>
</button>
</div>
<ul class="list-unstyled ps-0">
<li class="mb-1">
<button class="btn btn-toggle align-items-center rounded collapsed text-light" data-bs-toggle="collapse" data-bs-target="#aws-collapse" aria-expanded="true">
<div href="#" class="link-light fw-normal text-decoration-none">1. <a class="text-nowrap link-light fw-semibold text-decoration-none">Setting up an EC2 instance</a></div>
</button>
<div class="collapse show" id="aws-collapse">
<ul class="btn-toggle-nav list-unstyled fw-normal pb-1 small">
<li><a href="setting-up-ec2.html#section-1.1" class="link-light rounded">1.1 Creating the instance</a></li>
<li><a href="setting-up-ec2.html#section-1.2" class="link-light rounded">1.2 Connecting via SSH</a></li>
</ul>
</div>
</li>
<li class="mb-1">
<button class="btn btn-toggle align-items-center rounded collapsed text-light" data-bs-toggle="collapse" data-bs-target="#docker-collapse" aria-expanded="true">
<div href="#" class="link-light fw-normal text-decoration-none">2. <a class="text-nowrap link-light fw-semibold text-decoration-none">Setting up LAMP</a></div>
</button>
<div class="collapse show" id="docker-collapse">
<ul class="btn-toggle-nav list-unstyled fw-normal pb-1 small">
<li><a href="setting-up-lamp.html#section-2.1" class="link-light rounded">2.1 System configuration</a></li>
<li><a href="setting-up-lamp.html#section-2.2" class="link-light rounded">2.2 Creating docker containers</a></li>
<li><a href="setting-up-lamp.html#section-2.3" class="link-light rounded">2.3 Creating the SSL certificate</a></li>
<li><a href="setting-up-lamp.html#section-2.4" class="link-light rounded">2.4 Automating the startup</a></li>
</ul>
</div>
</li>
<li class="mb-1">
<button class="btn btn-toggle align-items-center rounded collapsed text-light" data-bs-toggle="collapse" data-bs-target="#private-collapse" aria-expanded="true">
<div href="#" class="link-light fw-normal text-decoration-none">3. <a class="text-nowrap link-light fw-semibold text-decoration-none">Creating a private area</a></div>
</button>
<div class="collapse show" id="private-collapse">
<ul class="btn-toggle-nav list-unstyled fw-normal pb-1 small">
<li><a href="creating-private-area.html#section-3.1" class="link-light rounded">3.1 Inserting a user</a></li>
<li><a href="creating-private-area.html#section-3.2" class="link-light rounded">3.2 Creating the API & login</a></li>
<li><a href="creating-private-area.html#section-3.3" class="link-light rounded">3.3 Creating the private area</a></li>
<li><a href="creating-private-area.html#section-3.4" class="link-light rounded">3.4 Logging in</a></li>
<li><a href="creating-private-area.html#section-3.5" class="link-light rounded">3.5 Redirecting to HTTPS</a></li>
</ul>
</div>
</li>
</ul>
</div>
</div>
<div class="side-divider" id="sidebar-divider"></div>
<!-- PAGE CONTENT -->
<div class="page-content overflow-auto flex-fill text-light m-2">
<h2>2. Setting up LAMP</h2><hr>
<h4 id="section-2.1">2.1 System configuration</h4>
<p class="p-0 m-0 mb-1 mt-2">The first thing that should be always done when configuring a fresh linux install is to update
the repository list with <code>apt update</code> and then install the new packages with <code>apt upgrade</code>, so go ahead and insert these commands
in your terminal. With it, we're also going to install docker.io
</p>
<div class="code-snippet">
<p>sudo apt update</p>
<p>sudo apt upgrade</p>
<p>sudo apt install docker.io</p>
</div>
<hr>
<h4 id="section-2.2">2.2 Creating the docker containers</h4>
<p class="p-0 m-0 mb-1 mt-2">There is a LAMP stack container that we can use that is already preconfigured from <code>mattrayner/lamp</code>, so let's install it with this command</p>
<div class="code-snippet">
<p>sudo docker run -d --name lamp -i -t -p "80:80" -v ~/app:/app mattrayner/lamp:latest</p>
</div>
<p class="p-0 m-0 mb-0 mt-1">Here we are creating a docker container, detaching from its process with <code>-d</code>, giving it a <code>--name</code> of lamp, exposing it on the port 80 and binding
the container's /app dir to a directory with the same that will be created in the user's PWD folder. <br>
The <code>~/app</code> directory is the root folder of the apache+php webserver, in here you can place your css, html, php and js files.</p>
<p class="p-0 m-0 mb-1 mt-2">To check if the installation worked, check if the container is running with this command</p>
<div class="code-snippet pb-1">
<p>sudo docker ps</p>
</div>
<img src="img/lamp/ssl-3.png" class="img-fluid" alt="">
<p class="p-0 m-0 mb-1 mt-2">Then simply get your EC2 instance's public IP and put it in a browser, you should see something similar</p>
<img src="img/lamp/ssl-4.png" class="img-fluid" alt="">
<p class="p-0 m-0 mb-1 mt-2">Let's create an index.php file in the ~/app dir to see if it works</p>
<div class="code-snippet">
<p>cd ~/app</p>
<p>touch index.php</p>
</div>
<p class="p-0 m-0 mb-1 mt-2">Then you can edit the index.php file to show phpinfo() with either <code>vi</code>, <code>nano</code> or any other editor you like. The file content should look like this.</p>
<div class="file-snippet">
<div class="file-name">File: ~/app/index.php</div>
<div class="file-content"><?php phpinfo() ?></div>
</div>
<p class="p-0 m-0 mb-1 mt-2">Go to your EC2 instance's public IP and you should see that everything is working!</p>
<img src="img/lamp/ssl-5.png" class="img-fluid" alt="">
<hr><div class="d-flex"><h4 id="section-2.3">2.3 Creating the SSL certificate <h4 class="fst-italic fw-lighter">(optional)</h4></h4></div>
<p class="p-0 m-0 mb-1 mt-2">We are going to create the /ssl directory containing the ssl key and certificate in the user's home dir,
to do this follow the instructions from this command
</p>
<div class="code-snippet">
<p>mkdir ~/ssl</p>
<p>sudo openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout ~/ssl/key.pem -out ~/ssl/cert.pem</p>
</div>
<p class="p-0 m-0 mb-1 mt-2">If you've done everything correctly, you should see a cert.pem and a key.pem file in the ~/ssl directory</p>
<img src="img/lamp/ssl-2.png" class="img-fluid" alt="">
<p class="p-0 m-0 mb-1 mt-2">Now we need a way to bind the SSL certificate to our website, to do this we can create a proxy using <code>fsouza/docker-ssl-proxy</code> on the port 443
by specifying where our certificates are, in this case we put them in <code>~/ssl</code>. You also need to set the correct domain for your machine
</p>
<div class="code-snippet">
<p>sudo docker run -d --name ssl-proxy --link lamp:proxyapp -p 443:443 -e DOMAIN=<span style="color: red">YOUR_EC2_PUBLIC_IP</span> -e TARGET_PORT=80 -e TARGET_HOST=proxyapp -e SSL_PORT=443 -v ~/ssl:/etc/nginx/certs --restart unless-stopped fsouza/docker-ssl-proxy</p>
</div>
<p class="p-0 m-0 mb-1 mt-2">If you go to https://YOUR_EC2_PUBLIC_IP you shold now see this</p>
<img src="img/lamp/ssl-6.png" class="img-fluid" alt="">
<p class="p-0 m-0 mb-1 mt-2">And you can see the certificate information</p>
<img src="img/lamp/ssl-7.png" class="img-fluid" alt="">
<hr>
<h4 id="section-2.4">2.4 Automating the startup</h4>
<p class="p-0 m-0 mb-1 mt-2">Now that everything is configured let's create a .sh script to start/restart the lamp and proxy-ssl containers authomatically</p>
<div class="file-snippet">
<div class="file-name">File: ~/run-lamp.sh</div>
<div class="file-content"># Start docker<br>
sudo docker start<br><br>
# Kill all running containers<br>
containers=$(docker ps -q)<br>
docker kill $containers<br><br>
# Start lamp and proxy server<br>
sudo docker start lamp<br>
sudo docker start proxy-ssl</div>
</div>
<hr>
<p class="p-0 m-0 mb-1 mt-2">Your LAMP stack is now working! Go onto the next section to learn how to make a private area</p>
<a href="creating-private-area.html" class="btn-toggle rounded text-light flex-wrap text-decoration-none">3. Creating a private area</a>
</div>
</div>
<!-- Libraries -->
<script src="lib/bootstrap-5.3.0-alpha3-dist/js/bootstrap.bundle.min.js"></script>
<script src="lib/jquery-3.6.3.js"></script>
<!-- Custom js -->
<script src="js/sidebar-collapser.js"></script>
</body>
</html>