sanders
sanders

Reputation: 10888

how to set up a php mysql site quickly?

I am looking for ways to setup a basic site quickly.

I have a basic site which works with a databasem, templates, css, js and so on. No I want to be able to quickly set up a site. What shoudld happen when i start the script is:

  1. ask for some variables
  2. on submit:
  3. create a folder in the webroot
  4. copy the standard site to that map
  5. create a database based on a default db
  6. add the new site to my vhost file
  7. restart apache
  8. add the new site to my host file
  9. start de basic site in a browser.

What is the best way to create this script? How can I accomplish this?

Upvotes: 0

Views: 160

Answers (6)

0scar
0scar

Reputation: 3200

Here's a complete (untested) hack that does about what you said. If you're comfortable in php then use that. Pseudo script:

#!/usr/bin/env php
<?php
echo "Site setup v0.0\n";
if($argc != 2){
    echo "Usage:\n   script sitename\n";
}
//set vars
$sitename = $argv[1];
$src_folder = "/path/to/some/folder";
$template_db ="template_site";

//copy files
`mkdir $sitename`;
`cp -R $src_folder $sitename`;

//copy template db
$dblink = mysql_connect("localhost");
if(!mysql_query($dblink, "CREATE DATABASE site_$sitename; USE site_$sitename;"))exit(-1);
$r = mysql_query($dblink, "SHOW TABLES FROM $template_db");
while($row = mysql_fetch_array($r)){
    $table = $row[0];
    mysql_query($dblink, "CREATE TABLE $table AS SELECT $template_db.$table");
}

//conf and restart apache
$f = fopen("httpd.conf","a");//open for append
fwrite("<VirtualHost $sitename> bla bla </VirtualHost>");
fclose($f);
`sudo apachectl -k restart`; //you'll be asked for a password here

//open in browser
`open http://$sitename/`; //on mac anyway...

?>

Make the file executable with

chmod +x filename

Remember that to run scripts in the current folder you need to add ./. Like

./scriptname sitename

Also note the slanted quotes ` <- They start a shell command. The first line is called a shebang-line (yes like that old 80s band, or what was it..) and tells a shell what to use to execute the file. (Env is a utility program that kinda finds other programs, in this case php. Good if you want to run the script in systems where php has different install locations.)

Also please note that this script is just pseudo code—it does not work! Don't run it before modifying it!

Upvotes: 2

jlleblanc
jlleblanc

Reputation: 3510

The best way to write this would be as a command line script, which you can write in PHP if you want. Use the $argv variable to get an array of the command line variables. Make sure that this PHP file is NOT web accessible.

Also, I would use SVN to do a export to the folder for the new site. This will ensure that you have the most up to date code and that it's a clean copy.

Upvotes: 0

Sander
Sander

Reputation: 17

Well, it depends on what OS you are using, if you use Linux you can create simple script to do this, it basicly should contain a sequence of commands to set this up.

#!/bin/sh
mkdir /var/www/sitename
cp -au /var/www/skeleton/* /var/www/newfolder
etcetera.

You can then launch your script by running it from command line with some parameters

./initiatesite.sh newsite.com databasename addwhateverparameteryouwant here.

More info on passing parameters to a shell script: http://osr600doc.sco.com/en/SHL_automate/_Passing_to_shell_script.html

Upvotes: 1

Alan Plum
Alan Plum

Reputation: 10892

You can't do that in PHP unless you run PHP as root (bad idea).

Think about what you want to do specifically and how you would do it. Starting the site in a browser is a user-specific desktop action, restarting the server is a root action as is all the copying/editing/moving stuff.

Maybe a proper installer would be more suited towards what you're trying to accomplish. It sounds like you're working with Windows -- do you only want to provide an installation for Windows? For Linux you'd need a shell script, for example.

Upvotes: 0

Charles Ma
Charles Ma

Reputation: 49151

You can write a shell script that basically accomplishes all of that.

So I guess you would have a web front end handled by php which takes some parameters, and have that php script invoke a shell script (via system call) on your server to set up your db, copy files, append some lines to vhost config, etc

If this is not for your own use but for clients, I would take extra care to make sure all input is untainted and secure first.

Upvotes: 0

Keeper
Keeper

Reputation: 3566

This is something you have to do on the server so you can use any language you prefer. It's just about copying files, append text to files and execute some shell commands.

Upvotes: 1

Related Questions