Linux课程笔记 Apache的介绍与安装

 

1 Apache特点

功能强大、配置简单、速度快、应用广泛、性能稳定可靠,并可做代理服务器或负载均衡器

 

2 Apache应用场合

(1)使用Apache运行静态HTML页、图片(处理静态小文件能力不及Nginx)

(2)使用Apache结合PHP引擎运行PHP、Perl等程序,LAMP被称之经典组合

(3)使用Apache结合Tomcat/Resin运行JSP,JAVA等程序,成为中小企业的首选

(4)使用Apache做代理、负载均衡、rewrite规则过滤等等

 

3 安装Apache

3.1 卸载系统默认安装的Apache软件包

[root@test2 ~]# rpm -qa httpd*    #查看系统默认安装的Apache软件包

httpd-manual-2.2.22-jason.1

httpd-2.2.22-jason.1

 

#可以使用yum remove httpd*的方式删除,也可以根据依赖关系一个个文件删除

 

3.2 下载Apache软件包

[root@test2 tools]# wget http://apache.dataguru.cn/httpd/httpd-2.2.25.tar.gz 

--2013-10-19 01:03:53--  http://apache.dataguru.cn/httpd/httpd-2.2.25.tar.gz

正在解析主机 apache.dataguru.cn... 112.81.51.52

Connecting to apache.dataguru.cn|112.81.51.52|:80... 已连接。

已发出 HTTP 请求,正在等待回应... 200 OK

长度:7445734 (7.1M) [application/octet-stream]

Saving to: `httpd-2.2.25.tar.gz'

 

100%[==============================================================================================================================>] 7,445,734    221K/s   in 32s    

 

2013-10-19 01:04:29 (230 KB/s) - `httpd-2.2.25.tar.gz' saved [7445734/7445734]

 

#可以去www.apache.org官网去查找有关的软件包,然后再使用wget的方式下载。

 

3.3  解压编译

 [root@test2 tools]# tar zxf httpd-2.2.25.tar.gz

[root@test2 tools]# cd httpd-2.2.25

[root@oldboy httpd-2.2.25]# ./configure \

> --prefix=/application/apache2.2.25 \  #指定安装路径,默认安装路径是/usr/local/apache2

> --enable-deflate \      #提供对内容的压缩传输编码支持,一般html,js,cdd等内容的站点,使用此参数功能会大大提高传输速度。在生产环境中,这是apache调优的一个重要选项之一。在服务器端压缩之后传输,在客户端浏览器再解压。

> --enable-expires \  #激活允许通过配置文件控制HTTP的“Expires:”和“Cache-Control:”头内容,即对网站图片、js、css等内容。提供在客户端浏览器缓存的设置。这是apache调优的一个重要选项之一。

> --enable-headers \  #提供允许对http请求头的控制

> --enable-modules=most \

> --enable-so \       #激活apache服务的DSO支持,即在以后可以以DSO的方式编译安装共享模块,这个模块本身不能以DSO方式编译

> --with-mpm=worker \  #选择apache mpm的模式为worker模式,因worker模式原理是更多的使用线程来处理,所以可以处理更多的并发请求,而系统资源的开销小鱼基于进程的MPM perfork(默认)

> --enable-rewrite     #提供基于URL规则的重写功能。即根据已知URL地址,转换其他想要的URL地址。如前文讲解的伪静态功能就是这个模块实现的。

 

#如果在安装apache软件包时,如果忘记安装了以上模块,可以在安装后,以dso方式编译安装,具体方法见后文。

 

[root@oldboy httpd-2.2.25]#make

[root@oldboy httpd-2.2.25]#make install

#初学者一定要学会在编译安装的过程,查看记录信息,看是否有ERROR等信息

 

[root@oldboy httpd-2.2.25]#ln -s /application/apache2.2.25  /application/apache

特别注意25后面不能有”/”。另外,要删除这个软链接,则执行rm   -rf  /application/apache这个后面也不能带”/”

