注意:php7需要安装mysql扩展,才能正常连接
# 创建目录:
# cert 存放证书文件的目录
# log 存放日志文件的目录
# nginx_conf 存放nginx.conf配置文件的目录
# wwwroot 存放网站文件的根目录
mkdir -p /www/cert /www/log /www/nginx_conf /www/wwwroot
# nginx.conf配置文件 配置http(跳转到https)/ https 访问
[root@VM_1_62_centos /]# cat /www/nginx_conf/nginx.conf
worker_processes 2;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
keepalive_timeout 65;
root /usr/share/nginx/html;
index index.html index.htm index.php;
server {
listen 80;
server_name localhost;
location / {
# 跳转到https进行访问
# rewrite ^/(.*) https://www.xxxxx.net/$1 permanent;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
location ~* .*\.(php|php5)?$ {
root /usr/share/nginx/html;
fastcgi_pass 192.158.0.2:9000;
fastcgi_index index.php;
include fastcgi.conf;
}
}
server {
listen 443;
server_name localhost;
ssl on;
###
# 配置https域名访问证书
###
ssl_certificate cert/1_www.xxxxx.net_bundle.crt;
ssl_certificate_key cert/2_www.xxxxx.net.key;
ssl_session_timeout 5m;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
location / {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
location ~* .*\.(php|php5)?$ {
root /usr/share/nginx/html;
fastcgi_pass 192.158.0.2:9000;
fastcgi_index index.php;
include fastcgi.conf;
}
}
}
1)编写Dockerfile文件,*****安装php连接mysql的扩展*****
[root@VM_1_62_centos ~]# vim Dockerfile
FROM php:7.2-fpm-alpine3.8
MAINTAINER "eric"
RUN /usr/local/bin/docker-php-ext-install pdo_mysql && /usr/local/bin/docker-php-ext-install mysqli
2)生成镜像 php-fpm-mysqli:v0.1-1 .
[root@VM_1_62_centos ~]# docker build -t php-fpm-mysqli:v0.1-1 .
3)配置网络network
docker network create --driver=bridge --subnet=192.158.0.0/16 root_nginx
4)启动php
docker run -d --rm --name fpm \
--network root_nginx \
--ip 192.158.0.2 \
-v /www/wwwroot/:/usr/share/nginx/html/ \
php-fpm-mysqli:v0.1-1
5)启动nginx
docker run --name nginx -d --rm \
-v /www/wwwroot/:/usr/share/nginx/html/ \
-v /www/nginx_conf/nginx.conf:/etc/nginx/nginx.conf \
-v /www/cert/:/etc/nginx/cert/ \
-v /www/log/access.log:/var/log/nginx/access.log \
--network root_nginx \
-p 80:80 -p 443:443 \
nginx:1.14.1-alpine
# 编写docker-compose.yml文件
[root@VM_1_62_centos ~]# cat /root/docker-compose.yml
version: "3"
services:
fpm:
image: php:7.2-fpm-alpine3.8
container_name: fpm
volumes:
- /www/wwwroot:/usr/share/nginx/html/
networks:
nginx:
ipv4_address: 192.158.0.2
nginx:
image: nginx:1.14.1-alpine
container_name: nginx
ports:
- 80:80
- 443:443
volumes:
- /www/wwwroot/:/usr/share/nginx/html/
- /www/nginx_conf/nginx.conf:/etc/nginx/nginx.conf
- /www/cert/:/etc/nginx/cert/
- /www/log/access.log:/var/log/nginx/access.log
networks:
nginx:
ipv4_address: 192.158.0.3
networks:
nginx:
driver: bridge
ipam:
config:
- subnet: 192.158.0.0/16
[root@VM_1_62_centos ~]# docker-compose up -d
[root@VM_1_62_centos ~]# docker ps -a
[root@VM_1_62_centos ~]# docker-compose stop
[root@VM_1_62_centos ~]# docker-compose rm
[root@VM_1_62_centos /]# cat /www/wwwroot/db.php
<?php
$conn = new mysqli("cdb-xxxxxx.bj.tencentcdb.com:10016", "wordpress", "woxxxxxxxxxxxxxx!#");
if ($conn->connect_error) {
die("connect error: " . $conn->connect_error);
}
echo "connect success!";
?>
注:如有错误,请指正,谢谢!
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。