Web 应用程序部署最佳实践:如何管理本地和本地应用程序 实时文件?

发布于 2024-07-13 20:53:24 字数 492 浏览 8 评论 0原文

我正在编写 php Web 应用程序,并通过 FTP 简单地部署它们。 为了使其工作,我经常需要进行一些调整/调试,因为我几乎无法控制托管我的(免费)网络服务器,因此在我的本地环境中工作的内容可能无法实时工作。

例如,我保留一个包含 class_db_myapp.php 的单独 php 文件,该文件使用特定的数据库参数扩展了 class_db.php:数据库名称、用户名、密码,这些参数在本地和实时情况下不会相同。 (参考信息:最近我开始使用 git 进行版本控制)

随着我的应用程序的发展,一些文件被重命名/删除/创建。 当需要上传新版本时,我必须依靠我的记忆来知道我必须上传/删除什么,或者简单地删除所有/上传所有。 但在第二种情况下,我需要避免删除 class_db_myapp.php 文件......

我还没有找到合适的解决方案。

该领域的最佳实践是什么?

我可能错过了关于这个主题的现有讨论,如果是这样,请指出我。

谢谢。

I am writing php web applications, and simply deploy them via FTP.
To make it work, I often have some tweaking/debugging to do given that I have little control over the (free) web server hosting me, so what's working in my local environment might not work live.

For example I keep a separate php file containing class_db_myapp.php which extends a class_db.php with specific DB parameters : db name, username, password which won't be the same local and live.
(For information : Lately I started using git for version control)

As my app evolves, some files get renamed / deleted / created.
When comes the time to upload a new version, I have to either rely on my memory to know what I have to upload / delete or simply delete all / upload all. But in the second case I need to avoid erasing the class_db_myapp.php file...

I haven't come up with a proper solution to this.

What are the best practices in this domain?

I may have missed an existing discussion on this subject, if so please point me to it.

Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

温柔戏命师 2024-07-20 20:53:24

如果 ftp 服务器支持符号链接,您可以使用以下技术:

  1. 将 public_html 文件夹设置为指向包含当前版本的文件夹的符号链接。 (例如“version1”)
  2. 将新版本上传到新文件夹中。
  3. 上传完成后,修改符号链接以使新版本生效。

如果出现问题,您可以通过再次修改符号链接轻松恢复到以前的版本。

对于实时环境中不同的数据库和其他设置,有多种选择:

  • 创建一个包含环境的文件:“live”或“local”,并根据环境设置将“if 语句”放入代码中。
  • 如果您能够检测 php 中的环境,请使用它而不是文件。
  • 将所有设置放在“versionX”文件夹外部的文件中。

If the ftp server supports symbolic links you can use the following technique:

  1. Make the public_html folder a symlink to the folder containing the current version. ("version1" for example)
  2. Upload the new version in a new folder.
  3. When the upload is completed, modify the symlink so the new version becomes active.

If something went wrong you can easily revert to the previous version by modifying the symlink again.

For database and other settings that are different in the live environment, there are several options:

  • Create a file containing environment: "live" or "local" and put "if statement" in the code based on the environment setting.
  • If you're able to dectect the enviroment in php, use that instead of a file.
  • Place all settings in a file outside the "versionX" folders.
等待我真够勒 2024-07-20 20:53:24

1)为了解决“开发和实时服务器上的不同配置”问题,我使用这个:

// Change 'localhost' to your dev server's address
define('IS_LIVE', 'localhost' != $_SERVER['HTTP_HOST']);

// Database configuration
$db_cfg = IS_LIVE?
  array(...): // Live server config
  array(...); // Dev server config

2)为了保持开发和实时文件同步,我使用 Beyond Compare,一个可视化差异工具,允许我比较整个目录,包括通过 (S)FTP 的远程目录。

我设置了一个配置文件,以便左侧窗口显示开发服务器上的文件,右侧窗口显示实时服务器上的文件。 通过这种方式,我可以看到服务器之间存在哪些差异(已更改、丢失或添加的文件),并允许我轻松复制它们之间的整个目录、文件或文件中的特定行。 很有用。

