如何快速设置php mysql网站?

时间:2021-01-27 22:10:42

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:

我有一个基本的站点,可以使用databasem,templates,css,js等。不,我希望能够快速建立一个网站。当我启动脚本时发生的事情是:

  1. ask for some variables
  2. 要求一些变数

  3. on submit:
  4. create a folder in the webroot
  5. 在webroot中创建一个文件夹

  6. copy the standard site to that map
  7. 将标准站点复制到该映射

  8. create a database based on a default db
  9. 基于默认数据库创建数据库

  10. add the new site to my vhost file
  11. 将新站点添加到我的vhost文件中

  12. restart apache
  13. add the new site to my host file
  14. 将新站点添加到我的主机文件中

  15. start de basic site in a browser.
  16. 在浏览器中启动基本站点。

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

创建此脚本的最佳方法是什么?我怎么能做到这一点?

6 个解决方案

#1


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

这是一个完整的(未经测试的)hack,可以解释你所说的内容。如果你对php感到满意,那就用它吧。伪脚本:

#!/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.)

还要注意倾斜的引号`< - 它们启动一个shell命令。第一行称为shebang-line(是的,就像那个80年代的老乐队,或它是什么......)并告诉shell使用什么来执行文件。 (Env是一个实用程序,有点找到其他程序,在这种情况下是php。如果你想在php有不同安装位置的系统中运行脚本,那就太好了。)

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

另请注意,此脚本只是伪代码 - 它不起作用!在修改它之前不要运行它!

#2


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.

这是您必须在服务器上执行的操作,因此您可以使用您喜欢的任何语言。它只是复制文件,将文本附加到文件并执行一些shell命令。

#3


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.

嗯,这取决于你使用的操作系统,如果你使用Linux,你可以创建简单的脚本来执行此操作,它基本上应该包含一系列命令来设置它。

#!/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.

./initiateite.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

有关将参数传递给shell脚本的更多信息:http://osr600doc.sco.com/en/SHL_automate/_Passing_to_shell_script.html

#4


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

您可以编写一个shell脚本,基本上可以完成所有这些操作。

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

所以我猜你会有一个由php处理的web前端,它需要一些参数,并让你的php脚本在服务器上调用一个shell脚本(通过系统调用)来设置你的数据库,复制文件,在vhost配置中添加一些行等等

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.

如果这不是供您自己使用,而是为了客户,我会特别注意确保所有输入都没有污染并且首先是安全的。

#5


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

你不能在PHP中这样做,除非你以root身份运行PHP(糟糕的主意)。

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.

也许正确的安装程序会更适合您要完成的任务。听起来您正在使用Windows - 您是否只想为Windows提供安装?例如,对于Linux,您需要一个shell脚本。

#6


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.

编写此代码的最佳方法是作为命令行脚本,如果需要,可以用PHP编写。使用$ argv变量获取命令行变量的数组。确保此PHP文件不可通过Web访问。

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.

另外,我会使用SVN导出到新站点的文件夹。这将确保您拥有最新的代码,并且它是一个干净的副本。

#1


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

这是一个完整的(未经测试的)hack,可以解释你所说的内容。如果你对php感到满意,那就用它吧。伪脚本:

#!/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.)

还要注意倾斜的引号`< - 它们启动一个shell命令。第一行称为shebang-line(是的,就像那个80年代的老乐队,或它是什么......)并告诉shell使用什么来执行文件。 (Env是一个实用程序,有点找到其他程序,在这种情况下是php。如果你想在php有不同安装位置的系统中运行脚本,那就太好了。)

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

另请注意,此脚本只是伪代码 - 它不起作用!在修改它之前不要运行它!

#2


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.

这是您必须在服务器上执行的操作,因此您可以使用您喜欢的任何语言。它只是复制文件,将文本附加到文件并执行一些shell命令。

#3


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.

嗯,这取决于你使用的操作系统,如果你使用Linux,你可以创建简单的脚本来执行此操作,它基本上应该包含一系列命令来设置它。

#!/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.

./initiateite.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

有关将参数传递给shell脚本的更多信息:http://osr600doc.sco.com/en/SHL_automate/_Passing_to_shell_script.html

#4


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

您可以编写一个shell脚本,基本上可以完成所有这些操作。

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

所以我猜你会有一个由php处理的web前端,它需要一些参数,并让你的php脚本在服务器上调用一个shell脚本(通过系统调用)来设置你的数据库,复制文件,在vhost配置中添加一些行等等

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.

如果这不是供您自己使用,而是为了客户,我会特别注意确保所有输入都没有污染并且首先是安全的。

#5


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

你不能在PHP中这样做,除非你以root身份运行PHP(糟糕的主意)。

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.

也许正确的安装程序会更适合您要完成的任务。听起来您正在使用Windows - 您是否只想为Windows提供安装?例如,对于Linux,您需要一个shell脚本。

#6


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.

编写此代码的最佳方法是作为命令行脚本,如果需要,可以用PHP编写。使用$ argv变量获取命令行变量的数组。确保此PHP文件不可通过Web访问。

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.

另外,我会使用SVN导出到新站点的文件夹。这将确保您拥有最新的代码,并且它是一个干净的副本。