Docker build PHP environment tutorial detailed explanation

Docker build PHP environment tutorial detailed explanation

Docker installation

Use the official installation script to install the latest version of Docker

curl -sSL https://get.docker.com/ | sh

After the installation is complete, start the Docker daemon with the following command and let it automatically load when the system starts

sudo service docker start
sudo chkconfig docker on
## or
sudo systemctl start docker
sudo systemctl enable docker

Add user (jerry) to the Docker group

sudo usermod -aG docker jerry

Command notes (centos), based on docker image 2233466866/lnmp

Download image

docker pull 2233466866/lnmp

Create a base directory

mkdir -p /app/lnmp/default /docker/lnmp/data/mysql /docker/lnmp/conf/vhost /docker/lnmp/logs /docker/lnmp/temp /docker/lnmp/backup

Download the container configuration file to the local corresponding directory

docker run -itd -v /sys/fs/cgroup:/sys/fs/cgroup:ro --privileged=true --name=lnmp 2233466866/lnmp
docker exec -it lnmp /bin/bash
docker cp lnmp:/etc/my.cnf /docker/lnmp/conf/my.cnf
docker cp lnmp:/usr/local/nginx/conf/nginx.conf /docker/lnmp/conf/nginx.conf
cp /docker/lnmp/conf/my.cnf /docker/lnmp/backup/my.cnf
cp /docker/lnmp/conf/nginx.conf /docker/lnmp/backup/nginx.conf

View or modify basic configuration (code directory, log storage directory and vhost directory configuration addition)

my.cnf

[mysqld]
datadir=/data/mysql
socket=/var/lib/mysql/mysql.sock
symbolic-links=0
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid

nginx.conf

user www;
worker_processes auto;
worker_cpu_affinity auto;


worker_cpu_affinity auto;
pid logs/nginx.pid;

events {
  worker_connections 102400;
}

http {
  charset utf-8;
  server_tokens off;

  log_format main '$remote_addr - $remote_user [$time_iso8601] "$request" '
            '$status $body_bytes_sent "$http_referer" '
            '"$http_user_agent" "$http_x_forwarded_for"';

  include mime.types;
  default_type application/octet-stream;

  client_max_body_size 20M;

  sendfile on;
  keepalive_timeout 20;

  gzip on;
  gzip_vary on;
  gzip_comp_level 1;
  gzip_types text/css application/javascript application/json image/png image/webp image/apng image/jpeg image/x-icon;

  autoindex_localtime on

  error_log /logs/z_error.log;
  access_log /logs/z_$host.log main;

  server {
    listen 80 default;
    root /www/default;
    return 500;
  }
  include vhost/*.conf;
}

Stop and delete the test container

docker stop lnmp
docker rm lnmp

Recreate the container

docker run -dit \
-p 80:80 \
-p 443:443 \
-p 3306:3306 \
-p 9000:9000 \
-e TC="Asia/Shanghai" \
-v /sys/fs/cgroup:/sys/fs/cgroup:ro \
-v /app/lnmp:/www \
-v /docker/lnmp/data/mysql:/data/mysql \
-v /docker/lnmp/conf/my.cnf:/etc/my.cnf \
-v /docker/lnmp/conf/nginx.conf:/usr/local/nginx/conf/nginx.conf \
-v /docker/lnmp/conf/vhost:/usr/local/nginx/conf/vhost \
-v /docker/lnmp/logs:/logs \
--privileged=true \
--name=lnmp \
2233466866/lnmp

mysql database configuration (/etc/my.cnf)

/bin/mysql_secure_installation
cat /var/log/mysqld.log|grep 'A temporary password'
SET PASSWORD = PASSWORD('123456');

php.ini configuration (/usr/local/php7/lib/php.ini)

mysqli.default_socket = /var/lib/mysql/mysql.sock

Linux related settings modification

Add user www who cannot log in

groupadd www
useradd -M -g www -s /usr/sbin/nologin www

Change time zone (/etc/profile)

TZ='Asia/Shanghai'; export TZ
cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

Multisite configuration

www.test.test.conf

server {
  listen 80;
  server_name test.test;

  rewrite ^(.*)$ $scheme://www.test.test$1 permanent;
}
server {
  listen 80;
  server_name www.test.test;

  if ($time_iso8601 ~ "^(\d{4}-\d{2}-\d{2})") {
    set $ttt $1;
  }
  access_log /logs/$host-$ttt-access.log main;

  root /www/test;

  location / {
    index index.php index.html index.htm;
  }

  location ~* \.php {
    include fastcgi_params;
    fastcgi_index index.php;
    fastcgi_pass 127.0.0.1:9000;
    fastcgi_split_path_info ^(.+\.php)(.*)$;
    fastcgi_param PATH_INFO $fastcgi_path_info;
    fastcgi_param SCRIPT_NAME $fastcgi_script_name;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  }

}

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • How to configure PHP development environment through docker on Mac
  • Docker installation of PHP and deployment example with Nginx
  • Explanation of the steps to install PHP extension in Docker
  • How to use Docker to build OpenLDAP+phpLDAPadmin unified user authentication
  • Detailed tutorial on installing php-fpm service/extension/configuration in docker
  • Docker's flexible implementation of building a PHP environment
  • How to deploy LNMP & phpMyAdmin in docker
  • PHP uses docker to run workerman case explanation

<<:  Some problems you may encounter when installing MySQL

>>:  JavaScript adds event listeners to event delegation in batches. Detailed process

Recommend

JS implements sliding up and down on the mobile terminal one screen at a time

This article shares with you the specific code of...

JavaScript implements simple calculator function

This article shares the specific code of JavaScri...

How to install Windows Server 2008 R2 on Dell R720 server

Note: All pictures in this article are collected ...

VUE realizes registration and login effects

This article example shares the specific code of ...

Gojs implements ant line animation effect

Table of contents 1. Gojs Implementation 1. Drawi...

Vue implements simple notepad function

This article example shares the specific code of ...

Node.js implements breakpoint resume

Table of contents Solution Analysis slice Resume ...

MySQL detailed single table add, delete, modify and query CRUD statements

MySQL add, delete, modify and query statements 1....

How to display small icons in the browser title bar of HTML webpage

Just like this effect, the method is also very si...

Nginx uses reverse proxy to implement load balancing process analysis

Introduction Based on docker container and docker...

Detailed explanation of MySQL combined index method

For any DBMS, indexes are the most important fact...

Vue batch update dom implementation steps

Table of contents Scene Introduction Deep respons...

Analyzing ab performance test results under Apache

I have always used Loadrunner to do performance t...