您的位置:首页 > 运维架构 > Apache

apache搭建基于IP/域名的虚拟主机

2014-03-11 16:34 821 查看
首先搭建基于IP的虚拟主机

//环境:

1.可能需要的工具包
shell> yum -y install gcc gcc-c++ make cmake automake autoconf cpp ncurses ncurses-devel libxml2-devel openssl-devel curl-devel libjpeg-devel libpng-devel pcre-devel libtool libtool-libs freetype-devel gd zlib-devel file bison patch mlocate flex diffutils readline-devel glibc-devel glib2-devel bzip2-devel gettext-devel libcap-devel

2.编译安装apache前需要先安装依赖包
shell> wget http://mirrors.axint.net/apache/apr/apr-util-1.4.1.tar.gz shell> wget http://mirrors.axint.net/apache/apr/apr-1.4.8.tar.gz shell> wget http://mirrors.axint.net/apache/apr/apr-iconv-1.2.1.tar.gz shell> wget ftp://mcrypt.hellug.gr/pub/crypto/mcrypt/libmcrypt/libmcrypt-2.5.7.tar.gz shell> wget http://apache.fayea.com/apache-mirror//httpd/httpd-2.4.7.tar.gz" 
3.解压
shell> tar xvf ***.tar.gz

4.安装libmcrypt apr apr-util apr-iconv
#libmcrypt install
shell> cd /home/softs/libmcrypt-2.5.7
shell> ./configure
shell> make && make install

#apr install
shell> cd /home/softs/apr-1.4.8
shell> sed -i "/.*\$RM\ \"\$cfgfile\"/d" configure
shell> ./configure '--prefix=/usr/local/apr'
shell> make && make install

#apr-util install
shell> cd /home/softs/apr-util-1.4.1
shell> ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr/bin/apr-1-config
shell> make && make install

#apr-iconv install
shell> cd "/home/softs/apr-iconv-1.2.1"
shell> ./configure --prefix=/usr/local/apr-iconv --with-apr=/usr/local/apr/bin/apr-1-config
shell> make && make install

5.apache编译安装
shell> ./configure --prefix=/usr/local/apache2 --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util --with-apr-iconv=/usr/local/apr-iconv --with-ssl --enable-ssl --enable-so --enable-deflate=shared --enable-expires=shared --enable-headers=shared --enable-rewrite=shared --enable-static-support
shell> make && make install
#复制启动项
shell> cp  /usr/local/apache2/bin/apachectl /etc/init.d/httpd

4.apache主配置文件
shell> /usr/local/apache2/conf/httpd.conf

5.配置DNS服务器解析
www.zijian.com  10.10.54.159
bbs.zijian.com  10.10.54.152


【全局设置】1.修改主配置文件

shell> vim /usr/local/apache2/conf/httpd.conf

#apache配置文件和日志文件等的根目录
ServerRoot "/usr/local/apache2"

#配置apache server监听端口
Listen 10080

#加载php模块支持,可以读取index.php文件
LoadModule php5_module        modules/libphp5.so
AddType application/x-httpd-php .php .phtml

#apache用户
<IfModule unixd_module>
User daemon
Group daemon
</IfModule>

#server 名字,没有的话重启会有提示
ServerName www.localhost.com:80

#配置网站内容根目录
#DocumentRoot "/var/www"    #因为有多个网站根目录,所以此项在虚拟主机配置文件里面设定

#针对目录:/var/www/html,/var/www/bbs 设置相应参数,对应后面的虚拟主机目录

#Indexes:如果在此目录下找不到“首页文件”,就显示整个目录下的文件名称,“首页文件”与DirectoryIndex设置值有关。
#FollowSymLinks:让在此目录下的连接文件可以连接出此目录外。一般来说被chroot的程序将无法离开其目录,也就是说默认的情况下,您在/var/www/html下面的连接文件只要连接到非此目录的其他地方,则该连接文件默认是失效的。但使用此设置即可让连接文件有效地离开本目录。
#AllowOverride:表示是否允许额外配置文件“.htaccess”的权限复写?可以在httpd.conf内设置好所有的权限,如此一来若用户自己的个人网页想要修改权限时将会对管理员造成困扰。因此Apache默认可以让用户以目录下面的“.htaccess”文件复写内的权限设置。这个项目则规定“.htaccess”可以复写的权限类型有哪些。常见的有以下几种。
· ALL:全部的权限均可被复写。
· AuthConfig:仅有网页认证(账号密码)可复写。
· Indexes:仅允许Indexes方面的复写。
· Limits:允许用户利用Allow、Deny与Order管理可浏览的权限。
· None:不可复写,即“.htaccess”文件失效

