もくじ
EC2側作業
# vi /etc/sysconfig/selinux SELINUX=disabled
# reboot now
$ sudo yum install -y yum-utils device-mapper-persistent-data lvm2 $ sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
yumのパッケージインデックスを更新(Dockerのインストールやアップデート前に推奨されている)
$ sudo yum makecache fast
# yum list docker-ce.x86_64 --showduplicates | sort -r * updates: ftp.iij.ad.jp Loading mirror speeds from cached hostfile Loaded plugins: fastestmirror * extras: ftp.iij.ad.jp docker-ce.x86_64 3:18.09.1-3.el7 docker-ce-stable docker-ce.x86_64 3:18.09.0-3.el7 docker-ce-stable docker-ce.x86_64 18.06.1.ce-3.el7 docker-ce-stable docker-ce.x86_64 18.06.0.ce-3.el7 docker-ce-stable docker-ce.x86_64 18.03.1.ce-1.el7.centos docker-ce-stable docker-ce.x86_64 18.03.0.ce-1.el7.centos docker-ce-stable docker-ce.x86_64 17.12.1.ce-1.el7.centos docker-ce-stable docker-ce.x86_64 17.12.0.ce-1.el7.centos docker-ce-stable docker-ce.x86_64 17.09.1.ce-1.el7.centos docker-ce-stable docker-ce.x86_64 17.09.0.ce-1.el7.centos docker-ce-stable docker-ce.x86_64 17.06.2.ce-1.el7.centos docker-ce-stable docker-ce.x86_64 17.06.1.ce-1.el7.centos docker-ce-stable docker-ce.x86_64 17.06.0.ce-1.el7.centos docker-ce-stable docker-ce.x86_64 17.03.3.ce-1.el7 docker-ce-stable docker-ce.x86_64 17.03.2.ce-1.el7.centos docker-ce-stable docker-ce.x86_64 17.03.1.ce-1.el7.centos docker-ce-stable docker-ce.x86_64 17.03.0.ce-1.el7.centos docker-ce-stable * base: ftp.iij.ad.jp Available Packages
sudo yum install -y docker-ce-17.12.1.ce-1.el7.centos sudo systemctl start docker sudo systemctl enable docker sudo systemctl status docker
curl -L https://github.com/docker/compose/releases/download/1.12.0/docker-compose-`uname -s`-`uname -m` > docker-compose sudo mv docker-compose /usr/local/bin/docker-compose sudo chmod +x /usr/local/bin/docker-compose
$ sudo docker pull nginx:latest $ sudo docker pull mysql:5.7
mkdir laravel_app touch docker-compose.yml mkdir php nginx src cd php
# vi /home/centos/docker-compose.yml version: '2' services: php: build: ./php volumes: - ./src:/var/www nginx: image: nginx ports: - "80:80" volumes: - ./src:/var/www - ./nginx/default.conf:/etc/nginx/conf.d/default.conf depends_on: - php mysql: image: mysql:5.7 environment: MYSQL_ROOT_PASSWORD: testdb
$ vi /home/centos/php/Dockerfile FROM php:7.1-fpm COPY php.ini /usr/local/etc/php/ # install composer RUN cd /usr/bin && curl -s http://getcomposer.org/installer | php && ln -s /usr/bin/composer.phar /usr/bin/composer RUN apt-get update \ && apt-get install -y \ git \ zip \ unzip \ vim WORKDIR /var/www RUN chown -R apache:apache /var/www/ && \ chmod -R 775 /var/www/
$ vi /home/centos/php/php.ini [Date] date.timezone = "Asia/Tokyo" [mbstring] mbstring.internal_encoding = "UTF-8" mbstring.language = "Japanese"
$ vi /home/centos/nginx/default.conf server { index index.php index.html; root /var/www; }
$ vi /home/centos/src/index.html Hello Laravel!
$ pwd /home/centos $ ls docker-compose.yml laravel_app nginx php src
$ sudo systemctl start docker $ sudo systemctl enable docker
[centos@ip-172-31-36-81 ~]$ which docker-compose /usr/local/bin/docker-compose
$ sudo /usr/local/bin/docker-compose up -d --build
http://IPアドレス/
Hello Laravel!
表示された
$ vi /home/centos/src/index.php <?php phpinfo(); ?>
$ vi /home/centos/nginx/default.conf server { index index.php index.html; root /var/www/; location / { try_files $uri $uri/ /index.php?$query_string; } location ~ \.php$ { try_files $uri =404; fastcgi_split_path_info ^(.+\.php)(/.+)$; fastcgi_pass php:9000; fastcgi_index index.php; include fastcgi_params; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_param PATH_INFO $fastcgi_path_info; } }
$ sudo /usr/local/bin/docker-compose restart Restarting centos_nginx_1 ... done Restarting centos_mysql_1 ... done Restarting centos_php_1 ... done
# vi /home/centos/docker-compose.yml version: '2' services: php: build: ./php volumes: - ./src/my-app/my-app:/var/www nginx: image: nginx ports: - "80:80" volumes: - ./src/my-app/my-app:/var/www - ./nginx/default.conf:/etc/nginx/conf.d/default.conf depends_on: - php mysql: image: mysql:5.7 environment: MYSQL_ROOT_PASSWORD: testdb
$ vi /home/centos/nginx/default.conf server { index index.php index.html; root /var/www/public; location / { try_files $uri $uri/ /index.php?$query_string; } location ~ \.php$ { try_files $uri =404; fastcgi_split_path_info ^(.+\.php)(/.+)$; fastcgi_pass php:9000; fastcgi_index index.php; include fastcgi_params; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_param PATH_INFO $fastcgi_path_info; } }
実行とビルド
$ /usr/local/bin/docker-compose run php composer create-project laravel/laravel --prefer-dist /home/${USER}/src/myapp $ sudo /usr/local/bin/docker-compose up -d --build $ sudo /usr/local/bin/docker-compose restart
$ sudo chmod 744 -R /home/centos/src/my-app/my-app/storage $ sudo chmod 744 -R /home/centos/src/my-app/my-app//bootstrap/cache
$ sudo yum install git $ sudo git clone https://github.com/hoge/circleciDemo.git
—————————————————–
新規でLaravelをインストール場合
$ sudo yum install epel-release $ sudo rpm -ivh http://rpms.famillecollet.com/enterprise/remi-release-7.rpm $ sudo yum -y install --enablerepo=remi-php71 php php-cli php-devel php-common php-mbstring php-mysql php-phpunit-PHPUnit php-pecl-xdebug php-gd php-pdo php-xml php-mcrypt php-fpm php-opcache php-pecl-apcu $ cd $ sudo php -r "readfile('https://getcomposer.org/installer');" | php $ sudo mv composer.phar /usr/local/bin/composer
—————————————————–
CircleCI
CircleCIにサインアップしてGitHubと連携を行っておきます。
version: 2 # use CircleCI 2.0 jobs: # a collection of steps build: # runs not using Workflows must have a `build` job as entry point docker: # run the steps with Docker - image: circleci/php:7.1-node-browsers # ...with this image as the primary container; this is where all `steps` will run working_directory: ~/laravel # directory where steps will run steps: # a set of executable commands - checkout # special step to check out source code to working directory - run: sudo apt install -y libsqlite3-dev zlib1g-dev - run: sudo docker-php-ext-install zip - run: sudo composer self-update - restore_cache: # special step to restore the dependency cache if `composer.lock` does not change keys: - composer-v1-{{ checksum "composer.lock" }} # fallback to using the latest cache if no exact match is found (See https://circleci.com/docs/2.0/caching/) - composer-v1- - run: composer install -n --prefer-dist - save_cache: # special step to save the dependency cache with the `composer.lock` cache key template key: composer-v1-{{ checksum "composer.lock" }} paths: - vendor - restore_cache: # special step to restore the dependency cache if `package.json` does not change keys: - node-v1-{{ checksum "package.json" }} # fallback to using the latest cache if no exact match is found (See https://circleci.com/docs/2.0/caching/) - node-v1- - run: yarn install - save_cache: # special step to save the dependency cache with the `package.json` cache key template key: node-v1-{{ checksum "package.json" }} paths: - node_modules - run: vendor/phpunit/phpunit/phpunit tests/ - run: touch storage/testing.sqlite - run: php artisan migrate --env=testing --database=sqlite_testing --force - run: ./vendor/bin/codecept build - run: ./vendor/bin/codecept run
$ cd /home/centos/src/my-app/my-app $ vendor/bin/phpunit PHPUnit 7.5.1 by Sebastian Bergmann and contributors. .. 2 / 2 (100%) Time: 146 ms, Memory: 14.00MB
Laravel側作業
Laravel側で下記でアクセス出来るようにルーティングを行っていて下さい。
- http://IPアドレス/
- http://IPアドレス/hello
今回は触れません。
PHPUnit用テストコード
$ vi /home/centos/src/my-app/my-app/tests/Feature/HelloTest.php <?php namespace Tests\Feature; use Tests\TestCase; use Illuminate\Foundation\Testing\WithFaker; use Illuminate\Foundation\Testing\RefreshDatabase; class HelloTest extends TestCase { /** * A basic test example. * * @return void */ public function testHello() { $this->assertTrue(true); //$response = $this->get('/'); //$response->assertStatus(404); $response = $this->get('/hello'); $response->assertStatus(200); //$msg = "Hello"; //$this->assertEquals('Hello', $msg); } }
/helloにルーティング出来るか確かめるものです。
GitHubにプッシュすると
$ sudo git add . --ignore-removal $ sudo git commit -m "testv3-badrouting" $ sudo git push origin master
流れ
- EC2 → GitHub → CircleCI
CircleCIでテストが走ってテストが単体出来る
更にAWS CodeDeployとも携すればCI/CDとなる
EC2 → GitHub → CircleCI → AWS CodeDeploy
参考サイト
@see https://tanaken.me/web/199