Guest User

Untitled

a guest
May 26th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.65 KB | None | 0 0
  1. #!/usr/bin/php
  2. <?
  3. # some useful file paths to rememebr
  4. define('NGINX_CONFIG_ROOT', "/etc/nginx/sites-enabled/");
  5.  
  6. if($_SERVER['argc'] <= 1) {
  7. show_help_msg();
  8. } else {
  9. $new_domain = trim($_SERVER['argv'][1]);
  10.  
  11. $parts = explode('.', $new_domain);
  12. if(count($parts) == 2) {
  13. add_full_domain($new_domain);
  14. } elseif(count($parts) == 3) {
  15. if($parts[0] == "www") {
  16. add_full_domain($parts[1].'.'.$parts[2]);
  17. } else {
  18. add_subdomain($new_domain);
  19. }
  20. } else {
  21. show_help_msg();
  22. }
  23. }
  24.  
  25. function add_full_domain($new_domain) {
  26. log_msg("Creating domain $new_domain...");
  27.  
  28. # first lets create a new nginx config file
  29. $file = NGINX_CONFIG_ROOT.str_replace('.', '_', $new_domain);
  30. log_msg("Creating config file at: $file");
  31.  
  32. if(!touch($file)) {
  33. error("There was an error creating the nginx config file, please check permissions.");
  34. }
  35.  
  36. log_msg("Opening config file for writing.");
  37. if(!is_writable($file)) {
  38. error("Config file is unwritable, please check permissions.");
  39. }
  40.  
  41. $fh = fopen($file, 'w');
  42. if(!$fh) {
  43. error("Unable to open config file for writing");
  44. }
  45.  
  46. log_msg("Writing config to file...");
  47. write_config_file($fh, $new_domain, "/home/".str_replace('.', '_', $new_domain)."/public_html/");
  48. log_msg("Config file created.");
  49.  
  50. $user = str_replace(".", "_", $new_domain);
  51.  
  52. passthru(escapeshellcmd("adduser $user"));
  53. log_msg("");
  54. passthru(escapeshellcmd("adduser $user www-data"));
  55.  
  56. log_msg("Finished creating user $user");
  57.  
  58. log_msg("Creating web root directory");
  59. mkdir("/home/$user/public_html/");
  60. chown("/home/$user/public_html/", "$user");
  61. chmod("/home/$user/public_html/", 0755);
  62. log_msg("Web root created and permissions set.");
  63.  
  64. log_msg("Restarting nginx, cross your fingers...");
  65. passthru("/etc/init.d/nginx restart");
  66.  
  67. log_msg("###################################");
  68. log_msg("Config: $file");
  69. log_msg("Web root: /home/$user/public_html/");
  70. log_msg("###################################");
  71. log_msg("Adding domain $new_domain is complete!");
  72. }
  73.  
  74. function add_subdomain($new_domain) {
  75. log_msg("Creating subdomain $new_domain...");
  76.  
  77. $parts = explode('.', $new_domain);
  78. $root_domain = $parts[1].'.'.$parts[2];
  79. $user = str_replace('.', '_', $root_domain);
  80. $sub_name = $parts[0];
  81. $web_root = "/home/$user/subdomains/$sub_name/public_html";
  82. $file = "/etc/nginx/sites-enabled/$user";
  83. log_msg("Opening config file for writing.");
  84. if(!is_writable($file)) {
  85. error("Config file is unwritable, please check permissions.");
  86. }
  87.  
  88. # open in append mode
  89. $fh = fopen($file, 'a');
  90. if(!$fh) {
  91. error("Unable to open config file for writing");
  92. }
  93.  
  94. log_msg("Adding config to file...");
  95. write_config_file($fh, $new_domain, $web_root);
  96. log_msg("Config file updated.");
  97.  
  98. log_msg("Creating web root directory");
  99. if(!is_dir("/home/$user/subdomains/")) {
  100. log_msg("Creating subdomains folder first...");
  101. mkdir("/home/$user/subdomains/");
  102. chown("/home/$user/subdomains/", $user);
  103. chmod("/home/$user/subdomains/", 0755);
  104. }
  105.  
  106. mkdir("/home/$user/subdomains/$sub_name");
  107. chown("/home/$user/subdomains/$sub_name", $user);
  108. chmod("/home/$user/subdomains/$sub_name", 0755);
  109. mkdir($web_root);
  110. chown($web_root, $user);
  111. chmod($web_root, 0755);
  112. log_msg("Web root created and permissions set.");
  113.  
  114. log_msg("Restarting nginx, cross your fingers...");
  115. passthru("/etc/init.d/nginx restart");
  116.  
  117. log_msg("###################################");
  118. log_msg("Updated config: $file");
  119. log_msg("Web root: $web_root");
  120. log_msg("###################################");
  121. log_msg("Adding domain $new_domain is complete!");
  122. }
  123.  
  124. function write_config_file($fh, $domain, $root_dir) {
  125. $config = "
  126. # $domain domain
  127. server {
  128.  
  129. listen 80; ## listen for ipv4
  130.  
  131. server_name $domain;
  132.  
  133. access_log /var/log/nginx/$domain.access.log;
  134.  
  135. location / {
  136. root $root_dir;
  137. index index.html index.htm index.php;
  138. }
  139.  
  140. #error_page 404 /404.html;
  141.  
  142. # redirect server error pages to the static page /50x.html
  143. #
  144. error_page 500 502 503 504 /50x.html;
  145. location = /50x.html {
  146. root /var/www/nginx-default;
  147. }
  148.  
  149. # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
  150. #
  151. location ~ \.php$ {
  152. fastcgi_pass 127.0.0.1:9000;
  153. fastcgi_index index.php;
  154. fastcgi_param SCRIPT_FILENAME {$root_dir}\$fastcgi_script_name;
  155. include /etc/nginx/fastcgi_params;
  156. }
  157. }
  158. ";
  159.  
  160. if(fwrite($fh, $config) === false) {
  161. error("There was an error writing to the config file.");
  162. }
  163.  
  164. fclose($fh);
  165.  
  166. return true;
  167. }
  168.  
  169. function log_msg($msg) {
  170. echo $msg."\n";
  171. }
  172.  
  173. function error($msg) {
  174. echo $msg."\n";
  175. exit;
  176. }
  177.  
  178. function show_help_msg() {
  179. echo "
  180. Automatic Domain Generator
  181. by Ryan LeFevre - 5/29/2010
  182.  
  183. Syntax: ./add_domain [new_domain]
  184.  
  185. Expecting more? Thats all there is to it :)
  186.  
  187. ";
  188. }
  189. ?>
Add Comment
Please, Sign In to add comment