它还允许您“忽略”您不想同步的特定目录,例如包含用户生成的文件或日志的目录。

1) To solve the "different configuration on dev and live servers" problem I use this:

// Change 'localhost' to your dev server's address
define('IS_LIVE', 'localhost' != $_SERVER['HTTP_HOST']);

// Database configuration
$db_cfg = IS_LIVE?
  array(...): // Live server config
  array(...); // Dev server config

2) To keep the dev and live files synched I use Beyond Compare, a visual diff tool that allows me to compare whole directories, including remote ones via (S)FTP.

I set up a profile so that the left window shows files on dev server, the right one shows files on live server. This way I can see what differences there are between the servers (changed, missing or added files) and allows me to easily copy whole directories, files, or specific lines in files between them. Very useful.

It also lets you 'ignore' particular directories that you don't want to synch, like the ones with user generated files or logs.

我不咬妳我踢妳 2024-07-20 20:53:24

移动文件

您正在寻找 rsync 或类似 rsync 的东西。 Rsync 是一个程序/系统,可让您将一组文件与另一组文件同步。 简而言之,它可以查看您的源代码和生产代码,并且只会上传不同的文件。

rsync -av --cvs-exclude /soure/dir [email protected]:./source/dir    

--cvs-exclude 标志的命名有点误导。 它将使 rsync 忽略 rcs 程序 cvs 忽略的文件,而不仅仅是 .cvs 目录。 您可以通过运行来获取有关标志的更多信息。

rsync --help

您托管应用程序的服务器将需要运行 rsync 守护进程,但这已成为当今课程的标准。

如果您想变得更聪明,您可以使用 SSH 密钥配置 rsync,甚至不需要输入密码。 然而,这绝不是必要的。

不同的配置信息

将本地服务器和实时服务器之间不同的任何配置信息分离到一个文件或一组文件中。 您可以通过 FTP 将这些文件传输到实时服务器,并使用 --exclude 选项告诉 rsync 忽略这些文件。

Moving Files

You're looking for rsync, or something rsync like. Rsync is a program/system that will let you synchronize one set of files with another. In oversimplified term, it can look thorugh your source code, and your production code, and will only upload files that are different.

rsync -av --cvs-exclude /soure/dir [email protected]:./source/dir    

The --cvs-exclude flag is named a little misleadingly. It will make rsync ignore files that the rcs program cvs ignores, not just .cvs directories. You can get more information on the flags by running

rsync --help

The server you're hosting your application on will need to be running an rsync daemon, but that's par for the course these days.

If you want to get really clever, you can configure rsync using SSH Keys, and you won't even need to enter a password. However, this is by no means necessary.

Different Configuration Information

Separate any configuration information that's going to vary between your local and live servers into a single file, or a single set of files. You can transfer these files to the live server via FTP, and the use the --exclude option to tell rsync to ignore these files.

撞了怀 2024-07-20 20:53:24

不确定它是否理想,但例如在我的数据库登录配置中,我让它检测代码正在运行的主机(下面只是部分片段):(

$active_group = "default";
if($_SERVER['SERVER_NAME'] == 'argent.local' || 
   $_SERVER['SERVER_NAME'] == 'beta.website.com') 
   $active_group = "development";

$db['default']['hostname'] = "localhost";
$db['default']['username'] = "username";
$db['default']['password'] = "password";
$db['default']['database'] = "dbname";
$db['default']['dbdriver'] = "mysql";

$db['development']['hostname'] = "localhost";
$db['development']['username'] = "username_dev";
$db['development']['password'] = "password_dev";
$db['development']['database'] = "dbname_dev";
$db['development']['dbdriver'] = "mysql";

argent.local这里是我的本地开发框)我这样做了一些其他地方也一样,就像在主类中一样,我根据服务器是生产还是本地开发来启用/禁用 PHP 错误输出(因为错误输出在开发期间非常有用,但我从不希望在生产中输出):

