osx虚拟机_在OSX中创建虚拟主机

osx虚拟机

As someone who had always developed on PCs, switching over to using Mac OS X was like going from peasant to prince.  Compared to Windows-based machines, Mac's OS X operating system is light years better.  One OS X feature I make much use of is the integrated Apache server.  Hosting websites on my local machine during development is a must.  Let me show you how to create multiple websites on your local machine.

作为一直在PC上进行开发的人,切换到使用Mac OS X就像从农民到王子。 与基于Windows的计算机相比,Mac的OS X操作系统要好几年。 我大量使用的OS X功能之一是集成的Apache服务器。 在开发过程中必须在本地计算机上托管网站。 让我向您展示如何在本地计算机上创建多个网站。

步骤1:建立网站资料夹 (Step 1:  Create Website Folder)

User websites appear in the Users/Sites/ directory.  Let's create a directory within Sites named "mynewsite":

用户网站出现在Users / Sites /目录中。 让我们在名为“ mynewsite”的站点中创建一个目录:


cd ~/Sites/
mkdir mynewsite


As you'll see in a moment, you can add this directory anywhere you'd like, but for the sake of this post we'll add it in the standard Sites directory.

稍后您将看到,可以将此目录添加到您想要的任何位置,但是出于本文的原因,我们将其添加到标准的Sites目录中。

步骤2:编辑httpd-vhosts.conf文件 (Step 2:  Edit httpd-vhosts.conf File)

The next step is adding a new record to Apache's httpd-vhosts.conf file.  My httpd-vhosts.conf file is located within the following directory:  private/etc/apache2/extra/.  This configuration file holds your website virtual host location details.  Let's add mynewsite to the configuration:

下一步是将新记录添加到Apache的httpd-vhosts.conf文件中。 我的httpd-vhosts.conf文件位于以下目录中: private/etc/apache2/extra/ 。 此配置文件包含您的网站虚拟主机位置的详细信息。 让我们将mynewsite添加到配置中:


<VirtualHost *:80>
    DocumentRoot "/Users/myUserName/Sites/mynewsite"
    ServerName mynewsite.local
</VirtualHost>


The two important configurations are DocumentRoot and ServerName.  DocumentRoot points to the directory we created in step 1.  ServerName refers to the address we want to type into the browser to get to this new site.  I generally create all my hosts with the ".local" domain.

两个重要的配置是DocumentRoot和ServerName。 DocumentRoot指向我们在步骤1中创建的目录。ServerName指向我们要在浏览器中键入的地址,以访问该新站点。 通常,我使用“ .local”域创建所有主机。

You can also place more specific configurations within this new virtual host, including ProxyPass, directory settings, error documents, redirects, and other settings you sometimes see in .htaccess files:

您还可以在此新的虚拟主机中放置更多特定的配置,包括ProxyPass,目录设置,错误文档,重定向以及有时在.htaccess文件中看到的其他设置:


<VirtualHost *:80>
	DocumentRoot "/Users/myUserName/Sites/mynewsite"
	ServerName mynewsite.local
	<directory "/Users/davidwalsh83/Sites/mynewsite/trunk/">
		Options Indexes FollowSymLinks
		AllowOverride All
		Order allow,deny
		Allow from all
	</directory>
	ProxyPass /web-service http://someotherwebsite.com/api/
	ProxyPassReverse /web-service http://someotherwebsite.com/api/
</VirtualHost>


You can learn more about the possible settings here:  http://httpd.apache.org/docs/2.2/vhosts/

您可以在此处了解有关可能的设置的更多信息: http : //httpd.apache.org/docs/2.2/vhosts/

步骤3:编辑hosts文件 (Step 3:  Edit hosts File)

The hosts file contains a list of host names and IP address to which they match.  We'll add another record to this file which points the new website's ServerName setting from above (mynewsite.local) to the localhost IP address:

hosts文件包含与之匹配的主机名和IP地址的列表。 我们将向该文件添加另一个记录,该记录将新网站的ServerName设置从上方(mynewsite.local)指向本地主机IP地址:


##
# Host Database
#
# localhost is used to configure the loopback interface
# when the system is booting.  Do not change this entry.
##
127.0.0.1	localhost
255.255.255.255	broadcasthost
::1             localhost 
fe80::1%lo0	localhost
127.0.0.1	mynewsite.local


Now mynewsite.local host name is pointing toward your machine.  Coupled with the httpd-vhosts.conf file's new settings, the address will find your mynewsites directory!

现在,mynewsite.local主机名指向您的计算机。 结合httpd-vhosts.conf文件的新设置,该地址将找到您的mynewsites目录!

步骤4:重新启动Apache (Step 4:  Apache Restart)

The last step in the process is restarting your Apache install:

该过程的最后一步是重新启动Apache安装:


apache restart


The other method of quickly restarting Apache is by opening System Settings >> Sharing and unchecking and then checking "Web Sharing".  That's all!  Now you can navigate to http://mynewsite.local and see your web files!

快速重启Apache的另一种方法是打开系统设置>>共享,然后取消选中,然后选中“ Web共享”。 就这样! 现在,您可以导航到http://mynewsite.local并查看您的Web文件!

快速附注 (Quick Side Note)

Your IT people would probably prefer you not use your machine's Apache and instead use a virtual machine for your web development.  I've personally felt the pain of using my MacBook Pro instead of a virtual machine but that was due to MySQL and not Apache.  This tutorial is meant to show you how to quickly get a new site up and running on your local machine. The same process can be used to create a new virtual host on other machines but the file locations will simply be different.

您的IT人员可能更希望您不使用计算机的Apache,而是使用虚拟机进行Web开发。 我个人感到使用MacBook Pro而不是虚拟机的痛苦,但这是由于MySQL而不是Apache。 本教程旨在向您展示如何快速在本地计算机上启动并运行新站点。 可以使用相同的过程在其他计算机上创建新的虚拟主机,但是文件位置将完全不同。

翻译自: https://davidwalsh.name/create-virtual-host

osx虚拟机

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值