docker 配置 nginx外部转发访问 php+mysql+redis+vue+node

外部nginx 配置

server {
        listen 80;
        listen 443 ssl;
        server_name web.docker.com;
        location / {
	proxy_pass http://192.168.56.101:8000;
	proxy_set_header Host $host;
	proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
	proxy_set_header X-Forwarded-Proto $scheme;
	proxy_http_version 1.1;
	proxy_set_header Connection "";
	proxy_buffering off;
	proxy_read_timeout 3600s;
	proxy_send_timeout 3600s;
       }
}

server {
        listen 80;
        #listen 8001;
        listen 443 ssl;
        server_name vue.docker.com;
        location / {
	proxy_pass http://192.168.56.101:7000;
	proxy_set_header Host $host;
	proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
	proxy_set_header X-Forwarded-Proto $scheme;
	proxy_http_version 1.1;
	proxy_set_header Connection "";
	proxy_buffering off;
	proxy_read_timeout 3600s;
	proxy_send_timeout 3600s;
       }
}

容器内部nginx


server
{
        listen          80;
        server_name     vue.docker.com;
        index           index.html index.php index.htm;
        root            /var/www/html/dist/;

      try_files $uri $uri/ /index.html;
}


server {
    listen 443 ssl;
    server_name     vue.docker.com;
    index           index.html index.php index.htm;
     root            /var/www/html/dist/;
     ssl_certificate /etc/ssl/certs/nginx-selfsigned.crt;
     ssl_certificate_key /etc/ssl/private/nginx-selfsigned.key;
     #ssl_dhparam /etc/ssl/certs/dhparam.pem;
     ssl_session_timeout  5m;
     ssl_prefer_server_ciphers on;
     ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDH:AES:HIGH:!aNULL:!MD5:!ADH:!DH;
     ssl_protocols TLSv1 TLSv1.1 TLSv1.2;

     try_files $uri $uri/ /index.html;
}

访问:https://web.docker.com | https://vue.docker.com
执行:sh docker.sh 一键部署并启动 | 更改代码后重启项目配置
docker 配置 nginx外部转发访问 php+mysql+redis+vue+node_第1张图片
docker 配置 nginx外部转发访问 php+mysql+redis+vue+node_第2张图片

entrypoint.sh

#!/bin/bash 
if [ ! -f "/etc/ssl/private/nginx-selfsigned.key" ];
then
sudo mkdir -p /etc/ssl/certs \
&& sudo mkdir -p /etc/ssl/private \
&& sudo openssl genrsa -out /etc/ssl/private/nginx-selfsigned.key 2048 \
&& sudo openssl req -new -subj "/C=US/ST=ShangHai/L=ShangHai/O=/OU=/CN=" -key /etc/ssl/private/nginx-selfsigned.key -out /etc/ssl/certs/nginx-selfsigned.csr \
&& sudo mv /etc/ssl/private/nginx-selfsigned.key /etc/ssl/private/nginx-selfsigned.origin.key \
&& sudo openssl rsa -in /etc/ssl/private/nginx-selfsigned.origin.key  -out /etc/ssl/private/nginx-selfsigned.key \
&& sudo openssl x509 -req -days 3650 -in /etc/ssl/certs/nginx-selfsigned.csr  -signkey /etc/ssl/private/nginx-selfsigned.key -out /etc/ssl/certs/nginx-selfsigned.crt

fi

sudo cp /var/www/html/docker/nginx/default.conf /etc/nginx/conf.d/default.conf;
sudo nginx -s reload;

if [ ! -f "/etc/bash_profile" ];
then
    sudo touch /etc/bash_profile
    sudo chmod 777 /etc/bash_profile
fi
sudo echo 'PATH=$PATH:$HOME/bin:/usr/bin' >> /etc/bash_profile 
source /etc/bash_profile

if [ ! -d "/var/www/html/node_modules" ];
then
sudo npm install -y
fi

sudo npm run build

docker.sh