<Directory "/var/www/html">
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>

<Directory "/var/www/bbs">
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>

#网站支持的index文件类型,可以写在虚拟主机配置文件中
#<IfModule dir_module>
#    DirectoryIndex index.php index.html
#</IfModule>

#虚拟主机配置文件
Include conf/extra/httpd-vhosts.conf


【基于IP的虚拟主机】2.虚拟主机配置文件/usr/local/apache2/conf/extra/httpd-vhosts.conf

首先添加虚拟IP
shell> ifconfig eth0:0 10.10.54.152 netmask 255.255.255.0

shell> vim /usr/local/apache2/conf/extra/httpd-vhosts.conf
<VirtualHost 10.10.54.157:80>
ServerAdmin xxx@xxx.com

#每一个虚拟主机对应一个网站,所以都有一个根目录
DocumentRoot "/var/www/html"

#/var/www/html目录下没有index.html文件,可以读取index.php文件
DirectoryIndex index.html index.php
ServerName www.zijian.com
ErrorLog "logs/dummy-host.example.com-error_log"
CustomLog "logs/dummy-host.example.com-access_log" common
</VirtualHost>

<VirtualHost 10.10.54.151:80>
ServerAdmin xxx@xxx.com
DocumentRoot "/var/www/bbs"
DirectoryIndex index.html index.php
ServerName bbs.zijian.com
ErrorLog "logs/dummy-host2.example.com-error_log"
CustomLog "logs/dummy-host2.example.com-access_log" common
</VirtualHost>

#上面端口可以使用同一个端口80,apache会在80端口监听客户端的连接请求,并根据客户端IP转到相应的DocumentRoot目录
#可以使用不同的端口,这时需要在主配置文件中添加
-----------
Listen 10080
Listen 10081
-----------
#虚拟主机配置文件中也做相应改动
-------------------------------
<VirtualHost 10.10.54.159:10080>
...
<VirtualHost 10.10.54.152:10081>
-------------------------------


【基于域名的虚拟主机】3.虚拟主机配置文件/usr/local/apache2/conf/extra/httpd-vhosts.conf

#基于域名的虚拟主机,有多个域名解析到一台web服务器上,这些域名都对应同一个IP
#像下面三个网站,解析到一个IP,每一个网站都有一个根目录,在下面的DocumentRoot中设置
www.zijian.com:10.10.54.157
bbs.zijian.com:10.10.54.157
news.zijian.com:10.10.54.157

#只需要在上面的配置文件中修改此部分即可
shell> vim /usr/local/apache2/conf/extra/httpd-vhosts.conf
<VirtualHost *:80>
ServerAdmin xxx@xxx.com
DocumentRoot "/var/www/html"
DirectoryIndex index.html index.php
ServerName www.zijian.com
ErrorLog "logs/www-zijian-error_log"
CustomLog "logs/www-zijian-access_log" common
</VirtualHost>

<VirtualHost *80>
ServerAdmin xxx@xxx.com
DocumentRoot "/var/www/bbs"
DirectoryIndex index.html index.php
ServerName bbs.zijian.com
ErrorLog "logs/bbs-zijian-error_log"
CustomLog "logs/bbs-zijian-access_log" common
</VirtualHost>

<VirtualHost *80>
ServerAdmin xxx@xxx.com
DocumentRoot "/var/www/news"
DirectoryIndex index.html index.php
ServerName news.zijian.com
ErrorLog "logs/news-zijian-error_log"
CustomLog "logs/news-zijian-access_log" common
</VirtualHost>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: