Deploy with EC2 / Docker / Laravel


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.

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.

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.

execution environment of laravel

Ubuntu:18.04 PHP : 7.3 mysql : 5.4 Docker : 19.03.13 Docker-compose : 1.17.1

Region selection

Create in the Tokyo region

Creating a VPC



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 Let's set it to Ipv4 of the public subnet. image.png

VPC image.png

Creating an EC2 instance

Select EC2 from the console and click Launch Instance image.png

Select Ubuntu 18.04 Select t2.micro in step 2 image.png

Step 3 Select the VPC you created earlier in the detailed instance settings. Let's enable the auto-assigned public IP from disabled. image.png

Step 6 Since HTTP 80 port is used in the security group setting, add it by adding a rule. image.png

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.

 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

 If you want to connect with SSH, you can do it at the bottom (click the copied command).


 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.
 This is the end of EC2 startup.

# Install Docker, Git, Docker-compose in EC2
[email protected]:~$ sudo su
[email protected]:/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 | sudo apt-key add -

$ apt-key fingerprint 0EBFCD88

$ add-apt-repository \
                      "deb [arch=amd64] \
                      $(lsb_release -cs) \
$ apt-get install docker-ce docker-ce-cli

$ apt install docker-compose


 Get out of root with

# Git clone Laradoc.

git clone
 Make sure you have a git clone with ls.

```$ cd laradock```As

#### **`$ cp env-example .env`**

 For the time being, PHP = 7.3
 Let's run it with mysql.

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`**

 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.

[email protected]:/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.


 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.


 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.)


 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.


# 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

Deploy with EC2 / Docker / Laravel
Rails deploy with Docker
Deploy your application with VPC + EC2 + Docker.
Deploy laravel using docker on EC2 on AWS ① (Create EC2 instance)
Deploy a Docker application with Greengrass
General error: 1812 occurs with docker + laravel
Deploy to EC2 with CircleCi + Capistrano
Create Laravel environment with Docker (docker-compose)
Build docker + laravel environment with laradock
Deploy laravel using docker on EC2 on AWS ② (Elastic IP acquisition-linking)
Deploy laravel using docker on EC2 on AWS ④ (git clone ~ deploy, migration)
Deploy laravel using docker on EC2 on AWS ③ (SSH connection ~ Docke-compose installation)
Laravel development environment construction with Docker (Mac)
Deploy to heroku with Docker (Rails 6, MySQL)
Environment construction with Docker (Ubuntu20.04) + Laravel + nginx
Laravel + MySQL + phpMyadmin environment construction with Docker
Display API specifications with Laravel + SwaggerUI + Docker
heroku: docker: deploy
Build a Laravel / Docker environment with VSCode devcontainer
Deploy Line bot with rails5 + Docker + Heroku Note
EC2 ✕ Manual deployment with Docker / docker-compose / Asset compilation
Launch MariaDB with Docker
Run Pico with docker
Explode Docker with WSL2
Use Puphpeteer with Docker
Operate Emby with Docker
Deploy RAILS on EC2
Try WildFly with Docker
Use ngrok with Docker
Run Payara with Docker
[Docker] Connection with MySQL
Php settings with Docker
Getting Started with Docker
Disposable PHP with Docker
Install Composer with Docker
A simple CRUD app made with Nuxt / Laravel (Docker)
[Note] Build a Python3 environment with Docker in EC2
How to make Laravel faster with Docker for Mac
Pytorch execution environment with Docker
Use GDAL with Python with Docker
Run TAO Core with Docker
Docker management with VS Code
Set up GitLab with docker
Hot deploy with IntelliJ IDEA
[Portfolio] Manage site with laravel APP implementation (Docker environment construction)
Run Rails whenever with docker
Get started with DynamoDB with docker
Docker autostart settings with wsl2
[Docker] Rails 5.2 environment construction with docker
Spring Boot starting with Docker
Build docker environment with WSL
Version control CocoaPods with Docker
Avoid Permission Denied that occurs when developing Laravel with Docker
Install docker on AWS EC2
Web application built with docker (1)
I tried BIND with Docker
Java: Start WAS with Docker and deploy your own application
React environment construction with Docker
Build DynamoDB local with Docker
[Docker] Use whenever with Docker + Rails
Using PlantUml with Honkit [Docker]