当前位置: 首页 > 工具软件 > Docker-PHP > 使用案例 >

docker 安装nginx 和php

程皓轩
2023-12-01

安装php

  1. 下载 php7.3.5镜像
    sudo docker pull php:7.3.5-fpm
  2. 创建映射目录
    sudo mkdir -p /http/htdocs
  3. 生成容器
    sudo docker run --name php-fpm -v /http/htdocs:/www -d php:7.3.5-fpm
    容器名称为 php-fpm,目录 htdocs挂载到容器的 /www
    php已配置完成
  4. php 安装mysql 扩展
    1. 进入docker 容器 :docker exec -it 6fe8f241c33e /bin/bash
    2. docker-php-ext-install mysqli

安装nginx

  1. 下载nginx1.16.0镜像
    sudo docker pull nginx:1.16.0
  2. 创建映射目录
    sudo mkdir -p /http/nginx/logs /http/nginx/conf /http/nginx/conf.d
  3. 设置/http/nginx/conf/nginx.conf
    使用sudo touch /http/nginx/conf/nginx.conf创建文件,编辑文件内容如下

user  nginx;
worker_processes  1;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;

events {
    worker_connections  1024;
}

http {
    include       /etc/nginx/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;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;
}
  1. 设置/http/nginx/conf.d/www.conf
    使用sudo touch /http/nginx/conf.d/www.conf创建文件,编辑文件内容如下

        

server {
    listen       80;
    server_name  localhost;

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm index.php;
    }

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    location ~ \.php$ {
        fastcgi_pass   php:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  /www/$fastcgi_script_name;
        include        fastcgi_params;
    }
}
  1. 生成容器并链接php
    sudo docker run --name nginx -p 80:80 -d -v /http/htdocs:/usr/share/nginx/html -v /http/nginx/conf:/etc/nginx/conf -v /http/nginx/conf.d:/etc/nginx/conf.d -v /http/nginx/logs:/var/log/nginx --link php-fpm:php nginx:1.16.0

  2. 测试php是否可以运行
    使用sudo touch /http/htdocs/index.php创建文件,编辑文件内容如下

<?php
phpinfo();

nginx 配置PHP取消index.php 

server {
listen       80;
server_name  localhost;

location / {
root   /usr/share/nginx/html;
index  index.html index.htm index.php;
if (!-e $request_filename) {
rewrite  ^(.*)$  /index.php?s=$1  last;
break;
}

}


error_page   500 502 503 504  /50x.html;
location = /50x.html {
root   /usr/share/nginx/html;
}

location ~ \.php$ {
fastcgi_pass   php:9000;
fastcgi_index  index.php;
fastcgi_param  SCRIPT_FILENAME  /www/$fastcgi_script_name;
include        fastcgi_params;
#fastcgi_param PATH_INFO $1;
#include  fastcgi.conf;

}
}

https配置:

server
{
    listen 443 ssl;  #配置HTTPS的默认访问端口号为443。此处如>果未配置HTTPS的默认访问端口,可能会造成Nginx无法启动。Nginx 1.15.0以上版本请使用listen 443 ssl代替listen 443和ssl on。
    server_name 域名地址; #将www.baidu.com修改为您证书绑定的域名,例如:www.example.com。如果您购买>的是通配符域名证书,要修改为通配符域名,例如:*.aliyun.com。
    index index.html index.htm index.php;
    root  /usr/share/nginx/html;
    ssl_certificate /etc/nginx/cert/cn.pem;  #将domain name.pem替换成您证书的文件名称。
    ssl_certificate_key /etc/nginx/cert/key.key;  #将domain name.key替换成您证书的密钥文件名称。
    ssl_session_timeout 5m;
    ssl_ciphers ECDHE-RSA-AES256-GCM-SHA384:ECDHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-SHA384:ECDHE-RSA-AES128-SHA256:ECDHE-RSA-AES256-SHA:ECDHE-RSA-AES128-SHA:DHE-RSA-AES256-SHA256:DHE-RSA-AES128-SHA256:DHE-RSA-AES256-SHA:DHE-RSA-AES128-SHA:AES256-GCM-SHA384:AES128-GCM-SHA256:AES256-SHA256:AES128-SHA256:AES256-SHA:AES128-SHA:HIGH:!aNULL:!eNULL:!EXPORT:!DES:!MD5:!PSK:!RC4; #使用此加密套件。
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2; #使用该协议进行配置>。
    ssl_prefer_server_ciphers on;

    location / {
            root   /usr/share/nginx/html;
            index  index.html index.htm index.php;
            if (!-e $request_filename) {
            rewrite  ^(.*)$  /index.php?s=$1  last;
            break;
        }
    
    }

    location /nginx_status
    {
    stub_status on;
    access_log   off;
    }

    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
    {
    expires      30d;
    }
    
    location ~ .*\.(js|css)?$
    {
    expires      12h;
    }

    location ~ /.well-known {
    allow all;
    }

    location ~ /\.
    {
    deny all;
    }
    location ~ \.php$ {
    fastcgi_pass   php:9000;
    fastcgi_index  index.php;
    fastcgi_param  SCRIPT_FILENAME /usr/share/nginx/html$fastcgi_script_name;
    #  fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
    include        fastcgi_params;
    #fastcgi_param PATH_INFO $1;
    #include  fastcgi.conf;

    }


}
 类似资料: