docker-compose搭建lnmp

docker-compose搭建lnmp

1. 用dockerfile创建nginx镜像

[root@localhost nginx]# ls
Dockerfile  nginx-1.22.1.tar.gz
[root@localhost nginx]# cat Dockerfile  
FROM rockylinux:8                                                                                          
RUN yum -y install gcc gcc-c++ make pcre-devel openssl-devel zlib-devel popt-devel openssl 
RUN  useradd -M -s /sbin/nologin nginx
ADD nginx-1.22.1.tar.gz /usr/local/ 
RUN cd /usr/local/nginx-1.22.1/  && ./configure --prefix=/usr/local/nginx/ --user=nginx --group=nginx --with-file-aio --with-http_stub_status_module --with-http_gzip_static_module --with-http_flv_module --with-http_ssl_module --with-pcre && make && make install 
EXPOSE 80 
CMD  [ "/usr/local/nginx/sbin/nginx","-g","daemon off;" ]
[root@localhost nginx]# docker volume create  nginx  
nginx
[root@localhost nginx]# docker build -t nginx:v1 .  
[root@localhost nginx]# docker images 
REPOSITORY   TAG       IMAGE ID       CREATED          SIZE
nginx        v1        c93c8c63c5cc   2 hours ago      431MB
rockylinux   8         4f2f5d89988f   2 weeks ago      198MB
centos       7         eeb6ee3f44bd   2 years ago      204MB
[root@localhost conf]# docker run -it --name nginx --network haha -p 80:80 -v nginx:/usr/local/nginx  --mount type=bind,src=/var/www/haha/,dst=/var/www/haha nginx:v1
[root@localhost nginx]# docker ps -a
CONTAINER ID   IMAGE      COMMAND                   CREATED          STATUS          PORTS                               NAMES
68893f089915   nginx:v1   "/usr/local/nginx/sb…"   12 minutes ago   Up 12 minutes   0.0.0.0:80->80/tcp, :::80->80/tcp   nginx

2. 用dockerfile创建mysql镜像

