Docker安装一个LNMP环境全过程

本人的工作环境:Mac一台,里面安装虚拟机ubuntu,在ubuntu上安装docker。虚拟机ubuntu IP 192.168.33.12

安装一个LNMP环境
vagrant@dragon:~$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
ubuntu18            apache24php72       15f84056ba6f        7 days ago          275MB
ubuntu              latest              93fd78260bd1        11 days ago         86.2MB
mysql               latest              f991c20cb508        2 weeks ago         486MB
centos              latest              75835a67d134        7 weeks ago         200MB
#本地已经有一个ubuntu18干净的镜像
vagrant@dragon:~$ docker run -p 80:80 -it ubuntu:latest
root@fd2f5316cfb5:/# apt-get update
...
安装nginx
root@fd2f5316cfb5:/# apt-get install nginx
...
root@fd2f5316cfb5:/# nginx -v
nginx version: nginx/1.14.0 (Ubuntu)
root@fd2f5316cfb5:~# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
root@fd2f5316cfb5:~# service nginx restart
 * Restarting nginx nginx                                                                                                                    [ OK ]
root@fd2f5316cfb5:~#

在浏览器输入http://192.168.33.12,可以看到Welcome to nginx! 表示nginx安装成功。

安装php7.2
root@fd2f5316cfb5:~# apt-get install php7.2
...
root@fd2f5316cfb5:~# php -v
PHP 7.2.10-0ubuntu0.18.04.1 (cli) (built: Sep 13 2018 13:45:02) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies
    with Zend OPcache v7.2.10-0ubuntu0.18.04.1, Copyright (c) 1999-2018, by Zend Technologies
#安装php常用扩展
root@fd2f5316cfb5:~# apt-get install php7.2-fpm php7.2-mysql php7.2-curl php7.2-json php7.2-mbstring php7.2-xml  php7.2-intl php7.2-gd php7.2-cgi  php7.2-opcache php7.2-zip
配置nginx, 让nginx可以启用php解释器,使php执行。
使用php-cgi tcp 方式通信
#备份
root@fd2f5316cfb5:~# cp sites-available/default sites-available/default.bak
root@fd2f5316cfb5:~# vi sites-available/default
nignx 配置
# 添加自动索引文件
index index.php index.html index.htm index.nginx-debian.html;
#项目根目录
root /var/www/html;
#域名
server_name localhost;
#根目录请求,未找到404报错
location / {
        try_files $uri $uri/ =404;
}
#使用tcp方式,使php-cgi与nginx通信,端口9000
location ~ \.php$ {
        include snippets/fastcgi-php.conf;
#
#       # With php-fpm (or other unix sockets):
#        fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
#       # With php-cgi (or other tcp sockets):
       fastcgi_pass 127.0.0.1:9000;
}
#禁止.htaccess
location ~ /\.ht {
    deny all;
}
测试
#重启nginx,添加检验文件
root@fd2f5316cfb5:~# service nginx restart
root@fd2f5316cfb5:/var/www/html# vi test.php

浏览器访问http://192.168.33.12/test.php, 502 Bad Gateway 报错

查看Fastcgi 的端口, 查看是否有127.0.0.1:9000,没有,开启9000端口
root@fd2f5316cfb5:/etc/nginx/sites-available# netstat -antp
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      17496/nginx: master
tcp        0      0 172.17.0.2:80           192.168.33.1:62329      ESTABLISHED -
tcp        0      0 172.17.0.2:80           192.168.33.1:62320      TIME_WAIT   -
tcp        0      0 172.17.0.2:80           192.168.33.1:62330      ESTABLISHED -
tcp        0      0 172.17.0.2:80           192.168.33.1:62328      TIME_WAIT   -
tcp6       0      0 :::80                   :::*                    LISTEN      17496/nginx: master
root@fd2f5316cfb5:/etc/nginx/sites-available# php-cgi -b 127.0.0.1:9000
[1] 17500
root@fd2f5316cfb5:/etc/nginx/sites-available# netstat -antp
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0 127.0.0.1:9000          0.0.0.0:*               LISTEN      17500/php-cgi
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      17496/nginx: master
tcp6       0      0 :::80                   :::*                    LISTEN      17496/nginx: master
root@fd2f5316cfb5:/etc/nginx/sites-available#

重新访问http://192.168.33.12/test.php,获得php的所有配置文件信息,nginx和php连接成功。

或者使用php-fpm sock 方式,使nginx php-fpm 通信
#打开使用php-fpm sock方式,与php通信,更新nginx配置
location ~ \.php$ {
        include snippets/fastcgi-php.conf;
#
#       # With php-fpm (or other unix sockets):
        fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
#       # With php-cgi (or other tcp sockets):
#      fastcgi_pass 127.0.0.1:9000;
}                   
root@fd2f5316cfb5:/etc/nginx/sites-available# service nginx restart
root@fd2f5316cfb5:/etc/nginx/sites-available# ps aux | grep php-fpm
root     17604  0.0  0.0  11464  1008 ?        S+   14:29   0:00 grep --color=auto php-fpm

浏览器访问http://192.168.33.12/test.php, 502 Bad Gateway 报错

root@fd2f5316cfb5:/etc/php/7.2/fpm/pool.d# service php7.2-fpm status
 * php-fpm7.2 is not running
