Skip to content

Commit f63e49d

Browse files
committed
first commit
0 parents  commit f63e49d

31 files changed

+5393
-0
lines changed

Diff for: .DS_Store

6 KB
Binary file not shown.

Diff for: README.md

+136
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
# 打造基于Docker的Ubuntu 14.04 + PHP-FPM + Nginx运行环境
2+
3+
### 准备工作
4+
- 安装[docker][1]、docker-compose
5+
- 更改docker[镜像源地址][2],加速访问
6+
7+
### 创建Dockerfile
8+
9+
1. 在用户目录下创建docker项目文件夹
10+
```
11+
$ mkdir -p ~/docker && cd ~/docker
12+
```
13+
2. 创建Dockerfile
14+
```
15+
$ vim Dockerfile
16+
```
17+
3. 由于我们是基于Ubuntu14.04做的镜像,所以定义好官方Ubuntu地址即可,在Dockerfile插入:`FROM ubuntu:trusty`,保存Dockerfile,然后运行`docker build -t ubuntu:14.04-php-nginx .`,如果你是第一次运行,需要从上面修改的docker源拉取ubuntu的镜像,会比较久。
18+
19+
![](media/14956799088514/14956931611685.jpg)
20+
21+
22+
4. 我们用`docker images`来看结果,会有两个镜像,一个是拉下来的`ubuntu:trusty`,一个是我们定制的镜像: `ubuntu:14.04-php-nginx`
23+
24+
![](media/14956799088514/14956932635561.jpg)
25+
26+
5. 现在我们已经拥有一个本地image,使用**交互模式**启动容器准备进行定制 `docker run -t -i ubuntu:14.04-php-nginx /bin/bash`
27+
28+
![](media/14956799088514/14956933269280.jpg)
29+
30+
6. 把它当成一台装了Ubuntu的虚拟机,该怎么样怎么样,我们准备安装PHP-FPM和Nginx及一些PHP扩展,那么正常命令是这样的:(容器以root用户登录,且默认没有sudo命令,则不需要sudo)
31+
```
32+
apt-get update
33+
apt-get install -y nginx nginx php5 php5-fpm php5-mongo php5-redis php5-gd php5-curl php5-mcrypt imagemagick php5-imagick supervisor
34+
```
35+
漫长的等待时间之后,安装完了
36+
7. 由于默认的系统是UTC时间,我们将它改为CST
37+
```
38+
rm /etc/localtime
39+
echo "Asia/Shanghai" > /etc/timezone
40+
dpkg-reconfigure -f noninteractive tzdata
41+
```
42+
成功之后会输出当前的`local time`
43+
44+
![](media/14956799088514/14956933774657.jpg)
45+
46+
8. 接下来我们不需要再安装其他软件了,那么可以删掉没有必要的apt缓存:`rm -rf /var/lib/apt/list/*`
47+
9. 由于docker是无状态的,且启动时只能保留一条`RUN`来执行我们要启动的命令,但是我们需要FPM和Nginx都一起启动,怎么办?于是我们通过`supervisor`来控制,那么我们需要配置`supervisor`
48+
supervisord配置:/etc/supervisor/conf.d/supervisord.conf
49+
```
50+
[supervisord]
51+
nodaemon=true
52+
```
53+
nginx配置:/etc/supervisor/conf.d/nginx.conf
54+
```
55+
[program:nginx]
56+
command=/usr/sbin/nginx
57+
autostart=true
58+
autorestart=true
59+
priority=10'
60+
```
61+
fpm配置:/etc/supervisor/conf.d/php-fpm.conf
62+
```
63+
[program:php-fpm]
64+
command=/usr/sbin/php5-fpm
65+
autostart=true
66+
autorestart=true
67+
priority=5'
68+
```
69+
10. 好了,需要的东西都安装配置了,接下来就剩下启动`supervisor`了,我们把这个命令放在Dockerfile来启动`CMD ["/usr/bin/supervisord", "-n", "-c", "/etc/supervisor/supervisord.conf"]`
70+
71+
### 完整的文件是这样子的
72+
```
73+
#FROM ubuntu:14.04.05
74+
FROM ubuntu:trusty
75+
76+
# 更换apt源
77+
RUN sed -i 's/http:\/\/archive\.ubuntu\.com\/ubuntu\//http:\/\/cn\.archive\.ubuntu\.com\/ubuntu\//g' /etc/apt/sources.list \
78+
&& apt-get update \
79+
&& apt-get install -y nginx php5 php5-fpm php5-mongo php5-redis php5-gd php5-curl php5-mcrypt imagemagick php5-imagick supervisor \
80+
&& rm /etc/localtime \
81+
&& echo "Asia/Shanghai" > /etc/timezone \
82+
&& dpkg-reconfigure -f noninteractive tzdata \
83+
# remove caches
84+
&& rm -rf /var/lib/apt/lists/* \
85+
# add supervisord conf before CMD
86+
&& mkdir -p /etc/supervisor/conf.d \
87+
&& echo '[supervisord] \nnodaemon=true' > /etc/supervisor/conf.d/supervisord.conf \
88+
&& echo '[program:nginx] \ncommand=/usr/sbin/nginx \nautostart=true \nautorestart=true \npriority=10' > /etc/supervisor/conf.d/nginx.conf \
89+
&& echo '[program:php-fpm] \ncommand=/usr/sbin/php5-fpm \nautostart=true \nautorestart=true \npriority=5' > /etc/supervisor/conf.d/php-fpm.conf
90+
91+
CMD ["/usr/bin/supervisord", "-n", "-c", "/etc/supervisor/supervisord.conf"]
92+
93+
```
94+
95+
### 使用方法
96+
- git pull 本工程
97+
- cd 进入`docker-compose.yml`文件所在目录(如ubuntu14.04)
98+
- `docker-compose up -d`,具体用法命令行输入`docker-compose --help`查看
99+
100+
### docker-compose修改
101+
- 修改ports端口映射
102+
- 修改目录映射
103+
- 修改www目录,将自己本机的目录共享给docker镜像
104+
- 修改log目录,以便不需要ssh进docker镜像也可以查看log
105+
> nginx/php-fpm/supervisor master process user为root,因此可以直接写入/var/log目录
106+
> php-fpm的worker process user为www-data(默认),因此将worker的error output设置成(php_errors = /tmp/php_error.log)以写入/tmp
107+
> 不建议修改系统默认文件夹权限设置,特别是将一些关键目录设置成777或者使用root权限运行程序
108+
- 修改`docker-compose.yml`文件完毕之后请运行`docker-compose down && docker-compose up -d`重新启动建立映射关系
109+
110+
### docker交互模式
111+
- 通过`docker-compose up -d`进入后台进程,可以使用`docker exec -i -t $containerId bash`进入交互模式
112+
- 通过`docker ps`查看所有运行的docker情况
113+
- 通过`docker-compose ps -q`查看当前镜像运行的containerId
114+
- 直接使用`docker exec -it $(docker-compose ps -q) bash`进入交互模式
115+
- 建议添加function到shell以便快速进入`sshdocker() { docker exec -it $(docker-compose ps -q) bash}`(alias在shell载入时会计算出结果,所以不能用alias)
116+
117+
### 其他参考
118+
- 为什么只用一条RUN命令?
119+
120+
> 每一次RUN都会导致镜像被包装一层,从而有不必要的额外信息被记录,更多详细信息参考:https://yeasy.gitbooks.io/docker_practice/content/image/build.html
121+
122+
- 有什么要注意的?
123+
124+
> supervisor启动的进程不要以daemon形式启动,所以在`php-fpm.conf``nginx.conf`都将daemon关闭了,具体详细参数请参考示例工程
125+
126+
- 示例工程:
127+
128+
> 请参考[docker-ubuntu-php-nginx][3],包含ubuntu14.04和ubuntu16.04的示例
129+
130+
- 更多docker的使用技巧请参考[官网][1][git book][4]
131+
132+
[1]: https://www.docker.com/
133+
[2]: https://c.163.com/wiki/index.php?title=DockerHub%E9%95%9C%E5%83%8F%E5%8A%A0%E9%80%9F
134+
[3]: https://github.com/CsHeng/docker-ubuntu-php-nginx
135+
[4]: https://yeasy.gitbooks.io/docker_practice/content/introduction/
136+

