---
# System prepended metadata

title: 部署冥想練習
tags: [部署, 史博館]

---

###### tags: `史博館` `部署`

部署冥想練習
===

# 1. 備份資料
```
mysql data
laradock .env
laravel .env
```

# 2. 安裝 docker
```
sudo yum update -y
sudo amazon-linux-extras install docker
sudo service docker start
sudo usermod -a -G docker ec2-user
```

# 3. 安裝 docker-compsoe
```
sudo curl -L "https://github.com/docker/compose/releases/download/1.24.1/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose
```

# 4. 使用 swap 2G
```
sudo dd if=/dev/zero of=/swapfile bs=1M count=2000
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
sudo vi /etc/fstab (/swapfile swap swap defaults 0 0)
free -m
```

# 5. 安裝 git
```
sudo yum install -y git
```

# 6. 建 deploy key 到 gitlab
```
ssh-keygen -t rsa -b 4096
```

# 7. 抓 laradock 及 專案
```
git clone https://github.com/laradock/laradock.git

mkdir -p ticket/backend
git clone git@gitlab.com:GoodideasStudio/nmth/ticket/backend.git ticket/backend
mkdir -p ticket/frontend
git clone git@gitlab.com:GoodideasStudio/nmth/ticket/frontend.git ticket/frontend

cd ticket/backend
git checkout stage
cd ../frontend
git checkout stage

cd ../../laradock
scp -i "~/nmth/nmth.pem" mysql.sql ec2-user@18.139.206.105:/home/ec2-user/laradock/backup.sql
scp -i "~/nmth/nmth.pem" .env-laradock ec2-user@18.139.206.105:/home/ec2-user/laradock/.env
scp -i "~/nmth/nmth.pem" .env-laravel ec2-user@18.139.206.105:/home/ec2-user/ticket/backend/.env

vim apache2/Dockerfile

vim apache2/ports.conf

vim apache2/sites/default.apache.conf

vim docker-compose.yml

docker-compose up -d apache2 mysql
docker-compose exec workspace bash
cd ticket/backend
composer install
cd ../frontend
yarn install

cat backup.sql | docker-compose exec -T mysql sh -c "mysql -uroot -pnmthprestage"

docker-compose exec apache2 bash -c "a2enmod proxy_http;service apache restart"

```

ports.conf
```
# If you just change the port or add more ports here, you will likely also
# have to change the VirtualHost statement in
# /etc/apache2/sites-enabled/000-default.conf

Listen 80
Listen 8080

<IfModule ssl_module>
	Listen 443
</IfModule>

<IfModule mod_gnutls.c>
	Listen 443
</IfModule>

# vim: syntax=apache ts=4 sw=4 sts=4 sr noet
```

apache2/Dockerfile
```
FROM webdevops/apache:ubuntu-16.04

LABEL maintainer="Eric Pfeiffer <computerfr33k@users.noreply.github.com>"

ARG PHP_UPSTREAM_CONTAINER=php-fpm
ARG PHP_UPSTREAM_PORT=9000
ARG PHP_UPSTREAM_TIMEOUT=60
ARG DOCUMENT_ROOT=/var/www/

ENV WEB_PHP_SOCKET=${PHP_UPSTREAM_CONTAINER}:${PHP_UPSTREAM_PORT}

ENV WEB_DOCUMENT_ROOT=${DOCUMENT_ROOT}

ENV WEB_PHP_TIMEOUT=${PHP_UPSTREAM_TIMEOUT}

EXPOSE 80 8080 443

WORKDIR /var/www/

COPY vhost.conf /etc/apache2/sites-enabled/vhost.conf

COPY ports.conf /etc/apache2/ports.conf

ENTRYPOINT ["/opt/docker/bin/entrypoint.sh"]

CMD ["supervisord"]
```

apache2/sites/default.apache.conf
```
<VirtualHost *:80>
    DocumentRoot /var/www/ticket/frontend/dist
    ServerName ticket.nmth.pre-stage.club
    ErrorDocument 404 index.html
</VirtualHost>

<VirtualHost *:80>
    ServerName api.ticket.nmth.pre-stage.club
    ProxyRequests off
    <Proxy *>
        Order allow,deny
        Allow from all
    </Proxy>

    ProxyPass / http://127.0.0.1:8080/
    ProxyPassReverse / http://127.0.0.1:8080/
    ProxyPreserveHost on
</VirtualHost>

<VirtualHost *:8080>
    DocumentRoot /var/www/ticket/backend/public
    ServerName ticket.nmth.pre-stage.club
    <Directory /var/www/ticket/backend/public>
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
    </Directory>
</VirtualHost>
```

docker-compose.yml
```
Apache Server
ports:
  - "8080:8080"
```