创建LNMP环境应用

本教程学习内容:

  1. 制作LNMP镜像并上传

  2. 添加镜像到URLOS

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

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

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

制作LNMP镜像并上传

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

    1. FROM php:7.3.3-fpm-alpine3.9
    2. ENV TIME_ZONE Asiz/Shanghai
    3. RUN sed -i 's@dl-cdn.alpinelinux.org@mirrors.aliyun.com@' /etc/apk/repositories \
    4. && apk add autoconf g++ make nginx \
    5. && apk add libxml2-dev icu-dev freetype-dev libjpeg-turbo-dev libpng-dev libzip-dev \
    6. && docker-php-ext-install zip bcmath intl mbstring mysqli pdo_mysql exif sockets \
    7. && docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpegdir=/usr/include/ \
    8. && docker-php-ext-install gd \
    9. # 安装redis 扩展
    10. && cd /var/www/html && wget https://pecl.php.net/get/redis-4.3.0.tgz \
    11. && tar xf redis-4.3.0.tgz && cd redis-4.3.0 \
    12. && phpize && ./configure && make && make install \
    13. && rm -rf /var/www/html/* \
    14. ##
    15. # 安装memcached扩展
    16. && cd /var/www/html && wget https://pecl.php.net/get/memcached-3.1.3.tgz \
    17. && apk add libmemcached-dev && tar xf memcached-3.1.3.tgz \
    18. && cd memcached-3.1.3 && phpize && ./configure && make && make install \
    19. && rm -rf /var/www/html/* \
    20. ##
    21. ## 安装composer
    22. && cd /var/www/html/ && php -r "copy('https://install.phpcomposer.com/installer', 'composer-setup.php');" \
    23. && php composer-setup.php \
    24. && php -r "unlink('composer-setup.php');" \
    25. && mv composer.phar /usr/bin/composer\
    26. ##
    27. && apk del g++ make
  2. 执行以下命令(包括最后面的”.”)生成镜像:

    1. docker build -t registry.cn-shanghai.aliyuncs.com/urlos4/php:7.3.3-fpm-alpine3.9 .

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

  1. 运行镜像并进入容器

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

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

  2. 设置nginx 虚拟站点配置

    1. vi /etc/nginx/conf.d/default.conf

    配置填入以下内容:

    1. server {
    2. listen 80 default_server;
    3. root /var/www/html/;
    4. index index.php index.html;
    5. location / {
    6. }
    7. location ~ \.(php|phtml)$ {
    8. include fastcgi.conf;
    9. fastcgi_pass 127.0.0.1:9000;
    10. }
    11. }
  3. 在/var/www/html目录下,创建index.php文件

    1. echo "<?php phpinfo();?>" > /var/www/html/index.php
  4. 启动 nginx 和 php:

    1. mkdir -p /run/nginx
    2. nginx
    3. php-fpm -D

    打开浏览器访问 http://ip:8080/

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

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

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

添加镜像到URLOS

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

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

    1. registry.cn-shanghai.aliyuncs.com/urlos4/php:7.3.3-fpm-alpine3.9

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

    image-20200408152153893

    image-20200409163123496

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

    1. docker images

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

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

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

  1. 进入应用管理页面,搜索id为”40”,这里以应用”PHP-7.3.0-网站环境“为例,选择复制应用 image-20200409165701261

  2. 更改应用名称版本号应用别名镜像选择我们刚才添加的镜像容器端口开发者信息image-20200409180829736

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

    脚本设置:

    image-20200409172525338

    1. 安装脚本:
    2. test -d /etc/nginx/conf.d/ || mkdir -p /etc/nginx/conf.d/
    3. test -d /var/tmp/nginx && chown -R nobody:nobody /var/tmp/nginx
    4. test -d /run/nginx && mkdir -p /run/nginx
    5. rm -rf /usr/local/etc/php/conf.d/*
    6. 启动脚本:
    7. php-fpm -D
    8. nginx
    9. 状态脚本:
    10. status1=0 && (ps -ef|grep "php-fpm"|grep "master process"|grep -v "grep") && status1=1;
    11. status2=0 && (ps -ef|grep "nginx"|grep "master process"|grep -v "grep") && status2=1;
    12. status3=0 && test -f /usr/local/bin/monitorFile.php && php /usr/local/bin/monitorFile.php|grep -E "(502|504)" || status3=1
    13. if [ ${status1} != 0 ] && [ ${status2} != 0 ] && [ ${status3} != 0 ]; then
    14. statusScriptResult=1
    15. fi
    16. 监控脚本:
    17. {w:statusScript:w}
    18. [ "$statusScriptResult" != 1 ] && exit 1

    按照上面配置后点击提交,保存应用。

  1. 修改LNMP应用的模板 进入LNMP应用管理模板页面。如图:

    image-20200409173812328

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

    image-20200409174142084

    image-20200409175925768

    image-20200409180244626

    php-fpm:php.ini配置内容如下:

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

安装LNMP环境应用并测试

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

    image-20200409181619126

    image-20200409181638315

    image-20200409181647963

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

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

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

    image-20200409185307750