分别安装nginx 和php多版本容器

发布于 2019-02-13  52 次阅读


#初始化目录结构
mkdir -p /data/nginx
mkdir -p /data/nginx/sites-enabled
mkdir -p /data/wwwroot/html
mkdir -p /data/log

/data/wwwroot/html/index.php内容:

< ?php echo PHP_VERSION; //phpinfo();

/data/nginx内容:
新建文件nginx.conf,内容如下

user nginx;
worker_processes 1;

error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;

events {
worker_connections 1024;
}

http {
sendfile on;

gzip on;
gzip_http_version 1.0;
gzip_proxied any;
gzip_min_length 500;
gzip_disable "MSIE [1-6]\.";
gzip_types text/plain text/xml text/css
text/comma-separated-values
text/javascript
application/x-javascript
application/atom+xml;
gzip_disable "msie6";

##
# Basic Settings
##

server_names_hash_bucket_size 64;

tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
client_body_buffer_size 128k;
client_max_body_size 500m;

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

##
# Logging Settings
##

access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;

##
# Virtual Host Configs
##

include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;

}

#安装nginx 和php多版本容器
(默认php:7-fpm安装的是最新版本的php7.3)
(先移除已安装的nginx容器,根据实际情况)


cd /data/wwwroot
docker run --name php5 -v $PWD:/var/www/ --restart always -d php:5.6-fpm
docker run --name php7 -v $PWD:/var/www/ --restart always -d php:7-fpm
docker run --name php72 -v $PWD:/var/www/ --restart always -d php:7.2-fpm

docker stop nginx && docker rm nginx
docker run --name nginx \
-v /data/nginx/nginx.conf:/etc/nginx/nginx.conf \
-v /etc/localtime:/etc/localtime \
-v $PWD:/var/www \
-v /data/nginx/sites-enabled:/etc/nginx/sites-enabled \
-v /data/log:/var/log/nginx \
--link php5 --link php7 --link php72 -d -p 80:80 -p 443:443 nginx

#本机hosts
vim /etc/hosts
127.0.0.1 site1 site2

/data/nginx/sites-enabled文件夹:
site1.conf

server {
listen 80 default_server;

server_name site1;

root /var/www/html;
index index.php index.html index.htm default.html default.htm;

fastcgi_buffers 8 16k;
fastcgi_buffer_size 32k;
fastcgi_read_timeout 180;

location ~ \.php$ {
fastcgi_pass php5:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SERVER_NAME $server_name;
}
}

site2.conf

server {
listen 80;

server_name site2;

root /var/www/html;
index index.php index.html index.htm default.html default.htm;

fastcgi_buffers 8 16k;
fastcgi_buffer_size 32k;
fastcgi_read_timeout 180;

location ~ \.php$ {
fastcgi_pass php7:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SERVER_NAME $server_name;
}
}

#安装php容器的额外扩展
以安装redis扩展为例:
docker exec -it php5 bash
pecl install redis && docker-php-ext-enable redis


一沙一世界,一花一天堂。君掌盛无边,刹那成永恒。