如何在laravel或php中测试nginx配置文件



我在Laravel中有一项任务,在存储文件夹中创建一个nginx.conf文件,并使用nginx命令测试该文件。我在存储文件夹中创建了一个配置文件"nginx.conf",并试图在nginx命令、shell_exec()或exec()的帮助下进行测试,但我收到了错误"权限被拒绝"。

下面是我用来测试nginx.conf文件的命令行https://www.nginx.com/resources/wiki/start/topics/tutorials/commandline/#

NginxController.php

public function nginxConfig(){
$path              = storage_path('app/nginx/nginx.conf'); //"/home/vagrant/Code/Laravel/storage/app/nginx.conf"
/*$change_mode       = exec('chmod -R 755 /home/vagrant/Code/Laravel/storage/app/nginx 2>&1');*/
$config            = exec('/home/vagrant/Code/Laravel/storage/app/nginx -t 2>&1');
dd($config);
/*$restart_config    = shell_exec('/etc/init.d/nginx restart');
$reload            = shell_exec('nginx -s reload');*/
}

消息获取

"sh: 1: /home/vagrant/Code/Laravel/storage/app/nginx: Permission denied"

nginx.conf

server {
listen 443 ssl http2
root /var/www/google.de/beta.google.de
index index.html index.htm index.php
server_name beta.google.de
include var/www/includes/contao
include var/www/includes/shopware
location ~* ^.+.(ogg|ogv|svg|svgz|eot|otf|woff|mp4|ttf|rss|atom|jpg|jpeg|gif|png|ico|zip|tgz|gz|rar|bz2|doc|xls|exe|ppt|tar|mid|midi|wav|bmp|rtf)$ {
         access_log off; log_not_found off; expires max;
  }
}

不幸的是,如果没有sudo,您就无法做到这一点,因此您可以尝试安装OpenSSH Server和Laravel的Envoy,将您的私人ssh密钥添加到root的授权密钥中,然后尝试在具有root访问权限的Envoy中写入。Envoy.blade.php看起来像:

@servers(['nginx' => isset($server) ? $server : 'root@localhost'])
@task('testconf', ['on' => 'nginx'])
{-- here you do what you whant to check --}
@endtask

然后在PHP中,您只需添加特使命令的shell执行,如:

envoy run testconf

最新更新