docker构建php+nginx+mysql:一、安装nginx - Go语言中文社区

docker构建php+nginx+mysql:一、安装nginx


一、查找镜像

[root@iz2ze62mggaeloz5x6ucurz ~]# docker search nginx
NAME                                                   DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
nginx                                                  Official build of Nginx.                        11197               [OK]                
jwilder/nginx-proxy                                    Automated Nginx reverse proxy for docker con…   1578                                    [OK]
richarvey/nginx-php-fpm                                Container running Nginx + PHP-FPM capable of…   702                                     [OK]
jrcs/letsencrypt-nginx-proxy-companion                 LetsEncrypt container to use with nginx as p…   500                                     [OK]
webdevops/php-nginx                                    Nginx with PHP-FPM                              124                                     [OK]
kitematic/hello-world-nginx                            A light-weight nginx container that demonstr…   124                                     
zabbix/zabbix-web-nginx-mysql                          Zabbix frontend based on Nginx web-server wi…   94                                      [OK]
bitnami/nginx                                          Bitnami nginx Docker Image                      65                                      [OK]
linuxserver/nginx                                      An Nginx container, brought to you by LinuxS…   58                                      
1and1internet/ubuntu-16-nginx-php-phpmyadmin-mysql-5   ubuntu-16-nginx-php-phpmyadmin-mysql-5          50                                      [OK]
tobi312/rpi-nginx                                      NGINX on Raspberry Pi / ARM                     25                                      [OK]
nginx/nginx-ingress                                    NGINX Ingress Controller for Kubernetes         17                                      
schmunk42/nginx-redirect                               A very simple container to redirect HTTP tra…   14                                      [OK]
nginxdemos/hello                                       NGINX webserver that serves a simple page co…   13                                      [OK]
wodby/drupal-nginx                                     Nginx for Drupal container image                12                                      [OK]
blacklabelops/nginx                                    Dockerized Nginx Reverse Proxy Server.          12                                      [OK]
centos/nginx-18-centos7                                Platform for running nginx 1.8 or building n…   10                                      
centos/nginx-112-centos7                               Platform for running nginx 1.12 or building …   7                                       
nginxinc/nginx-unprivileged                            Unprivileged NGINX Dockerfiles                  4                                       
1science/nginx                                         Nginx Docker images that include Consul Temp…   4                                       [OK]
mailu/nginx                                            Mailu nginx frontend                            3                                       [OK]
travix/nginx                                           NGinx reverse proxy                             2                                       [OK]
toccoag/openshift-nginx                                Nginx reverse proxy for Nice running on same…   1                                       [OK]
ansibleplaybookbundle/nginx-apb                        An APB to deploy NGINX                          0                                       [OK]
wodby/nginx                                            Generic nginx                                   0                                       [OK]
[root@iz2ze62mggaeloz5x6ucurz ~]# 

二、docker pull

[root@iz2ze62mggaeloz5x6ucurz ~]# docker pull nginx

[root@iz2ze62mggaeloz5x6ucurz ~]# docker images nginx

[root@iz2ze62mggaeloz5x6ucurz ~]# docker images
REPOSITORY            TAG                 IMAGE ID            CREATED             SIZE
liuguangyuan/ubuntu   v2                  41405366c114        8 days ago          137MB
nginx                 latest              2bcb04bdb83f        13 days ago         109MB
hello-world           latest              fce289e99eb9        3 months ago        1.84kB
ubuntu                15.10               9b9cb95443b5        2 years ago         137MB
training/webapp       latest              6fae60ef3446        3 years ago         349MB

三、创建要挂载的文件夹

[root@iz2ze62mggaeloz5x6ucurz ~]# mkdir -p /data/nginx/{conf,conf.d,html,logs}

四、准备nginx配置文件

[root@iz2ze62mggaeloz5x6ucurz ~]vim /data/nginx/conf/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 {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    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;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;
}
[root@iz2ze62mggaeloz5x6ucurz ~]vim /data/nginx/conf.d/default.conf
server {  
    listen       80;  
    server_name  localhost;  
  
    #charset koi8-r;  
    #access_log  /var/log/nginx/log/host.access.log  main;  
  
    location / {  
        root   /data/nginx/html;  
       # root   /usr/nginx/html;  
        index  index.html index.htm;  
        autoindex  on;  
    try_files $uri /index/index/page.html;  
        #try_files $uri /index/map/page.html;  
    }  
  
    #error_page  404              /404.html;  
  
    # redirect server error pages to the static page /50x.html  
    #  
    error_page   500 502 503 504  /50x.html;  
    location = /50x.html {  
        root   /usr/share/nginx/html;  
    }  
  
    # proxy the PHP scripts to Apache listening on 127.0.0.1:80  
    #  
    #location ~ .php$ {  
    #    proxy_pass   http://127.0.0.1;  
    #}  
  
    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000  
    #  
    #location ~ .php$ {  
    #    root           html;  
    #    fastcgi_pass   127.0.0.1:9000;  
    #    fastcgi_index  index.php;  
    #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;  
    #    include        fastcgi_params;  
    #}  
  
    # deny access to .htaccess files, if Apache's document root  
    # concurs with nginx's one  
    #  
    #location ~ /.ht {  
    #    deny  all;  
    #}  
}

五、启动容器

#将容器中nginx的80端口映射到本地的80端口
[root@iz2ze62mggaeloz5x6ucurz ~]docker run --name nginx-d -p 80:80 -v /data/nginx/html:/usr/share/nginx/html -v /data/nginx/conf/nginx.conf:/etc/nginx/nginx.conf  -v /data/nginx/logs:/var/log/nginx -v /data/nginx/conf.d:/etc/nginx/conf.d -d nginx:latest

六、查看容器是否启动

[root@iz2ze62mggaeloz5x6ucurz ~]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                NAMES
e7d63ec031e2        nginx:latest        "nginx -g 'daemon of…"   18 hours ago        Up 17 hours         0.0.0.0:80->80/tcp   nginx
#如果启动失败使用docker logs 查看日志
[root@iz2ze62mggaeloz5x6ucurz ~]#docker logs  e7d63ec031e2

七、nginx是404

#是容器内的nginx配置与挂载的目录不一致  进入容器内
[root@iz2ze62mggaeloz5x6ucurz ~]# docker exec -it nginx /bin/bash
root@e7d63ec031e2:/# apt-get update  #更新docker 源
root@e7d63ec031e2:/#apt-get install vim  #速度慢请配置国内镜像


八、进入docker容器内 nginx的配置将站点目录更改为/usr/share/nginx/html与挂在一致的目录即可

#重启docker nginx
[root@iz2ze62mggaeloz5x6ucurz ~]# docker restart  nginx

九、在挂在目录下准备首页,成功访问。

在这里插入图片描述

版权声明:本文来源CSDN,感谢博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/liuchen1314/article/details/89151191
站方申明:本站部分内容来自社区用户分享,若涉及侵权,请联系站方删除。

0 条评论

请先 登录 后评论

官方社群

GO教程

猜你喜欢