MakeNginxConfCommand.php 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. namespace App\Console\Commands;
  3. use Illuminate\Console\Command;
  4. class MakeNginxConfCommand extends Command
  5. {
  6. /**
  7. * The name and signature of the console command.
  8. *
  9. * @var string
  10. */
  11. //部署文件目录名称,需手动输入
  12. protected $signature = 'make:nginx-conf {dir}';
  13. /**
  14. * The console command description.
  15. *
  16. * @var string
  17. */
  18. protected $description = '自动生成nginx配置文件';
  19. /**
  20. * Execute the console command.
  21. *
  22. * @return mixed
  23. */
  24. public function handle()
  25. {
  26. $url = config('app.web_url');
  27. $parsedUrl = parse_url($url);
  28. $host = $parsedUrl['host'];
  29. $dir = $this->argument('dir');
  30. $project = base_path();
  31. $project = explode('/', $project);
  32. array_pop($project);
  33. $project[] = $dir;
  34. $project = implode('/', $project);
  35. $certKey = resource_path('cert').'/apiclient_key.pem';
  36. $certPem = resource_path('cert').'/apiclient_cert.pem';
  37. $conf =
  38. 'server {
  39. listen 80;
  40. server_name ' . $host . ';
  41. return 301 https://' . $host . '$request_uri;
  42. }
  43. server{
  44. listen 443 ssl;
  45. server_name ' . $host . ';
  46. ssl_certificate "'.$certPem.'";
  47. ssl_certificate_key "'.$certKey.'";
  48. ssl_session_timeout 5m;
  49. ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
  50. ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
  51. ssl_prefer_server_ciphers on;
  52. index index.html index.htm index.php;
  53. root ' . $project . '/public;
  54. location = /favicon.ico {
  55. log_not_found off;
  56. access_log off;
  57. }
  58. location /resources/ {
  59. alias "' . $project . '/resources/upload/";
  60. }
  61. location /admin_me_15446 {
  62. alias "' . $project . '/public/dist";
  63. index index.html;
  64. }
  65. location /static/ {
  66. alias "' . $project . '/public/dist/static/";
  67. }
  68. location / {
  69. try_files $uri $uri/ /index.php?$query_string;
  70. }
  71. location ~ .*\.(php|php5)?$
  72. {
  73. index index.html index.htm index.php;
  74. fastcgi_pass 127.0.0.1:9000;
  75. fastcgi_index index.php;
  76. fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  77. include fastcgi.conf;
  78. }
  79. }';
  80. $path = resource_path('conf/');
  81. if (!is_dir($path)) {
  82. mkDirs($path);
  83. }
  84. $fileName = $path . $dir .'_nginx.conf';
  85. file_put_contents($fileName, $conf);
  86. echo 'echo "include '.$fileName.';" >> /etc/nginx/nginx.conf'.PHP_EOL;
  87. }
  88. }