#这条ln命令的意义十分深远重大。这可是生产环境的经验 。

#安装时指定版本号路径是便于查看区分当前使用的apache版本、也方便以后升级。当apache软件升级带新版本号后,删除原来软链接再重新建立新的到/application/apache的软链接就好了。

#程序中如果有应用apache路径的地方,不需要做任何更改,因为升级后访问路径还是/application/apache.

 

3.4 检查Apache安装情况

[root@test2 application]# /application/apache/bin/apachectl -l

[root@test2 application]# /application/apache/bin/apachectl -M

# /application/apache/bin/apachectl使用这个命令,可以查看apache的参数

 

3.5 启用Apache

[root@test2 application]#  /application/apache/bin/apachectl start

 

3.6 验证启动情况

1.[root@test2 application]# lsof -i tcp:80  #查看80端口的使用情况

COMMAND  PID   USER   FD   TYPE DEVICE SIZE/OFF NODE NAME

httpd   6417   root    3u  IPv6 335624      0t0  TCP *:http (LISTEN)

httpd   6419 daemon    3u  IPv6 335624      0t0  TCP *:http (LISTEN)

httpd   6421 daemon    3u  IPv6 335624      0t0  TCP *:http (LISTEN)

httpd   6423 daemon    3u  IPv6 335624      0t0  TCP *:http (LISTEN)

 

2.[root@test2 application]# ps -ef | grep httpd  #查看httpd进程

root      6417     1  0 02:07 ?        00:00:00 /application/apache2.2.25/bin/httpd -k start

daemon    6418  6417  0 02:07 ?        00:00:00 /application/apache2.2.25/bin/httpd -k start

daemon    6419  6417  0 02:07 ?        00:00:00 /application/apache2.2.25/bin/httpd -k start

daemon    6421  6417  0 02:07 ?        00:00:00 /application/apache2.2.25/bin/httpd -k start

daemon    6423  6417  0 02:07 ?        00:00:00 /application/apache2.2.25/bin/httpd -k start

root      6619 10864  0 02:09 pts/4    00:00:00 grep httpd

 

3.在浏览器输入192.168.1.3(这是安装apache服务的ip地址)

 

3.7 故障排查

如果不能显出It works,请从下面原因依次排查

(1) iptables防火墙和selinux是否关闭

[root@test2 ~]# /etc/init.d/iptables status

防火墙已停

如果是生产环境,请允许80端口的访问,而不是关闭防火墙

iptables  -I INPUT -p tcp --dport 80 -j ACCEPT

 

关闭selinux

临时关闭:setenforce 0

永久关闭:/etc/selinux/config

 

(2) 通过下面命令确认httpd端口是否存在

[root@test2 ~]# netstat -tupnl |grep :80

tcp     0      0 :::80        :::*               LISTEN      6417/httpd

 

(3) 查看是否有http进程存在

[root@test2 application]# ps -ef | grep httpd

root      6417     1  0 02:07 ?        00:00:00 /application/apache2.2.25/bin/httpd -k start

daemon    6418  6417  0 02:07 ?        00:00:00 /application/apache2.2.25/bin/httpd -k start

daemon    6419  6417  0 02:07 ?        00:00:00 /application/apache2.2.25/bin/httpd -k start

daemon    6421  6417  0 02:07 ?        00:00:00 /application/apache2.2.25/bin/httpd -k start

daemon    6423  6417  0 02:07 ?        00:00:00 /application/apache2.2.25/bin/httpd -k start

root      6619 10864  0 02:09 pts/4    00:00:00 grep httpd

 

(4) 在服务器本地wget http://192.168.1.3测试

#注意2和3都不符合要求,4就不用查了

 

(5) 查看apache服务的日志

[root@test2 ~]# tail -100 /application/apache/logs/error_log

 

 

4  Apache目录结构介绍(注释过的重点掌握)

[root@test2 apache]# ls -l /application/apache/

总计 112