#!/bin/bash
if ! [ -x "$(command -v docker)" ]
 then 
   if [ -e /etc/os-release ]
   then
      source /etc/os-release	  
         echo 'os-release ' $ID $VERSION_ID;
      if [ $ID == centos -a $VERSION_ID -eq 8 ]
      then
            echo "dnf remove docker-----"
            # systemctl stop firewalld.service 
            sudo dnf remove docker-*

            echo "install device-mapper-persistent-data lvm2"   
            sudo dnf install device-mapper-persistent-data lvm2 -y
            sudo dnf config-manager --add-repo=https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
            echo "install docker-ce"   
            sudo dnf install docker-ce --nobest -y
            systemctl enable docker
            systemctl start docker
         elif  [ $ID == 'ubuntu' ]
         then   
          echo 'apt-get instll docker ----'
             sudo apt-get update
             sudo apt-get upgrade
             sudo apt-get install docker.io
             sudo service docker start
         else   
          
            echo 'yum instll docker -----'
            sudo yum remove docker-*
            sudo yum install -y yum-utils \
                  device-mapper-persistent-data \
                  lvm2      
            sudo yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo  
            sudo yum install docker-ce --nobest -y   
            systemctl enable docker
            systemctl start docker
      fi
   fi
fi

if ! [ -x "$(command -v docker-compose)" ]
  then
      sudo rm -rf /usr/local/bin/docker-compose && rm -rf /usr/bin/docker-compose
      echo "curl docker-compose"
      sudo curl -L "https://github.com/docker/compose/releases/download/1.24.1/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
      echo "chmod docker-compose"
      sudo chmod +x /usr/local/bin/docker-compose
      echo "ln doker-compose"
      sudo ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose
      docker-compose --version
fi

export DOCKER_SH_MY_PATH=`pwd`
# export $(xargs < $path/.env)
cd docker

if docker inspect -f "{{.State.Running}}" web >/dev/null 2>&1; then
    docker compose restart web
fi
if docker inspect -f "{{.State.Running}}" vue >/dev/null 2>&1; then
    docker compose restart vue
fi

docker compose up -d 

Dockerfile

FROM centos:latest

WORKDIR /var/www/html

