标签

docker运行centos后傻瓜式安装nginx+php

By 小鸟游·飒

  • nginx部分


FROM centos:7.2.1511

RUN curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo

CMD ["/bin/bash"]

#安装依赖

yum makecache fast


yum -y install \

    lynx \

    wget \

    openssl \

    openssl-devel \

    pcre \

    pcre-devel \

    zlib


yum group -y install "Development Tools"


#安装nginx(编译开启ssl部分)

wget http://nginx.org/download/nginx-1.18.0.tar.gz \

    && tar -zxvf nginx-1.18.0.tar.gz \

    && mv nginx-1.18.0 nginx \

    && rm -f nginx-1.18.0.tar.gz \

    && cd nginx \

    && ./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module \

    && make \

    && make install


#查看nginx路径

whereis nginx


#查询nginx进程

ps aux|grep nginx


#单独的nginx运行

cd /usr/local/nginx/sbin/

./nginx 

./nginx -s stop

./nginx -s quit

./nginx -s reload

docker exec -itd nginx /usr/local/nginx/sbin/nginx -s reload


#设置容器内nginx自启动

cd /

vi start.sh


#复制如下进入 start.sh


#!/bin/sh

   /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf

   /bin/bash


#给予权限

chmod -R 777 start.sh


#重建新镜像

docker commit -a="hayate<391942308@163.com>" -m="备注" [CONTAINER ID] [IMAGE]:[TAG]


#通过脚本启动新容器

docker run -it -p 80:80 -p 443:443 [IMAGE]:[TAG] /start.sh


#设置容器随docker启动而启动

docker container update --restart=always [CONTAINER ID]


#重启docker 尝试是否成功

systemctl restart docker

#nginx配置

主配置

worker_processes auto;

error_log  /www/serverlog/nginx/nginx_error.log;

pid        /www/serverdata/nginx/nginx.pid;

worker_rlimit_nofile 51200;


events

    {

        use epoll;

        worker_connections 51200;

        multi_accept on;

    }


http

    {

        include       mime.types;


        default_type  application/octet-stream;


        server_names_hash_bucket_size 512;

        client_header_buffer_size 32k;

        large_client_header_buffers 4 32k;

        client_max_body_size 256m;


        sendfile   on;

        tcp_nopush on;


        keepalive_timeout 60;


        tcp_nodelay on;


        fastcgi_connect_timeout 300;

        fastcgi_send_timeout 300;

        fastcgi_read_timeout 300;

        fastcgi_buffer_size 64k;

        fastcgi_buffers 4 64k;

        fastcgi_busy_buffers_size 128k;

        fastcgi_temp_file_write_size 256k;

fastcgi_intercept_errors on;


        gzip on;

        gzip_min_length  1k;

        gzip_buffers     4 16k;

        gzip_http_version 1.1;

        gzip_comp_level 2;

        gzip_types     text/plain application/javascript application/x-javascript text/javascript text/css application/xml application/json;

        gzip_vary on;

        gzip_proxied   expired no-cache no-store private auth;

        gzip_disable   "MSIE [1-6]\.";


        limit_conn_zone $binary_remote_addr zone=perip:10m;

limit_conn_zone $server_name zone=perserver:10m;


        server_tokens off;

        access_log off;


include /www/serverconf/nginx/*.conf;

}


站点配置

server {

    listen       80;

    server_name  localhost;

    root         [绝对路径,最后/不需要加(/www/web)];


    #charset koi8-r;


    #access_log  logs/host.access.log  main;


    #ERROR-PAGE-START  错误页配置,可以注释、删除或修改

    error_page 404 /404.html;

    error_page 502 /502.html;

    #ERROR-PAGE-END


    #禁止访问的文件或目录

    location ~ ^/(\.user.ini|\.htaccess|\.git|\.svn|\.project|LICENSE|README.md)

    {

        return 404;

    }

    

    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$

    {

        expires      30d;

        error_log off;

        access_log off;

    }

    location ~ .*\.(js|css)?$

    {

        expires      12h;

        error_log off;

        access_log off; 

    }

    location / {

            index  index.php index.html index.htm;

             #如果请求既不是一个文件,也不是一个目录,则执行一下重写规则

             if (!-e $request_filename)

             {

                #地址作为将参数rewrite到index.php上。

                rewrite ^/(.*)$ /index.php?s=$1;

                #若是子目录则使用下面这句,将subdir改成目录名称即可。

                #rewrite ^/wcapp/public/(.*)$ /subdir/index.php?s=$1;

             }

        }

    location /api/ {

        index  index.php index.html index.htm;

         #如果请求既不是一个文件,也不是一个目录,则执行一下重写规则

         if (!-e $request_filename)

         {

            #地址作为将参数rewrite到index.php上。

            #rewrite ^/(.*)$ /index.php?s=$1;

            #若是子目录则使用下面这句,将subdir改成目录名称即可。

            rewrite ^/api/(.*)$ /api/index.php?s=$1;

         }

    }


    # proxy the PHP scripts to Apache listening on 127.0.0.1:80

    #

    #location ~ \.php$ {

    #    proxy_pass   http://127.0.0.1;

    #}


    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000

    #

    location ~ \.php$ {

        fastcgi_pass   php:9000;

        fastcgi_index  index.php;

        fastcgi_param  SCRIPT_FILENAME  [绝对路径,最后/不需要加(/www/web)]$fastcgi_script_name;

        include        fastcgi_params;

    }


    # deny access to .htaccess files, if Apache's document root

    # concurs with nginx's one

    #

    location ~ /\.ht {

        deny  all;

    }

}



  • php部分


docker pull php:7.2-fpm


//重新进入容器

docker exec -it php /bin/bash


//安装redis扩展

curl -L -o /tmp/redis.tar.gz https://github.com/phpredis/phpredis/archive/3.1.3.tar.gz

tar xfz /tmp/redis.tar.gz

rm -r /tmp/redis.tar.gz

mkdir -p /usr/src/php/ext

mv phpredis-3.1.3 /usr/src/php/ext/redis

docker-php-ext-install redis


//更新软件源

apt update

//安装扩展依赖

apt install -y libwebp-dev libjpeg-dev libpng-dev libfreetype6-dev

//解压源码

docker-php-source extract


//安装GD扩展

cd /usr/src/php/ext/gd

//编译

docker-php-ext-configure gd --with-webp-dir=/usr/include/webp --with-jpeg-dir=/usr/include --with-png-dir=/usr/include --with-freetype-dir=/usr/include/freetype2

(7.4版本开始修改为 <docker-php-ext-configure gd --with-webp=/usr/include/webp --with-jpeg=/usr/include --with-freetype=/usr/include/freetype2/>)

//安装

docker-php-ext-install gd


//安装pdo_mysql扩展

docker-php-ext-install pdo pdo_mysql


//重启容器

docker restart php


#重建新镜像

docker commit -a="hayate<391942308@163.com>" -m="备注" [CONTAINER ID] [IMAGE]:[TAG]



  • mysql部分


docker pull mysql:5.7

***可能会因为php缺少mysqli,可以进回php容器内,docker-php-ext-install mysqli



刮刮乐


补充部分:

对于单php-fpm形式部署多站点,暂时不清楚不同端口如何区分,统一端口的话,按照普通服务器方式配置即可。

2021-02-23 17:02
暂时关闭评论
暂无相关评论