drwxr-xr-x  2 root root  4096 10-19 01:42 bin

drwxr-xr-x  2 root root  4096 10-19 01:43 build

drwxr-xr-x  2 root root  4096 10-19 01:42 cgi-bin

drwxr-xr-x  4 root root  4096 10-19 01:42 conf

drwxr-xr-x  3 root root  4096 10-19 01:42 error

drwxr-xr-x  2 root root  4096 06-29 04:37 htdocs

drwxr-xr-x  3 root root  4096 10-19 01:42 icons

drwxr-xr-x  2 root root  4096 10-19 01:43 include

drwxr-xr-x  3 root root  4096 10-19 01:42 lib

drwxr-xr-x  2 root root  4096 10-19 02:07 logs

drwxr-xr-x  4 root root  4096 10-19 01:43 man

drwxr-xr-x 14 root root 12288 06-29 04:38 manual

drwxr-xr-x  2 root root  4096 10-19 01:42 modules

 

4.1 bin目录介绍

[root@test2 apache]# tree bin

bin

|-- ab   #Apache HTTP性能测试工具,简单易用

|-- apachectl  #Apache的启动脚本

|-- apr-1-config

|-- apu-1-config

|-- apxs  #一个为Apache HTTP服务器编译和安装拓展模块的工具,在进行DSO方式编译模块时用到,后文在编译php时就用到此参数,--with-apxs2=/application/apache/bin/apxs

|-- checkgid

|-- dbmmanage

|-- envvars

|-- envvars-std

|-- htcacheclean   #这是情理磁盘缓冲区的命令,需要在编译时指定相关参数才能使用,一般很少使用

|-- htdbm

|-- htdigest

|-- htpasswd   #建立和更新基本认证文件,配置nagios等监控服务时会用到

|-- httpd      #为apache的程序控制命令,apachectl执行时会调用到httpd

|-- httxt2dbm

|-- logresolve

`-- rotatelogs   #apache自带的日志轮询命令,但老师习惯用cronlog代替

 

0 directories, 17 files

 

4.2 conf目录介绍

[root@test2 apache]# tree conf -L 1

conf

|-- extra   #这是额外的apache配置文件目录,会经常访问修改,httpd-vhosts.conf就在此目录

|-- httpd.conf  #apache的主配置文件,每一行的命令参数都要弄清楚

|-- magic

|-- mime.types

`-- original

 

2 directories, 3 files

 

4.3 htdocs目录介绍

#htdocs目录是编译安装时默认的站点目录,如果是yum的方式安装,就不一定是这个目录了,在主配置文件httpd.conf文件中的DocumentRoot参数设定的。

[root@test2 apache]# tree htdocs/

htdocs/

`-- index.html   #默认的首页文件,在主配置文件httpd.conf中DirectoryIndex参数设定的。

 

0 directories, 1 file

 

4.4  logs目录介绍

[root@test2 apache]# tree logs/

logs/

|-- access_log   #apache的默认访问日志

|-- cgisock.6417

|-- error_log    #apache的错误日志

`-- httpd.pid    #apache的进程文件,把主进程文件写进来,如果要杀死apache进程,只要杀死这个主进程就可以了

 

0 directories, 4 files

[root@test2 apache]# ps -ef |grep httpd |grep -v grep

root      6417     1  0 02:08 ?      00:00:00 /application/apache2.2.25/bin/httpd -k start

daemon    6418  6417  0 02:08 ?     00:00:00 /application/apache2.2.25/bin/httpd -k start

daemon    6419  6417  0 02:08 ?     00:00:00 /application/apache2.2.25/bin/httpd -k start

daemon    6421  6417  0 02:08 ?     00:00:00 /application/apache2.2.25/bin/httpd -k start

daemon    6423  6417  0 02:08 ?     00:00:00 /application/apache2.2.25/bin/httpd -k start

daemon    6754  6417  0 02:12 ?     00:00:00 /application/apache2.2.25/bin/httpd -k start

