share-image
ESC

Apache 服务

http 协议

http 协议 (超文本传输协议)
略……

安装

[root@server ~]# yum -y install httpd
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
Package httpd-2.4.6-45.el7.centos.x86_64 already installed and latest version
Nothing to do
[root@server ~]# systemctl enable httpd
Created symlink from /etc/systemd/system/multi-user.target.wants/httpd.service to /usr/lib/systemd/system/httpd.service.
[root@server ~]# systemctl start httpd

浏览器访问

命令行访问

[root@desktop smb1]# curl -I http://172.10.100.128 -v
* About to connect() to 172.10.100.128 port 80 (#0)
* Trying 172.10.100.128...
* Connected to 172.10.100.128 (172.10.100.128) port 80 (#0)
> HEAD / HTTP/1.1
> User-Agent: curl/7.29.0
> Host: 172.10.100.128
> Accept: */*
>
< HTTP/1.1 403 Forbidden
HTTP/1.1 403 Forbidden
< Date: Sun, 07 May 2017 06:10:27 GMT
Date: Sun, 07 May 2017 06:10:27 GMT
< Server: Apache/2.4.6 (CentOS) OpenSSL/1.0.1e-fips mod_fcgid/2.3.9
Server: Apache/2.4.6 (CentOS) OpenSSL/1.0.1e-fips mod_fcgid/2.3.9
< Last-Modified: Thu, 16 Oct 2014 13:20:58 GMT
Last-Modified: Thu, 16 Oct 2014 13:20:58 GMT
< ETag: "1321-5058a1e728280"
ETag: "1321-5058a1e728280"
< Accept-Ranges: bytes
Accept-Ranges: bytes
< Content-Length: 4897
Content-Length: 4897
< Content-Type: text/html; charset=UTF-8
Content-Type: text/html; charset=UTF-8

<
* Connection #0 to host 172.10.100.128 left intact

工作目录

  • /etc/httpd/ 配置文件,主程序目录
  • /var/www/html/ web 主目录
  • httpd 脚本程序
  • /var/log

在主目录添加一个文件内容为 hello world

[root@server html]# ls
index.html
[root@server html]# cat index.html
hello world
[root@server html]# pwd
/var/www/html
[root@server html]#

内容

<html>
<head>
<title>This is test page</title>
</head>
<body>
<h1>hello world! </h1>
</body>
</html>

主配置文件

[root@server html]# cat /etc/httpd/conf/httpd.conf

ServerRoot "/etc/httpd"

Listen 80 #监听端口

Include conf.modules.d/*.conf #导入外部的模块配置文件

User apache #运行用户
Group apache #运行组


ServerAdmin root@localhost #管理员邮箱
ServerName www.example.com:80 #域名

<Directory />
AllowOverride none
Require all denied
</Directory>


DocumentRoot "/var/www/html" #web 文件主目录

<Directory "/var/www">
AllowOverride None
# Allow open access:
Require all granted
</Directory>

<Directory "/var/www/html">
#
# Possible values for the Options directive are "None", "All",
# or any combination of:
# Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews
#
# Note that "MultiViews" must be named *explicitly* --- "Options All"
# doesn't give it to you.
#
# The Options directive is both complicated and important. Please see
# http://httpd.apache.org/docs/2.4/mod/core.html#options
# for more information.
#
Options Indexes 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

#
# Controls who can get stuff from this server.
#
Require all granted
</Directory>

<IfModule dir_module>
DirectoryIndex index.html
</IfModule>

<Files ".ht*">
Require all denied
</Files>

ErrorLog "logs/error_log"

LogLevel warn

<IfModule log_config_module>
#
# The following directives define some format nicknames for use with
# a CustomLog directive (see below).
#
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
LogFormat "%h %l %u %t \"%r\" %>s %b" common

<IfModule logio_module>
# You need to enable mod_logio.c to use %I and %O
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %I %O" combinedio
</IfModule>

#
# The location and format of the access logfile (Common Logfile Format).
# If you do not define any access logfiles within a <VirtualHost>
# container, they will be logged here. Contrariwise, if you *do*
# define per-<VirtualHost> access logfiles, transactions will be
# logged therein and *not* in this file.
#
#CustomLog "logs/access_log" common

#
# If you prefer a logfile with access, agent, and referer information
# (Combined Logfile Format) you can use the following directive.
#
CustomLog "logs/access_log" combined
</IfModule>

<IfModule alias_module>
#
# Redirect: Allows you to tell clients about documents that used to
# exist in your server's namespace, but do not anymore. The client
# will make a new request for the document at its new location.
# Example:
# Redirect permanent /foo http://www.example.com/bar

#
# Alias: Maps web paths into filesystem paths and is used to
# access content that does not live under the DocumentRoot.
# Example:
# Alias /webpath /full/filesystem/path
#
# If you include a trailing / on /webpath then the server will
# require it to be present in the URL. You will also likely
# need to provide a <Directory> section to allow access to
# the filesystem path.

#
# ScriptAlias: This controls which directories contain server scripts.
# ScriptAliases are essentially the same as Aliases, except that
# documents in the target directory are treated as applications and
# run by the server when requested rather than as documents sent to the
# client. The same rules about trailing "/" apply to ScriptAlias
# directives as to Alias.
#
ScriptAlias /cgi-bin/ "/var/www/cgi-bin/"

</IfModule>

<Directory "/var/www/cgi-bin">
AllowOverride None
Options None
Require all granted
</Directory>

<IfModule mime_module>
#
# TypesConfig points to the file containing the list of mappings from
# filename extension to MIME-type.
#
TypesConfig /etc/mime.types

#
# AddType allows you to add to or override the MIME configuration
# file specified in TypesConfig for specific file types.
#
#AddType application/x-gzip .tgz
#
# AddEncoding allows you to have certain browsers uncompress
# information on the fly. Note: Not all browsers support this.
#
#AddEncoding x-compress .Z
#AddEncoding x-gzip .gz .tgz
#
# If the AddEncoding directives above are commented-out, then you
# probably should define those extensions to indicate media types:
#
AddType application/x-compress .Z
AddType application/x-gzip .gz .tgz

#
# AddHandler allows you to map certain file extensions to "handlers":
# actions unrelated to filetype. These can be either built into the server
# or added with the Action directive (see below)
#
# To use CGI scripts outside of ScriptAliased directories:
# (You will also need to add "ExecCGI" to the "Options" directive.)
#
#AddHandler cgi-script .cgi

# For type maps (negotiated resources):
#AddHandler type-map var

#
# Filters allow you to process content before it is sent to the client.
#
# To parse .shtml files for server-side includes (SSI):
# (You will also need to add "Includes" to the "Options" directive.)
#
AddType text/html .shtml
AddOutputFilter INCLUDES .shtml
</IfModule>

AddDefaultCharset UTF-8 #默认编码格式是 UFT-8

<IfModule mime_magic_module>
#
# The mod_mime_magic module allows the server to use various hints from the
# contents of the file itself to determine its type. The MIMEMagicFile
# directive tells the module where the hint definitions are located.
#
MIMEMagicFile conf/magic
</IfModule>


EnableSendfile on
IncludeOptional conf.d/*.conf #导入外部的配置文件

日志


[root@server html]# tail -f /var/log/httpd/access_log
172.10.100.1 - - [07/May/2017:02:18:21 -0400] "GET /noindex/css/fonts/Light/OpenSans-Light.woff HTTP/1.1" 404 241 "http://172.10.100.128/noindex/css/open-sans.css" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.81 Safari/537.36"
172.10.100.1 - - [07/May/2017:02:18:21 -0400] "GET /noindex/css/fonts/Bold/OpenSans-Bold.woff HTTP/1.1" 404 239 "http://172.10.100.128/noindex/css/open-sans.css" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.81 Safari/537.36"
172.10.100.1 - - [07/May/2017:02:18:21 -0400] "GET /noindex/css/fonts/Light/OpenSans-Light.ttf HTTP/1.1" 404 240 "http://172.10.100.128/noindex/css/open-sans.css" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.81 Safari/537.36"
172.10.100.1 - - [07/May/2017:02:18:21 -0400] "GET /noindex/css/fonts/Bold/OpenSans-Bold.ttf HTTP/1.1" 404 238 "http://172.10.100.128/noindex/css/open-sans.css" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.81 Safari/537.36"
172.10.100.1 - - [07/May/2017:02:18:21 -0400] "GET /favicon.ico HTTP/1.1" 404 209 "http://172.10.100.128/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.81 Safari/537.36"
172.10.100.1 - - [07/May/2017:02:19:12 -0400] "-" 408 - "-" "-"
172.10.100.1 - - [07/May/2017:02:19:12 -0400] "-" 408 - "-" "-"
172.10.100.1 - - [07/May/2017:02:19:12 -0400] "-" 408 - "-" "-"
172.10.100.1 - - [07/May/2017:02:22:08 -0400] "GET / HTTP/1.1" 200 12 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.81 Safari/537.36"
172.10.100.1 - - [07/May/2017:02:22:08 -0400] "GET /favicon.ico HTTP/1.1" 404 209 "http://172.10.100.128/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.81 Safari/537.36"

格式通常在配置文件中定义

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

以这条日志分析

172.10.100.1 - - [07/May/2017:02:22:08 -0400] "GET /favicon.ico HTTP/1.1" 404 209 "http://172.10.100.128/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.81 Safari/537.36"

从左到右分别是 [请求的 ip 地址] – [请求的时间] [http 的方法] [请求的 uri] [http 的协议] [请求的状态码] [请求的文件大小] [请求的url] [user-agent]

基于主机名的虚拟主机

[root@server html]# cp /usr/share/doc/httpd-2.4.6/httpd-vhosts.conf /etc/httpd/conf.d/01-www.baidu.com.conf
[root@server html]# cp /usr/share/doc/httpd-2.4.6/httpd-vhosts.conf /etc/httpd/conf.d/00-www.google.com.conf

内容

    <VirtualHost *:@@Port@@>
ServerAdmin webmaster@dummy-host.example.com
DocumentRoot "@@ServerRoot@@/docs/dummy-host.example.com"
ServerName dummy-host.example.com
ServerAlias www.dummy-host.example.com
ErrorLog "/var/log/httpd/dummy-host.example.com-error_log"
CustomLog "/var/log/httpd/dummy-host.example.com-access_log" common
</VirtualHost>

<VirtualHost *:@@Port@@>
ServerAdmin webmaster@dummy-host2.example.com
DocumentRoot "@@ServerRoot@@/docs/dummy-host2.example.com"
ServerName dummy-host2.example.com
ErrorLog "/var/log/httpd/dummy-host2.example.com-error_log"
CustomLog "/var/log/httpd/dummy-host2.example.com-access_log" common
</VirtualHost>


2.创建主机目录

   [root@server html]# mkdir -p /var/www/baidu
[root@server html]# mkdir -p /var/www/google
[root@server html]# echo baidu >> /var/www//baidu/index.html
[root@server html]# echo google >> /var/www/google/index.html

3.修改 Google 的配置文件

vim /etc/httpd/conf.d/00-www.google.com.conf

修改内容如下

<VirtualHost *:80>
#ServerAdmin webmaster@dummy-host.example.com
DocumentRoot "/var/www/google/"
ServerName www.google.com
ServerAlias www.dummy-host.example.com
ErrorLog "/var/log/httpd/google.com-error_log"
CustomLog "/var/log/httpd/google.com-access_log" common
<Directory "/var/www/google">
Require all granted
</Directory>
</VirtualHost>

4.修改 baidu 的配置文件

vim /etc/httpd/conf.d/00-www.baidu.com.conf

修改如下

<VirtualHost *:80>
#ServerAdmin webmaster@dummy-host.example.com
DocumentRoot "/var/www/baidu/"
ServerName www.baidu.com
#ServerAlias www.dummy-host.example.com
ErrorLog "/var/log/httpd/baidu.com-error_log"
CustomLog "/var/log/httpd/baidu.com-access_log" common
<Directory "/var/www/baidu">
Require all granted
</Directory>
</VirtualHost>

5.重启 httpd

[root@server html]# systemctl restart httpd

重启 httpd

6.客户端访问

[root@desktop httpd]# vim /etc/hosts
[root@desktop httpd]# cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
172.10.100.128 www.baidu.com
172.10.100.128 www.google.comc

7.访问

[root@desktop ~]# curl -X GET http://www.baidu.com
baidu
[root@desktop ~]# curl -X GET http://www.google.com
google

基于端口的虚拟主机

其实基于端口就是在 /etc/httpd/conf/httpd.conf 增加一个监听端口

[root@server html]# cat  /etc/httpd/conf/httpd.conf | grep ^Listen
Listen 80
Listen 8080

然后比如我要把 Google 的访问端口改为8080,就是将原来的端口80 修改为8080

[root@server html]# cat /etc/httpd/conf.d/00-www.google.com.conf | grep 8080
<VirtualHost *:8080>

客户端访问

1.先测下端口通不通

[root@desktop ~]# telnet 172.10.100.128 8080
Trying 172.10.100.128...
Connected to 172.10.100.128.
Escape character is '^]'.
^C
Connection closed by foreign host.

2.然后访问

[root@desktop ~]#
[root@desktop ~]# curl -X GET http://www.google.com:8080 -v
* About to connect() to www.google.com port 8080 (#0)
* Trying 172.10.100.128...
* Connected to www.google.com (172.10.100.128) port 8080 (#0)
> GET / HTTP/1.1
> User-Agent: curl/7.29.0
> Host: www.google.com:8080
> Accept: */*
>
< HTTP/1.1 200 OK
< Date: Sun, 07 May 2017 07:20:52 GMT
< Server: Apache/2.4.6 (CentOS) OpenSSL/1.0.1e-fips mod_fcgid/2.3.9
< Last-Modified: Sun, 07 May 2017 07:00:15 GMT
< ETag: "7-54ee9acb5b2f4"
< Accept-Ranges: bytes
< Content-Length: 7
< Content-Type: text/html; charset=UTF-8
<
google
* Connection #0 to host www.google.com left intact
[root@desktop ~]#

文章作者:阿文
文章链接: https://www.awen.me/post/1424908464.html
版权声明:本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 阿文的博客
本文于 2017-06-14 发布,已超过半年(3151天),请注意甄别内容是否已过期。