创建LNMP环境应用
本教程学习内容:
如果还没有docker镜像仓库账户的,先到 阿里云容器镜像仓库注册一个账户,创建好名为php的仓库,再进行下面的教程
制作LNMP镜像并上传
使用ssh登录云主机,创建名为Dockerfile的文件并填入以下内容:
FROM php:7.3.3-fpm-alpine3.9
ENV TIME_ZONE Asiz/Shanghai
RUN sed -i 's@dl-cdn.alpinelinux.org@mirrors.aliyun.com@' /etc/apk/repositories \
&& apk add autoconf g++ make nginx \
&& apk add libxml2-dev icu-dev freetype-dev libjpeg-turbo-dev libpng-dev libzip-dev \
&& docker-php-ext-install zip bcmath intl mbstring mysqli pdo_mysql exif sockets \
&& docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpegdir=/usr/include/ \
&& docker-php-ext-install gd \
# 安装redis 扩展
&& cd /var/www/html && wget https://pecl.php.net/get/redis-4.3.0.tgz \
&& tar xf redis-4.3.0.tgz && cd redis-4.3.0 \
&& phpize && ./configure && make && make install \
&& rm -rf /var/www/html/* \
##
# 安装memcached扩展
&& cd /var/www/html && wget https://pecl.php.net/get/memcached-3.1.3.tgz \
&& apk add libmemcached-dev && tar xf memcached-3.1.3.tgz \
&& cd memcached-3.1.3 && phpize && ./configure && make && make install \
&& rm -rf /var/www/html/* \
##
## 安装composer
&& cd /var/www/html/ && php -r "copy('https://install.phpcomposer.com/installer', 'composer-setup.php');" \
&& php composer-setup.php \
&& php -r "unlink('composer-setup.php');" \
&& mv composer.phar /usr/bin/composer\
##
&& apk del g++ make
执行以下命令(包括最后面的”.”)生成镜像:
docker build -t registry.cn-shanghai.aliyuncs.com/urlos4/php:7.3.3-fpm-alpine3.9 .
注:这里的urlos4为命名空间,registry.cn-shanghai.aliyuncs.com为阿里仓库线路的域名,请根据自己实际的替换
运行镜像并进入容器
docker run -itd --name test001 -p 8080:80 registry.cn-shanghai.aliyuncs.com/urlos4/php:7.3.3-fpm-alpine3.9 # 创建容器的命令 对外端口是8080,根据情况自己填写合适的端口
docker exec -it test001 sh # 进入容器的命令
创建容器失败的情况:如果是端口冲突,换一个端口;如果是容器名字冲突,换一个容器的名字或者删除已存在的容器。查看所有容器:“docker ps -a”,删除容器:执行命令“docker rm -f 容器id”。
设置nginx 虚拟站点配置
vi /etc/nginx/conf.d/default.conf
配置填入以下内容:
server {
listen 80 default_server;
root /var/www/html/;
index index.php index.html;
location / {
}
location ~ \.(php|phtml)$ {
include fastcgi.conf;
fastcgi_pass 127.0.0.1:9000;
}
}
在/var/www/html目录下,创建index.php文件
echo "<?php phpinfo();?>" > /var/www/html/index.php
启动 nginx 和 php:
mkdir -p /run/nginx
nginx
php-fpm -D
打开浏览器访问 http://ip:8080/
确认无误后,将镜像上传到docker仓库:
docker login --username=您的用户名 registry.cn-shanghai.aliyuncs.com # 登录到阿里云容器镜像仓库
docker push registry.cn-shanghai.aliyuncs.com/urlos4/php:7.3.3-fpm-alpine3.9 # 上传镜像
注:这里的urlos4为命名空间,registry.cn-shanghai.aliyuncs.com为阿里仓库线路的域名,请根据自己实际的替换
添加镜像到URLOS
登录URLOS, 未安装URLOS的,请参考官方教程
选择镜像管理 ,点击页面右上角添加按钮:
registry.cn-shanghai.aliyuncs.com/urlos4/php:7.3.3-fpm-alpine3.9
注:这里的urlos4为命名空间,registry.cn-shanghai.aliyuncs.com为阿里仓库线路的域名,请根据自己实际的替换
填写镜像大小,执行以下命令可查看本地仓库中显示的镜像大小
docker images
配置填写后,点击提交,保存到URLOS。
基于添加的镜像创建LNMP应用
之前我们通过添加应用来新建一个helloWorld应用,其实还有一种更加便捷的方式:复制应用
进入应用管理页面,搜索id为”40”,这里以应用”PHP-7.3.0-网站环境“为例,选择复制应用
更改应用名称、版本号、应用别名、镜像选择我们刚才添加的镜像、容器端口、开发者信息,
其他选项默认即可:
脚本设置:
安装脚本:
test -d /etc/nginx/conf.d/ || mkdir -p /etc/nginx/conf.d/
test -d /var/tmp/nginx && chown -R nobody:nobody /var/tmp/nginx
test -d /run/nginx && mkdir -p /run/nginx
rm -rf /usr/local/etc/php/conf.d/*
启动脚本:
php-fpm -D
nginx
状态脚本:
status1=0 && (ps -ef|grep "php-fpm"|grep "master process"|grep -v "grep") && status1=1;
status2=0 && (ps -ef|grep "nginx"|grep "master process"|grep -v "grep") && status2=1;
status3=0 && test -f /usr/local/bin/monitorFile.php && php /usr/local/bin/monitorFile.php|grep -E "(502|504)" || status3=1
if [ ${status1} != 0 ] && [ ${status2} != 0 ] && [ ${status3} != 0 ]; then
statusScriptResult=1
fi
监控脚本:
{w:statusScript:w}
[ "$statusScriptResult" != 1 ] && exit 1
按照上面配置后点击提交,保存应用。
修改LNMP应用的模板 进入LNMP应用管理模板页面。如图:
修改PHP版本号和模板内容:
php-fpm:php.ini配置内容如下:
[PHP]
engine = On
short_open_tag = {w:PHP_short_open_tag:w}
asp_tags = Off
precision = 14
output_buffering = 4096
zlib.output_compression = Off
implicit_flush = Off
unserialize_callback_func =
serialize_precision = 17
disable_functions =
disable_classes =
zend.enable_gc = On
expose_php = On
max_execution_time = 30
max_input_time = 60
memory_limit = {w:PHP_memory_limit:w}
error_reporting = E_ALL & ~E_DEPRECATED & ~E_STRICT
display_errors = Off
display_startup_errors = Off
log_errors = On
log_errors_max_len = 1024
ignore_repeated_errors = Off
ignore_repeated_source = Off
report_memleaks = On
track_errors = Off
html_errors = On
variables_order = "GPCS"
request_order = "GP"
register_argc_argv = Off
auto_globals_jit = On
post_max_size = {w:upload_max_filesize:w}
auto_prepend_file =
auto_append_file =
default_mimetype = "text/html"
default_charset = "UTF-8"
doc_root =
user_dir =
enable_dl = Off
file_uploads = On
upload_max_filesize = {w:upload_max_filesize:w}
max_file_uploads = 20
allow_url_fopen = On
allow_url_include = Off
default_socket_timeout = 60
extension=gd.so
extension=memcached.so
extension=redis.so
extension=sockets.so
extension=mysqli.so
extension=exif.so
extension=pdo_mysql.so
extension=bcmath.so
extension=zip.so
[CLI Server]
cli_server.color = On
[Date]
[filter]
[iconv]
[intl]
[sqlite3]
[Pcre]
[Pdo]
[Pdo_mysql]
pdo_mysql.cache_size = 2000
pdo_mysql.default_socket=
[Phar]
[mail function]
SMTP = localhost
smtp_port = 25
mail.add_x_header = On
[SQL]
sql.safe_mode = Off
[ODBC]
odbc.allow_persistent = On
odbc.check_persistent = On
odbc.max_persistent = -1
odbc.max_links = -1
odbc.defaultlrl = 4096
odbc.defaultbinmode = 1
[Interbase]
ibase.allow_persistent = 1
ibase.max_persistent = -1
ibase.max_links = -1
ibase.timestampformat = "%Y-%m-%d %H:%M:%S"
ibase.dateformat = "%Y-%m-%d"
ibase.timeformat = "%H:%M:%S"
[MySQL]
mysql.allow_local_infile = On
mysql.allow_persistent = On
mysql.cache_size = 2000
mysql.max_persistent = -1
mysql.max_links = -1
mysql.default_port =
mysql.default_socket =
mysql.default_host =
mysql.default_user =
mysql.default_password =
mysql.connect_timeout = 60
mysql.trace_mode = Off
[MySQLi]
mysqli.max_persistent = -1
mysqli.allow_persistent = On
mysqli.max_links = -1
mysqli.cache_size = 2000
mysqli.default_port = 3306
mysqli.default_socket =
mysqli.default_host =
mysqli.default_user =
mysqli.default_pw =
mysqli.reconnect = Off
[mysqlnd]
mysqlnd.collect_statistics = On
mysqlnd.collect_memory_statistics = Off
[OCI8]
[PostgreSQL]
pgsql.allow_persistent = On
pgsql.auto_reset_persistent = Off
pgsql.max_persistent = -1
pgsql.max_links = -1
pgsql.ignore_notice = 0
pgsql.log_notice = 0
[Sybase-CT]
sybct.allow_persistent = On
sybct.max_persistent = -1
sybct.max_links = -1
sybct.min_server_severity = 10
sybct.min_client_severity = 10
[bcmath]
bcmath.scale = 0
[browscap]
[Session]
session.save_handler = files
session.use_strict_mode = 0
session.use_cookies = 1
session.use_only_cookies = 1
session.name = PHPSESSID
session.auto_start = 0
session.cookie_lifetime = 0
session.cookie_path = /
session.cookie_domain =
session.cookie_httponly =
session.serialize_handler = php
session.gc_probability = 1
session.gc_divisor = 1000
session.gc_maxlifetime = 1440
session.referer_check =
session.cache_limiter = nocache
session.cache_expire = 180
session.use_trans_sid = 0
session.hash_function = 0
session.hash_bits_per_character = 5
url_rewriter.tags = "a=href,area=href,frame=src,input=src,form=fakeentry"
[MSSQL]
mssql.allow_persistent = On
mssql.max_persistent = -1
mssql.max_links = -1
mssql.min_error_severity = 10
mssql.min_message_severity = 10
mssql.compatibility_mode = Off
mssql.secure_connection = Off
[Assertion]
[COM]
[mbstring]
[gd]
[exif]
[Tidy]
tidy.clean_output = Off
[soap]
soap.wsdl_cache_enabled=1
soap.wsdl_cache_dir="/tmp"
soap.wsdl_cache_ttl=86400
soap.wsdl_cache_limit = 5
[sysvshm]
[ldap]
ldap.max_links = -1
[mcrypt]
[dba]
[opcache]
[curl]
[openssl]
安装LNMP环境应用并测试
在应用管理列表找到刚才新添加的LNMP应用,选择创建服务
按照上面配置填写后,提交部署。
部署完成后,在浏览器访访问刚才填写的域名+端口号,如图:
说明:如果只是测试的话,网站域名随意填写,更改访问终端的hosts文件