[root@test2 apache]# ps -ef |grep httpd |grep -v grep |awk '{print $2}'

6417

6418

6419

6421

6423

6754

[root@test2 apache]# cat logs/httpd.pid             

6417

 

4.5 modules目录介绍

#apache的模块目录

[root@test2 apache]# tree modules/

modules/

`-- httpd.exp

 

0 directories, 1 file

 

5 Apache主配置文件httpd.conf介绍

#ServerRoot: The top of the directory tree under which the server's configuration, error, and log files are kept.

ServerRoot  "/application/apache2.2.25" 

 

#Change this to Listen on specific IP addresses as shown below to prevent Apache from glomming onto all bound IP addresses.

#Listen 12.34.56.78:80

Listen 80

 

#If you wish httpd to run as a different user or group, you must run httpd as root initially and it will switch. 

#User/Group: The name (or #number) of the user/group to run httpd as.It is usually good practice to create a dedicated user and group for  running httpd, as with most system services.

User daemon

Group daemon

 

# ServerAdmin: Your address, where problems with the server should be e-mailed.  This address appears on some server-generated pages, such as error documents.     

e.g. admin@your-domain.com

ServerAdmin you@example.com

 

# ServerName gives the name and port that the server uses to identify itself.

# This can often be determined automatically, but we recommend you specify it explicitly to prevent problems during startup.

# If your host doesn't have a registered DNS name, enter its IP address here.

#ServerName www.example.com:80

ServerName 127.0.0.1:80  #如果使用注释的写法,在启动apache的时候,会有提示你修改这个参数,但是依然会成功启动

 

#DocumentRoot: The directory out of which you will serve your documents. By default, all requests are taken from this directory, but symbolic links and aliases may be used to point to other locations.

DocumentRoot "/application/apache2.2.25/htdocs"

 

<Directory />

Options FollowSymLinks  #允许符号链接,但是不允许在没有首页文件的情况下,浏览所有的文件夹

# AllowOverride controls what directives may be placed in .htaccess files. It can be "All", "None", or any combination of the keywords:  Options FileInfo AuthConfig Limit

AllowOverride None     #不允许被重写,即不支持在.htaccess文件重写这个目录

Order deny,allow

 Deny from all

</Directory>

 

<Directory "/application/apache2.2.25/htdocs">

  Options Indexes FollowSymLinks

  AllowOverride None

  Order allow,deny

  Allow from all

</Directory>

 

# DirectoryIndex: sets the file that Apache will serve if a directory is requested.

<IfModule dir_module>

    DirectoryIndex index.html  #可以配置多个默认首页,会优先访问前面的页面

</IfModule>

 

# The following lines prevent .htaccess and .htpasswd files from being viewed by Web clients.

<FilesMatch "^\.ht">    

    Order allow,deny

    Deny from all    #阻止客户端访问匹配以”.ht”开头的文件

    Satisfy All

</FilesMatch>

 

ErrorLog "logs/error_log"

#LogLevel: Control the number of messages logged to the error_log. Possible values include: debug, info, notice, warn, error, crit, alert, emerg.

LogLevel  warn   #日志的级别为警告级别

老师对httpd.conf的注释

[root@test2 apache]# egrep -v "^.*#|^$" conf/httpd.conf | nl

1  ServerRoot "/application/apache2.2.25"  #apache的根目录,应只能root访问

2  Listen 80  #apache监听的端口,默认为80端口,如果同时监听81端口,可以另加一行

3  <IfModule !mpm_netware_module>

 

#3~8行为apache的用户和组配置,即在apache在运行时以此用户的身份启动服务和读取文件

 

4  <IfModule !mpm_winnt_module>

5  User daemon   #apache的用户,默认为daemon,建议修改下,如”qinbf”

6  Group daemon  #apache的用户组,默认为daemon,建议修改下,如”qinbf”

7  </IfModule>

8  </IfModule>

 

9  ServerAdmin you@example.com  #系统管理员的信箱

10  DocumentRoot "/application/apache2.2.25/htdocs"  #apache的默认web站点,路径结尾不要加斜线

 

#11~16行行为对根目录的限制

 

11  <Directory />

12      Options FollowSymLinks #表示允许使用符号链接,没加的选项默认禁用

13      AllowOverride None    #表示禁止用户对目录配置文件(.htaccess进行修改重载,普通站点目录此项建议不开)

14      Order deny,allow  #以deny方式优先处理,没有明确说明拒绝的都将通过

15      Deny from all     #明确指出,拒绝所有访问

16  </Directory>

17  <Directory "/application/apache2.2.25/htdocs">

18      Options Indexes FollowSymLinks

19      AllowOverride None

20      Order allow,deny

21      Allow from all

22  </Directory>

23  <IfModule dir_module>

24      DirectoryIndex index.html  #配置默认的apache首页文件,首页文件可以有多个,每个文件用空格分开,调用时,前面优先匹配。

25  </IfModule>

26  <FilesMatch "^\.ht">  #防止.htaccess和.htpasswd等重要隐藏文件被web用户查看

27      Order allow,deny

28      Deny from all

29      Satisfy All

30  </FilesMatch>

31  ErrorLog "logs/error_log"   #错误日志路径

32  LogLevel warn

33  <IfModule log_config_module>

34      LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined

#访问日志格式

35      LogFormat "%h %l %u %t \"%r\" %>s %b" common  #普通访问日志格式

36      <IfModule logio_module>

37        LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %I %O" combinedio

38      </IfModule>

39      CustomLog "logs/access_log" common   #默认站点访问日志配置

40  </IfModule>

#41~51行为cgi的配置,没有特殊需求应考虑全部注释掉或者干脆删掉

41  <IfModule alias_module>

42      ScriptAlias /cgi-bin/ "/application/apache2.2.25/cgi-bin/"

43  </IfModule>

44  <IfModule cgid_module>

45  </IfModule>

46  <Directory "/application/apache2.2.25/cgi-bin">  #允许cgi路径访问

47      AllowOverride None

48      Options None

49      Order allow,deny

50      Allow from all

51  </Directory>

52  DefaultType text/plain  #DefaultType:定义不能确定MIME类型时服务器提供的默认MIME类型.如果服务主要包含text或HTML文档,”text/plain”是一个很好的选择

53  <IfModule mime_module>

54      TypesConfig conf/mime.types

55      AddType application/x-compress .Z 

56      AddType application/x-gzip .gz .tgz

57  </IfModule>

58  <IfModule ssl_module>

59  SSLRandomSeed startup builtin

60  SSLRandomSeed connect builtin

61  </IfModule>

 

6  apach扩展的配置文件

6.1

# Virtual hosts

#Include conf/extra/httpd-vhosts.conf

#如果取消此行前面的#号,表示开启虚拟主机的配置,即加载conf/extra/httpd-vhosts.conf配置文件

 

[root@test2 apache]# tree -L 2 conf/extra/

conf/extra/

|-- httpd-autoindex.conf

|-- httpd-dav.conf

|-- httpd-default.conf    #这个文件里配置的是apache的相关服务参数如:超时时间、保持连接时间

|-- httpd-info.conf

|-- httpd-languages.conf  #语言支持配置

|-- httpd-manual.conf

|-- httpd-mpm.conf   #服务器池管理,也就是优化apache的一个配置文件,常用的模式有worker模式和profork模式,偶人情况是profork模式。

|-- httpd-multilang-errordoc.conf

|-- httpd-ssl.conf      #提供apache SSL支持配置文件

|-- httpd-userdir.conf

`-- httpd-vhosts.conf   #虚拟主机的配置文件

 

