当前位置:首页 > PHP教程 > php高级应用 > 列表

php管理nginx虚拟主机shell脚本

发布:smiling 来源: PHP粉丝网  添加日期:2015-04-09 14:13:45 浏览: 评论:0 

下文整理了一段php管理nginx虚拟主机shell脚本,希望这段脚本能帮助到各位同学,使用php作为shell脚本是一件很方便的事情,理所当然,我们可以使用php脚本来管理 nginx虚拟主机,下面是笔者的 脚本 文件供各位参考,代码如下:

  1. #!/usr/bin/php -q 
  2. <?php 
  3.  
  4. start: fwrite(STDOUT,"===========Vhost Script===========\n"); 
  5. fwrite(STDOUT,"= Choose an operation \n"); 
  6. fwrite(STDOUT,"= 1.Create 2.Delete 3.Exit\n"); 
  7. fwrite(STDOUT,"==================================\n"); 
  8. $operate = trim(fgets(STDIN)); 
  9. if ( $operate == 1  ){ 
  10. fwrite(STDOUT,"Please Enter a  Domain Name:"); 
  11. $domain = trim(fgets(STDIN)); 
  12. $path = "/home/sites/{$domain}"; 
  13. $nginx_conf = "/etc/nginx/sites/{$domain}"; 
  14. $nginx_template = "/etc/nginx/template/site_conf"; 
  15. $apache_conf = "/etc/httpd/conf/httpd.conf"; 
  16. $conf_str = ""; 
  17.  
  18. //变量初始化 
  19.  
  20. if( file_exists($path ) ) exit ("Domain Existed!\n"); 
  21. else mkdir($path,0700); 
  22. if(file_exists($nginx_conf)) exit ("Nginx Config file existed!\n"); 
  23. else { 
  24.     $conf_str = file_get_contents( $nginx_template ); 
  25. } 
  26.  
  27. //目录检测及配置文件拷贝 
  28.  
  29. eval ( "\$conf_str = \"$conf_str\";" ); 
  30.  
  31. $succes = file_put_contents($nginx_conf,$conf_str); 
  32. if( !$succes ) exit ("Write Config File Fauile!"); 
  33. else echo "Create Vhost success!\n"; 
  34. goto start; 
  35. //写入配置文件 
  36. } 
  37. else if ($operate == 2){ 
  38.     $confs_dir = dir("/etc/nginx/sites"); 
  39.     $confs_list = array(); 
  40.     $count = 0; 
  41.     while ( false !== ( $conf_file = $confs_dir->read() ) ){ 
  42.         if( $conf_file == "." ) continue; 
  43.         if( $conf_file == ".." ) continue; 
  44.         if ( is_file( $confs_dir->path ."/". $conf_file) ) { 
  45.             $confs_list[$count++] =  $conf_file; 
  46.         } 
  47.     } 
  48.     echo "Select a site by number which to delete:\n"; 
  49.     if( count( $confs_list ) >0 ) 
  50.         foreach ( $confs_list as $k=>$v ){ 
  51.             echo "{$k}. $v\n"; 
  52.         } 
  53.     $index = trim(fgets(STDIN)); 
  54.     if( in_array ( $index,array_keys( $confs_list ) ) ){ 
  55.         copy(  $confs_dir->path ."/". $confs_list[$index],"/etc/nginx/backup/{$confs_list[$index]}" ); 
  56.         unlink ( $confs_dir->path ."/". $confs_list[$index] ); 
  57.         exec("tar -zcf  /home/sites/{$confs_list[$index]}.tar.gz /home/sites/".$confs_list[$index] ); 
  58.         exec("rm -Rf /home/sites/".$confs_list[$index]); 
  59.     } //开源软件:phpfensi.com 
  60.     //删除指定配置,并保存备份 
  61. } 
  62. else if( $operate == 3 ) { 
  63.     exit; 
  64. } 
  65. else { 
  66.  exit ("No Operation Selected!"); 
  67. }?> 

下面是nginx的配置模版,代码如下:

  1. server { 
  2. listen 80; 
  3. server_name {$domain}; 
  4. access_log /var/log/nginx/{$domain}_access_log; 
  5. error_log /var/log/nginx/{$domain}_error_log; 
  6. root {$path}; 
  7. #不记录对站点图标访问 
  8. location = /favicon.ico { 
  9. log_not_found off; 
  10. access_log off; 
  11. } 
  12. #不记录对robots.txt的访问 
  13. location = /robots.txt { 
  14. allow all; 
  15. log_not_found off; 
  16. access_log off; 
  17. } 
  18. location = / { 
  19. try_files @proxy; 
  20. } 
  21. location / { 
  22. index index.htm index.html index.php; 
  23. try_files \$uri @proxy; 
  24. } 
  25. #匹配html 
  26. location ~* \.(html|htm)$ { 
  27. expires 30s; 
  28. gzip off; 
  29. add_header Content-Encoding gzip; 
  30. try_files \$uri \$uri/ /wp-content/cache/supercache/\$http_host/\$request_uri/index.html.gz @proxy; 
  31. } 
  32. #匹配图片、脚本文件等 
  33. location ~* \.(jpe?g|gif|png|ico|css|js|flv|swf|avi|zip|rar|svg|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mp3)$ { 
  34. expires 30d; 
  35. } 
  36. #传递给apache 
  37. location @proxy { 
  38. index index.htm index.html index.php; 
  39. proxy_pass   http://127.0.0.1:81; 
  40. include /etc/nginx/proxy.conf; 
  41. } 
  42. }

Tags: php虚拟主机 nginx虚拟主机

分享到: