在Web服务器上设置共享php脚本文件的最佳方法是什么?

时间:2021-01-09 02:47:26

I'm running the same php script on many domains on an apache2 server. Only some of the files change between domains, and the rest are always the same. Right now, every time I set up a new domain, I copy all the files, but I'd really like to have these common files in one place so any changes would affect all domains.

我在apache2服务器上的许多域上运行相同的php脚本。只有部分文件在域之间发生变化,其余文件始终相同。现在,每次我设置一个新域时,我都会复制所有文件,但我真的很想将这些公共文件放在一个地方,所以任何更改都会影响所有域。

I've thought of using a bunch of symlinks to point at common files. Is this an ok approach, or are there some simple edits I can make to the php scripts or apache configuration files to make this more efficient?

我曾想过使用一堆符号链接指向常见文件。这是一个好方法,还是我可以对php脚本或apache配置文件进行一些简单的编辑,以提高效率?

Thanks!

4 个解决方案

#1


I'd suggest abstracting the common code into a set of 'library' scripts. Placing these in a common directory, and making that available by modifying PHP's include_path variable. This means you most likely won't have to modify your current scripts, while still removing the need to have more than one copy.

我建议将公共代码抽象为一组“库”脚本。将它们放在一个公共目录中,并通过修改PHP的include_path变量使其可用。这意味着您很可能不必修改当前脚本,同时仍然无需拥有多个副本。

This path could (and probably should) be outside of your public directories. This enhances the security of your websites by not making them directly available to outside users.

此路径可能(也可能应该)在您的公共目录之外。这样可以提高网站的安全性,而不是直接向外部用户提供。

#2


The way I do this kind of thing is to create a "common" directory, where I place all the file that can be shared between each site. Then I simply include them wherever they are needed.

我这样做的方法是创建一个“公共”目录,在那里我放置可以在每个站点之间共享的所有文件。然后我只需将它们包含在需要它们的任何地方。

This is pretty good because allows to add features across multiple sites.

这非常好,因为允许跨多个站点添加功能。

#3


This can be a bit tricky, as the application almost needs to know you're doing this. IME, it works best when you can divide the app into common code and instance code in two separate directory trees. The common code also needs to not do anything silly like include a file that has to be in the instance tree.

这可能有点棘手,因为应用程序几乎需要知道你正在做这件事。 IME,当您将应用程序划分为两个单独的目录树中的公共代码和实例代码时,它最有效。公共代码也不需要做任何愚蠢的事情,例如包含必须在实例树中的文件。

A single point of entry to load the common code is also a big bonus because then you can chain a few very small files: the instance code includes one in it's own directory; that file includes a file outside the instance code; that file then either loads the entry point file for the common code, or loads another that does. Now this is only one way to do it, but it means you have just one file that needs to know where the common code is (so you can move it if you have to with minimal effort), and if you do it right, all the various instance code trees load it, albeit indirectly.

加载公共代码的单一入口点也是一个很大的好处,因为那时你可以链接一些非常小的文件:实例代码包含一个在它自己的目录中;该文件包含实例代码外的文件;该文件然后加载公共代码的入口点文件,或加载另一个。现在这只是一种方法,但这意味着你只有一个文件需要知道公共代码的位置(所以你可以轻松地移动它),如果你做得对,那么所有各种实例代码树加载它,虽然是间接的。

#4


You could have a library directory that sits above all of your sites, and a config file that states which library files your sites should include by default. You can then have another config file within each site that overrides the global config. These config files can be used to generate include('../../lib/*.php') statements to build the basic function toolkit needed for each site.

您可以拥有一个位于所有站点上方的库目录,以及一个配置文件,该文件指出您的站点默认应包含哪些库文件。然后,您可以在每个站点中使用另一个覆盖全局配置的配置文件。这些配置文件可用于生成include('../../ lib / * .php')语句,以构建每个站点所需的基本功能工具包。

some_high_level_directory/
-> lib/
   ->*.php (library files)
-> config.php (global library includes)
-> site_1/
   -> config.php (library includes that only relate to site_1)
   -> www/
-> site_2/
   -> config.php (library includes that only relate to site_2)
   -> www/
-> etc, etc

Hopefully that makes sense... :)

希望这是有道理的...... :)

#1


I'd suggest abstracting the common code into a set of 'library' scripts. Placing these in a common directory, and making that available by modifying PHP's include_path variable. This means you most likely won't have to modify your current scripts, while still removing the need to have more than one copy.

我建议将公共代码抽象为一组“库”脚本。将它们放在一个公共目录中,并通过修改PHP的include_path变量使其可用。这意味着您很可能不必修改当前脚本,同时仍然无需拥有多个副本。

This path could (and probably should) be outside of your public directories. This enhances the security of your websites by not making them directly available to outside users.

此路径可能(也可能应该)在您的公共目录之外。这样可以提高网站的安全性,而不是直接向外部用户提供。

#2


The way I do this kind of thing is to create a "common" directory, where I place all the file that can be shared between each site. Then I simply include them wherever they are needed.

我这样做的方法是创建一个“公共”目录,在那里我放置可以在每个站点之间共享的所有文件。然后我只需将它们包含在需要它们的任何地方。

This is pretty good because allows to add features across multiple sites.

这非常好,因为允许跨多个站点添加功能。

#3


This can be a bit tricky, as the application almost needs to know you're doing this. IME, it works best when you can divide the app into common code and instance code in two separate directory trees. The common code also needs to not do anything silly like include a file that has to be in the instance tree.

这可能有点棘手,因为应用程序几乎需要知道你正在做这件事。 IME,当您将应用程序划分为两个单独的目录树中的公共代码和实例代码时,它最有效。公共代码也不需要做任何愚蠢的事情,例如包含必须在实例树中的文件。

A single point of entry to load the common code is also a big bonus because then you can chain a few very small files: the instance code includes one in it's own directory; that file includes a file outside the instance code; that file then either loads the entry point file for the common code, or loads another that does. Now this is only one way to do it, but it means you have just one file that needs to know where the common code is (so you can move it if you have to with minimal effort), and if you do it right, all the various instance code trees load it, albeit indirectly.

加载公共代码的单一入口点也是一个很大的好处,因为那时你可以链接一些非常小的文件:实例代码包含一个在它自己的目录中;该文件包含实例代码外的文件;该文件然后加载公共代码的入口点文件,或加载另一个。现在这只是一种方法,但这意味着你只有一个文件需要知道公共代码的位置(所以你可以轻松地移动它),如果你做得对,那么所有各种实例代码树加载它,虽然是间接的。

#4


You could have a library directory that sits above all of your sites, and a config file that states which library files your sites should include by default. You can then have another config file within each site that overrides the global config. These config files can be used to generate include('../../lib/*.php') statements to build the basic function toolkit needed for each site.

您可以拥有一个位于所有站点上方的库目录,以及一个配置文件,该文件指出您的站点默认应包含哪些库文件。然后,您可以在每个站点中使用另一个覆盖全局配置的配置文件。这些配置文件可用于生成include('../../ lib / * .php')语句,以构建每个站点所需的基本功能工具包。

some_high_level_directory/
-> lib/
   ->*.php (library files)
-> config.php (global library includes)
-> site_1/
   -> config.php (library includes that only relate to site_1)
   -> www/
-> site_2/
   -> config.php (library includes that only relate to site_2)
   -> www/
-> etc, etc

Hopefully that makes sense... :)

希望这是有道理的...... :)