创建LAMP环境应用

本教程学习内容:

  1. 制作LAMP镜像并上传

  2. 添加镜像到URLOS

  3. 基于添加的镜像创建LAMP应用

  4. 安装LAMP环境应用并测试

如果还没有docker镜像仓库账户的,先到 阿里云容器镜像仓库注册一个账户,创建好名为php的仓库,再进行下面的教程

制作LAMP镜像并上传

  1. 使用ssh登录云主机,创建名为Dockerfile的文件并填入以下内容:

    1. FROM php:7.2.16-apache-stretch
    2. ENV TIME_ZONE Asiz/Shanghai
    3. RUN sed -i 's~//.*.debian.org~//mirrors.aliyun.com~' /etc/apt/sources.list \
    4. && apt-get update \
    5. && apt-get install -y libxml2-dev libicu-dev libzip-dev \
    6. && docker-php-ext-install zip bcmath intl mbstring mysqli pdo_mysql exif sockets \
    7. # 安装gd扩展
    8. && apt-get install -y libfreetype6-dev libjpeg62-turbo-dev libpng-dev\
    9. && docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpegdir=/usr/include/ \
    10. && docker-php-ext-install gd \
    11. ##
    12. # 安装redis扩展
    13. && cd /var/www/html && curl -O https://pecl.php.net/get/redis-4.3.0.tgz \
    14. && tar xf redis-4.3.0.tgz && cd redis-4.3.0 && phpize && ./configure && make && make install \
    15. && rm -rf /var/www/html/* \
    16. ##
    17. # 安装memcached扩展
    18. && cd /var/www/html && curl -O https://pecl.php.net/get/memcached-3.1.3.tgz \
    19. && apt-get install -y libmemcached-dev && tar xf memcached-3.1.3.tgz \
    20. && cd memcached-3.1.3 && phpize && ./configure && make && make install \
    21. && rm -rf /var/www/html/* \
    22. ##
    23. && cd /var/www/html/ && php -r "copy('https://install.phpcomposer.com/installer','composer-setup.php');" \
    24. && php composer-setup.php \
    25. && php -r "unlink('composer-setup.php');" \
    26. && mv composer.phar /usr/bin/composer \
    27. && apt-get autoremove -y g++ make \
    28. && rm -rf /var/lib/apt/*
  2. 执行以下命令(包括最后面的”.”)生成镜像:

    1. docker build -t registry.cn-shanghai.aliyuncs.com/urlos4/php:7.2.16-apache-stretch .

    注:这里的urlos4为命名空间,registry.cn-shanghai.aliyuncs.com为阿里仓库线路的域名,请根据自己实际的替换

  3. 运行镜像并进入容器

    1. docker run -itd --name apache001 -p 8080:80 registry.cn-shanghai.aliyuncs.com/urlos4/php:7.2.16-apache-stretch # 创建容器的命令 对外端口是8080,根据情况自己填写合适的端口
    2. docker exec -it apache001 bash # 进入容器的命令

    创建容器失败的情况:如果是端口冲突,换一个端口;如果是容器名字冲突,换一个容器的名字或者删除已存在的容器。查看所有容器:“docker ps -a”,删除容器:执行命令“docker rm -f 容器id”。

  4. 在/var/www/html目录下,创建index.php文件

    1. echo "<?php phpinfo();?>" > /var/www/html/index.php
  5. 打开浏览器访问 http://ip:8080/,如图: image-20200408160745371

  6. 确认无误后,将镜像上传到docker仓库:

    1. docker login --username=您的用户名 registry.cn-shanghai.aliyuncs.com # 登录到阿里云容器镜像仓库
    2. docker push registry.cn-shanghai.aliyuncs.com/urlos4/php:7.2.16-apache-stretch # 上传镜像

    注:这里的urlos4为命名空间,registry.cn-shanghai.aliyuncs.com为阿里仓库线路的域名,请根据自己实际的替换

添加镜像到URLOS

  1. 登录URLOS, 未安装URLOS的,请参考官方教程

  2. 选择镜像管理 ,点击页面右上角添加按钮: image-20200411095128290

    1. registry.cn-shanghai.aliyuncs.com/urlos4/php:7.2.16-apache-stretch

    注:这里的urlos4为命名空间,registry.cn-shanghai.aliyuncs.com为阿里仓库线路的域名,请根据自己实际的替换

    image-20200408152153893

    image-20200408152738848

    填写镜像大小,执行以下命令可查看本地仓库中显示的镜像大小

    1. docker images

    配置填写后,点击提交,保存到URLOS。

基于添加的镜像创建LAMP应用

之前我们通过添加应用来新建一个helloWorld应用,其实还有一种更加便捷的方式:复制应用

  1. 进入应用管理页面,搜索”apache”,这里以应用”Apache-PHP-7.3.1网站环境“为例,选择复制应用 image-20200408165525742

  2. 更改应用名称版本号、选择我们刚才添加的镜像 image-20200408171630772

    其他选项默认即可: image-20200408171224196

    image-20200408171238969

    image-20200408171255030

    image-20200408171334821

    image-20200410104813789

    点击提交,保存应用。

  3. 修改LAMP应用的模板 进入LAMP应用管理模板页面。如图: image-20200408172719575

    修改PHP版本号和模板内容: image-20200408172818057

    image-20200408172919813

    image-20200408174551999

    php.ini配置内容如下:

    1. [PHP]
    2. engine = On
    3. short_open_tag = {w:short_open_tag:w}
    4. precision = 14
    5. output_buffering = 4096
    6. zlib.output_compression = Off
    7. implicit_flush = Off
    8. unserialize_callback_func =
    9. serialize_precision = -1
    10. disable_functions =
    11. disable_classes =
    12. zend.enable_gc = On
    13. expose_php = On
    14. max_execution_time = 30
    15. max_input_time = 60
    16. memory_limit = 128M
    17. error_reporting = E_ALL & ~E_DEPRECATED & ~E_STRICT
    18. display_errors = Off
    19. display_startup_errors = Off
    20. log_errors = On
    21. log_errors_max_len = 1024
    22. ignore_repeated_errors = Off
    23. ignore_repeated_source = Off
    24. report_memleaks = On
    25. html_errors = On
    26. variables_order = "GPCS"
    27. request_order = "GP"
    28. register_argc_argv = Off
    29. auto_globals_jit = On
    30. post_max_size = {w:PHP_memory_limit:w}
    31. auto_prepend_file =
    32. auto_append_file =
    33. default_mimetype = "text/html"
    34. default_charset = "UTF-8"
    35. doc_root =
    36. user_dir =
    37. enable_dl = Off
    38. file_uploads = On
    39. upload_max_filesize = {w:upload_max_filesize:w}
    40. max_file_uploads = 20
    41. allow_url_fopen = On
    42. allow_url_include = Off
    43. default_socket_timeout = 60
    44. extension=gd.so
    45. extension=memcached.so
    46. extension=redis.so
    47. extension=sockets.so
    48. extension=mysqli.so
    49. extension=pdo_mysql.so
    50. extension=bcmath.so
    51. extension=zip.so
    52. [CLI Server]
    53. cli_server.color = On
    54. [Date]
    55. [filter]
    56. [iconv]
    57. [imap]
    58. [intl]
    59. [sqlite3]
    60. [Pcre]
    61. [Pdo]
    62. [Pdo_mysql]
    63. pdo_mysql.default_socket=
    64. [Phar]
    65. [mail function]
    66. SMTP = localhost
    67. smtp_port = 25
    68. mail.add_x_header = Off
    69. [ODBC]
    70. odbc.allow_persistent = On
    71. odbc.check_persistent = On
    72. odbc.max_persistent = -1
    73. odbc.max_links = -1
    74. odbc.defaultlrl = 4096
    75. odbc.defaultbinmode = 1
    76. [Interbase]
    77. ibase.allow_persistent = 1
    78. ibase.max_persistent = -1
    79. ibase.max_links = -1
    80. ibase.timestampformat = "%Y-%m-%d %H:%M:%S"
    81. ibase.dateformat = "%Y-%m-%d"
    82. ibase.timeformat = "%H:%M:%S"
    83. [MySQLi]
    84. mysqli.max_persistent = -1
    85. mysqli.allow_persistent = On
    86. mysqli.max_links = -1
    87. mysqli.default_port = 3306
    88. mysqli.default_socket =
    89. mysqli.default_host =
    90. mysqli.default_user =
    91. mysqli.default_pw =
    92. mysqli.reconnect = Off
    93. [mysqlnd]
    94. mysqlnd.collect_statistics = On
    95. mysqlnd.collect_memory_statistics = Off
    96. [OCI8]
    97. [PostgreSQL]
    98. pgsql.allow_persistent = On
    99. pgsql.auto_reset_persistent = Off
    100. pgsql.max_persistent = -1
    101. pgsql.max_links = -1
    102. pgsql.ignore_notice = 0
    103. pgsql.log_notice = 0
    104. [bcmath]
    105. bcmath.scale = 0
    106. [browscap]
    107. [Session]
    108. session.save_handler = files
    109. session.use_strict_mode = 0
    110. session.use_cookies = 1
    111. session.use_only_cookies = 1
    112. session.name = PHPSESSID
    113. session.auto_start = 0
    114. session.cookie_lifetime = 0
    115. session.cookie_path = /
    116. session.cookie_domain =
    117. session.cookie_httponly =
    118. session.cookie_samesite =
    119. session.serialize_handler = php
    120. session.gc_probability = 1
    121. session.gc_divisor = 1000
    122. session.gc_maxlifetime = 1440
    123. session.referer_check =
    124. session.cache_limiter = nocache
    125. session.cache_expire = 180
    126. session.use_trans_sid = 0
    127. session.sid_length = 26
    128. session.trans_sid_tags = "a=href,area=href,frame=src,form="
    129. session.sid_bits_per_character = 5
    130. [Assertion]
    131. zend.assertions = -1
    132. [COM]
    133. [mbstring]
    134. [gd]
    135. [exif]
    136. [Tidy]
    137. tidy.clean_output = Off
    138. [soap]
    139. soap.wsdl_cache_enabled=1
    140. soap.wsdl_cache_dir="/tmp"
    141. soap.wsdl_cache_ttl=86400
    142. soap.wsdl_cache_limit = 5
    143. [sysvshm]
    144. [ldap]
    145. ldap.max_links = -1
    146. [dba]
    147. [opcache]
    148. [curl]
    149. [openssl]

    点击提交,保存模板数据。

安装LAMP环境应用并测试

  1. 应用管理列表找到刚才新添加的LAMP应用,选择创建服务 image-20200408175016238

    image-20200409190233108 image-20200409190352537 image-20200408175526020

    按照上面配置填写后,提交部署。

    部署完成后,在浏览器访访问刚才填写的域名+端口号,如图:

    说明:如果只是测试的话,网站域名随意填写,更改访问终端的hosts文件

    image-20200409190440511

    image-20200408183307052