0 directories, 11 files

 

6.2  apache虚拟主机配置文件介绍

[root@test2 extra]# egrep -v "^#|^$" httpd-vhosts.conf

NameVirtualHost *:80

#这表示使用基于名称的虚拟主机配置,这是产品环境下最常用的配置。

<VirtualHost *:80>  #定义一个虚拟主机,监听主机所有ip地址80端口上提供的http请求

    ServerAdmin webmaster@dummy-host.example.com

    DocumentRoot "/application/apache2.2.25/docs/dummy-host.example.com"

    ServerName dummy-host.example.com

#这里配置提供服务的域名,如www.baidu.com,如果是生产环境需要把www.baidu.com解析到服务器配置的外网VIP或IP上,如果是测试,也可以在本机上做hosts解析。

    ServerAlias www.dummy-host.example.com

#这是配置虚拟主机的别名,也就是可以配置多个域名访问同一个站点,但是需要apache mod_alias模块支持

    ErrorLog "logs/dummy-host.example.com-error_log"

#这里是配置apache错误的日志路径

    CustomLog "logs/dummy-host.example.com-access_log" common

#访问日志配置,在生产环境用,我们一般用combined格式代替common格式,以获得更多的格式输出。

</VirtualHost>

<VirtualHost *:80>

    ServerAdmin webmaster@dummy-host2.example.com

    DocumentRoot "/application/apache2.2.25/docs/dummy-host2.example.com"

    ServerName dummy-host2.example.com

    ErrorLog "logs/dummy-host2.example.com-error_log"

    CustomLog "logs/dummy-host2.example.com-access_log" common

