📜 ⬆️ ⬇️

Programming in PHP for command line

Foreword

Ubuntu provides the apache2 bundle utilities for turning on / off virtual hosts and modules. However, creating configs for virtual hosts takes extra time. Therefore, I wanted to correct this flaw. It was possible, of course, to make automatic subdomains for Apache, but I decided to write a script that creates virtual hosts configuration files for Apache, and, if necessary, adds the host name to the / etc / hosts file. I do not write scripts very well in bash, so I decided to use PHP for my fairly simple task, which I also know quite well.
So, in this article, we will do two useful things at once: let's get acquainted with the input / output operations of the command line in PHP and write a script that will make life a little easier for us.

Authorization Check

, , PHP
#!/usr/bin/env php
Linux.

, , , , root . :
  1. $virtual_hosts_dir = "/etc/apache2/sites-available/";
  2. if (!is_dir($virtual_hosts_dir) || !is_writable($virtual_hosts_dir))
  3. {
  4.     echo "You must run this script as root!\n";
  5.     exit;
  6. }



, , . , , . PHP $argc $argv. , — . - — . , :
  1. if ($argc>1) 
  2. { 
  3.     for ($i=1; $i<$argc; $i++) 
  4.     { 
  5.         $option = explode("=", $argv[$i]); 
  6.         switch ($option[0]) 
  7.         { 
  8.             case "-h": 
  9.             case "--add-to-hosts": 
  10.                 $add_to_hosts = true; 
  11.             break;
  12.  
  13.             case "-n": 
  14.             case "--no-add-to-hosts": 
  15.                 $add_to_hosts = false; 
  16.             break;
  17.  
  18.             case "-a": 
  19.             case "--server-alias": 
  20.                 if (isset($option[1])) 
  21.                 { 
  22.                     $server_alias = $option[1]; 
  23.                 } 
  24.                 else 
  25.                 { 
  26.                     echo "Wrong option: {$argv[$i]}\n"; 
  27.                 } 
  28.             break;
  29.  
  30.             case "-d": 
  31.             case "--document-root": 
  32.                 if (isset($option[1])) 
  33.                 { 
  34.                     if ($option[1] == "default") 
  35.                     { 
  36.                         $document_root = $default_doc_root 
  37.                     } 
  38.                     else if (is_dir(dirname($option[1]))) 
  39.                     { 
  40.                         $document_root = $option[1]; 
  41.                     } 
  42.                 } 
  43.                 else 
  44.                 { 
  45.                     echo "Wrong option: {$argv[$i]}\n"; 
  46.                 } 
  47.             break;
  48.  
  49.             default: 
  50.                 if (substr($argv[$i], 1, 1) == '-') 
  51.                 { 
  52.                     echo "Unknown option: {$argv[$i]}\n"; 
  53.                 } 
  54.             break; 
  55.         } 
  56.     } 
  57. }
  58.  


\

. , . \, . PHP \ STDIN, STDOUT, STDERR. , . , :
  1. $line = fgets(STDIN);

$line. , , .. PHP . , , . , \ :
  1. $stdin = fopen("php://stdin", "r");
  2. $line1 = gets($stdin);
  3. $line2 = gets($stdin);
  4. fclose($stdin);

, .
, echo.

. - , , - :
  1. while (!$server_alias)
  2. {
  3.     echo "Enter your hostname: ";
  4.     $server_alias = trim(fgets(STDIN));
  5. }


, , /etc/hosts, , . , /etc/hosts, , Enter :
  1. if ($add_to_hosts === null)
  2. {
  3.     echo "Add $server_alias to your /etc/hosts ? (Y/N) [Y]: ";
  4.     $line = trim(fgets(STDIN));
  5.     if ($line == 'n' || $line == 'N')
  6.     {
  7.         $add_to_hosts = false;
  8.     }
  9.     else
  10.     {
  11.         $add_to_hosts = true;
  12.     }
  13. }


:
  1. if (!$document_root) 
  2. { 
  3.     $default_doc_root = $default_doc_root.'/'.$server_alias; 
  4.     echo "Enter your document root [$default_doc_root]: "; 
  5.     $line = trim(fgets(STDIN)); 
  6.     if ($line && is_dir(dirname($line))) 
  7.     { 
  8.         $document_root = $line; 
  9.     } 
  10.     else 
  11.     { 
  12.         $document_root = $default_doc_root; 
  13.     } 
  14. }
  15.  


-

, , , . , , — :
  1. if (!is_dir($document_root))
  2. {
  3.     mkdir($document_root);
  4. }


/etc/hosts. , :
  1. if ($add_to_hosts)
  2. {
  3.     $hosts = file_get_contents("/etc/hosts");
  4.     $hosts .= "127.0.0.1\t$server_alias\n";
  5.     file_put_contents("/etc/hosts", $hosts);
  6. }


:
  1. $host_template = <<<HOST
  2. <VirtualHost *:80> 
  3. ServerAdmin i@bogus.in 
  4. ServerAlias $server_alias
  5.  
  6. DocumentRoot $document_root 
  7. <Directory $document_root> 
  8.     Options Indexes FollowSymLinks MultiViews 
  9.     AllowOverride All 
  10.     Order allow,deny 
  11.     allow from all 
  12. </Directory>
  13.  
  14. ErrorLog ${APACHE_LOG_DIR}/$server_alias-error.log; 
  15. LogLevel warn 
  16. CustomLog ${APACHE_LOG_DIR}/$server_alias-access.log combined
  17. </VirtualHost>
  18. HOST;
  19.  


, a2ensite:
  1. file_put_contents("/etc/apache2/sites-available/$server_alias", $host_template); 
  2. echo "Apache config for this hostname created successfully! Don't forget to run a2ensite $server_alias\n";
  3.  


. +x, :
chmod +x a2addsite


. :
% sudo ./a2addsite
Add test.local to your /etc/hosts? (Y/N) [Y]:
Enter your document root [/home/www/test.local]:
Apache config for this hostname created successfully! Don't forget to run a2ensite test.local


:
% sudo ./a2addsite --server-alias=test2.local --add-to-hosts --document-root=default
Apache config for this hostname created successfully! Don't forget to run a2ensite test2.local



PHP , . , , - .



, PHP PHP (.):
Command line usage
PHP input/output

')

Source: https://habr.com/ru/post/112146/


All Articles