LoginSignup
2
1

More than 5 years have passed since last update.

docker-compseでnginx構築

Last updated at Posted at 2018-05-20

docker-compseでnginx構築

nginxのインストール

$ sudo mkdir /var/www
$ sudo mkdir /var/www/devops
$ sudo chown -R ec2-user:ec2-user /var/www/devops
$ cd /var/www/devops
$ mkdir nginx
$ sudo chown -R ec2-user:ec2-user nginx
$ touch docker-compose.yml

ymlファイルを編集します。

docker-compose.yml
version: '3'
services:
  web:
    image: nginx
    ports:
      - "80:80"
    volumes:
      - ./nginx/mysite.template:/etc/nginx/conf.d/mysite.template
    command: /bin/bash -c "envsubst < /etc/nginx/conf.d/mysite.template > /etc/nginx/conf.d/default.conf && nginx -g 'daemon off;'"

volumesで指定したconfを作成します。

$ mkdir nginx
$ vi nginx/mysite.template
mysite.template
server {
    listen 80 default;
    server_name _;
    root /var/www/html;
    charset utf-8;

    access_log off;
    error_log off;

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }
}

インストール&起動します。

$ docker-compose up -d

セキュリティグループを変更する

このままでは外部からのアクセスができないため、AWSコンソールでセキュリティグループの変更をしてHTTPポートを開放します。

  1. AWSの「インスタンス」より、起動中インスタンスに表示されているセキュリティグループを選択します。
  2. 「インバウンド」の「編集」ボタンをクリックし、タイプ「HTTP」を選択して保存します。
  3. ブラウザからhttp://[インスタンスのIPアドレス]でアクセスします。

参考

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1