Diff for: media/.DS_Store

6 KB
Binary file not shown.

Diff for: media/14956799088514/14956808705455.jpg

21.2 KB
Loading

Diff for: media/14956799088514/14956809209231.jpg

23.5 KB
Loading

Diff for: media/14956799088514/14956809340343.jpg

21.9 KB
Loading

Diff for: media/14956799088514/14956811521868.jpg

35.9 KB
Loading

Diff for: media/14956799088514/14956813356352.jpg

35.9 KB
Loading

Diff for: media/14956799088514/14956820704823.jpg

11.3 KB
Loading

Diff for: media/14956799088514/14956825572710.jpg

30.5 KB
Loading

Diff for: media/14956799088514/14956931611685.jpg

21.3 KB
Loading

Diff for: media/14956799088514/14956932635561.jpg

52.5 KB
Loading

Diff for: media/14956799088514/14956933269280.jpg

13.5 KB
Loading

Diff for: media/14956799088514/14956933596248.jpg

31.1 KB
Loading

Diff for: media/14956799088514/14956933774657.jpg

34.4 KB
Loading

Diff for: ubuntu14.04/Dockerfile

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#FROM ubuntu:14.04.05
2+
FROM ubuntu:trusty
3+
4+
## 更换apt源
5+
RUN sed -i 's/http:\/\/archive\.ubuntu\.com\/ubuntu\//http:\/\/cn\.archive\.ubuntu\.com\/ubuntu\//g' /etc/apt/sources.list \
6+
&& apt-get update \
7+
&& apt-get install -y nginx php5 php5-fpm php5-mongo php5-redis php5-gd php5-curl php5-mcrypt imagemagick php5-imagick supervisor \
8+
&& rm /etc/localtime \
9+
&& echo "Asia/Shanghai" > /etc/timezone \
10+
&& dpkg-reconfigure -f noninteractive tzdata \
11+
# remove caches
12+
&& rm -rf /var/lib/apt/lists/* \
13+
# add supervisord conf before CMD
14+
&& mkdir -p /etc/supervisor/conf.d \
15+
&& echo '[supervisord] \nnodaemon=true' > /etc/supervisor/conf.d/supervisord.conf \
16+
&& echo '[program:nginx] \ncommand=/usr/sbin/nginx \nautostart=true \nautorestart=true \npriority=10' > /etc/supervisor/conf.d/nginx.conf \
17+
&& echo '[program:php-fpm] \ncommand=/usr/sbin/php5-fpm \nautostart=true \nautorestart=true \npriority=5' > /etc/supervisor/conf.d/php-fpm.conf
18+
19+
CMD ["/usr/bin/supervisord", "-n", "-c", "/etc/supervisor/supervisord.conf"]

Diff for: ubuntu14.04/README.md

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# ubuntu 14.04/trusty 基础镜像定制
2+
3+
- 修改apt源
4+
- 修改时区为CST
5+
- 安装nginx php-fpm supervisor
6+
- 安装PHP mongo/redis等库,具体请看`Dockerfile`
7+
8+
### build本地镜像
9+
- `docker build -t ubuntu:14.04-php-nginx .`
10+
11+
### push到仓库
12+
- login `docker login --username=[用户名] [仓库地址]`(本地登录Docker registry,后续的push、pull操作需要)
13+
- tag `docker tag ubuntu:14.04-php-nginx [仓库地址]/ubuntu:14.04-php-nginx`
14+
- push `docker push [仓库地址]/ubuntu:14.04-php-nginx`
15+
16+
### 使用本镜像的Dockerfile
17+
- `FROM [仓库地址]/ubuntu:14.04-php-nginx`
18+
- 镜像默认已启动命令:`CMD ["/usr/bin/supervisord", "-n", "-c", "/etc/supervisor/supervisord.conf"]`

Diff for: ubuntu14.04/docker-compose.yml

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
version: '3'
2+
services:
3+
web:
4+
tty: true
5+
build: .
6+
image: ubuntu:14.04-php5.5.9
7+
ports:
8+
- "80:80"
9+
volumes:
10+
# php日志映射
11+
# - /Users/CsHeng/Downloads/log:/tmp
12+
# nginx/php-fpm/supervisor日志映射
13+
# - /Users/CsHeng/Downloads/log:/var/log
14+
# 代码文件夹映射,注意修改nginx default conf
15+
# - /Users/CsHeng/Workspace/Web/server-api:/www/api
16+
- ./nginx/nginx.conf:/etc/nginx/nginx.conf
17+
- ./nginx/sites-enabled/default:/etc/nginx/sites-enabled/default
18+
# - ./php5/fpm/php.ini:/etc/php5/fpm/php.ini
19+
- ./php5/fpm/php-fpm.conf:/etc/php5/fpm/php-fpm.conf
20+
- ./php5/fpm/pool.d/www.conf:/etc/php5/fpm/pool.d/www.conf

Diff for: ubuntu14.04/nginx/nginx.conf

+101
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
user www-data;
2+
worker_processes 4;
3+
pid /run/nginx.pid;
4+
5+
daemon off;
6+
7+
events {
8+
worker_connections 768;
9+
# multi_accept on;
10+
}
11+
12+
http {
13+
14+
##
15+
# Basic Settings
16+
##
17+
18+
sendfile on;
19+
tcp_nopush on;
20+
tcp_nodelay on;
21+
keepalive_timeout 65;
22+
types_hash_max_size 2048;
23+
# server_tokens off;
24+
25+
# server_names_hash_bucket_size 64;
26+
# server_name_in_redirect off;
27+
28+
include /etc/nginx/mime.types;
29+
default_type application/octet-stream;
30+
31+
##
32+
# Logging Settings
33+
##
34+
35+
#log_format scripts '$document_root$fastcgi_script_name > $request';
36+
37+
#access_log /www/log/nginx/access.log;
38+
#error_log /www/log/nginx/error.log;
39+
access_log /var/log/nginx/access.log;
40+
error_log /var/log/nginx/error.log;
41+
42+
##
43+
# Gzip Settings
44+
##
45+
46+
gzip on;
47+
gzip_disable "msie6";
48+
49+
# gzip_vary on;
50+
# gzip_proxied any;
51+
# gzip_comp_level 6;
52+
# gzip_buffers 16 8k;
53+
# gzip_http_version 1.1;
54+
# gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
55+
56+
##
57+
# nginx-naxsi config
58+
##
59+
# Uncomment it if you installed nginx-naxsi
60+
##
61+
62+
#include /etc/nginx/naxsi_core.rules;
63+
64+
##
65+
# nginx-passenger config
66+
##
67+
# Uncomment it if you installed nginx-passenger
68+
##
69+
70+
#passenger_root /usr;
71+
#passenger_ruby /usr/bin/ruby;
72+
73+
##
74+
# Virtual Host Configs
75+
##
76+
77+
include /etc/nginx/conf.d/*.conf;
78+
include /etc/nginx/sites-enabled/*;
79+
}
80+
81+
82+
#mail {
83+
# # See sample authentication script at:
84+
# # http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript
85+
#
86+
# # auth_http localhost/auth.php;
87+
# # pop3_capabilities "TOP" "USER";
88+
# # imap_capabilities "IMAP4rev1" "UIDPLUS";
89+
#
90+
# server {
91+
# listen localhost:110;
92+
# protocol pop3;
93+
# proxy on;
94+
# }
95+
#
96+
# server {
97+
# listen localhost:143;
98+
# protocol imap;
99+
# proxy on;
100+
# }
101+
#}

Diff for: ubuntu14.04/nginx/sites-enabled/default

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
server {
2+
listen 80 default_server;
3+
#listen [::]:80 default_server ipv6only=on;
4+
5+
root /www/api/application;
6+
index index.php index.html index.htm;
7+
8+
server_name localhost;
9+
10+
location ~ \.php$ {
11+
fastcgi_split_path_info ^(.+\.php)(/.+)$;
12+
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
13+
fastcgi_pass unix:/var/run/php5-fpm.sock;
14+
#fastcgi_pass 127.0.0.1:9000;
15+
fastcgi_index index.php;
16+
include fastcgi_params;
17+
}
18+
19+
if (!-e $request_filename) {
20+
rewrite /v2d5/(.*)$ /v2d5/index.php?$1 last;
21+
rewrite /(.*)$ /legacy/index.php?$1 last;
22+
#rewrite /(.*)$ /index.php?$1 last;
23+
break;
24+
}
25+
26+
}

0 commit comments

Comments
 (0)