Browse Source

优化nginx,添加定时配置检测

main
xiaoz 1 year ago
parent
commit
770128166e
  1. 6
      README.md
  2. 2
      mysql/.env
  3. 27
      nginx/conf/nginx.conf
  4. 1
      nginx/docker-compose.yml
  5. 8
      nginx/sh/install_nginx.sh
  6. 16
      nginx/sh/xc.sh
  7. 19
      php/Dockerfile
  8. 53
      php/install.sh
  9. 158
      php/nginx.conf
  10. 6
      php/run.sh
  11. 2
      php74/docker-compose.yaml
  12. 4
      php74/www.conf
  13. 49
      wwwroot/default/index.html
  14. BIN
      wwwroot/phpMyAdmin.zip

6
README.md

@ -1,2 +1,4 @@ @@ -1,2 +1,4 @@
# dnmp
Docker + nginx + MySQL + PHP
# DNMP
Learn more about how DNMP can streamline your development process and make your projects more efficient.

2
mysql/.env

@ -1,2 +1,2 @@ @@ -1,2 +1,2 @@
TZ=Asia/Shanghai
MYSQL_ROOT_PASSWORD=9N7DahRCqinY
MYSQL_ROOT_PASSWORD=root3306

27
nginx/conf/nginx.conf

@ -89,7 +89,7 @@ http { @@ -89,7 +89,7 @@ http {
open_file_cache_min_uses 2;
open_file_cache_errors on;
# default site
server {
listen 80;
server_name localhost;
@ -98,23 +98,16 @@ http { @@ -98,23 +98,16 @@ http {
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
root /var/www/html/default;
index index.php index.html index.htm;
# PHP 配置
location ~ [^/]\.php(/|$) {
include fastcgi_params;
fastcgi_pass 127.0.0.1:9074;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
# phpmyadmin
location /phpmyadmin {
alias /var/www/html/phpMyAdmin/; # /path/to/phpmyadmin 替换为你的实际目录
index index.php index.html index.htm;
# PHP 配置
location ~ /phpmyadmin/.+\.php$ {
include fastcgi_params;
fastcgi_pass 127.0.0.1:9074;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /var/www/html/phpMyAdmin$fastcgi_script_name;
}
}
#error_page 404 /404.html;

1
nginx/docker-compose.yml

@ -12,6 +12,7 @@ services: @@ -12,6 +12,7 @@ services:
- ./conf/vhost:/usr/local/nginx/conf/vhost
- ./conf/stream:/usr/local/nginx/conf/stream
- ./logs:/usr/local/nginx/logs
- ./ssl:/usr/local/nginx/conf/ssl
restart:
always

8
nginx/sh/install_nginx.sh

@ -43,7 +43,9 @@ install_before(){ @@ -43,7 +43,9 @@ install_before(){
#创建软连接
ln -s /usr/local/nginx/sbin/nginx /usr/sbin/nginx
#创建缓存文件夹
mkdir -p /data/caches
# mkdir -p /data/caches
# 创建SSL证书文件夹
mkdir -p /usr/local/nginx/conf/ssl
#创建站点文件夹
mkdir -p /var/www/html
}
@ -71,7 +73,9 @@ install_nginx(){ @@ -71,7 +73,9 @@ install_nginx(){
#添加定时任务
add_crontab() {
echo "添加定时任务"
# echo "*/3 * * * * /root/pull.sh" >> /etc/crontabs/root
# 检测配置文件和SSL证书变化
echo "*/3 * * * * /root/xc.sh check_change" >> /etc/crontabs/root
# 日志分割
echo "50 23 * * * /usr/sbin/logrotate -f /etc/logrotate.d/nginx" >> /etc/crontabs/root
}

16
nginx/sh/xc.sh

@ -9,6 +9,11 @@ @@ -9,6 +9,11 @@
NGINX_PATH="/usr/local/nginx"
nginx="${NGINX_PATH}/sbin/nginx"
# 配置文件路径
CONF_PATH="${NGINX_PATH}/conf/"
# SSL证书路径
SSL_PATH="${CONF_PATH}/ssl/"
#获取用户传递的参数
arg1=$1
@ -45,6 +50,14 @@ function check_conf() { @@ -45,6 +50,14 @@ function check_conf() {
$nginx -t
}
# 检查配置/SSL证书是否有更新,有更新则重载
function check_change() {
find ${CONF_PATH}/conf/ -mmin -1 -exec /usr/sbin/xc.sh reload {} +
echo '-------------------------------------'
sleep 3
find ${SSL_PATH}/ssl/ -mmin -1 -exec /usr/sbin/xc.sh reload {} +
}
# 根据用户输入执行不同动作
case ${arg1} in
@ -63,6 +76,9 @@ case ${arg1} in @@ -63,6 +76,9 @@ case ${arg1} in
'-t')
check_conf
;;
'check_change')
check_change
;;
*)
echo 'Parameter error!'
;;

19
php/Dockerfile

@ -1,19 +0,0 @@ @@ -1,19 +0,0 @@
FROM php:7.3-fpm-alpine3.12
#设置时区,仅Debian需要
#ENV TZ=Asia/Shanghai
#工作目录
WORKDIR /root
#复制安装脚本
COPY ./install.sh /root
#复制启动脚本
COPY ./run.sh /usr/sbin
#执行安装脚本
RUN sh install.sh
#暴露配置文件
VOLUME /etc/nginx/conf.d
VOLUME /var/www/html
VOLUME /var/log/nginx
EXPOSE 80
EXPOSE 443
#运行crontab和nginx
CMD ["/usr/sbin/run.sh"]

53
php/install.sh

@ -1,53 +0,0 @@ @@ -1,53 +0,0 @@
#!/bin/sh
#更新软件
apk update
#安装timezone
apk add -U tzdata
#查看时区列表
ls /usr/share/zoneinfo
#拷贝需要的时区文件到localtime
cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
#查看当前时间
date
#为了精简镜像,可以将tzdata删除了
apk del tzdata
#安装wget
apk add wget
chmod +x /usr/sbin/run.sh
#安装nginx
function install_nginx() {
#安装nginx
apk add nginx
mkdir -p /run/nginx/
#wget -P /etc/nginx https://github.com/helloxz/dnmp/raw/main/php/nginx.conf
}
#设置PHP
function set_php(){
#安装依赖
apk add --no-cache autoconf gcc musl-dev g++ zlib-dev make libpng libpng-dev libjpeg-turbo-dev libwebp-dev zlib-dev libxpm-dev
#安装GD库 依赖
#apk add libpng-dev
#设置php.ini
cp /usr/local/etc/php/php.ini-development /usr/local/etc/php/php.ini
#zend_extension=/usr/local/lib/php/extensions/no-debug-non-zts-20180731/xdebug.so
#sed -i "s/www-data/www/g" /usr/local/etc/php-fpm.d/www.conf
#安装扩展
docker-php-ext-install gd
pecl install redis-5.1.1 \
&& pecl install xdebug-2.8.1 \
&& docker-php-ext-enable redis xdebug
}
#清理工作
function run_clean(){
apk del autoconf gcc musl-dev g++ zlib-dev make libpng libpng-dev libjpeg-turbo-dev libwebp-dev zlib-dev libxpm-dev
#清理编译
rm -rf /var/cache/apk/*
}
install_nginx && set_php && run_clean
echo '-----------------------------'
echo 'nginx + php install success.'
echo '-----------------------------'

158
php/nginx.conf

@ -1,158 +0,0 @@ @@ -1,158 +0,0 @@
user www-data www-data;
worker_processes auto;
worker_rlimit_nofile 50000;
error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
pid /var/run/nginx.pid;
events {
use epoll;
worker_connections 51200;
#worker_connections 1024;
multi_accept on;
}
http {
include mime.types;
default_type application/octet-stream;
server_names_hash_bucket_size 128;
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;
client_max_body_size 1024m;
client_body_buffer_size 10m;
sendfile on;
tcp_nopush on;
keepalive_timeout 120;
server_tokens off;
tcp_nodelay on;
proxy_headers_hash_max_size 51200;
proxy_headers_hash_bucket_size 6400;
#开启Brotli压缩
brotli on;
brotli_comp_level 6;
#最小长度
brotli_min_length 512;
brotli_types text/plain text/javascript text/css text/xml text/x-component application/javascript application/x-javascript application/xml application/json application/xhtml+xml application/rss+xml application/atom+xml application/x-font-ttf application/vnd.ms-fontobject image/svg+xml image/x-icon font/opentype;
brotli_static always;
gzip on;
gzip_buffers 16 8k;
gzip_comp_level 6;
gzip_http_version 1.1;
gzip_min_length 256;
gzip_proxied any;
gzip_vary on;
gzip_types
text/xml application/xml application/atom+xml application/rss+xml application/xhtml+xml image/svg+xml
text/javascript application/javascript application/x-javascript
text/x-json application/json application/x-web-app-manifest+json
text/css text/plain text/x-component
font/opentype application/x-font-ttf application/vnd.ms-fontobject
image/x-icon;
gzip_disable "MSIE [1-6]\.(?!.*SV1)";
#If you have a lot of static files to serve through Nginx then caching of the files' metadata (not the actual files' contents) can save some latency.
open_file_cache max=1000 inactive=20s;
open_file_cache_valid 30s;
open_file_cache_min_uses 2;
open_file_cache_errors on;
#limit connection
limit_conn_zone $binary_remote_addr zone=addr:10m;
server {
listen 80;
server_name localhost;
root /var/www/html;
index index.html index.htm index.php;
#charset koi8-r;
#access_log logs/host.access.log main;
#location / {
# root /var/www/html;
# index index.html index.htm index.php;
#}
location ~ [^/]\.php(/|$) {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi.conf;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# 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$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$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;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
include cdn/*.conf;
include vhost/*.conf;
}

6
php/run.sh

@ -1,6 +0,0 @@ @@ -1,6 +0,0 @@
#!/bin/sh
#start nginx
/usr/sbin/nginx
echo 'start nginx success.'
#start php-fpm
/usr/local/sbin/php-fpm

2
php74/docker-compose.yaml

@ -5,6 +5,8 @@ services: @@ -5,6 +5,8 @@ services:
image: helloz/php:7.4-fpm-alpine
network_mode: host
container_name: php-7.4
# Specify User ID and User Group
# user: "1000:1000"
volumes:
- ./www.conf:/usr/local/etc/php-fpm.d/www.conf
- ./php.ini:/usr/local/etc/php/conf.d/php.ini

4
php74/www.conf

@ -434,6 +434,6 @@ catch_workers_output = yes @@ -434,6 +434,6 @@ catch_workers_output = yes
; specified at startup with the -d argument
;php_admin_value[sendmail_path] = /usr/sbin/sendmail -t -i -f www@my.domain.com
;php_flag[display_errors] = off
;php_admin_value[error_log] = /var/log/fpm-php.www.log
;php_admin_flag[log_errors] = on
php_admin_value[error_log] = log/fpm-php.www.log
php_admin_flag[log_errors] = on
;php_admin_value[memory_limit] = 32M

49
wwwroot/default/index.html

@ -0,0 +1,49 @@ @@ -0,0 +1,49 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Welcome to DNMP</title>
<link href="https://libs.xiaoz.top/bootstrap/4.6.1/css/bootstrap.min.css" rel="stylesheet">
<style>
body {
padding-top: 20px;
background-color: #f4f4f4; /* 轻微的灰色背景 */
}
.jumbotron {
background-color: #fff; /* 亮色背景提高对比度 */
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1); /* 轻微的阴影效果 */
}
.btn {
margin: 5px; /* 增加按钮间距 */
}
@media (max-width: 768px) {
.btn {
width: 100%; /* 在较小的屏幕上按钮占满整行 */
margin-bottom: 10px;
}
}
</style>
</head>
<body>
<div class="container">
<div class="jumbotron">
<h1 class="display-4">Welcome to DNMP!</h1>
<p class="lead">DNMP is a powerful stack for web development, combining Docker, Nginx, MySQL, and PHP.</p>
<hr class="my-4">
<p>Learn more about how DNMP can streamline your development process and make your projects more efficient.</p>
<div class="button-container">
<a class="btn btn-primary" href="#" role="button" target = "_blank">Project address</a>
<a class="btn btn-secondary" href="#" role="button" target = "_blank">Help documentation</a>
<a class="btn btn-success" href="/phpMyAdmin/index.php" role="button" target = "_blank">Open phpMyAdmin</a>
<!-- More buttons can be added here in future -->
</div>
</div>
</div>
<!--<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.1.16/dist/umd/popper.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>-->
</body>
</html>

BIN
wwwroot/phpMyAdmin.zip

Binary file not shown.
Loading…
Cancel
Save