I'm starting to study web application development using Laradoc.
AWS and Laravel, docker, docker-compose About a month after I started studying, I'm used to using Docker / Doker Compose / AWS, so I'd like to deploy it with Laradoc, so I'll leave it for my own study. (As of November 4, 2020)
The sites I referred to are as follows.
https://laraweb.net/tutorial/6578/ https://noumenon-th.net/programming/2019/06/16/laradock/
The book I referred to was "Network & Server Construction from the Basics of Amazon Web Services Revised 3rd Edition".
I studied the basic knowledge of docker and docker-compose in the following udemy course.
https://www.udemy.com/course/aidocker/
I am grateful to the above authors for giving me various knowledge.
Let's do it now
Deploy a Laravel web app that you can access from anywhere on AWS. Originally, I think that you make Laradoc locally, develop a web application and git clone it, It's all done on AWS.
Ubuntu:18.04 PHP : 7.3 mysql : 5.4 Docker : 19.03.13 Docker-compose : 1.17.1
Create in the Tokyo region
Create a VPC with one public subnet. If you want to create a private subnet, create a VPC with the public and private subnets below. At this time, the IPv4 CIDR block is 10.0.0.0/16 Let's set it to Ipv4 10.0.0.0/24 of the public subnet.
VPC
Select EC2 from the console and click Launch Instance
Select Ubuntu 18.04 Select t2.micro in step 2
Step 3 Select the VPC you created earlier in the detailed instance settings. Let's enable the auto-assigned public IP from disabled.
Step 6 Since HTTP 80 port is used in the security group setting, add it by adding a rule.
Click Start in step 7. Download the key pair (mykey.pem) for SSH connection.
During this time, let's authenticate mykey.pem.
Move to the download folder and execute the following command Then the authentication will change.
chmod 400 mykey.pem
Details can be found on the aws authentication page.
https://docs.aws.amazon.com/ja_jp/AWSEC2/latest/UserGuide/ec2-key-pairs.html
After a while, the instance will be running, so click the EC2 dashboard ⇒ running instance ⇒ click the created instance ID
Click the connection in the upper right
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/129576/7ee3781a-a5f4-5ff4-9569-335b7732ceee.png)
If you want to connect with SSH, you can do it at the bottom (click the copied command).
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/129576/a55ba871-2aef-0b68-f858-2baf4e43cde4.png)
Open the terminal
Change to the directory where mykey.pem is (cd)
I just copied it on AWS
```ssh ~~~```
Paste in the terminal. Enter
Then put it in the EC2 you created earlier.
afterwards,
```yes```Enter
This is the end of EC2 startup.
# Install Docker, Git, Docker-compose in EC2
~~~
ubuntu@ip-10-0-0-205:~$ sudo su
root@ip-10-0-0-205:/home/ubuntu#
~~~
Work in root below
Let's type the command below
Update apt-get and install git
```apt-get update```
```apt-get install git```
Install docker and docker-compose
~~~
$ apt-get install \apt-transport-https \ca-certificates \curl \gnupg-agent \software-properties-common
$ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
$ apt-key fingerprint 0EBFCD88
$ add-apt-repository \
"deb [arch=amd64] https://download.docker.com/linux/ubuntu \
$(lsb_release -cs) \
stable"
$ apt-get install docker-ce docker-ce-cli containerd.io
$ apt install docker-compose
~~~
exit
Get out of root with
# Git clone Laradoc.
https://laradock.io/
~~~
git clone https://github.com/Laradock/laradock.git
~~~
Make sure you have a git clone with ls.
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/129576/9278a506-1ba4-d41c-d552-5c5b2fa8a4bb.png)
```$ cd laradock```As
#### **`$ cp env-example .env`**
```env
For the time being, PHP = 7.3
Let's run it with mysql.
Run.
docker-compose up -d nginx mysql
Then start docker of nginx mysql.
Wait for about 10 minutes. Let's relax while watching on Youtube.
# Enter the workspace container.
After installing docker, execute the following command and
Let's work in the workspace.
docker-compose exec --user=laradock workspace bash
In workspace, start docker-compose.
This will take you to ``` var / www $` ``.
Because there is a laradock folder
```$ cd laradoock```
Edit the .env file as.
#### **`$ cp env-example .env`**
```env
let's do it
Add the following to `` `.env```
#### **`vi .Execute env and add the following.`**
```Execute env and add the following.
DB_HOST=mysql DB_DATABASE=default DB_USERNAME=default DB_PASSWORD=secret
## Creating a web app with Laravel
This time, the sample application name is SampleProject.
laradock@e00afef1d36e:/var/www$ composer create-project laravel/laravel SampleProject "5.5.*"
Then you can create a Sample Project.
var/www --- laradock |_ SampleProject
It will be configured as.
Enter SampleProject and edit .env.
DB_CONNECTION=mysql DB_HOST=mysql DB_PORT=3306 DB_DATABASE=default DB_USERNAME=default DB_PASSWORD=secret
Set DB_HOST to mysql.
Then, edit .env in Laradock as follows and select the laravel application to start.
If you do not write here, it will not be recognized.
APP_CODE_PATH_HOST=../SampleProject
For the time being, the settings are complete.
Go to the laradoc folder and restart docker-compose to load your changes.
docker-compose up -d nginx mysql phpmyadmin
It will be the last in a little while.
## Laravel initial screen display
Copy the EC2 public Ipv4 DNS and
Paste it into your browser's URL bar. (The URL can be fixed with Elastic IP, but this time it will not be done.)
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/129576/afad51fc-f102-9652-77f7-9f691c7a38d5.png)
You can go. I'm skeptical, so you can check if it works on your iphone to see if it's global instead of local.
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/129576/b2d4e000-6e69-ae1e-150a-6f408b03dc61.png)
# Precautions when restarting EC2
1) Restart docker-compose every time you restart EC2.
It seems that it will restart when EC2 is stopped, so
Since you have to start docker-compose every time you start EC2,
Execute the following command.
sudo su cd laradock/ docker-compose up -d nginx mysql phpmyadmin
2) When EC2 is stopped, the Ip address has changed, so
Let's fix it with Elastic IP.
3) After changing the Laradock file
```docker-compose up -d mysql phpmyadmin```
let's do it.
Of course, the file to edit is Sample Project.
# Impressions and the future
I thought it would be very convenient to launch using EC2, and I'd be happy if I could deploy it.
However, starting up Docker-compose after restarting EC2,
When I launched EC2, I thought it was a hassle to put in git or Docker / Docker-compose.
From now on, I will study EC2 redundancy, ECS (Fargate), and CI / CD as advanced contents.
I hope it will be helpful to you.
that's all
Recommended Posts