if($_SERVER['SERVER_NAME'] == 'argent.local' || 
   $_SERVER['SERVER_NAME'] == 'beta.website.com') 
    error_reporting(E_ALL);
    else error_reporting(0);

这样我的代码签入 SVN 将在 live 和 dev 盒子上运行,而无需跟踪哪个部署在哪里。 这样我就可以擦除所有现有文件并重新部署,而不必担心移动任何文件。

Not sure if it's ideal, but for example within my DB login config I have it detect what host the code is running on (just a partial snip below):

$active_group = "default";
if($_SERVER['SERVER_NAME'] == 'argent.local' || 
   $_SERVER['SERVER_NAME'] == 'beta.website.com') 
   $active_group = "development";

$db['default']['hostname'] = "localhost";
$db['default']['username'] = "username";
$db['default']['password'] = "password";
$db['default']['database'] = "dbname";
$db['default']['dbdriver'] = "mysql";

$db['development']['hostname'] = "localhost";
$db['development']['username'] = "username_dev";
$db['development']['password'] = "password_dev";
$db['development']['database'] = "dbname_dev";
$db['development']['dbdriver'] = "mysql";

(argent.local here being my local dev box) I do this a few other places too, like in the main class I enable/disable PHP error output depending on if the server is production or local dev (since error output is very useful during dev, but I never want that outputted in production):

if($_SERVER['SERVER_NAME'] == 'argent.local' || 
   $_SERVER['SERVER_NAME'] == 'beta.website.com') 
    error_reporting(E_ALL);
    else error_reporting(0);

This way the code I check into SVN will work on both live and dev boxes without having to keep track of which to deploy where. This way I can wipe all existing files and just redeploy fresh without having to worry about moving any files over.

财迷小姐 2024-07-20 20:53:24

由于您没有 ssh 访问权限,因此请在本地计算机上测试并提交所有内容。 然后你可以使用 ftp 客户端 filezilla 的目录比较功能,它可以让你看到哪些文件已被修改。 你不需要记住任何事情。

最佳实践是使用 git push 推送到您的服务器。 但由于您的选择有限,上述解决方案就可以了。

since you don't have ssh access, test and commit everything on your local machine. then you could use directory comparison feature of ftp client filezilla which let you see what files have been modified. you won't need to remember anything.

best practices would be to push to your server with git push. but since your options are limited, above-mentioned solution will do just fine.

屋顶上的小猫咪 2024-07-20 20:53:24

构建工具,例如 phingant 应该能够做您想做的事情。 通过编写一些脚本,您应该能够自动执行当前获取应用程序配置的步骤并使用单个命令上传应用程序。

A build tool such as phing or ant should be able to do what you are looking to do. With a little scripting, you should be able to automate the steps you are currently taking to get the application configuration and upload the application with a single command.

饮惑 2024-07-20 20:53:24

我推荐 Capistrano,它是 Rails 领域的通用部署工具。 它基本上是从头开始设计的,旨在管理部署。 这是 Capistrano 主页:

http://capify.org/

这里有几个网站展示了其他人如何使用 Capistrano PHP 部署:

http://www.simplisticcomplexity。 com/2006/08/16/automated-php-deployment-with-capistrano/

http://donc.wordpress.com/2006/10/29/deploying-php-with-capistrano/

注意:您必须编写/自定义您的部署脚本,但 Capistrano 应该让你做你需要做的事。

I'd recommend Capistrano, which is a generalized deployment tool from the Rails world. It's basically designed from the ground up to manage deployments. Here's the Capistrano homepage:

http://capify.org/

and here's a couple of sites showing how others use Capistrano for PHP deployment:

http://www.simplisticcomplexity.com/2006/08/16/automated-php-deployment-with-capistrano/

http://donc.wordpress.com/2006/10/29/deploying-php-with-capistrano/

Note: you will have to write/customize your deployment script, but Capistrano should let you do what you need.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文