</VirtualHost>

提示:在虚拟主机配置文件httpd-vhosts.conf中,我们发现了有些参数选项是和httpd.conf重复的。这里是局部(httpd-vhosts.conf)优先原则,如果局部没有配置,则使用全局配置。

7 Apache基于域名的虚拟主机实战配置

(1) 开启主配置文件httpd.conf包含httpd-vhost.conf文件的配置

[root@test2~]#cp /application/apache/conf/httpd.conf/application/apache/conf/httpd.conf.qinbf.2013.10.21 #备份

[root@test2~]# sed -i 's#\#Include conf/extra/httpd-vhosts.conf#Include conf/extra/httpd-vhosts.conf#'   /application/apache/conf/httpd.conf   #使用sed命令快速修改配置文件

[root@test2 ~]# grep "Include conf/extra/httpd-vhosts.conf" /application/apache/conf/httpd.conf                                           

Include conf/extra/httpd-vhosts.conf   #验证配置文件

(2) 修改httpd-vhost.conf文件的配置

[root@test2 extra]# egrep -v "\#|^$" httpd-vhosts.conf

NameVirtualHost *:80

<VirtualHost *:80>

    ServerAdmin 530917@868.com

    DocumentRoot "/var/blog"

    ServerName blog.qinbf.com

    ServerAlias qinbf.com

    ErrorLog "logs/blog.qinbf.com-error_log"  #错误日志路径

    CustomLog "logs/blog.qinbf.com-access_log" common  #普通访问日志路径

</VirtualHost>

(3) 检查修改过的配置文件的语法

[root@test2 extra]# ../../bin/apachectl -t

#生产环境这一步特别重要,它可以在重启前检查出配置中的语法错误,避免在重启的过程中宕机,这在正规互联网公司是无法接受的

Warning: DocumentRoot [/var/blog] does not exist

Syntax OK

[root@test2 extra]# mkdir /var/blog

[root@test2 extra]# chown daemon.daemon /var/blog

[root@test2 extra]# ll -d /var/blog/

drwxr-xr-x 2 daemon daemon 4096 10-21 23:56 /var/blog/

[root@test2 extra]# ../../bin/apachectl -t

Syntax OK

(4) 执行graceful重启apache

[root@test2 extra]# ../../bin/apachectl graceful

#这里参数时graceful,而不是restart。这个参数可以使正在访问的用户无法感知,即重启时不会中断用户的请求

(5) 增加网站站点的授权

#由于/var/blog在httpd.conf文件中没有授权,所以需要增加授权

