1. 程式人生 > >docker建立lnmp映象

docker建立lnmp映象

docker是一個輕量級的虛擬化技術,而lnmp是一個強大、開源的web執行環境,這裡我們就演示用Docker來構建一個lnmp映象。

PS:為了保持輕量化和可伸縮,Docker鼓勵我們 “one process per container”,也就是不要在一個映象中整合太多的功能,我們這裡主要是為了學習研究,所以違背了這個準則,更好的方案是Nginx、Mysql、PHP各自建立映象,在單獨的容器裡執行,然後通過容器間的互聯實現通訊。

1、拉取官方的 centos:6.9 作為基礎映象,安裝好基礎環境

[[email protected] ~]# docker pull centos:6.9
6.9: Pulling from centos
055b9989266a: Pull complete 
f1070d829305: Pull complete 
e071bce628ba: Pull complete 
Digest: sha256:e7bdc458659b6e644ae85694f2baaf3727c06ad82186fca80f4e3a8e88907cc3
Status: Downloaded newer image for centos:6.9
[
[email protected]
~]# [roo[email protected] ~]# docker images REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE httpd 2.4.29 91199e851c7a 4 weeks ago 177.3 MB centos 6.9 e071bce628ba 4 weeks ago 194.7 MB ubuntu 14.04 b44ce450cb60 11 weeks ago 188 MB [
[email protected]
~]# [[email protected] ~]# docker run -it centos:6.9 /bin/bash [[email protected] /]# ls bin dev etc home lib lib64 lost+found media mnt opt proc root sbin selinux srv sys tmp usr var [[email protected] ~]# [[email protected] /]# yum install -y epel-release .......

2、安裝PHP

[[email protected] /]# yum install -y php php-fpm php-mysql
.........
.........
[[email protected] /]# php -v
PHP 5.3.3 (cli) (built: Mar 22 2017 12:27:09) 
Copyright (c) 1997-2010 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2010 Zend Technologies
[[email protected] /]# php-fpm -v
PHP 5.3.3 (fpm-fcgi) (built: Mar 22 2017 12:28:05)
Copyright (c) 1997-2012 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2010 Zend Technologies
[[email protected] /]# 
[[email protected] /]# php -m
[PHP Modules]
bz2
calendar
Core
ctype
curl
date
ereg
exif
fileinfo
filter
ftp
gettext
gmp
hash
iconv
json
libxml
mysql
mysqli
openssl
pcntl
pcre
PDO
pdo_mysql
pdo_sqlite
Phar
readline
Reflection
session
shmop
SimpleXML
sockets
SPL
sqlite3
standard
tokenizer
xml
zip
zlib

[Zend Modules]

3、安裝Nginx並配置PHP fastcgi
[[email protected] /]# yum install -y nginx
.......

配置Nginx以支援PHP,修改 /etc/nginx/conf.d/default.conf 如下:
#
# The default server
#

server {
    listen       80 default_server;
    listen       [::]:80 default_server;
    server_name  _;
    root         /www;   #change document_root to /www

    # Load configuration files for the default server block.
    include /etc/nginx/default.d/*.conf;

    location / {
    }

    #support php access
    location ~ .*\.php$ {
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }

    error_page 404 /404.html;
        location = /40x.html {
    }

    error_page 500 502 503 504 /50x.html;
        location = /50x.html {
    }

}

建立 /www 目錄,並且修改使用者和使用者組為 apache (php-fpm預設執行使用者和組為apache)

[[email protected] /]# mkdir /www
[[email protected] /]# chown apache:apache /www 

[[email protected] /]# ll / | grep www
drwxr-xr-x.   2 apache apache  4096 Dec  4 08:21 www
4、安裝mysql
[[email protected] /]# yum install -y mysql mysql-server
......
......

[[email protected] /]# /etc/init.d/mysqld start
Starting mysqld:                                           [  OK  ]

[[email protected] /]# /usr/bin/mysqladmin -u root password '123456'

[[email protected] /]# /etc/init.d/mysqld stop
Stopping mysqld:                                           [  OK  ]

5、編寫啟動命令
[[email protected] /]# touch /bin/startup.sh
[[email protected] /]# chmod +x /bin/startup.sh

然後向startup.sh寫入如下內容:
#!/bin/sh

/etc/init.d/mysqld start
/etc/init.d/php-fpm start
/etc/init.d/nginx start


#dead loop
while ((1))
do
        sleep 1h
done

6、清理不再需要的資料以縮減映象的尺寸,然後docker commit生成新映象
[[email protected] /]# yum remove -y epel-release
[[email protected] /]# yum clean all
Loaded plugins: fastestmirror, ovl
Cleaning repos: base extras updates
Cleaning up Everything
Cleaning up list of fastest mirrors

[[email protected] /]# exit
exit

[[email protected] ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                      PORTS               NAMES
df040eea8088        centos:6.9          "/bin/bash"         2 hours ago         Exited (0) 31 seconds ago                       sleepy_yalow        
[[email protected] ~]# docker commit df0 centos:lnmp
efa371fe9b0ede828fe1999e7831491d1dd24cda26f1a27092a9565f4c781e06

[[email protected] ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
centos              lnmp                efa371fe9b0e        57 seconds ago      453.4 MB
httpd               2.4.29              91199e851c7a        4 weeks ago         177.3 MB
centos              6.9                 e071bce628ba        4 weeks ago         194.7 MB
ubuntu              14.04               b44ce450cb60        11 weeks ago        188 MB



7、測試映象,啟動容器

[[email protected] ~]# docker run -d -p 8848:80 -v /www:/www centos:lnmp /bin/startup.sh
[[email protected] www]# cd /www
[[email protected] www]# echo "<?php echo time() . PHP_EOL;" > index.php
[[email protected] www]# cat index.php 
<?php echo time() . PHP_EOL;
[[email protected] www]# curl '127.0.0.1:8848/index.php'
1512380042

再測試mysql是否正常
[[email protected] www]# touch /www/db.php

程式碼內容:

<?php

$db = new mysqli('127.0.0.1', 'root', '123456', 'mysql');

if ($db->connect_errno)
{
    die("連線資料庫失敗". $db->connect_error);
}

echo "連線成功";

$db->close();

訪問 127.0.0.1:8848/db.php

[[email protected] www]# curl '127.0.0.1:8848/db.php'
連線成功[[email protected] www]# 

That's it ^^