#查看配置文件
root@fd2f5316cfb5:/etc/php/7.2/fpm/pool.d# ls
www.conf
#查看是否监听socket
listen = /run/php/php7.2-fpm.sock
#去掉分号;使用当前用户组访问
listen.mode = 0660
root@fd2f5316cfb5:/etc/php/7.2/fpm/pool.d# service php7.2-fpm restart
 * Restarting PHP 7.2 FastCGI Process Manager php-fpm7.2                                                                                     [ OK ]
root@fd2f5316cfb5:/etc/php/7.2/fpm/pool.d# service php7.2-fpm status
 * php-fpm7.2 is running

浏览器访问http://192.168.33.12/test.php, 502 Bad Gateway 仍旧是报错, 查看php7.2-fpm.log没有异常,查看nginx error.log, 有一个权限报错。

root@fd2f5316cfb5:/var/log# tail -f php7.2-fpm.log
root@fd2f5316cfb5:/var/log/nginx# tail -f error.log
2018/12/01 17:19:28 [crit] 17753#17753: *21 connect() to unix:/var/run/php/php7.2-fpm.sock failed (13: Permission denied) while connecting to upstream, client: 192.168.33.1, server: localhost, request: "GET /test.php HTTP/1.1", upstream: "fastcgi://unix:/var/run/php/php7.2-fpm.sock:", host: "192.168.33.12"
注意到nginx.conf 用户是www-data, 修改user root;重启nignx,[http://192.168.33.12/test.php]
可以访问了。说明是用户权限的问题。
...
user www-data;
worker_processes auto;
...
查看/etc/php/7.2/fpm/pool.d/www.conf,  找到sock的执行目录
listen = /run/php/php7.2-fpm.sock
root@fd2f5316cfb5:/run/php# ls -al
total 12
drwxr-xr-x  2 www-data root 4096 Dec  1 17:50 .
drwxr-xr-x 12 root     root 4096 Dec  1 18:12 ..
-rw-r--r--  1 root root    5 Dec  1 17:50 php7.2-fpm.pid
srw-rw----  1 root root    0 Dec  1 17:50 php7.2-fpm.sock
#把php的执行文件都加到www-data用户和组里面
root@fd2f5316cfb5:/run# chown www-data:www-data -R php
root@fd2f5316cfb5:/run/php# ls -al
total 12
drwxr-xr-x  2 www-data www-data 4096 Dec  1 18:21 .
drwxr-xr-x 12 root     root     4096 Dec  1 18:12 ..
-rw-r--r--  1 www-data www-data    5 Dec  1 18:21 php7.2-fpm.pid
srw-rw----  1 www-data www-data    0 Dec  1 18:21 php7.2-fpm.sock
root@fd2f5316cfb5:/run/php#

浏览器访问http://192.168.33.12/test.php, 能够看到php的配置文件了。php-fpm开始运行了。

安装mysql, 安装默认的mysql5.7

root@fd2f5316cfb5:~# apt-get install mysql-server
root@fd2f5316cfb5:~# apt-get install mysql-client
root@fd2f5316cfb5:~# apt-get install libmysqlclient-dev
...
root@fd2f5316cfb5:~# service mysql start
 * Starting MySQL database server mysqld                                                                                                            No directory, logging in with HOME=/
                                                                                                                                             [ OK ]
#登录mysql
root@fd2f5316cfb5:~# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 6
Server version: 5.7.24-0ubuntu0.18.04.1 (Ubuntu)

Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.00 sec)
root@fd2f5316cfb5:~# ps aux | grep mysql
mysql    20200  0.0  0.0   4628  1664 ?        S    18:36   0:00 /bin/sh /usr/bin/mysqld_safe
mysql    20550  0.1  8.7 1154572 178496 ?      Sl   18:36   0:00 /usr/sbin/mysqld --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib/mysql/plugin --log-error=/var/log/mysql/error.log --pid-file=/var/run/mysqld/mysqld.pid --socket=/var/run/mysqld/mysqld.sock --port=3306 --log-syslog=1 --log-syslog-facility=daemon --log-syslog-tag=
root     20626  0.0  0.0  11464  1072 ?        S+   18:40   0:00 grep --color=auto mysql
服务器根目录加入测试代码,测试mysql连接
close(); 

浏览器可以看到 连接成功!的输出。mysql和php通信成功。

保存docker镜像
vagrant@dragon:/vagrant$ docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS                NAMES
fd2f5316cfb5        ubuntu:latest       "/bin/bash"         1 hours ago         Up 7 hours          0.0.0.0:80->80/tcp   dreamy_ptolemy
vagrant@dragon:/vagrant$ docker commit fd2f5316cfb5 ubuntu18:nginx114php72mysql57
vagrant@dragon:/vagrant$ docker images
REPOSITORY          TAG                    IMAGE ID            CREATED             SIZE
ubuntu18            nginx114php72mysql57   7f183e09be60        29 seconds ago      687MB
ubuntu18            apache24php72          15f84056ba6f        8 days ago          275MB
ubuntu              latest                 93fd78260bd1        11 days ago         86.2MB
mysql               latest                 f991c20cb508        2 weeks ago         486MB
centos              latest                 75835a67d134        7 weeks ago         200MB
vagrant@dragon:/vagrant$

你可能感兴趣的:(Docker安装一个LNMP环境全过程)