-
-
Save muhamed-didovic/2704d732f73379f1ce3d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/php | |
| <?php | |
| /** | |
| * Script for creating a new Apache2 VHost and MySQL User + Database. | |
| * | |
| * @author Julius Beckmann (github@h4cc.de) | |
| */ | |
| if(6 != count($argv)) { | |
| echo "--- New Vhost --- | |
| Help: | |
| $ new_vhost HOSTNAME DOCROOT DB_USERNAME DB_PASSWORD DB_ROOT_PASSWORD | |
| Example: | |
| $ new_vhost foo /var/www/foo/web foo foo 1234 | |
| -> http://foo.local | |
| -> /var/log/apache2/foo_(access|error).log | |
| -> MySQL: foo@foo | |
| "; | |
| die(); | |
| } | |
| $hostname = $argv[1]; | |
| $docroot = $argv[2]; | |
| $db_username = $argv[3]; | |
| $db_password = $argv[4]; | |
| $db_root_password = $argv[5]; | |
| if(!is_dir($docroot)) { | |
| die("\n\n--- Docroot '$docroot' does not exist. ---\n\n"); | |
| } | |
| $conn = mysql_connect("localhost", "root", $db_root_password); | |
| if (!$conn) { | |
| die("\n\n--- DB Connect Error: " . mysql_error()."---\n\n"); | |
| } | |
| // Add Hostname to /etc/hosts | |
| if(!is_writable('/etc/hosts')) { | |
| die("\n\n--- Need to be root! ---\n\n"); | |
| } | |
| // Write hosts | |
| file_put_contents('/etc/hosts', "\n# Added ".$hostname." on ".date('c')."\n127.0.0.1 ".$hostname.".local\n", FILE_APPEND); | |
| echo "--- Created /etc/hosts entry \n"; | |
| // Write vhost | |
| file_put_contents('/etc/apache2/sites-enabled/'.$hostname.'.conf', getApache2Vhost($hostname, $docroot)); | |
| echo "--- Created Vhost \n"; | |
| // Reload Webserver | |
| passthru('service apache2 stop; service apache2 start'); | |
| echo "--- Restarted Webserver \n"; | |
| // Create MySQL Database | |
| foreach(getNewUserAndDatabaseSQL($db_username, $db_password) as $sql) { | |
| $result = mysql_query($sql); | |
| if (!$result) { | |
| die("Query error ".mysql_error()."\nSQL: '$sql'\n"); | |
| } | |
| } | |
| echo "--- Created MySQL User and Table \n"; | |
| echo "--- DONE - HAVE FUN :) \n"; | |
| // --- Functions | |
| function getNewUserAndDatabaseSQL($username, $password) { | |
| return array( | |
| "CREATE USER '".$username."'@'localhost' IDENTIFIED BY '".$password."';", | |
| "GRANT USAGE ON *.* TO '".$username."'@'localhost' IDENTIFIED BY '".$password."' WITH MAX_QUERIES_PER_HOUR 0 MAX_CONNECTIONS_PER_HOUR 0 MAX_UPDATES_PER_HOUR 0 MAX_USER_CONNECTIONS 0;", | |
| "CREATE DATABASE IF NOT EXISTS `".$username."`;", | |
| "GRANT ALL PRIVILEGES ON `".$username."`.* TO '".$username."'@'localhost';", | |
| ); | |
| } | |
| function getApache2Vhost($hostname, $docroot) { | |
| return "<VirtualHost *:80> | |
| ServerName \"".$hostname.".local\" | |
| DocumentRoot \"".$docroot."\" | |
| <Directory \"".$docroot."\"> | |
| Options -Indexes +FollowSymLinks +MultiViews | |
| AllowOverride All | |
| Order Allow,Deny | |
| Allow from all | |
| </Directory> | |
| ErrorLog \"/var/log/apache2/".$hostname."_error.log\" | |
| # Possible values include: debug, info, notice, warn, error, crit, alert, emerg. | |
| LogLevel debug | |
| CustomLog \"/var/log/apache2/".$hostname."_access.log\" combined | |
| </VirtualHost> | |
| "; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment