LoginSignup
0
0

More than 3 years have passed since last update.

Create LAMP with Self-signed certificate environment using Docker.

Last updated at Posted at 2019-04-10

Purpose

Since I hoped to create LAMP environment for developing an application, I executed these.
And I added Self-signed certification just in case.

Directory Structure

on_your_computer
Docker/
 ├ docker-compose.yml
 ├ phpapache/
 │ └ html/
 │ └ Dockerfile
 │ └ entrypoint.sh
 │ └ ssl.conf
 └ mysql/
   └ Dockefile
   └ db_data/

Files

docker-compose.yml
version: '3' 
services:    
  db:        
    build:   
      context: ./mysql
    environment: 
      MYSQL_ROOT_PASSWORD: root
      MYSQL_DATABASE: test_db
      MYSQL_USER: docker 
      MYSQL_PASSWORD: docker
    container_name: mydb
    ports: 
      - "3306:3306"
    tty: true
    volumes:
      - ./mysql/db_data:/var/lib/mysql
  web:
    container_name: myserver
    build:
      context: ./phpapache
    volumes:
      - ./phpapache/html:/var/www/html
    tty: true
    ports:
      - "8080:80"
      - "443:443"
    depends_on:
      - db    
phpapache/Dockerfile
FROM php:7.1-apache

RUN apt-get update && \
    docker-php-ext-install pdo_mysql mysqli mbstring

ADD ssl.conf /etc/apache2/sites-available/ssl.conf                                 
ADD entrypoint.sh /opt/entrypoint.sh                                               

RUN chmod a+x /opt/entrypoint.sh                                                   
RUN /bin/bash -c "source /opt/entrypoint.sh /etc/apache2/ssl_keys localhost"       
RUN a2enmod ssl                                                                    
RUN a2ensite ssl                                                                   

CMD ["/usr/sbin/apache2ctl", "-D", "FOREGROUND"]
phpapache/ssl.conf
<VirtualHost _default_:443>    
ServerAdmin webmaster@test.com    

DocumentRoot /var/www/html        

ErrorLog ${APACHE_LOG_DIR}/error.log 
CustomLog ${APACHE_LOG_DIR}/access.log combined      

SSLEngine on       

SSLCertificateFile /etc/apache2/ssl_keys/server.crt      
SSLCertificateKeyFile /etc/apache2/ssl_keys/server.key   

<FilesMatch "\.(cgi|shtml|phtml|php)$"> 
SSLOptions +StdEnvVars        
</FilesMatch>                 
<Directory /usr/lib/cgi-bin>  
SSLOptions +StdEnvVars        
</Directory>                  

<Directory /var/www/html>     
Options Indexes FollowSymLinks
AllowOverride all       
Require all granted     
</Directory>            
DirectoryIndex index.php


BrowserMatch "MSIE [2-6]" \                                   
                 nokeepalive ssl-unclean-shutdown \                                
                 downgrade-1.0 force-response-1.0                                  
# MSIE 7 and newer should be able to use keepalive                                 
                 BrowserMatch "MSIE [17-9]" ssl-unclean-shutdown                   
                 </VirtualHost>                                                    
phpapache/entrypoint.sh
# !/bin/sh          

path=$1       
servername=$2 

mkdir -p ${path}
openssl genrsa -out ${path}/server.key 2048            
openssl req -new -key ${path}/server.key -out ${path}/server.csr -subj '/C=JP/ST=Tokyo/L=Tokyo/O=Example Ltd./OU=Web/CN='${servername}
openssl x509 -in ${path}/server.csr -days 3650 -req -signkey ${path}/server.key -out ${path}/server.crt
mysql/Dockerfile
FROM mysql:5.6 

Run

docker-composer up --build

Refs

docker-compose up -dでphp+apache+sslの環境を作る

0
0
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
0
0