标签: nginx

  • Docker部署nginx 搭建简单web页面

    前言

    如果你的服务器没有安装docker,可以使用get.docker.com一件脚本来安装docker,代码如下

    $curl -fsSL https://get.docker.com -o install-docker.sh
    $sudo sh install-docker.sh

    人非圣贤,上面这段代码肯定记不住的,到用的时候又不太好找,这里教一个小窍门,直接在浏览器打开get.docker.com,打开是一段脚本代码,可以在注释里找到我们想要的代码如下

    # 1. download the script
    #
    #   $ curl -fsSL https://get.docker.com -o install-docker.sh
    #
    # 2. verify the script's content
    #
    #   $ cat install-docker.sh
    #
    # 3. run the script with --dry-run to verify the steps it executes
    #
    #   $ sh install-docker.sh --dry-run
    #
    # 4. run the script either as root, or using sudo to perform the installation.
    #
    #   $ sudo sh install-docker.sh

    一 准备html

    新建一个html文件夹,在文件夹中新建index.html写上hello world

    mkdir html && cd html
    vim index.html

    二 准备Dockerfile文件

    在html的同级目录,新建Dockerfile

    FROM nginx
    COPY ./html /usr/share/nginx/html

    三 构建docker 镜像

    在Dockerfile的同级目录 运行下面命令

    docker build -t some-content-nginx .

    四 运行docker容器

    docker run --name some-nginx -d -p 8080:80 some-content-nginx

    在浏览器输入服务器ip:8080 就能打开网页了

  • docker本地开发thinkphp

    本地开发thinkphp可以使用docker-compose来配置一个本地的开发环境,要是使用到的镜像有php,nginx,mysql,phpmyadmin,主要用的是前两个,后面两个可以根据项目来判断是否需要。

    文件目录如下

    1. docker文件夹放dockerfile和其他配置文件
    2. html文件夹放thinkphp源码
    3. log是nginx的日志文件
    4. mysql是mysql的数据文件夹
    5. docker-compose.yml

    首先我们新建文件夹mythinkphp,并在文件夹中创建docker-compose.yml文件。

    你需要将下面配置中的mysql环境变量定义成自己的,有了这些你就可以使用phpmyadmin的后台管理数据库了。

    version: '3.9'
    services: 
      db:
        image: mysql:5.7
        volumes:
          - ./mysql:/var/lib/mysql
        restart: always
        environment:
          MYSQL_ROOT_PASSWORD: yourpassword//1设置root的密码
          MYSQL_DATABASE: thinkphp//2设置数据库名
          MYSQL_USER: thinphp//3设置数据库用户名
          MYSQL_PASSWORD: &fhf%$#pa//4设置数据库密码
        depends_on:
          - php
      nginx:
        build: ./docker/nginx/
        container_name: nginx-container
        ports: 
          - 1234:80
        volumes:
          - ./html:/var/www/html
          - ./log:/var/log/nginx/
        links:
          - php
      php:
        # image: php:7.4-fpm
        build: ./docker/php/
        container_name: php-container
        expose:
          - 9000
        volumes: 
          - ./html:/var/www/html
          # - ./phpconfig:/usr/local/etc/php/
      phpmyadmin:
        image: phpmyadmin
        restart: always
        ports:
          - 8080:80
        depends_on:
          - db

    如何再创建一个docker的文件夹,主要放镜像的dockerfile文件和其他配置文件

    /docker/nginx/Dockerfile

    FROM nginx:latest   
    COPY ./default.conf /etc/nginx/conf.d/default.conf

    nginx的配置文件/docker/nginx/default.conf

    server {  
    
         listen 80 default_server;  
         root /var/www/html/tp5/public;  
         index index.html index.php;  
    
         charset utf-8;  
    
    
    
         location / {
             if (!-e $request_filename) {
       		rewrite  ^(.*)$  /index.php?s=/$1  last;
              }
         }  
    
         location = /favicon.ico { access_log off; log_not_found off; }  
         location = /robots.txt { access_log off; log_not_found off; }  
    
         access_log off;  
         error_log /var/log/nginx/error.log error;  
    
         sendfile off;  
    
         client_max_body_size 100m;  
    
         location ~ .php$ {  
          fastcgi_split_path_info ^(.+.php)(/.+)$;  
          fastcgi_pass php:9000;  
          fastcgi_index index.php;  
          include fastcgi_params;  
          fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;  
          fastcgi_intercept_errors off;  
          fastcgi_buffer_size 16k;  
          fastcgi_buffers 4 16k;  
        }
    
         location ~ /.ht {  
          deny all;  
         }  
        } 

    php的容器生成文件/docker/php/Dockerfile

    FROM php:7.4-fpm
    RUN mv "$PHP_INI_DIR/php.ini-production" "$PHP_INI_DIR/php.ini"
    RUN docker-php-ext-install mysqli pdo_mysql

    接下来我们将thinkphp的文件放入tp5的文件夹,并放到html文件夹中

    设置完成后,我们就可以启动本地环境了

    docker-compose up -d
    • 加-d参数意思在后台启动服务

    关闭本地环境

    docker-compose down

    这就是thinkphp用docker的方式配置的的本地开发环境。