使用Docker搭建LNMP环境以及安装eolinker

  • 使用Docker搭建LNMP环境以及安装eolinker已关闭评论
  • 421 views
  • A+
所属分类:docker

docker的推出,确实帮我们省了不少事,起码好多软件不用再手动安装了,今天我通过docker搭建了一个LNMP环境,节约了好多时间。

环境:

操作系统:centos7

docker版本:17.03.2

nginx版本:1.10.3

php版本:7.0

mysql版本:5.6

docker安装:

参考 CentOS 7上安装配置Docker

MySQL镜像配置:

首先,我们从仓库拉取MySQL的镜像,如下:

[root@bogon ~]# docker pull mysql:5.6
5.6: Pulling from library/mysql
8176e34d5d92: Already exists 
17e372a8ec90: Already exists 
47b869561d3a: Already exists 
c90ab4483f28: Already exists 
d6af16572c5c: Already exists 
7a9bfd3d8b1a: Pull complete 
1a41c756aa77: Pull complete 
7f9c172f85d3: Pull complete 
a7b9665f817b: Pull complete 
5de94f1b7d21: Pull complete 
6093d58fb454: Pull complete 
Digest: sha256:ed2bb560e3188817a3c838ce50e5ac9af520e046d660747dfa6fd15296f1a911
Status: Downloaded newer image for mysql:5.6

查看下载的镜像,如下:

[root@bogon html]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql 5.6 0248eeb807c7 2 weeks ago 256 MB

镜像下载下来后,我们需要创建一个phpfpm容器,命令如下:

docker run -d -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123456 --name lnmp_mysql mysql:5.6
参数说明 
-d 让容器在后台运行 
-p 添加主机到容器的端口映射 
-e 设置环境变量,这里是设置mysql的root用户的初始密码,这个必须设置 
–name 容器的名字,随便取,但是必须唯一
注:目录也可以映射指到的目录,用-v参数
docker run -d -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123456 -v /data/mysql/conf:/etc/mysql -v /data/mysql/data:/var/lib/mysql --name lnmp_mysql mysql:5.6

好了,启动成功了,我们需要通过命令查看一下刚才启动的容器

[root@bogon eolinker]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0dca921885dc mysql:5.6 "docker-entrypoint..." 2 hours ago Up 2 hours 0.0.0.0:3307->3306/tcp heyuan_mysql

这里我们可以看到我的容器状态的Up状态,表示容器正在运行,并且把可以看到主机和容器的端口映射关系。

我们也可以的登录到刚刚创建的容器中,测试一下,如下:

[root@bogon eolinker]# docker exec -ti lnmp_mysql /bin/bash
root@0dca921885dc:/# mysql -uroot -p123456
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.6.39 MySQL Community Server (GPL)

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>
参数说明 
-t 在容器里生产一个伪终端 
-i 对容器内的标准输入 (STDIN) 进行交互

容器中默认是没有vi的,所以我们首先要安装vi,需要注意的是安装前记得先执行yum update命令,不然安装会出现问题。 
进入到mysql容器后,我们通过创建一个远程可以访问的用户,这样我们就能从别的主机访问到我们的数据库了。

php-fpm镜像配置:

同样的道理,安装php-fpm镜像,我们这里要安装eolinker,官方给出的php最好用7.0,所以我们就下载php:7.0-fpm,如下:

docker pull php:7.0-fpm

再创建一个phpfpm容器,命令如下:

docker run -d -v /var/www/html:/var/www/html -p 9000:9000 --link lnmp_mysql:mysql --name lnmp_phpfpm php:7.0-fpm
参数说明 
-d 让容器在后台运行 
-p 添加主机到容器的端口映射 
-v 添加目录映射,即主机上的/var/www/html和容器中/var/www/html目录是同步的 
–name 容器的名字 
–link 与另外一个容器建立起联系,这样我们就可以在当前容器中去使用另一个容器里的服务。

这里如果不指定–link参数其实也是可以得,因为容易本身也是有ip的且唯一,所以我们也可以直接利用ip去访问容器。

php模块安装:

安装eolinker需要连接数据库,所以首先登录到docker的lnmp_phpfpm,如下:

#docker exec -ti lnmp_phpfpm /bin/bash

