为本地开发/测试设置 Apache?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/118423/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 16:45:02  来源:igfitidea点击:

Set up Apache for local development/testing?

phpcodeigniterapache

提问by Kyle Cronin

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)?

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

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.

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



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.

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

I just found MAMP Prowhich 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?

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

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.

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

回答by Alan Storm

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.

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

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.chand install the provided PHP5 package.

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

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.

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

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, .devis now an actual TLD so you probably don't want to do this -- .localhostseems like an emerging defacto standard. Edit your /etc/hosts file and add the following lines

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

127.0.0.1    www.example.localhost
127.0.0.1    example.localhost

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.

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

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

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

sudo apachectl graceful

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

这将优雅地重新启动 apache。如果您在配置文件中出现语法错误,apache 将不会重新启动。您可以突出显示配置问题

sudo apachectl configtest

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.

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

#NameVirtualHost *  

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.

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

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

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.

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

回答by Jurassic_C

Sorry Kyle, I don't have enough cred to respond directly to your comment. But if you want to have each project be served on a different port, try setting up your virtual host config exactly like Kelly's above (minus the dns stuff) except instead of 80, give each virtualhost its own port number, assuming that you've added this port to your ports.conf file.

抱歉,凯尔,我没有足够的信誉直接回复您的评论。但是,如果您想让每个项目都在不同的端口上提供服务,请尝试像上面的 Kelly 一样设置您的虚拟主机配置(减去 dns 内容),除了 80 之外,为每个虚拟主机提供自己的端口号,假设您已经将此端口添加到您的 ports.conf 文件。

NameVirtualHost *

<virtualhost *:80>
DocumentRoot /site1/documentroot
</virtualhost>

<virtualhost *:81>
DocumentRoot /site2/documentroot
</virtualhost>

<virtualhost *:82>
DocumentRoot /site3/documentroot
</virtualhost>

<virtualhost *:83>
DocumentRoot /site4/documentroot
</virtualhost>

Hope that helps :/

希望有帮助:/

回答by Bazman

I also download the latest binaries for each and set them up manually. I've found it to be a painless process, as long as you know a little bit about configuring Apache.

我还为每个下载最新的二进制文件并手动设置它们。我发现这是一个轻松的过程,只要您对配置 Apache 有所了解。

On my development machine, I have apache setup with name-based virtual hosting. I also have a dyndns.orgaccount which maps my development domain to my local machine. DynDNSprovides a wildcard domain, and therefore using name based virtual hosts I can easily create as many sites (with separate document roots) for as many development domains as I want, all running off the one Apache instance.

在我的开发机器上,我使用基于名称的虚拟主机进行了 apache 设置。我还有一个dyndns.org帐户,它将我的开发域映射到我的本地机器。DynDNS提供了一个通配符域,因此使用基于名称的虚拟主机,我可以轻松地为任意数量的开发域创建任意数量的站点(具有单独的文档根),所有站点都运行在一个 Apache 实例上。

e.g. Apache config for the virtual hosts might be something like

例如,虚拟主机的 Apache 配置可能类似于

NameVirtualHost *:80

<virtualhost *:80>
ServerName site1.mydyndns.dyndns.org
DocumentRoot /site1/documentroot
</virtualhost>

<virtualhost *:80>
ServerName site2.mydyndns.dyndns.org
DocumentRoot /site2/documentroot
</virtualhost>

This has been the quickest and easiest way for me to easily maintain many development sites on my local machine.

这是我在本地机器上轻松维护许多开发站点的最快捷、最简单的方法。

I hope that makes sense.

我希望这是有道理的。

Cheers, Kelly.

干杯,凯利。



回答by Jurassic_C

I don't use Macos, but I do use Apache. In my apache configuration file (on linux its usually at /etc/apache2/apache2.conf), look for a reference to a file called ports.conf. Find this file and add the line

我不使用 Macos,但我使用 Apache。在我的 apache 配置文件中(在 Linux 上,它通常位于 /etc/apache2/apache2.conf)中,查找对名为 ports.conf 的文件的引用。找到这个文件并添加行

Listen 8080

听8080

Then restart the apache process. After that you should be in business. I apologize in advance if this doesn't work on a mac :)

然后重启apache进程。在那之后,你应该做生意。如果这在 mac 上不起作用,我提前道歉:)

回答by phloopy

You could use a low up front setup package such as XAMPPand run it as a separate instance. There are many other similar projects as well.

您可以使用诸如XAMPP 之类的低前期设置包并将其作为单独的实例运行。还有许多其他类似的项目。

回答by Eran Galperin

For PHP you have several high-quality packages for deploying Apache+Mysql+PHP, such as WAMPand XAMPP. Personally, I download the latest binaries of each and install manually to have more fine grained control. There are plenty of online tutorials on how to handle that.

对于 PHP,您有几个用于部署 Apache+Mysql+PHP 的高质量包,例如WAMPXAMPP。就个人而言,我下载每个文件的最新二进制文件并手动安装以获得更细粒度的控制。有很多关于如何处理的在线教程。

Database migrations should be straightforward - dump the database on the server, either at the command line or through an interface such as PHPMyAdmin, and export it back in similar ways (PHPMyAdmin is recommended if you have no experience with the Mysql command line).

数据库迁移应该很简单——在服务器上转储数据库,无论是在命令行还是通过PHPMyAdmin 之类的接口,然后以类似的方式将其导出(如果您没有使用 Mysql 命令行的经验,建议使用 PHPMyAdmin)。

回答by rg88

You can use MAMP pro but the free version is a very good choice as well. Get it here: http://www.mamp.info/en/mamp.html

您可以使用 MAMP pro,但免费版本也是一个非常好的选择。在这里获取:http: //www.mamp.info/en/mamp.html

回答by EmmEff

I might recommend using a separate LAMP virtual appliance for each development environment you wish to experiment with. Run them on VMware Server or VirtualBox.

我可能会建议为您希望试验的每个开发环境使用单独的 LAMP 虚拟设备。在 VMware Server 或 VirtualBox 上运行它们。