[root@localhost mysql]# ls
Dockerfile  my.cnf  mysql-5.7.18-linux-glibc2.5-x86_64.tar.gz  mysqld.service
[root@localhost mysql]# cat Dockerfile 
FROM rockylinux:8
RUN yum -y install ncurses-compat-libs  libaio
ADD mysql-5.7.18-linux-glibc2.5-x86_64.tar.gz   /usr/local/
RUN mv /usr/local/mysql-5.7.18-linux-glibc2.5-x86_64/ /usr/local/mysql && useradd -r -s /sbin/nologin mysql 
COPY my.cnf /etc/my.cnf 
COPY mysqld.service  /usr/lib/systemd/system/
RUN mkdir -p /usr/local/mysql/data/ && mkdir -p /usr/local/mysql/logs/ &&  touch /usr/local/mysql/logs/error.log && chown -R mysql.mysql /usr/local/mysql/ && chown -R mysql.mysql /etc/my.cnf && chmod +x /usr/local/mysql/bin/mysqld && ln -s /usr/local/mysql/bin/* /usr/local/sbin/ && /usr/local/mysql/bin/mysqld --initialize-insecure --user=mysql --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data/ && cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld && sed -i '46c basedir=/usr/local/mysql' /etc/init.d/mysqld && sed -i '47c datadir=/usr/local/mysql/data' /etc/init.d/mysqld  &&  sed -i '63c mysqld_pid_file_path=/usr/local/mysql/data/mysqld.pid' /etc/init.d/mysqld 
ENV PATH=/usr/local/mysql/bin:/usr/local/mysql/lib:$PATH
RUN /etc/init.d/mysqld start && mysqladmin -uroot password 123456 && mysql -uroot -p123456 -e "grant all on *.* to 'root'@'%' identified by '123456';" &> /dev/null && mysql -uroot -p123456 -e "create database skyucdb;" &> /dev/null &&  mysql -uroot -p123456 -e "grant all on *.* to 'runskyuc'@'%' identified by '123456';"  &&  mysql -uroot -p123456 -e "flush privileges;"
EXPOSE 3306
ENTRYPOINT ["/usr/local/mysql/bin/mysqld"]
[root@localhost mysql]# cat my.cnf 

[client]
port = 3306
socket=/usr/local/mysql/mysql.sock
 
[mysql]
port = 3306
socket = /usr/local/mysql/mysql.sock
auto-rehash
 
[mysqld]
user = mysql
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data
port = 3306
character-set-server=utf8
pid-file = /usr/local/mysql/mysqld.pid
socket=/usr/local/mysql/mysql.sock
bind-address = 0.0.0.0
skip-name-resolve
max_connections=2048
default-storage-engine=INNODB
max_allowed_packet=16M
server-id = 1
 
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_AUTO_VALUE_ON_ZERO,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,PIPES_AS_CONCAT,ANSI_QUOTES
[root@localhost mysql]# cat mysqld.service   
[mysqld]
 [Unit]
 Description=MySQL
 SourcePath=/etc/init.d/mysqld
 Before=shutdown.target
 
 [Service]
 User=mysql
 Type=forking
 ExecStart=/etc/init.d/mysqld start
 ExecStop=/etc/init.d/mysqld stop
 
 [Install] 
 WantedBy=multi-user.target
[root@localhost mysql]# docker build -t mysql:v1 .  
[root@localhost mysql]# docker images   
REPOSITORY   TAG       IMAGE ID       CREATED          SIZE
nginx        v1        c93c8c63c5cc   2 hours ago      431MB
mysql        v1        31c0f75a4df1   7 hours ago      8.55GB
rockylinux   8         4f2f5d89988f   2 weeks ago      198MB
centos       7         eeb6ee3f44bd   2 years ago      204MB

3. 用dockerfile创建php镜像

[root@localhost php]# ls
Dockerfile              mysql-5.7.18-linux-glibc2.5-x86_64.tar.gz
libmcrypt-2.5.8.tar.gz  php
mcrypt-2.6.8.tar.gz     php-5.3.28.tar.gz
mhash-0.9.9.9.tar.gz    ZendGuardLoader-php-5.3-linux-glibc23-x86_64.tar.gz
[root@localhost php]# cat Dockerfile  
FROM rockylinux:8
RUN yum -y install gcc gcc-c++ make libxml2-devel gd zlib-devel libpng-devel libjpeg-devel
ADD libmcrypt-2.5.8.tar.gz /usr/src/
RUN cd /usr/src/libmcrypt-2.5.8/ && ./configure &&make &&make install && ln -s /usr/local/lib/libmcrypt.* /usr/lib64/ 
ADD mhash-0.9.9.9.tar.gz /usr/src/
RUN cd /usr/src/mhash-0.9.9.9/ && ./configure &&make &&make install && ln -s /usr/local/lib/libmhash* /usr/lib64/ && useradd -M -s /sbin/nologin nginx 
ADD mysql-5.7.18-linux-glibc2.5-x86_64.tar.gz /usr/local/
RUN mv /usr/local/mysql-5.7.18-linux-glibc2.5-x86_64/ /usr/local/mysql
ADD mcrypt-2.6.8.tar.gz /usr/src/
RUN cd /usr/src/mcrypt-2.6.8/ && ./configure &&make &&make install 
ADD php-5.3.28.tar.gz /usr/src/
RUN cd /usr/src/php-5.3.28/ && ./configure --prefix=/usr/local/php5 --with-gd --with-zlib --with-mysql=/usr/local/mysql --with-config-file-path=/usr/local/php5 --enable-mbstring --enable-fpm  --with-jpeg-dir=/usr/lib && make && make install &&  cp php.ini-development /usr/local/php5/php.ini && ln -s /usr/local/php5/bin/* /usr/local/bin/ && ln -s /usr/local/php5/sbin/* /usr/local/sbin/
ADD ZendGuardLoader-php-5.3-linux-glibc23-x86_64.tar.gz /usr/src/
RUN cd /usr/src/ZendGuardLoader-php-5.3-linux-glibc23-x86_64/php-5.3.x/ && cp ZendGuardLoader.so /usr/local/php5/ && cp /usr/local/php5/etc/php-fpm.conf.default /usr/local/php5/etc/php-fpm.conf && /usr/local/php5/sbin/php-fpm 
EXPOSE 9000
CMD [ "/etc/init.d/php/php-fpm","-F"]
[root@localhost conf]# docker volume create php 
php
[root@localhost php]# docker build -t php:v1 .  
[root@localhost php]# docker images  
REPOSITORY   TAG       IMAGE ID       CREATED          SIZE
php          v1        3144b8040a20   41 minutes ago   6.11GB
nginx        v1        c93c8c63c5cc   2 hours ago      431MB
mysql        v1        31c0f75a4df1   7 hours ago      8.55GB
rockylinux   8         4f2f5d89988f   2 weeks ago      198MB
centos       7         eeb6ee3f44bd   2 years ago      204MB
[root@localhost conf]# docker run -it --name php --network haha -p 9000:9000 --mount type=bind,src=/var/www/haha/,dst=/var/www/haha -v php:/usr/local/php5/ php:v1 
[root@localhost php]# docker ps -a   
CONTAINER ID   IMAGE      COMMAND                   CREATED          STATUS          PORTS                                       NAMES
b3c388b1eed5   php:v1     "/usr/local/php5/sbi…"   9 seconds ago    Up 7 seconds    0.0.0.0:9000->9000/tcp, :::9000->9000/tcp   php
68893f089915   nginx:v1   "/usr/local/nginx/sb…"   18 minutes ago   Up 18 minutes   0.0.0.0:80->80/tcp, :::80->80/tcp           nginx

4. 配置优化php服务

[root@localhost php]# cd /var/lib/docker/volumes/php/_data/etc/  
[root@localhost etc]# ls
pear.conf  php-fpm.conf  php-fpm.conf.default
[root@localhost etc]# docker inspect php  
                    "Gateway": "192.168.86.1",
                    "IPAddress": "192.168.86.3",    #ip地址
                    "IPPrefixLen": 24,
                    "IPv6Gateway": "",
[root@localhost etc]# cat php-fpm.conf   
[root@localhost etc]# cat -n  php-fpm.conf
21   [global]	
25   pid = run/php-fpm.pid	
122  [www]	
140  user = nginx	
141  group = nginx	
151	listen = 192.168.86.3:9000    
206  pm = dynamic

[root@localhost _data]# ls
bin  etc  include  lib  man  php.ini  sbin  share  var  ZendGuardLoader.so
[root@localhost _data]# cat php.ini 
[Zend Guard]
zend_extension=/usr/local/php5/ZendGuardLoader.so	
zend_loader.enable=1	 
zend_loader.disable_licensing=0
zend_loader.obfuscation_level_support=3

5. 配置nginx支持php解析

[root@localhost _data]# cd /var/lib/docker/volumes/nginx/_data/ 
[root@localhost _data]# ls    
client_body_temp conf fastcgi_temp  html  logs  proxy_temp  sbin  scgi_temp  uwsgi_temp
[root@localhost _data]# cat conf/nginx.conf 
worker_processes  1;
events {
  worker_connections  1024;
}
http {
  include    mime.types;
  default_type  application/octet-stream;
  sendfile     on;
  keepalive_timeout  65;
  server {
    listen    80;
    server_name  www.haha.com;	
    charset utf-8;
    location / {
      root  /var/www/haha/;	   
      index  index.html index.htm index.php;	
    }
	location ~ \.php {    			
	   root /var/www/haha/; 		
	   fastcgi_pass 192.168.86.3:9000;		 #	php-fpm的监听地址和端口号
	   fastcgi_index index.php;				
	   include fastcgi.conf;				
	}
    error_page  500 502 503 504  /50x.html;
    location = /50x.html {
      root  html;
    }
  }
}

6. 编辑测试页面

[root@localhost _data]# cd /var/www/haha/   
[root@localhost haha]# ls
index.html  skyuc  SKYUC.v3.4.2.SOURCE  SKYUC.v3.4.2.SOURCE.zip  testa.php  testb.php
[root@localhost haha]# cat index.html  
hello
[root@localhost haha]# cat testa.php  
<?php 
phpinfo();
?>
[root@localhost haha]# cat testb.php  
<?php
$link=mysql_connect('192.168.86.3','root','123456');
if($link) echo "mysql ok!";
mysql_close();
?>
[root@localhost haha]# 

7. 测试结果

[root@localhost ~]# curl 192.168.100.120:8080/testb.php 
mysql ok!
[root@localhost ~]# curl 192.168.100.120:8080/testa.php
......

9. 启动mysql数据库

[root@localhost mysql]# docker run -it --name mysql --network haha -p 30:3306 --mount type=bind,src=/var/www/haha/,dst=/var/www/haha/ mysql:v1  
[root@localhost php]# docker ps -a
CONTAINER ID   IMAGE      COMMAND                   CREATED             STATUS             PORTS                                       NAMES
797459f3d253   mysql:v1   "/usr/local/mysql/bi…"   10 seconds ago      Up 8 seconds       0.0.0.0:3306->3306/tcp, :::3306->3306/tcp   mysql
b3c388b1eed5   php:v1     "/usr/local/php5/sbi…"   52 minutes ago      Up 52 minutes      0.0.0.0:9000->9000/tcp, :::9000->9000/tcp   php
68893f089915   nginx:v1   "/usr/local/nginx/sb…"   About an hour ago   Up About an hour   0.0.0.0:80->80/tcp, :::80->80/tcp           nginx
[root@localhost mysql]# docker inspect mysql   #查看mysql容器的ip
                   "Gateway": "192.168.86.1",
                    "IPAddress": "192.168.86.4",
                    "IPPrefixLen": 24,
                    "IPv6Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,
                    "MacAddress": "02:42:c0:a8:56:04",
                    "DriverOpts": null
                }

[root@localhost ~]# mysql -uroot -h 192.168.86.4 -p  
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.18 MySQL Community Server (GPL)

Copyright (c) 2000, 2017, 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> create database skyucdb;
Query OK, 1 row affected (0.00 sec)

10. 在compose中创建工程,搭建lnmp架构

[root@localhost lnmp]# cat docker-compose.yml  
version: "3"
networks: 
  haha:
    external: true
services: 
  nginx: 
    container_name: nginx1
    image: nginx:v1
    ports: 
    - "8080:80"
    volumes:
    - /var/lib/docker/volumes/nginx/_data/conf:/usr/local/nginx/conf  
    - /var/www/haha/:/var/www/haha/   
    hostname: nginx1
    networks: 
      haha:
        ipv4_address: 192.168.86.2  

  php:
    container_name: php1
    image: php:v1
    ports:
    - "9090:9000"
    volumes:
    - /var/lib/docker/volumes/php/_data/etc/:/usr/local/php5/etc/   
    - /var/www/haha/:/var/www/haha/   
    hostname: php1
    networks:
      haha: 
        ipv4_address: 192.168.86.3   

  mysql:
    container_name: mysql1
    image: mysql:v1
    ports: 
    - "3030:3306"
    volumes:
    - /var/www/haha/skyuc:/var/www/haha/skyuc
    hostname: mysql1
    networks:
      - haha
[root@localhost lnmp]# docker compose up -d  
[+] Running 3/3
 ✔ Container php1    Started             0.0s 
 ✔ Container mysql1  Started             0.0s 
 ✔ Container nginx1  Started             0.0s 
[root@localhost lnmp]# docker compose  ps -a   
NAME      IMAGE      COMMAND                                          SERVICE   CREATED          STATUS         PORTS
mysql1    mysql:v1   "/usr/local/mysql/bin/mysqld"                    mysql     12 seconds ago   Up 8 seconds   0.0.0.0:3030->3306/tcp, :::3030->3306/tcp
nginx1    nginx:v1   "/usr/local/nginx/sbin/nginx -g 'daemon off;'"   nginx     12 seconds ago   Up 8 seconds   0.0.0.0:8080->80/tcp, :::8080->80/tcp
php1      php:v1     "/etc/init.d/php/php-fpm -F"                     php       12 seconds ago   Up 8 seconds   0.0.0.0:9090->9000/tcp, :::9090->9000/tcp
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值