Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/php
- <?
- # some useful file paths to rememebr
- define('NGINX_CONFIG_ROOT', "/etc/nginx/sites-enabled/");
- if($_SERVER['argc'] <= 1) {
- show_help_msg();
- } else {
- $new_domain = trim($_SERVER['argv'][1]);
- $parts = explode('.', $new_domain);
- if(count($parts) == 2) {
- add_full_domain($new_domain);
- } elseif(count($parts) == 3) {
- if($parts[0] == "www") {
- add_full_domain($parts[1].'.'.$parts[2]);
- } else {
- add_subdomain($new_domain);
- }
- } else {
- show_help_msg();
- }
- }
- function add_full_domain($new_domain) {
- log_msg("Creating domain $new_domain...");
- # first lets create a new nginx config file
- $file = NGINX_CONFIG_ROOT.str_replace('.', '_', $new_domain);
- log_msg("Creating config file at: $file");
- if(!touch($file)) {
- error("There was an error creating the nginx config file, please check permissions.");
- }
- log_msg("Opening config file for writing.");
- if(!is_writable($file)) {
- error("Config file is unwritable, please check permissions.");
- }
- $fh = fopen($file, 'w');
- if(!$fh) {
- error("Unable to open config file for writing");
- }
- log_msg("Writing config to file...");
- write_config_file($fh, $new_domain, "/home/".str_replace('.', '_', $new_domain)."/public_html/");
- log_msg("Config file created.");
- $user = str_replace(".", "_", $new_domain);
- passthru(escapeshellcmd("adduser $user"));
- log_msg("");
- passthru(escapeshellcmd("adduser $user www-data"));
- log_msg("Finished creating user $user");
- log_msg("Creating web root directory");
- mkdir("/home/$user/public_html/");
- chown("/home/$user/public_html/", "$user");
- chmod("/home/$user/public_html/", 0755);
- log_msg("Web root created and permissions set.");
- log_msg("Restarting nginx, cross your fingers...");
- passthru("/etc/init.d/nginx restart");
- log_msg("###################################");
- log_msg("Config: $file");
- log_msg("Web root: /home/$user/public_html/");
- log_msg("###################################");
- log_msg("Adding domain $new_domain is complete!");
- }
- function add_subdomain($new_domain) {
- log_msg("Creating subdomain $new_domain...");
- $parts = explode('.', $new_domain);
- $root_domain = $parts[1].'.'.$parts[2];
- $user = str_replace('.', '_', $root_domain);
- $sub_name = $parts[0];
- $web_root = "/home/$user/subdomains/$sub_name/public_html";
- $file = "/etc/nginx/sites-enabled/$user";
- log_msg("Opening config file for writing.");
- if(!is_writable($file)) {
- error("Config file is unwritable, please check permissions.");
- }
- # open in append mode
- $fh = fopen($file, 'a');
- if(!$fh) {
- error("Unable to open config file for writing");
- }
- log_msg("Adding config to file...");
- write_config_file($fh, $new_domain, $web_root);
- log_msg("Config file updated.");
- log_msg("Creating web root directory");
- if(!is_dir("/home/$user/subdomains/")) {
- log_msg("Creating subdomains folder first...");
- mkdir("/home/$user/subdomains/");
- chown("/home/$user/subdomains/", $user);
- chmod("/home/$user/subdomains/", 0755);
- }
- mkdir("/home/$user/subdomains/$sub_name");
- chown("/home/$user/subdomains/$sub_name", $user);
- chmod("/home/$user/subdomains/$sub_name", 0755);
- mkdir($web_root);
- chown($web_root, $user);
- chmod($web_root, 0755);
- log_msg("Web root created and permissions set.");
- log_msg("Restarting nginx, cross your fingers...");
- passthru("/etc/init.d/nginx restart");
- log_msg("###################################");
- log_msg("Updated config: $file");
- log_msg("Web root: $web_root");
- log_msg("###################################");
- log_msg("Adding domain $new_domain is complete!");
- }
- function write_config_file($fh, $domain, $root_dir) {
- $config = "
- # $domain domain
- server {
- listen 80; ## listen for ipv4
- server_name $domain;
- access_log /var/log/nginx/$domain.access.log;
- location / {
- root $root_dir;
- index index.html index.htm index.php;
- }
- #error_page 404 /404.html;
- # redirect server error pages to the static page /50x.html
- #
- error_page 500 502 503 504 /50x.html;
- location = /50x.html {
- root /var/www/nginx-default;
- }
- # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
- #
- location ~ \.php$ {
- fastcgi_pass 127.0.0.1:9000;
- fastcgi_index index.php;
- fastcgi_param SCRIPT_FILENAME {$root_dir}\$fastcgi_script_name;
- include /etc/nginx/fastcgi_params;
- }
- }
- ";
- if(fwrite($fh, $config) === false) {
- error("There was an error writing to the config file.");
- }
- fclose($fh);
- return true;
- }
- function log_msg($msg) {
- echo $msg."\n";
- }
- function error($msg) {
- echo $msg."\n";
- exit;
- }
- function show_help_msg() {
- echo "
- Automatic Domain Generator
- by Ryan LeFevre - 5/29/2010
- Syntax: ./add_domain [new_domain]
- Expecting more? Thats all there is to it :)
- ";
- }
- ?>
Add Comment
Please, Sign In to add comment