How to setup multiple sites hosted on your Mac with OSX 10.8 + (MAMP Part 5)

转自:http://machiine.com/2014/how-to-setup-multiple-sites-hosted-on-your-mac-with-osx-10-8-mamp-part-5/


How to setup multiple sites hosted on your Mac with OSX 10.8 + (MAMP Part 5)

Welcome to part 5 of the MAMP tutorial. In this post we will go over setting up friendly URLs on your Mac server. We will be adding a new site called http://mysite.localhost

You will also learn how to run multiple domain sites on your local server. Having Virtual Hosts enabled is really useful when developing sites on your localhost. It’s also super easy to setup.

What green means: any lines that you need to replace in this tutorial will be in green.


1. Enable virtual hosts

1. Open your Terminal (found in your Utilities folder which is inside your Applications folder)

Then you will need to open the file Apache httpd.conf file with the Nano editor and uncomment the virtual hosts line.

2. Type the following line in your Terminal window

sudo nano /etc/apache2/httpd.conf

terminal nano editor3. With the file open find the line where the Virtual Hosts file is commented out.

#Include /private/etc/apache2/extra/httpd-vhosts.conf

4. Remove the # in front of the line. This will enable the Virtual Hosts configuration file.

Include /private/etc/apache2/extra/httpd-vhosts.conf

5. Save and exit the file. Hold control + x to save and exit. Type Y to confirm. Then press enter.

2. Configure the virtual hosts file to setup the directories and logs for Apache

Next you want to configure your Virtual Host file. To do that you first need to open the virtual hosts file using Nano.

1. Type the following command in your Terminal window.

sudo nano /etc/apache2/extra/httpd-vhosts.conf

2. Scroll down to the end of the file and add the following.

<VirtualHost *:80>
   DocumentRoot "/User/username/Sites"
</VirtualHost>
<VirtualHost *:80>
   DocumentRoot "/path/to/web/folder/where/site/is/located" 
   ServerName mysite.localhost
   ErrorLog "/path/to/error/log/mysite.localhost.error_log"
   <Directory "/path/to/web/folder/where/site/is/located"> 
     AllowOverride All
     Order allow,deny
     Allow from all 
   </Directory>
 </VirtualHost>

The first VirtualHost record tells the server where to go if only http://localhost is entered in the browser. You can set this to any location you want.

The second VirtualHost record sets up your paths for a specific local domain. You will need to create a new record like this for each new site you want to add.Virtual Host record example

3. Save and exit the file. Hold control + x to save and exit. Type Y to confirm. Then press enter.

3. Adding the url record to your hosts file

This is very easy to do.

1. Open the hosts file in your nano editor using Terminal.

sudo nano /etc/hosts

2. Add a virtual hostname for each website.

Scroll down to the end of the file and add a new record.

127.0.0.1 mysite.localhost

Just like the virtual host file above you will need to add a new record foreach new website you want to add.

3. Save and exit the file. Hold control + x to save and exit. Type Y to confirm. Then press enter.

4. Restart Apache

Open terminal and run the following command to restart your Apache server

sudo apachectl restart

5. (Optional) Give Apache permission to access your site.

If you are getting a Forbidden message or you store your website folder in a location other than the /Users/username/Sites/ directory you will need to give Apache permission to access it.

Fix MAMP forbidden error

Here’s how you do it.

1. Open Terminal and navigate to base of your site. For example if your site is stored in /srv/www/mysite/ go to it’s parent directory

cd /srv/www/

2. Next give the Apache user ownership rights of your site’s folder

sudo chown -R :_www mysite

3. Lastly, give the Apache group now associated with your folder permissions to access and write to it

sudo chmod -R g+w mysite

Restart Apache again and Congrats. Open your browser and go to the url http://mysite.localhost/

You will see your site there.

I created an index.php file to demonstrate how this works.

Demo index file


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值