将PHP代码组织好以便部署到我们的服务器上。

29
我们刚刚为新系统购买了新服务器,我想知道如何使我的新代码尽可能高效,并且如何组织它。我希望找到一个好的解决方案,以便一年后不必重新组织它(例如),并且我想要最佳实践和技术,以确保我的代码足够长时间存活,避免以后需要重新设计它。这里不使用框架
这是我的新环境:
- 8个Web服务器LAMP(Apache 2,PHP 5.3.5,CentOS 5) -(Xeon E5645,32 GB RAM,RAID 10 1 TB 15k RPM) - 一个负载均衡器来管理它们 - 12个数据库MySQL 5.5服务器(与上述相同),具有复制功能(四个主服务器和八个从服务器) - 一个SVN服务器(我们使用的旧服务器)
我的想法是将它们(Web服务器)镜像,然后将代码从SVN推送到所有服务器。这样做好吗?

2
SVN服务器是否与Web服务器在同一网络上?或者您的SVN托管在不属于您的单独域上? - Book Of Zeus
SVN服务器位于相同的IP范围内,因此与服务器在同一网络上,但无法从外部访问。 - Gilbert Kakaz
1个回答

36
您可以将代码集中放置在一个公共文件夹中(可以创建一个脚本将所有代码复制到八个服务器上,或使用NFS)。这个集中的代码可以在SVN安装中的一个或多个仓库中。因此,当您推送时,只需推送所需的内容。例如,您可以为自己的PHP库(数据库类、XML、IMAP等)创建一个仓库。在一个简单的结构中,当您调用这些文件时,只需要简单地这样做:
require('/web/lib/DatabaseMySQL.class.php');

This way, you know all your required files are in the same place and very easy to maintain. Especially if your code requires files that require files.
You can create as many repositories as you want and repeat this if you don't want to mix up files - for example third-party (Smarty, PHPMailer) with the code you create.
The other thing is, don't reinvent the wheel. There's plenty of good code out there that probably does what you already need to do. Like sending email (PHPMailer or any others) or template system (Smarty or any others). This way you save development time and when an update is available, you simply download, copy (commit if you have it in a repository) and push.
Script VS NFS.
Creating a script to push all your code to eight web servers is easy to make. The downside of this is you need to make sure all the folders and all the files you have on each server are identical in order to avoid errors.

另外,如果您的网络延迟或连接在推送过程中断开,则某些服务器将没有相同的代码。这会导致更多错误。与NFS解决方案相比,这种方法运行速度稍快。

创建一个NFS可以解决上述问题,因为您只使用一个位置,但如果该位置下线,则所有服务器都无法正确运行。因此,当您推送代码时,只需推送到一个位置,所有其他服务器将自动拥有新代码。您还需要知道的是:这种方法比直接放在硬盘上慢一点。

以下是您可以使用的示例脚本:

您可以创建一个.sh脚本,将代码从存储库(例如,您从存储库检出的代码)复制到所有服务器,如下所示:

// file: pushcode.sh
#!/bin/bash
/usr/bin/rsync -avz --exclude='.svn' -e ssh /path/to/code/checkedout/ user@server1:/path/to/code
/usr/bin/rsync -avz --exclude='.svn' -e ssh /path/to/code/checkedout/ user@server2:/path/to/code

将此脚本设为可执行并运行:

./pushcode.sh

为了确保代码复制时不需要每次提示密码,您需要绕过SSH登录
这是一个不错的问题,您可能会喜欢:https://serverfault.com/questions/195035/linux-nfs-performance-vs-other-file-systems

谢谢你提供这个,非常有用。我想我会选择脚本来开始,起初看起来更容易。对于这个脚本,你有什么推荐吗?我该怎么做? - Gilbert Kakaz

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接