samba 集成DEV开发环境 php 监控某目录下文件变动,自动执行某些命令操作 重启nginx ,nginx 实现动态域名,动态配置加载
测试环境:
centos 7系统,预先安装好bt 宝塔系统,安装好samba,宝塔里面安装好 lnmp软件,php设置 取消 exec 函数限制:
将www用户添加到dev组
usermod -G dev wwwphp 写了一个脚本,实现监控目录下文件的变量,然后重启nginx的例子:
<?php
class NginxRewrite {
public $dir = "/www/wwwroot/";
private $files = [];
public function __construct() {
$this->files = $this->readDir();
}
public function run() {
$oldFiles = $this->files;
while (true) {
$_files = $this->readDir();
foreach ($_files as $f=>$t) {
if (!isset($oldFiles[$f]) || $oldFiles[$f]!=$t) {
$oldFiles = $_files;
@exec("nginx -t", $out, $status);
if ($status === 0) {
echo "rewrite change, nginx reload\n";
@exec("nginx -s reload");
}
break;
}
}
sleep(10);
}
}
//目录目标目录
private function readDir($dir = null) {
$dir = $dir ? $dir : $this->dir;
$files = scandir($dir);
$deep = count(explode(DIRECTORY_SEPARATOR, $this->dir));
//var_dump($deep);
$res = [];
foreach ($files as $file) {
if ($file == '..' || $file == '.')
continue;
$_path = rtrim($dir, '\\/') . DIRECTORY_SEPARATOR . $file;
$_deep = count(explode(DIRECTORY_SEPARATOR, $_path));
if (is_dir($_path) && $_deep - $deep < 2) {
//var_dump($_deep - $deep, $_path);
$res = array_merge($res, $this->readDir($_path));
} elseif (is_file($_path) && preg_match("/nginx\.htaccess$/", $file)) {
$res[$_path] = filemtime($_path);
}
}
return $res;
}
}
$mod = new NginxRewrite();
$mod->run();
nginx 实现了动态域名,动态加载用户的匹配文件的方法
/root/nginx_config_tpl/www.nginx.tpl
server
{
listen 80;
server_name ~^(\w+)\.USER_NAME\.tell520\.com$;
set $www $1;
index index.php index.html index.htm default.php default.htm default.html;
root /www/wwwroot/USER_NAME/$www;
#SSL-START SSL相关配置,请勿删除或修改下一行带注释的404规则
#error_page 404/404.html;
#SSL-END
#ERROR-PAGE-START 错误页配置,可以注释、删除或修改
#error_page 404 /404.html;
#error_page 502 /502.html;
#ERROR-PAGE-END
#PHP-INFO-START PHP引用配置,可以注释或修改
include enable-php-71.conf;
#PHP-INFO-END
#REWRITE-START URL重写规则引用,修改后将导致面板设置的伪静态规则失效
location / {
include /www/wwwroot/USER_NAME/*/nginx.htaccess;
access_log /www/wwwroot/USER_NAME/nginx.log;
}
#REWRITE-END
#禁止访问的文件或目录
location ~ ^/(\.user.ini|\.htaccess|\.git|\.svn|\.project|LICENSE|README.md)
{
return 404;
}
#一键申请SSL证书验证目录相关设置
location ~ \.well-known{
allow all;
}
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 30d;
error_log off;
access_log /dev/null;
}
location ~ .*\.(js|css)?$
{
expires 12h;
error_log off;
access_log /dev/null;
}
}自动创建samba用户,并创建用户的网站目录和默认的例子文件脚本:
#! /bin/bash
if [ $# -ne 1 ]; then
echo -e "use: ./create_dev_user.sh username"
exit 1
fi
useradd $1 -g dev
echo -e "123456\n123456\n" | smbpasswd -a $1
mkdir /www/wwwroot/$1
mkdir /www/wwwroot/$1/test
touch /www/wwwroot/$1/test/index.php
cat>/www/wwwroot/$1/test/index.php<<EOF
<?php
echo "<pre>";
echo "hello world!\n";
var_export(\$_GET);
echo "</pre>";
EOF
touch /www/wwwroot/$1/test/nginx.htaccess
cat>/www/wwwroot/$1/test/nginx.htaccess<<EOF
rewrite ^([^\.]*)/(\w+)/(\w+).html\$ \$1/index.php?r=\$2/\$3 last;
rewrite ^([^\.]*)$ \$1/index.php?r=index/index last;
if (!-e \$request_filename) {
rewrite ^(.*)\$ /index.php/\$1 last;
}
EOF
chown $1:dev /www/wwwroot/$1/ -R
chmod 776 /www/wwwroot/$1/ -R
sed "s/USER_NAME/$1/" /root/nginx_config_tpl/www.nginx.tpl > /www/server/nginx/conf/vhost/$1.conf
echo -e "\033[34mAdd hosts to browse the example site : 192.168.2.164 test.$1.tell520.com\033[0m"
echo -e "\033[34mAdd network drive : \\\\192.168.2.164\dev\033[0m"
echo -e "\033[34mUser AND Password : $1, 123456\033[0m"
nginx -t && nginx -s reload
samba配置文件:
# See smb.conf.example for a more detailed config file or
# read the smb.conf manpage.
# Run 'testparm' to verify the config is correct after
# you modified it.
[global]
server string = dev Servera Version %v
interfaces = ens192 192.168.2.164/24
workgroup = SAMBA
security = user
passdb backend = tdbsam
printing = cups
printcap name = cups
load printers = yes
cups options = raw
[dev]
comment = dev dir
path = /www/wwwroot/yxb
browseable = Yes
writable = Yes
available = Yes
valid users = @dev
public = No
[homes]
comment = Home Directories
valid users = %S, %D%w%S
browseable = Yes
read only = No
inherit acls = Yes
[printers]
comment = All Printers
path = /var/tmp
printable = Yes
create mask = 0600
browseable = No
[print$]
comment = Printer Drivers
path = /var/lib/samba/drivers
write list = @printadmin root
force group = @printadmin
create mask = 0664
directory mask = 0775版权属于:Joyber
本文链接:https://blog.qqvbc.com/default/323.html
转载时须注明出处及本声明