Ubuntu 使用 Docker 搭建 Nginx+PHP 环境

0x01. 添加用户

1
adduser username

0x02. 修改 sudoers

1
2
3
# chmod a+w /etc/sudoers
# username (ALL:ALL) ALL
# chmod a-w /etc/sudoers

0x03. 禁止 root 登录

1
2
3
vim /etc/ssh/sshd_config
# PermitRootLogin yes
PermitRootLogin no

0x04. Ubuntu 安装 Docker-CE

1
2
3
4
5
6
7
8
9
10
11
12
lsb_release -cs //查看发版编号
sudo apt-get -y install apt-transport-https ca-certificates curl
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
# 如果提示为找到 add-apt-repository command
# sudo apt-get install software-properties-common
sudo apt-get update
sudo apt-get -y install docker-ce

0x05. 安装 Docker-composer

注意要去官网查看当前的最新版本替换掉 {version}

1
2
3
4
curl -L https://github.com/docker/compose/releases/download/{version}/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

0x06. 配置 nginx ,大致有这么几个配置文件

  • /path/to/nginx/nginx.conf

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    #user root;
    worker_processes 4;
    events {
    worker_connections 1024;
    }
    http {
    include mime.types;
    default_type application/octet-stream;
    server_names_hash_bucket_size 128;
    client_header_buffer_size 32k;
    large_client_header_buffers 4 32k;
    client_max_body_size 50m;
    sendfile on;
    tcp_nopush on;
    keepalive_timeout 60;
    tcp_nodelay on;
    fastcgi_connect_timeout 300;
    fastcgi_send_timeout 300;
    fastcgi_read_timeout 300;
    fastcgi_buffer_size 64k;
    fastcgi_buffers 4 64k;
    fastcgi_busy_buffers_size 128k;
    fastcgi_temp_file_write_size 256k;
    log_format main '$remote_addr - $remote_user [$time_local] "$request" '
    '$status $body_bytes_sent "$http_referer" '
    '"$http_user_agent" "$http_x_forwarded_for"';
    access_log /var/log/nginx/access.log main;
    error_log /var/log/nginx/error.log;
    gzip on;
    gzip_min_length 1k;
    gzip_buffers 4 16k;
    gzip_http_version 1.0;
    gzip_comp_level 6;
    gzip_types text/plain text/css text/javascript application/json application/javascript application/x-javascript application/xml;
    gzip_vary on;
    include servers/*.conf;
    }
  • 文件 /path/to/nginx/conf.d/php-fpm.conf

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
location ~ \.php$ {
try_files $uri = 404;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_intercept_errors on;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param QUERY_STRING $query_string;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param CONTENT_LENGTH $content_length;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_param REQUEST_URI $request_uri;
fastcgi_param DOCUMENT_URI $document_uri;
fastcgi_param DOCUMENT_ROOT $document_root;
fastcgi_param SERVER_PROTOCOL $server_protocol;
fastcgi_param HTTPS $https if_not_empty;
fastcgi_param GATEWAY_INTERFACE CGI/1.1;
fastcgi_param SERVER_SOFTWARE nginx/$nginx_version;
fastcgi_param REMOTE_ADDR $remote_addr;
fastcgi_param REMOTE_PORT $remote_port;
fastcgi_param SERVER_ADDR $server_addr;
fastcgi_param SERVER_PORT $server_port;
fastcgi_param SERVER_NAME $server_name;
# PHP only, required if PHP was built with --enable-force-cgi-redirect
fastcgi_param REDIRECT_STATUS 200;
}
  • 文件 /path/to/nginx/servers/default.conf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
server {
listen 80;
server_name mailan.shop www.mailan.shop;
root /usr/share/nginx/html/coming/;
access_log /var/log/mailan/access.log;
error_log /var/log/mailan/error.log;
location / {
#try_files $uri $uri/ /index.php$is_args$args;
index index.html index.php;
}
# include /usr/local/etc/nginx/conf.d/php-fpm.conf;
}

0x07 .编辑 nginx 的 docker-compose

1
2
3
4
5
6
7
8
9
10
11
12
version: '3.0'
services:
nginx-box:
image: nginx:latest
volumes:
- /home/www:/usr/share/nginx/html
- /usr/local/etc/nginx/nginx.conf:/etc/nginx/nginx.conf:ro
- /usr/local/etc/nginx/conf.d:/etc/nginx/conf.d:ro
- /usr/local/etc/nginx/servers:/etc/nginx/servers:ro
- /home/logs:/var/log
ports:
- "80:80"

0x08. 添加 PHP container支持

  1. build 自己的 PHP镜像,安装 memcached 和 pdo_mysql
  2. 配置 php-fpm.conf 和 php.ini
1
2
3
4
5
6
7
8
9
10
php-box:
image: php-mysql:latest
volumes:
- /home/www:/usr/share/nginx/html
- /usr/local/etc/php/php.ini:/usr/local/etc/php/php.ini
- /usr/local/etc/php/php-fpm.conf:/usr/local/etc/php-fpm.conf
- /usr/local/etc/php/www.pool.conf:/usr/local/etc/php-fpm.d/www.conf
- /home/logs/php:/usr/local/var/log
expose:
- "9000"