其次执行安装命令,如下:

#docker-php-ext-install pdo_mysql

安装完扩展了,我们通过命令查看一下,如下:

root@05acf6a7ff6f:/var/www/html# php -m
[PHP Modules]
Core
ctype
curl
date
dom
fileinfo
filter
ftp
hash
iconv
json
libxml
mbstring
mysqlnd
openssl
pcre
PDO
pdo_mysql
pdo_sqlite
Phar
posix
readline
Reflection
session
SimpleXML
SPL
sqlite3
standard
tokenizer
xml
xmlreader
xmlwriter
zlib

[Zend Modules]

通过上面可以看出,模块已经安装成功,然后重启php-fpm加载一下就ok了。

注:

错误一:
configure: error: xml2-config not found. Please check your libxml2 installation.
而我已经安装过了libxml2,但是还是有这个提示:
解决办法:
# sudo apt-get install libxml2-dev

错误二:
configure: error: Please reinstall the BZip2 distribution
而我也已经安装了bzip2,网上找到得解决方案都是需要安装bzip2-dev,可是11.10里面没有这个库。
解决办法:在网上找到bzip2-1.0.5.tar.gz,解压,直接make ,sudo make install.(我使用的该源来自于http://ishare.iask.sina.com.cn/f/9769001.html)

错误三:
configure: error: Please reinstall the libcurl distribution -easy.h should be in /include/curl/
解决办法:
# sudo apt-get install libcurl4-gnutls-dev

错误四:
configure: error: jpeglib.h not found.
解决办法:
# sudo apt-get install libjpeg-dev

错误五:   --------------------安装gd支持的时候遇到的
configure: error: png.h not found. 
解决办法:
# sudo apt-get install libpng-dev

错误六:
configure: error: libXpm.(a|so) not found.
解决办法:
# sudo apt-get install libxpm-dev

错误七:
configure: error: freetype.h not found.
解决办法:
# sudo apt-get install libfreetype6-dev

错误八:
configure: error: Your t1lib distribution is not installed correctly. Please reinstall it.
解决办法:
# sudo apt-get install libt1-dev

错误九:
configure: error: mcrypt.h not found. Please reinstall libmcrypt.
解决办法:
# sudo apt-get install libmcrypt-dev

错误十:
configure: error: Cannot find MySQL header files under yes.
Note that the MySQL client library is not bundled anymore!
解决办法:
# sudo apt-get install libmysql++-dev

错误十一:
configure: error: xslt-config not found. Please reinstall the libxslt >= 1.1.0 distribution
解决办法:
# sudo apt-get install libxslt1-dev


错误十二:
checking for the location of zlib... configure: error: zip support requires ZLIB. Use --with-zlib-dir=<DIR> to specify prefix where ZLIB include and library are located
apt-get install zip

nginx模块安装:

还是先下载nginx镜像,如下:

#docker pull nginx:1.10.3

然后运行nginx容器,如下:

docker run -d -p 80:80 --name lnmp_nginx -v /var/www/html:/var/www/html --link lnmp_phpfpm:phpfpm --name lnmp_nginx nginx:1.10.3

参数我就不多说了,上面已经说明了

nginx配置:

我们把lnmp_nginx容器中的nginx配置文件拷贝到本地,然后进行修改,如下:

docker cp lnmp_nginx:/etc/nginx/conf.d/default.conf ./default.conf

我修改的default.conf内容如下:

server {
 listen 80;
 server_name localhost;

#charset koi8-r;
 #access_log /var/log/nginx/log/host.access.log main;

location / {
 root /var/www/html;
 index index.html index.htm;
 }
error_page 500 502 503 504 /50x.html;
 location = /50x.html {
 root /var/www/html;
 }
location ~ \.php$ {
 fastcgi_pass phpfpm:9000;
 fastcgi_index index.php;
 fastcgi_param SCRIPT_FILENAME /var/www/html$fastcgi_script_name;
 fastcgi_param SCRIPT_NAME $fastcgi_script_name;
 include fastcgi_params;
 }
}

修改完成后,然后把default.conf拷贝到容器中,如下:

docker cp ./default.conf lnmp_nginx:/etc/nginx/conf.d/default.conf

进入到nginx容器中重新载入配置文件

#docker exec -it lnmp_nginx /bin/bash
#service nginx reload

好了,接下来我们测试一下环境是否正常,创建index.php文件放到主机/var/www/html目录下,然后访问一下试试,访问http://10.99.11.76/index.php如下:

使用Docker搭建LNMP环境以及安装eolinker

好了,环境成功了。

查看各个容器的ip信息:

[root@bogon eolinker]# docker inspect lnmp_mysql | grep "IPAddress"
 "SecondaryIPAddresses": null,
 "IPAddress": "172.17.0.3",

扩展安装eolinker:

github下载安装包https://github.com/eolinker/eoLinker-API-Management-System-for-php/tree/master/release%5B%E6%AD%A3%E5%BC%8F%E5%AE%89%E8%A3%85%E5%8C%85%5D

下载完成后,上传到服务器/var/www/html解压,如下:

[root@bogon html]# ls -al
总用量 4
drwxr-xr-x. 3 root root 39 3月 14 17:44 .
drwxr-xr-x. 3 root root 18 3月 14 17:17 ..
drwxr-xr-x. 3 root root 153 3月 14 19:54 eolinker

访问http://10.99.11.70/eolinker/index.php,后,开始配置数据库信息,数据库和用户名和密码需要你创建,不建议直接用root

使用Docker搭建LNMP环境以及安装eolinker

点击下一步后,发现可能还存在一些目录权限的问题,这时候你需要查看php-fpm容器中的运行权限,并且将程序目录修改成运行权限就行,如下:

root@05acf6a7ff6f:/var/www/html# more /usr/local/bin/php-config
#! /bin/sh

SED="/bin/sed"
prefix="/usr/local"
datarootdir="/usr/local/php"
exec_prefix="${prefix}"
version="7.0.28"
vernum="70028"
include_dir="${prefix}/include/php"
includes="-I$include_dir -I$include_dir/main -I$include_dir/TSRM -I$include_dir/Zend -I$include_dir/ext -I$include_dir/ext/date/lib"
ldflags=" -Wl,-O1 -Wl,--hash-style=both -pie"
libs="-lcrypt -lresolv -lcrypt -ledit -ltermcap -lrt -lcurl -lz -lrt -lm -ldl -lnsl -lxml2 -lssl -lcrypto -lcurl -lxml2 -lssl -lcrypto -lxml2 -lcrypt -lxml2 -lxml2 -lxml
2 -lssl -lcrypto -lcrypt "
extension_dir='/usr/local/lib/php/extensions/no-debug-non-zts-20151012'
man_dir=`eval echo ${datarootdir}/man`
program_prefix=""
program_suffix=""
exe_extension=""
php_cli_binary=NONE
php_cgi_binary=NONE
configure_options=" '--build=x86_64-linux-gnu' '--with-config-file-path=/usr/local/etc/php' '--with-config-file-scan-dir=/usr/local/etc/php/conf.d' '--disable-cgi' '--enable-ftp' '--enable-mbstring' '--enable-mysqlnd' '--with-curl' '--with-libedit' '--with-openssl' '--with-zlib' '--with-libdir=lib/x86_64-linux-gnu' '--enable-fpm' '--with-fpm-user=www-data' '--with-fpm-group=www-data' 'build_alias=x86_64-linux-gnu'"php_sapis=" cli fpm phpdbg"

通过上面可以看出php-fpm运行是以www-data运行的,那我我们就需要将运行文件权限修改成www-data的权限,这步要登录到php容器中去修改,如下:

root@05acf6a7ff6f:/var/www/html# ls -al
total 8
drwxr-xr-x. 3 root root 39 Mar 14 09:44 .
drwxr-xr-x. 3 root root 4096 Mar 14 09:07 ..
drwxr-xr-x. 8 www-data www-data 153 Mar 14 11:54 eolinker

好了,下来我们再检测一下环境,通过,安装完成,访问10.99.11.76/eolinker/index.php,就可以正常访问了,如图:

使用Docker搭建LNMP环境以及安装eolinker

  • 安卓客户端下载
  • 微信扫一扫
  • weinxin
  • 微信公众号
  • 微信公众号扫一扫
  • weinxin
avatar