[root@server30 ~]#yum -y install http*
Loaded plugins: langpacks, product-id, subscription-manager
This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.
base | 4.1 kB 00:00
Resolving Dependencies
……………
[root@server30 httpd]# vim /etc/httpd/conf/httpd.conf
………前面省略部分…………..
# Load config files in the "/etc/httpd/conf.d" directory, if any.
IncludeOptional conf.d/*.conf
[root@server30 httpd]# ll /etc/httpd/conf.d/
total 24
-rw-r--r--. 1 root root 2893 Mar 20 2014 autoindex.conf
-rw-r--r--. 1 root root 1511 May 19 19:29 httpd-vhosts.conf
-rw-r--r--. 1 root root 295 Mar 20 2014 manual.conf
-rw-r--r--. 1 root root 366 Mar 20 2014 README
-rw-r--r--. 1 root root 1252 Mar 20 2014 userdir.conf
//个人主页文件
-rw-r--r--. 1 root root 516 Mar 20 2014 welcome.conf
4.3.Web与DNS的关系
我们知道,访问网页是通过URL即http://www.baidu.com的方式实现的,通过域名来解析IP地址,实现网页的访问,而这个过程是需要DNS(Domain Name Server)来实现的。否则,我们访问网页只能通过IP的形式来实现,对于专业人士来说,可能不算什么,但是对于非专业人士来说,通过IP访问网页就是件很难的事了。所以,本部分内容我们主要介绍怎样通过DNS来实现域名解析IP,实现网页访问。
DNS的安装,启动及配主配置文件的设置详见【第四章:DNS】,本部分主要介绍如何配置解析文件来实现域名的解析。
假设现在服务器端server30.example.com(172.16.30.130)有网站server30.example.com(IP:172.16.30.130),www.example.com(IP:172.16.30.130),那么怎样实现解析呢?如下:
[root@server30 ~]# vim /etc/unbound/local.d/example.conf
local-zone: "example.com." static
local-data: "example.com. IN SOA ns.example.com. root 1 1D 1H 1W 1H"
local-data: "ns.example.com. IN A 172.16.30.130"
local-data: "server30.example.com. IN A 172.16.30.130"
local-data: "www.example.com. IN A 172.16.30.130"
local-data-ptr: "172.16.30.130 ns.example.com."
local-data-ptr: "172.16.30.130 server30.example.com."
local-data-ptr: "172.16.30.130 www.example.com."
配置完解析文件,可以使用unbound-check检查配置,然后重启服务
[root@freeit ~]# unbound-checkconf
unbound-checkconf: no errors in /etc/unbound/unbound.conf
[root@freeit ~]# systemctl restart unbound
[root@server30 httpd]# vim /etc/httpd/conf.d/userdir.conf
1 #
2 # UserDir: The name of the directory that is appended onto a user's home
3 # directory if a ~user request is received.
4 #
5 # The path to the end user account 'public_html' directory must be
6 # accessible to the webserver userid. This usually means that ~userid 7 # must have permissions of 711, ~userid/public_html must have permissions 8 # of 755, and documents contained therein must be world-readable.
9 # Otherwise, the client will only receive a "403 Forbidden" message.
10 #
11
12 #
13 # UserDir is disabled by default since it can confirm the presence
14 # of a username on the system (depending on home directory
15 # permissions).
16 #
17 UserDir disabled
18
19 #
20 # To enable requests to /~user/ to serve the user's public_html
21 # directory, remove the "UserDir disabled" line above, and uncomment
22 # the following line instead:
23 #
24 #UserDir public_html
25
26
27 #
28 # Control access to UserDir directories. The following is an example
29 # for a site where these directories are restricted to read-only.
30 #
31
32 AllowOverride FileInfo AuthConfig Limit Indexes
33 Options MultiViews Indexes SymLinksIfOwnerMatch IncludesNoExec
34 Require method GET POST OPTIONS
35
36
[root@server30 ~]# id user1
uid=1001(user1) gid=1001(user1) groups=1001(user1)
[root@server30 ~]# mkdir /home/user1/public_html
[root@server30 ~]# vim /home/user1/public_html/index.html Hello! My name is user1! Let's make friends ! //索引文件内容
[root@server30 ~]# cd /etc/httpd/conf.d/
[root@server30 conf.d]# cp -p /usr/share/doc/httpd-2.4.6/httpd-vhosts.conf .
cp: overwrite ‘./httpd-vhosts.conf’? y
[root@server30 conf.d]# ll
total 24
-rw-r--r--. 1 root root 2893 Mar 20 2014 autoindex.conf -rw-r--r--. 1 root root 1511 Mar 20 2014 httpd-vhosts.conf
-rw-r--r--. 1 root root 295 Mar 20 2014 manual.conf
-rw-r--r--. 1 root root 366 Mar 20 2014 README
-rw-r--r--. 1 root root 1297 May 19 20:37 userdir.conf
-rw-r--r--. 1 root root 516 Mar 20 2014 welcome.conf
打开文件查看
[root@server30 conf.d]# vim httpd-vhosts.conf
5 # If you want to maintain multiple domains/hostnames on your
6 # machine you can setup VirtualHost containers for them. Most configurations
7 # use only name-based virtual hosts so the server doesn't need to worry abou
t
8 # IP addresses. This is indicated by the asterisks in the directives below.
9 #
10 # Please see the documentation at
11 #
12 # for further details before you try to setup virtual hosts.
13 #
14 # You may use the command line option '-S' to verify your virtual host
15 # configuration.
16
17 #
18 # VirtualHost example:
19 # Almost any Apache directive may go into a VirtualHost container.
20 # The first VirtualHost section is used for all requests that do not
21 # match a ServerName or ServerAlias in any block.
22 #
23
24 ServerAdmin webmaster@dummy-host.example.com
25 DocumentRoot "@@ServerRoot@@/docs/dummy-host.example.com"
26 ServerName dummy-host.example.com
27 ServerAlias www.dummy-host.example.com
28 ErrorLog "/var/log/httpd/dummy-host.example.com-error_log"
29 CustomLog "/var/log/httpd/dummy-host.example.com-access_log" common
30
31
32
33 ServerAdmin webmaster@dummy-host2.example.com
34 DocumentRoot "@@ServerRoot@@/docs/dummy-host2.example.com"
35 ServerName dummy-host2.example.com
36 ErrorLog "/var/log/httpd/dummy-host2.example.com-error_log"
37 CustomLog "/var/log/httpd/dummy-host2.example.com-access_log" common
38
有效行为38行,我们可以复制23到28行内容设置虚拟主机,复制后直接进行修改,如下,为修改后的内容
40 ①
41 ServerAdmin root@server30.example.com ②
42 DocumentRoot "/var/www/html" ③
43 ServerName server30.example.com ④
44 ErrorLog "/var/log/httpd/server30.example.com-error_log" ⑤
45 CustomLog "/var/log/httpd/server30.example.com-access_log" common ⑥
46
47
48
49 ServerAdmin root@www.example.com
50 DocumentRoot "/var/www/virtual"
51 ServerName www.example.com
52 ErrorLog "/var/log/httpd/www.example.com-error_log"
53 CustomLog "/var/log/httpd/www.example.com-access_log" common
54
[root@server30 ~]# vim /etc/httpd/conf.d/httpd-vhosts.conf
6 # machine you can setup VirtualHost containers for them. Most configurations
8 # IP addresses. This is indicated by the asterisks in the directives below.
9 #
10 # Please see the documentation at
11 #
12 # for further details before you try to setup virtual hosts.
13 #
14 # You may use the command line option '-S' to verify your virtual host
15 # configuration.
16
17 #
18 # VirtualHost example:
19 # Almost any Apache directive may go into a VirtualHost container.
20 # The first VirtualHost section is used for all requests that do not
21 # match a ServerName or ServerAlias in any block.
22 #
23
24 ServerAdmin webmaster@dummy-host.example.com
25 DocumentRoot "@@ServerRoot@@/docs/dummy-host.example.com"
26 ServerName dummy-host.example.com
27 ServerAlias www.dummy-host.example.com
28 ErrorLog "/var/log/httpd/dummy-host.example.com-error_log"
29 CustomLog "/var/log/httpd/dummy-host.example.com-access_log" common
30
31
32
33 ServerAdmin webmaster@dummy-host2.example.com
34 DocumentRoot "@@ServerRoot@@/docs/dummy-host2.example.com"
35 ServerName dummy-host2.example.com
36 ErrorLog "/var/log/httpd/dummy-host2.example.com-error_log"
37 CustomLog "/var/log/httpd/dummy-host2.example.com-access_log" common
38
39
40
41 ServerAdmin root@server30.example.com
42 DocumentRoot "/var/www/html"
43
44 Options Indexes
45 Order deny,allow 46 deny from all 47 allow from 172.16.30.130
48
49 ServerName server30.example.com
50 ErrorLog "/var/log/httpd/server30.example.com-error_log"
51 CustomLog "/var/log/httpd/server30.example.com-access_log" common
52
53
54
55 ServerAdmin root@www.example.com
56 DocumentRoot "/var/www/virtual"
57 ServerName www.example.com
[root@server30 ~]# vim /etc/httpd/conf.d/httpd-vhosts.conf
8 # IP addresses. This is indicated by the asterisks in the directives below.
9 #
10 # Please see the documentation at
11 #
12 # for further details before you try to setup virtual hosts.
13 #
14 # You may use the command line option '-S' to verify your virtual host
15 # configuration.
16
17 #
18 # VirtualHost example:
19 # Almost any Apache directive may go into a VirtualHost container.
20 # The first VirtualHost section is used for all requests that do not
21 # match a ServerName or ServerAlias in any block.
22 #
23
24 ServerAdmin webmaster@dummy-host.example.com
25 DocumentRoot "@@ServerRoot@@/docs/dummy-host.example.com"
26 ServerName dummy-host.example.com
27 ServerAlias www.dummy-host.example.com
28 ErrorLog "/var/log/httpd/dummy-host.example.com-error_log"
29 CustomLog "/var/log/httpd/dummy-host.example.com-access_log" common
30
31
32
33 ServerAdmin webmaster@dummy-host2.example.com
34 DocumentRoot "@@ServerRoot@@/docs/dummy-host2.example.com"
35 ServerName dummy-host2.example.com
36 ErrorLog "/var/log/httpd/dummy-host2.example.com-error_log"
37 CustomLog "/var/log/httpd/dummy-host2.example.com-access_log" common
38
39
40
41 ServerAdmin root@server30.example.com
42 DocumentRoot "/var/www/html"
43
44 Options Indexes
45 Require ip 172.16.30.30
46
47 ServerName server30.example.com
48 ErrorLog "/var/log/httpd/server30.example.com-error_log"
49 CustomLog "/var/log/httpd/server30.example.com-access_log" common
50
51
52
53 ServerAdmin root@www.example.com
54 DocumentRoot "/var/www/virtual"
55 ServerName www.example.com
56 ErrorLog "/var/log/httpd/www.example.com-error_log"
57 CustomLog "/var/log/httpd/www.example.com-access_log" common
58
[root@server30 ~]# vim /etc/httpd/conf.d/httpd-vhosts.conf
# machine you can setup VirtualHost containers for them. Most configurations
# use only name-based virtual hosts so the server doesn't need to worry about
# IP addresses. This is indicated by the asterisks in the directives below.
#
# Please see the documentation at
#
# for further details before you try to setup virtual hosts.
#
# You may use the command line option '-S' to verify your virtual host
# configuration.
#
# VirtualHost example:
19 # Almost any Apache directive may go into a VirtualHost container.
20 # The first VirtualHost section is used for all requests that do not
21 # match a ServerName or ServerAlias in any block.
22 #
23
24 ServerAdmin webmaster@dummy-host.example.com
25 DocumentRoot "@@ServerRoot@@/docs/dummy-host.example.com"
26 ServerName dummy-host.example.com
27 ServerAlias www.dummy-host.example.com
28 ErrorLog "/var/log/httpd/dummy-host.example.com-error_log"
29 CustomLog "/var/log/httpd/dummy-host.example.com-access_log" common
30
31
32
33 ServerAdmin webmaster@dummy-host2.example.com
34 DocumentRoot "@@ServerRoot@@/docs/dummy-host2.example.com"
35 ServerName dummy-host2.example.com
36 ErrorLog "/var/log/httpd/dummy-host2.example.com-error_log"
37 CustomLog "/var/log/httpd/dummy-host2.example.com-access_log" common
38
39
40
41 ServerAdmin root@server30.example.com
42 DocumentRoot "/var/www/html"
43
44 Options Indexes
45 Require all denied
46
47 ServerName server30.example.com
48 ErrorLog "/var/log/httpd/server30.example.com-error_log"
49 CustomLog "/var/log/httpd/server30.example.com-access_log" common
50
51
52
53 ServerAdmin root@www.example.com
54 DocumentRoot "/var/www/virtual"
55 ServerName www.example.com
56 ErrorLog "/var/log/httpd/www.example.com-error_log"
57 CustomLog "/var/log/httpd/www.example.com-access_log" common
58
[root@server30 ~]# systemctl restart httpd.service
[root@server30 ~]# htpasswd -cm /etc/httpd/.htpasswd user1
New password:
Re-type new password:
Adding password for user user1
[root@server30 ~]# htpasswd -m /etc/httpd/.htpasswd user2
New password:
Re-type new password:
Adding password for user user2
[root@server30 ~]# cat /etc/httpd/.htpasswd
user1:$apr1$qKhBbCLY$RJM5cCivDnJgAtyFazd1q/
user2:$apr1$V6c62hbd$C74QZ.QtlTMsN3LwzEavm.
说明:
此处的密码与系统定义的用户密码无关;
-c为创建用户,-m为修改现有用户的密码。
修改配置文件,针对server30的/data目录进行目录身份认证
[root@server30 ~]# vim /etc/httpd/conf.d/httpd-vhosts.conf
# If you want to maintain multiple domains/hostnames on your
# machine you can setup VirtualHost containers for them. Most configurations
# use only name-based virtual hosts so the server doesn't need to worry about
# IP addresses. This is indicated by the asterisks in the directives below.
#
# Please see the documentation at
#
# for further details before you try to setup virtual hosts.
#
# You may use the command line option '-S' to verify your virtual host
# configuration.
#
# VirtualHost example:
19 # Almost any Apache directive may go into a VirtualHost container.
20 # The first VirtualHost section is used for all requests that do not
21 # match a ServerName or ServerAlias in any block.
22 #
23
24 ServerAdmin webmaster@dummy-host.example.com
25 DocumentRoot "@@ServerRoot@@/docs/dummy-host.example.com"
26 ServerName dummy-host.example.com
27 ServerAlias www.dummy-host.example.com
28 ErrorLog "/var/log/httpd/dummy-host.example.com-error_log"
29 CustomLog "/var/log/httpd/dummy-host.example.com-access_log" common
30
31
32
33 ServerAdmin webmaster@dummy-host2.example.com
34 DocumentRoot "@@ServerRoot@@/docs/dummy-host2.example.com"
35 ServerName dummy-host2.example.com
36 ErrorLog "/var/log/httpd/dummy-host2.example.com-error_log"
37 CustomLog "/var/log/httpd/dummy-host2.example.com-access_log" common
38
39
40
41 ServerAdmin root@server30.example.com
42 DocumentRoot "/var/www/html"
43
44 Options Indexes
45 AuthName Test ① 46 AuthType basic ② 47 AuthUserFile /etc/httpd/.htpasswd ③ 48 Require valid-user ④
49
50 ServerName server30.example.com
51 ErrorLog "/var/log/httpd/server30.example.com-error_log"
52 CustomLog "/var/log/httpd/server30.example.com-access_log" common
53
54
55
56 ServerAdmin root@www.example.com
57 DocumentRoot "/var/www/virtual"
58 ServerName www.example.com
59 ErrorLog "/var/log/httpd/www.example.com-error_log"
60 CustomLog "/var/log/httpd/www.example.com-access_log" common
[root@server30 ~]# yum -y install mod_ssl
Loaded plugins: langpacks, product-id, subscription-manager
This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.
base | 4.1 kB 00:00
Resolving Dependencies
………………..
配置/etc/httpd/conf.d/ssl.conf文件
[root@server30 ~]# cd /etc/httpd/conf.d/
[root@server30 conf.d]# ll
total 36
-rw-r--r--. 1 root root 2893 Mar 20 2014 autoindex.conf
-rw-r--r--. 1 root root 2251 May 20 10:36 httpd-vhosts.conf
-rw-r--r--. 1 root root 295 Mar 20 2014 manual.conf
-rw-r--r--. 1 root root 366 Mar 20 2014 README -rw-r--r--. 1 root root 9426 Mar 20 2014 ssl.conf
-rw-r--r--. 1 root root 1297 May 19 20:37 userdir.conf
-rw-r--r--. 1 root root 516 Mar 20 2014 welcome.conf
[root@server30 conf.d]# vim ssl.conf
……………………….. 100 SSLCertificateFile /etc/pki/tls/certs/localhost.crt
101
102 # Server Private Key:
103 # If the key is not combined with the certificate, use this
104 # directive to point at the key file. Keep in mind that if
105 # you've both a RSA and a DSA private key you can configure
106 # both in parallel (to also allow the use of DSA ciphers, etc.) 107 SSLCertificateKeyFile /etc/pki/tls/private/localhost.key
108
109 # Server Certificate Chain:
110 # Point SSLCertificateChainFile at a file containing the
111 # concatenation of PEM encoded CA certificates which form the
112 # certificate chain for the server certificate. Alternatively
113 # the referenced file can be the same as SSLCertificateFile
114 # when the CA certificates are directly appended to the server
115 # certificate for convinience.
116 #SSLCertificateChainFile /etc/pki/tls/certs/server-chain.crt
117
118 # Certificate Authority (CA):
119 # Set the CA certificate verification path where to find CA
120 # certificates for client authentication or alternatively one
121 # huge file containing all of them (file must be PEM encoded) 122 #SSLCACertificateFile /etc/pki/tls/certs/ca-bundle.crt
……….
[root@server30 conf.d]# vim ssl.conf
………………………..
100 #SSLCertificateFile /etc/pki/tls/certs/localhost.crt 101 SSLCertificateFile /etc/pki/tls/certs/server30.crt
102
103 # Server Private Key:
104 # If the key is not combined with the certificate, use this
105 # directive to point at the key file. Keep in mind that if
106 # you've both a RSA and a DSA private key you can configure
107 # both in parallel (to also allow the use of DSA ciphers, etc.)
108 #SSLCertificateKeyFile /etc/pki/tls/private/localhost.key 109 SSLCertificateKeyFile /etc/pki/tls/private/server30.key
110
111 # Server Certificate Chain:
112 # Point SSLCertificateChainFile at a file containing the
113 # concatenation of PEM encoded CA certificates which form the
114 # certificate chain for the server certificate. Alternatively
115 # the referenced file can be the same as SSLCertificateFile
116 # when the CA certificates are directly appended to the server
117 # certificate for convinience.
118 SSLCertificateChainFile /etc/pki/tls/certs/server-chain.crt
119
120 # Certificate Authority (CA):
121 # Set the CA certificate verification path where to find CA
122 # certificates for client authentication or alternatively one
123 # huge file containing all of them (file must be PEM encoded)
124 #SSLCACertificateFile /etc/pki/tls/certs/ca-bundle.crt 125 SSLCACertificateFile /etc/pki/tls/certs/group30.crt
……………
下载所需证书信息文件到指定路径
-----------------下载server30.crt和group30.crt-------------------
[root@server30 conf.d]# cd /etc/pki/tls/certs/
[root@server30 certs]# wget http://ldap.example.com/pub/server30.crt
--2015-05-20 11:03:29-- http://ldap.example.com/pub/server30.crt
Resolving ldap.example.com (ldap.example.com)... 172.16.30.254
Connecting to ldap.example.com (ldap.example.com)|172.16.30.254|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 3147 (3.1K)
Saving to: ‘server30.crt’
100%[======================================>] 3,147 --.-K/s in 0s
2015-05-20 11:03:29 (284 MB/s) - ‘server30.crt’ saved [3147/3147]
[root@server30 certs]# wget http://ldap.example.com/pub/group30.crt
--2015-05-20 11:05:23-- http://ldap.example.com/pub/group30.crt
Resolving ldap.example.com (ldap.example.com)... 172.16.30.254
Connecting to ldap.example.com (ldap.example.com)|172.16.30.254|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 3256 (3.2K)
Saving to: ‘group30.crt’
100%[======================================>] 3,256 --.-K/s in 0s
2015-05-20 11:05:23 (209 MB/s) - ‘group30.crt’ saved [3256/3256]
[root@server30 certs]# ls
ca-bundle.crt group30.crt make-dummy-cert renew-dummy-cert
ca-bundle.trust.crt localhost.crt Makefile server30.crt
------------------------下载server30.key--------------------
[root@server30 certs]# cd /etc/pki/tls/private/
[root@server30 private]# wget http://ldap.example.com/pub/server30.key
--2015-05-20 11:07:23-- http://ldap.example.com/pub/server30.key
Resolving ldap.example.com (ldap.example.com)... 172.16.30.254
Connecting to ldap.example.com (ldap.example.com)|172.16.30.254|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 887
Saving to: ‘server30.key’
100%[======================================>] 887 --.-K/s in 0s
2015-05-20 11:07:23 (120 MB/s) - ‘server30.key’ saved [887/887]
[root@server30 private]# ls
localhost.key server30.key
[root@server30 wsgi]# yum -y install mod_wsgi
Loaded plugins: langpacks, product-id, subscription-manager
This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.
Resolving Dependencies
………………….
创建虚拟主机的DocumentRoot目录并下载索引文件
[root@server30 ~]# mkdir /var/www/wsgi
[root@server30 ~]# cd /var/www/wsgi
[root@server30 wsgi]# wget http://ldap.example.com/pub/webapp.wsgi
--2015-05-20 13:09:33-- http://ldap.example.com/pub/webapp.wsgi
Resolving ldap.example.com (ldap.example.com)... 172.16.30.254
Connecting to ldap.example.com (ldap.example.com)|172.16.30.254|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 277
Saving to: ‘webapp.wsgi’
100%[=========================================>] 277 --.-K/s in 0s
2015-05-20 13:09:33 (23.0 MB/s) - ‘webapp.wsgi’ saved [277/277]
[root@server30 wsgi]# ls webapp.wsgi
修改配置文件
[root@server30 ~]# vim /etc/httpd/conf.d/httpd-vhosts.conf
…………………
Listen 890