docker部署nginx、php-fpm
假设机器上已经安装好docker环境
⼀.运⾏nginx容器
1.查Docker Hub 上的nginx 镜像
runoob@runoob:~/nginx$ docker search nginx
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
nginx Official build of Nginx. 3260 [OK]
jwilder/nginx-proxy Automated Nginx reverse proxy for 674 [OK]
richarvey/nginx-php-fpm Container running Nginx + PHP-FPM capable ... 207 [OK]
million12/nginx-php Nginx + PHP-FPM 5.5, 5.6, 7.0 (NG), 67 [OK]
maxexcloo/nginx-php Docker framework container with Nginx and ... 57 [OK]
webdevops/php-nginx Nginx with PHP-FPM 39 [OK]
h3nrik/nginx-ldap NGINX web server with LDAP/AD, SSL 27 [OK]
bitnami/nginx Bitnami nginx Docker Image 19 [OK]
maxexcloo/nginx Docker framework container with 7 [OK]
...
2.拉取官⽅镜像
runoob@runoob:~/nginx$ docker pull nginx
3.查看本地镜像
runoob@runoob:~/nginx$ docker images nginx
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest 555bbd91e13c 3 days ago 182.8 MB
4.创建html静态⽂件,运⾏nginx 容器
1)新建⼀个html⼦⽬录。
mkdir html
2)在这个⼦⽬录⾥⾯,放置⼀个index.html⽂件,内容如下。
<h1>Hello World</h1>
docker container run \
-d \
-p 80:80 \
--rm \
--name mynginx \
--volume "$PWD/html":/usr/share/nginx/html \
nginx
3)打开浏览器访问127.0.0.1,应该就能看到 Hello World 了。
mkdir /Users/playcrab/nginx-docker-demo
cd /Users/playcrab/nginx-docker-demo
mkdir php
mkdir
5.配置nginx配置⽂件运⾏nginx容器,查看nginx是否起作⽤
1)把容器⾥⾯的 Nginx 配置⽂件拷贝到本地。
$ docker container cp mynginx:/etc/nginx .
说明:上⾯命令的含义是,把mynginx容器的/etc/nginx拷贝到当前⽬录。不要漏掉最后那个点。执⾏完成后,当前⽬录应该多出⼀个nginx⼦⽬录。然后,把这个⼦⽬录改名为conf。
2)执⾏完成后,当前⽬录应该多出⼀个nginx⼦⽬录。然后,把这个⼦⽬录改名为conf。
$ mv nginx conf
3)进⼊配置⽂件⽬录编辑本地配置⽂件,此⽂件会被映射到容器的对应⽬录⾥
server {
listen 80;
server_name localhost;
location / {
root /usr/share/nginx/html;//容器的⽬录
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;//容器的静态⽂件⽬录
}
}
4)cd /Users/playcrab/nginx-docker-demo,运⾏如下命令
docker container run \
-d \
-p 80:80 \
--rm \
--name mynginx \
--volume "$PWD/html":/usr/share/nginx/html \
--volume "$PWD/conf":/etc/nginx\
nginx
ps:$PWD是当前⽬录的意思
在浏览器查看127.0.0.1可以正常访问
6.配置与php容器通信的nginx容器
1)编辑f配置⽂件
server {
listen 80;
server_name docker;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
location ~ \.php$ {
fastcgi_pass php:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /var/www/html/$fastcgi_script_name;
include fastcgi_params;
}
}
ps: /var/www/html/ 容器中的php⽂件⽬录,会在运⾏php-fpm容器时⽤到
2)运⾏如下命令
docker container run \
--rm \
--name mynginx \
--volume "$PWD/html":/usr/share/nginx/html \
--volume "$PWD/conf":/etc/nginx \
-p 80:80 \
--link myphp-fpm:php \
-d \
nginx
ps:
--link myphp-fpm:php 把 myphp-fpm 的⽹络并⼊mynginx,并通过修改mynginx的 /etc/hosts,把域名php映射成 127.0.0.1,让 nginx 通过 php:9000 访问 php-fpm。
3)配置本地host⽂件,加⼊下⾯代码
127.0.0.1 docker
⼆、运⾏php容器
1.查Docker Hub 上的php 镜像
zhangxueqing:~ playcrab$ docker search php
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
php While designed for web development, the PHP … 3987
[OK]
phpmyadmin/phpmyadmin A web interface for MySQL and MariaDB. 642 [OK] richarvey/nginx-php-fpm Container running Nginx + PHP-FPM capable of… 634 [OK] composer Composer is a dependency manager written in … 292 [OK]
adminer Database management in a single PHP file. 180 [OK]
php-zendserver Zend Server - the integrated PHP application… 155 [OK]
webdevops/php-nginx Nginx with PHP-FPM 117 [OK]
webdevops/php-apache-dev PHP with Apache for Development (eg. with xd… 82 [OK] webdevops/php-apache Apache with PHP-FPM (based on webdevops/php) 74 [OK] phpunit/phpunit PHPUnit is a programmer-oriented testing fra… 63 [OK]
bitnami/php-fpm Bitnami PHP-FPM Docker Image 61 [OK]
nazarpc/phpmyadmin phpMyAdmin as Docker container, based on off… 58 [OK]
1and1internet/ubuntu-16-nginx-php-phpmyadmin-mysql-5 ubuntu-16-nginx-php-phpmyadmin-mysql-5 47 [OK] tetraweb/php PHP 5.5, 5.6, 7.0, 7.1 for CI and running te… 30 [OK]
wodby/drupal-php PHP for Drupal 25 [OK]
circleci/php CircleCI images for PHP 20
centos/php-56-centos7 Platform for building and running PHP 5.6 ap (16)
clinta/phpipam phpIPAM web IP address management applicatio… 16 [OK]
antage/apache2-php5 Docker image for running Apache 2.x with PHP… 11 [OK]
graze/php-alpine Smallish php7 alpine image with some common … 11 [OK]
1and1internet/ubuntu-16-nginx-php-phpmyadmin-mariadb-10 ubuntu-16-nginx-php-phpmyadmin-mariadb-10 9 [OK] appsvc/php Azure App Service php dockerfiles 6 [OK]
phpspec/phpspec PHPSpec: A php toolset to drive emergent des… 5 [OK]
lephare/php PHP container 4 [OK]
isotopab/php Docker PHP 0 [OK]
2.拉取官⽅镜像,标签为phpdockerio/php71-fpm
nginx 配置文件zhangxueqing:~ playcrab$ docker pull phpdockerio/php71-fpm
3.本地创建php⽬录,创建index.php⽂件,⽂件内容如下
<?php
echo phpinfo();
4运⾏容器
docker run -p 9000:9000 --name myphp-fpm -v "$PWD/php":/var/www/html -d phpdockerio/php71-fpm
6.浏览器访问查看运⾏结果
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论