RUN sed -i 's/mirrorlist/#mirrorlist/g' /etc/yum.repos.d/CentOS-* \ 
&& sed -i 's|#baseurl=http://mirror.centos.org|baseurl=http://vault.centos.org|g' /etc/yum.repos.d/CentOS-* \
&& yum makecache && dnf distro-sync -y \
&& dnf install -y curl git sudo psmisc net-tools\
&& dnf module reset -y nginx && dnf module enable -y nginx:1.20 && dnf module list  nginx && dnf install -y nginx && systemctl enable nginx \
&& dnf install -y https://dl.fedoraproject.org/pub/epel/epel-release-latest-8.noarch.rpm \
&& dnf install -y https://rpms.remirepo.net/enterprise/remi-release-8.4.rpm \
&& dnf repolist \
&& dnf module enable php:remi-8.0 -y && dnf module disable php:remi-8.0 -y && dnf module enable php:remi-8.2 -y \
&& dnf install -y php php-cli php-common php-fpm  && systemctl enable php-fpm \
&& dnf install -y php-gd php-imagick php-zip php-redis php-swoole php-xdebug php-mysql php-memcached php-soap\
&& php -r "readfile('https://getcomposer.org/installer');" | php -- --install-dir=/usr/bin/ --filename=composer \
&& curl --silent --location https://rpm.nodesource.com/setup_18.x | bash -  \
&& dnf install -y nodejs && npm install -g npm\
&& curl -sL https://dl.yarnpkg.com/rpm/yarn.repo | sudo tee /etc/yum.repos.d/yarn.repo \
&& dnf install -y yarn \
&& dnf install -y supervisor &&  systemctl enable supervisord\
&& rm -rf /tmp/* /var/tmp/*

RUN groupadd  sail 
RUN useradd -ms /bin/bash --no-user-group -g sail -u 1337 sail && echo "sail ALL=(ALL:ALL) NOPASSWD: ALL" >> /etc/sudoers
RUN mkdir -p /var/log/docker && chmod -R 744 /var/log/docker

RUN sed -i 's/pid = \/run\/php-fpm\/php-fpm.pid/pid =\/usr\/sbin\/php-fpm.pid/g' /etc/php-fpm.conf \ 
&& sed -i 's/\/run\/php-fpm\/www.sock/127.0.0.1:9000/g' /etc/php-fpm.d/www.conf \ 
&& sed -i 's/user = apache/user = nginx/g' /etc/php-fpm.d/www.conf \ 
&& sed -i 's/group = apache/group = nginx/g' /etc/php-fpm.d/www.conf 

docker-compose.yaml

#!/bin/bash
if ! [ -x "$(command -v docker)" ]
 then 
   if [ -e /etc/os-release ]
   then
      source /etc/os-release	  
         echo 'os-release ' $ID $VERSION_ID;
      if [ $ID == centos -a $VERSION_ID -eq 8 ]
      then
            echo "dnf remove docker-----"
            # systemctl stop firewalld.service 
            sudo dnf remove docker-*

            echo "install device-mapper-persistent-data lvm2"   
            sudo dnf install device-mapper-persistent-data lvm2 -y
            sudo dnf config-manager --add-repo=https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
            echo "install docker-ce"   
            sudo dnf install docker-ce --nobest -y
            systemctl enable docker
            systemctl start docker
         elif  [ $ID == 'ubuntu' ]
         then   
          echo 'apt-get instll docker ----'
             sudo apt-get update
             sudo apt-get upgrade
             sudo apt-get install docker.io
             sudo service docker start
         else   
          
            echo 'yum instll docker -----'
            sudo yum remove docker-*
            sudo yum install -y yum-utils \
                  device-mapper-persistent-data \
                  lvm2      
            sudo yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo  
            sudo yum install docker-ce --nobest -y   
            systemctl enable docker
            systemctl start docker
      fi
   fi
fi

if ! [ -x "$(command -v docker-compose)" ]
  then
      sudo rm -rf /usr/local/bin/docker-compose && rm -rf /usr/bin/docker-compose
      echo "curl docker-compose"
      sudo curl -L "https://github.com/docker/compose/releases/download/1.24.1/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
      echo "chmod docker-compose"
      sudo chmod +x /usr/local/bin/docker-compose
      echo "ln doker-compose"
      sudo ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose
      docker-compose --version
fi

export DOCKER_SH_MY_PATH=`pwd`
# export $(xargs < $path/.env)
if [ -d '.git' ]; then
    git pull origin master
fi

cd docker

if docker inspect -f "{{.State.Running}}" web >/dev/null 2>&1; then
    docker compose restart web
fi
if docker inspect -f "{{.State.Running}}" vue >/dev/null 2>&1; then
    docker compose restart vue
fi


docker compose up -d
  

supervisor.ini

[supervisord]
nodaemon=true
user=root
logfile=/var/log/supervisor/supervisord.log
pidfile=/var/run/supervisord.pid

[program:nginx]
directory=/usr/sbin
command=/usr/sbin/nginx -g 'daemon off;' -c /etc/nginx/nginx.conf
user=root                                               
autostart=true                                       
autorestart=true                                     
stopasgroup=true                                        
killasgroup=true                                        
stdout_logfile=/var/log/nginx/supervisord-out.log         
stderr_logfile=/var/log/nginx/supervisord-err.log

[program:php-fpm]
directory=/usr/sbin
command=sudo /usr/sbin/php-fpm --nodaemonize -c /etc/php-fpm.conf
user=sail
killasgroup=true
stopasgroup=true
autostart=true
autorestart = true
stdout_logfile=/var/log/php-fpm/std-out.log
stdout_logfile_maxbytes=0
stderr_logfile=/var/log/php-fpm/std-err.log
stderr_logfile_maxbytes=0

[program:web]
command=sh /var/www/html/docker/entrypoint.sh
user=sail
environment=LARAVEL_SAIL="1"
stdout_logfile=/var/log/docker/web-std-out.log
stderr_logfile=/var/log/docker/web-std-err.log

你可能感兴趣的:(docker,nginx,php)