设为首页收藏本站

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 308|回复: 0

Setting Up Name Based Virtual Hosting

[复制链接]
发表于 2013-2-16 23:51:21 | 显示全部楼层 |阅读模式
Installing Apache 2
I’ll be discussing name based virtual hosting with Apache (Apache 2 to be specific).  We’ll need to install that before we dive in:
  1. sudo aptitude install apache2
复制代码
Once these are setup you should be able to connect to localhost in your browser and see a test page.


NameVirtualHost
With the default configuration you are only serving up one site, and that site is based on your IP address.  What I’m setting up is name-based virtual hosting, meaning the Apache server will serve specific content based on the domain name requested.  In this way a single server can host multiple sites, and serve up unique content based on the domain requested.
My preferred method of using name based virtual hosting is creating a seperate file for each domain.  These can all be done within one file, but I’ll be creating a new file for each site.
First we need to define to Apache that we’re using name based virtual hosting instead of IP based.  You can append the following line to your /etc/apache2/apache2.conf to define this:
NameVirtualHost ip.address:port

The above should be your public facing IP address (assuming you’re creating a public site), and port is generally port 80 by default.  After this we’ll create the base configuration for your virtual hosts.  Debian and Ubuntu use /etc/apache2/sites-available/ and /etc/apache2/sites-enabled/ directories for defining virtual hosting.  One nice thing about this is that you can have more sites “available” than you have “enabled”, meaning not everything configured is actually live and listening.  This is nice to quickly disable a site for whatever reason.
I like to create unique files for each of my domains within the /etc/apache2/sites-available/ folder.  For example I have a file called “ubuntu-tutorials.com” in that directory, with the following contents:
  1. <VirtualHost 67.207.131.28:80>
  2. ServerName ubuntu-tutorials.com
  3. ServerAlias www.ubuntu-tutorials.com
  4. ServerAdmin christer.edwards@ubuntu.com
  5. DocumentRoot /var/www/ubuntu-tutorials.com/html
  6. </VirtualHost>
复制代码
What these settings do is as follows:
  • ServerName listens for requests asking for a certain domain
  • ServerAlias defines any additional domains that should match
  • ServerAdmin is the contact for the site
  • DocumentRoot is the path to the content for that site
Now that this file is created in the /etc/apache2/sites-available/ folder we’re just about ready to start, but we need to enable it.  We can do that by creating a symbolic link from one folder to the next.
  1. cd /etc/apache2/sites-enabled/
  2. ln -s ../sites-available/ubuntu-tutorials.com .
复制代码
This site is now available (as in configured) and enabled (as in listening) once we restart the apache service:
  1. sudo /etc/init.d/apache2 restart
复制代码
Testing
To test your configuration you can, temporarily, configure your /etc/hosts file to point the domain to your IP address and see if your server loads up the correct site. This is only needed if the hostname or domain name does not already resolve to your IP address.  Editing the /etc/hosts by adding the following line:
  1. ip.address domain.tld
复制代码
Open your browser, try to access domain.tld and see if it loads the contents from your local DocumentRoot (from the configuration above).  You might want to drop a file in the DocumentRoot to verify its pulling your local content.
  1. cd /var/www/ubuntu-tutorials.com/html
  2. echo "Hello World" > index.html
复制代码
Conclusion
I hope I didn’t miss anything here.  One of the main purposes of this writeup is to document what I did to setup my server.  I do it so rarely I don’t always remember all the steps when I need to.  If this helps you setup name based virtual hosting, great.  Leave a comment and let me know.  If I forgot anything critical please also let me know so I can update the contents.

http://ubuntu-tutorials.com


您需要登录后才可以回帖 登录 | 注册

本版积分规则

手机版|小黑屋|BC Morning Website ( Best Deal Inc. 001 )  

GMT-8, 2025-8-26 06:57 , Processed in 0.018446 second(s), 17 queries .

Supported by Best Deal Online X3.5

© 2001-2025 Discuz! Team.

快速回复 返回顶部 返回列表