为本地开发/测试设置 Apache? [英] Set up Apache for local development/testing?

查看:21
本文介绍了为本地开发/测试设置 Apache?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Rails 的截屏视频给我留下了深刻的印象,这些截屏演示了内置的 Web 服务器和允许在本地机器上进行开发和测试的数据库.我怎样才能让 Apache 的实例将项目目录作为它的 DocumentRoot 执行,并可能在端口 8080(或类似的东西)上提供文件?

I've been impressed by the screencasts for Rails that demonstrate the built-in web server, and database to allow development and testing to occur on the local machine. How can I get an instance of Apache to execute a project directory as its DocumentRoot, and maybe serve up the files on port 8080 (or something similar)?

我之所以这么问是因为我要试用 CodeIgniter,而且我想将它用于多个项目.我宁愿不要把我机器的 DocumentRoot 每一个都弄得乱七八糟.也欢迎提供有关如何进行数据库迁移的建议.

The reason why I'm asking is because I'm going to be trying out CodeIgniter, and I would like to use it for multiple projects. I would rather not clutter up my machine's DocumentRoot with each one. Suggestions on how to do database migrations are also welcome.

感谢您到目前为止的回复.我应该澄清一下,我使用的是 Mac OS X.看起来 WAMP 仅适用于 Windows.此外,XAMPP 看起来是安装 Apache 和许多其他 Web 工具的好方法,但我没有看到加载实例以提供项目目录的方法.Mac OS X 安装了 Apache 和 PHP - 我只是在寻找一种方法让它在非标准端口上提供项目.

Thank you for your responses so far. I should clarify that I'm on Mac OS X. It looks like WAMP is Windows-only. Also, XAMPP looks like a great way to install Apache and many other web tools, but I don't see a way of loading up an instance to serve up a project directory. Mac OS X has both Apache and PHP installed - I'm just looking for a way to get it to serve up a project on a non-standard port.

我刚刚发现 MAMP Pro 可以满足我的需求,但更简约如果可能的话,方法会更好.有没有人有可以编辑并放入项目目录的 httpd.conf 文件?

I just found MAMP Pro which does what I want, but a more minimalist approach would be better if it's possible. Does anyone have a httpd.conf file that can be edited and dropped into a project directory?

另外,很抱歉我刚刚提出了那个数据库迁移问题.我希望找到的东西能让我将架构更改推送到实时服务器上,而不会丢失现有数据.我怀疑这很困难,并且高度依赖于环境因素.

Also, sorry that I just threw in that database migration question. What I'm hoping to find is something that will enable me to push schema changes onto a live server without losing the existing data. I suspect that this is difficult, and highly dependent on environmental factors.

推荐答案

您的 Mac 带有 Apache Web 服务器和 PHP 版本.这是该平台深受网络开发者喜爱的重要原因之一.

Your Mac comes with both an Apache Web Server and a build of PHP. It's one of the big reasons the platform is well loved by web developers.

由于您使用的是 Code Igniter,因此您需要 PHP 5,这是 PHP 10.5 附带的默认版本.如果您使用的是以前版本的操作系统,请转到 entropy.ch 并安装提供 PHP5 包.

Since you're using Code Igniter, you'll want PHP 5, which is the default version of PHP shipped with 10.5. If you're on a previous version of the OS hop on over to entropy.ch and install the provided PHP5 package.

接下来,您需要打开 Apache.在共享首选项面板中,打开个人网络共享.这将在您的本地机器上启动 apache.

Next, you'll want to turn Apache on. In the sharing preferences panel, turn on personal web sharing. This will start up apache on your local machine.

接下来,您需要设置一些用于您的网站的虚假开发网址.长期以来的传统是我们为此使用假 TLD .dev(例如 stackoverflow.dev).然而,.dev 现在是一个实际的 TLD,所以你可能不想这样做——.localhost 似乎是一个新兴的事实上的标准.编辑您的/etc/hosts 文件并添加以下行

Next, you'll want to setup some fake development URLs to use for your sites. Long standing tradition was that we'd use the fake TLD .dev for this (ex. stackoverflow.dev). However, .dev is now an actual TLD so you probably don't want to do this -- .localhost seems like an emerging defacto standard. Edit your /etc/hosts file and add the following lines

127.0.0.1    www.example.localhost
127.0.0.1    example.localhost

这会将上述 URL 指向您的本地计算机.最后一步是配置apache.具体来说,启用命名虚拟主机,启用 PHP 并设置一些虚拟主机.如果你使用了entropy PHP 包,那么启用PHP 就已经完成了.如果没有,您需要按照此处所述编辑您的 http.conf 文件.基本上,您是在取消注释将加载 PHP 模块的行.

This points the above URLs at your local machine. The last step is configuring apache. Specifically, enabling named virtual hosting, enabling PHP and setting up a few virtual hosts. If you used the entropy PHP package, enabling PHP will already be done. If not, you'll need to edit your http.conf file as described here. Basically, you're uncommenting the lines that will load the PHP module.

每当您对 apache 配置进行更改时,您都需要重新启动 apache 以使更改生效.在终端窗口中,键入以下命令

Whenever you make a change to your apache config, you'll need to restart apache for the changes to take effect. At a terminal window, type the following command

sudo apachectl graceful

这将优雅地重新启动 apache.如果您在配置文件中出现语法错误,apache 将不会重新启动.您可以使用

This will gracefully restart apache. If you've made a syntax error in the config file apache won't restart. You can highlight config problems with

sudo apachectl configtest

因此,在启用 PHP 的情况下,您需要打开 NamedVirtualHosts.这将使 apache 响应多个 URL.在您的 http.conf 文件中查找以下(或类似)行并取消注释.

So,with PHP enabled, you'll want to turn on NamedVirtualHosts. This will let apache respond to multiple URLs. Look for the following (or similar) line in your http.conf file and uncomment it.

#NameVirtualHost *  

最后,您需要告诉 apache 应该在哪里寻找新虚拟主机的文件.您可以通过将以下内容添加到 http.conf 文件来实现.注意:我发现将这样的配置规则分解到一个单独的文件中并使用 include 指令来包含您的更改是一个很好的最佳实践.这将阻止任何自动更新清除您的更改.

Finally, you'll need to tell apache where it should look for the files for your new virtual hosts. You can do so by adding the following to your http.conf file. NOTE: I find it's a good best practice to break out config rules like this into a separate file and use the include directive to include your changes. This will stop any automatic updates from wiping out your changes.

<VirtualHost *>
    DocumentRoot /Users/username/Sites/example.localhost
    ServerName example.localhost
    ServerAlias www.example.localhost
</VirtualHost>

您可以将任何文件夹指定为 DocumentRoot,但我发现使用您的个人 Sites 文件夹很方便,因为它已被配置为包含文件的正确权限.

You can specify any folder as the DocumentRoot, but I find it convenient to use your personal Sites folder, as it's already been configured with the correct permissions to include files.

这篇关于为本地开发/测试设置 Apache?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