<Directory /var/blog>

    Options FollowSymLinks

    AllowOverride None

    Order deny,allow

    Deny from all

</Directory>

 

 

8 基于IP的虚拟主机实战配置

1.增加ip

[root@test2 ~]# ifconfig eth0:20 192.168.1.20 netmask 255.255.255.0

[root@test2 ~]# ifconfig

eth0      Link encap:Ethernet  HWaddr 00:0C:29:DF:E8:87 

          inet addr:192.168.1.3  Bcast:192.168.1.255  Mask:255.255.255.0

          inet6 addr: fe80::20c:29ff:fedf:e887/64 Scope:Link

          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1

          RX packets:33305704 errors:0 dropped:0 overruns:0 frame:0

          TX packets:55719435 errors:0 dropped:0 overruns:0 carrier:0

          collisions:0 txqueuelen:1000

          RX bytes:3249594786 (3.0 GiB)  TX bytes:5437750706 (5.0 GiB)

 

eth0:20   Link encap:Ethernet  HWaddr 00:0C:29:DF:E8:87 

          inet addr:192.168.1.20  Bcast:192.168.1.255  Mask:255.255.255.0

          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1

 

lo        Link encap:Local Loopback 

          inet addr:127.0.0.1  Mask:255.0.0.0

          inet6 addr: ::1/128 Scope:Host

          UP LOOPBACK RUNNING  MTU:16436  Metric:1

          RX packets:58474 errors:0 dropped:0 overruns:0 frame:0

          TX packets:58474 errors:0 dropped:0 overruns:0 carrier:0

          collisions:0 txqueuelen:0

          RX bytes:5146512 (4.9 MiB)  TX bytes:5146512 (4.9 MiB)

2.修改配置文件

NameVirtualHost *:80

<VirtualHost  192.168.1.3:80>

    ServerAdmin 530917@868.com

    DocumentRoot "/var/blog"

    ServerName blog.qinbf.com

    ServerAlias qinbf.com

    ErrorLog "logs/blog.qinbf.com-error_log"

    CustomLog "logs/blog.qinbf.com-access_log" common

</VirtualHost>

 

<VirtualHost  192.168.1.20:80>

    ServerAdmin 530917@868.com

    DocumentRoot "/var/bbs"

    ServerName bbs.qinbf.com

    ServerAlias qinbf.com

    ErrorLog "logs/bbs.qinbf.com-error_log"

    CustomLog "logs/bbs.qinbf.com-access_log" common

</VirtualHost>

[root@test2 ~]# /application/apache/bin/apachectl -t

Syntax OK

[root@test2 ~]# /application/apache/bin/apachectl graceful

[root@test2 ~]# ps -ef |grep httpd

daemon    4501  6417  0 12:45 ?     00:00:00 /application/apache2.2.25/bin/httpd -k start

daemon    4502  6417  0 12:45 ?     00:00:00 /application/apache2.2.25/bin/httpd -k start

daemon    4504  6417  0 12:45 ?     00:00:00 /application/apache2.2.25/bin/httpd -k start

daemon    4506  6417  0 12:45 ?     00:00:00 /application/apache2.2.25/bin/httpd -k start

root      4613  4396  0 12:48 pts/2   00:00:00 grep httpd

root      6417     1  0 Oct21 ?      00:00:01 /application/apache2.2.25/bin/httpd -k start

 

3.在浏览器中分别输入192.168.1.3和192.168.1.20来访问站点的内容

 

9 基于端口的虚拟主机实战配置

1.需要在httpd.conf文件下面增加监听端口

Listen 80

Listen 8080

Listen 8081

 

2.修改httpd-vhost.conf文件,把80端口换成其他端口号

3.在浏览器输入http://www.qinbf.com:8080或者http://192.168.1.3:8080才能访问(如果是默认80端口,不需要指定是http协议)

转载于:https://www.cnblogs.com/fengze/p/6822966.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值