Jump to content

WAMP Server New index.php


wrathican

Recommended Posts

Hi there folks.

 

I don't know about you but i use subdomains on my localhost to make developing that much easier.

 

I have created a php script to manage these subdomains. It is a very basic cut down version of the current index.php file, that accesses the windows HOSTS file and writes an entry for the new 'project' you create. It also accesses the httpd-vhosts.conf file and writes a virual host section in there too.

 

after it writes to these files it then restarts apache by running a batch file using the exec command. You need to restart apache for the vhost information to take effect.

 

My problem appears when restarting apache. Because the web service stops running for a moment the rest of the script fails to run.

 

What i am asking is is there a way of detecting/performing a service restart using php?

 

hope fully looking at my script might help you understand/help me solve my problem!

<?php
session_start();
# Default File Locations
$hosts = '../../../../../../../../Windows/System32/drivers/etc/HOSTS';
$vhosts = '../../../../../../../../wamp/bin/apache/Apache2.2.11/conf/extra/httpd-vhosts.conf';

#Project Processing
if(isset($_GET['action']) && $_GET['action'] != ''){
$action = $_GET['action'];
switch($action){
	case "addproj":
		$name = htmlentities(strtolower(preg_replace('/\s+/', '_', $_POST['name'])), ENT_QUOTES);
		if(!is_dir($name)){
			$projDir = mkdir($name);
			if($projDir){
				$rootDir = mkdir($name.'/htdocs');
				if($rootDir){
					mkdir($name.'/logs');
					#add to hosts file and vhosts
					// hosts
					$file = fopen($hosts, 'a');
					if($file){
						fwrite($file, "\n127.0.0.1\t\t".$name.".localhost");
						fclose($file);
					}
					$file = fopen($vhosts, 'a');
					if($file){
						$vhosts_a = "
#start$name
<VirtualHost *:80>
DocumentRoot \"C:/wamp/www/$name/htdocs\"
ServerName $name.localhost
ErrorLog \"logs/$name-error.log\"
CustomLog \"logs/$name-access.log\" common
</VirtualHost>
#end$name";
						fwrite($file, $vhosts_a);
						fclose($file);
					}
					$restartfile = 'restart.bat';
					exec($restartfile);
				}else{
					$_SESSION['error'] = "Cannot create root directory";
					rmdir($name);
				}
			}else{
				$_SESSION['error'] = "Cannot create project directory";
			}
		}else{
			$_SESSION['error'] = "Project already exists. Please specify another name.";
		}
	break;

	case "info":
		phpinfo();
		die();
	break;
}
}

#Content
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Project Management</title>
<link rel="stylesheet" type"text/css" href="css.css" />
</head>
<body>
<h2>Tools</h2>
<ul class="tools">
	<li><a href="?action=info">phpinfo()</a></li>
	<li><a href="phpmyadmin/">phpmyadmin</a></li>
	<li><a href="sqlitemanager/">sqlitemanager</a></li>
</ul>
<h2>Projects</h2>
<h3>Create Project</h3>
<p class="error">
<?php
echo (isset($_SESSION['error'])) ? $_SESSION['error'] : '';
?>
<form id="addproj" action="?action=addproj" method="post" >
	<label for="name">Name:</label>
	<input type="text" name="name" id="name" />
	<input type="submit" name="create" value="Create" />
</form>
<h3>Current Projects</h3>
<ul class="projects">
<?php
$lines = file_get_contents($hosts);
$projects = split("\n", $lines);
for($i = 0; $i < count($projects); $i++){
	if($i > 21 && $projects[$i] != ''){
		$url = trim(substr($projects[$i], 9));
		$name = preg_replace('~\.localhost~', '', $url);
		echo "<li><a href=\"http://$url\">$name</a></li>";
	}
}
?>
</ul>
</body>
</html>
<?php
unset($_SESSION['error']);

 

Thanks in advance

 

Wrath

Link to comment
Share on other sites

Great idea to automatize everything :)

 

I didn't use wamp, i install everything separatly so it may not be the same but this is what i got so far :

net stop Apache2.2
net start Apache2.2

This stop the service then crash. I have to manually kill the remaining process before i can start my Apache again. (Work fine when running the .bat not from the .php)

 

I think it need to be run in 1 command instead of 2 like that :

cd C:\Program Files\Apache Software Foundation\Apache2.2\bin\
httpd.exe -k restart

Work fine in batch file, crash when run from php with this error in my apache error.log :

 

Usage: httpd.exe [-D name] [-d directory] [-f file]

                [-C "directive"] [-c "directive"]

                [-w] [-k start|restart|stop|shutdown]

                [-k install|config|uninstall] [-n service_name]

                [-v] [-V] [-h] [-l] [-L] [-t] [-S]

Options:

  -D name            : define a name for use in <IfDefine name> directives

  -d directory      : specify an alternate initial ServerRoot

  -f file            : specify an alternate ServerConfigFile

  -C "directive"    : process directive before reading config files

  -c "directive"    : process directive after reading config files

  -n name            : set service name and use its ServerConfigFile

  -k start          : tell Apache to start

  -k restart        : tell running Apache to do a graceful restart

  -k stop|shutdown  : tell running Apache to shutdown

  -k install        : install an Apache service

  -k config          : change startup Options of an Apache service

  -k uninstall      : uninstall an Apache service

  -w                : hold open the console window on error

  -e level          : show startup errors of level (see LogLevel)

  -E file            : log startup errors to file

  -v                : show version number

  -V                : show compile settings

  -h                : list available command line options (this page)

  -l                : list compiled in modules

  -L                : list available configuration directives

  -t -D DUMP_VHOSTS  : show parsed settings (currently only vhost settings)

  -S                : a synonym for -t -D DUMP_VHOSTS

  -t -D DUMP_MODULES : show all loaded modules

  -M                : a synonym for -t -D DUMP_MODULES

  -t                : run syntax check for config files

httpd.exe: illegal option -- k

 

That just doesn't make any sense. httpd.exe: illegal option -- k is wrong since it work when i click on the .bat file. And the usage screen is dropped into this file instead of the normal error/log like this : [sat May 02 09:49:31 2009] [notice] Child 2504: Child process is running

 

Apache is running as this user : NT AUTHORITY\SYSTEM, which is different from my current user, but the batch file use the same user as Apache web server, so i don't think it's comming from the user right.

 

My next idea would be to create a .exe file that separate from it's father process allow the php file to end, wait some seconds then restart the apache server.....but i'm not sure it will work.

 

Assuming you don't start 425 projects a day it will probably be a waste of time, and restart the web server manually is probably the best choice you have.

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.