本文共 18798 字,大约阅读时间需要 62 分钟。
Httpd常见配置 (注:httpd -t 检查语法专用)
首先创建一个网页:[root@centos7 ~]#cd /var/www/html[root@centos7 html]#lsindex.html[root@centos7 html]#vim test1.html<h1>www.magedu.com</h1>[root@centos7 html]#curl <h1>www.magedu.com</h1>[root@centos7 html]#curl -I HTTP/1.1 200 OKDate: Thu, 28 Feb 2019 02:37:18 GMTServer: Apache/2.4.6 (CentOS)Last-Modified: Thu, 28 Feb 2019 02:31:27 GMTETag: "18-582eb16574d0e"Accept-Ranges: bytesContent-Length: 24Content-Type: text/html; charset=UTF-81、显示服务器版本信息[root@centos7 html]#vim /etc/httpd/conf/httpd.conf servertokens prod 添加在文件底,是为了安全,不暴露版本信息[root@centos7 html]#systemctl reload httpd[root@centos7 html]#curl -I HTTP/1.1 200 OKDate: Thu, 28 Feb 2019 02:46:14 GMTServer: ApacheLast-Modified: Thu, 28 Feb 2019 02:31:27 GMTETag: "18-582eb16574d0e"Accept-Ranges: bytesContent-Length: 24Content-Type: text/html; charset=UTF-8[root@centos7 html]#curl -I HTTP/1.1 200 OKDate: Thu, 28 Feb 2019 02:46:32 GMTServer: ApacheLast-Modified: Thu, 28 Feb 2019 02:31:27 GMTETag: "18-582eb16574d0e"Accept-Ranges: bytesContent-Length: 24Content-Type: text/html; charset=UTF-8 访问本机的2个IP均可以。2、修改监听的IP和PortListen [IP:]PORT (1) 省略IP表示为本机所有IP (2) Listen指令至少一个,可重复出现多次 Listen 80 Listen 8080[root@centos7 html]#vim /etc/httpd/conf/httpd.confListen 192.168.141.200:80 只指定该IP可以访问。[root@centos7 html]#systemctl reload httpd[root@centos7 html]#ss -ntlState Recv-Q Send-Q Local Address:Port Peer Address:Port LISTEN 0 50 :3306 : LISTEN 0 128 :111 : LISTEN 0 128 :6000 : LISTEN 0 5 192.168.122.1:53 : LISTEN 0 128 :22 : LISTEN 0 128 127.0.0.1:631 : LISTEN 0 100 127.0.0.1:25 : [root@centos7 html]#systemctl restart httpd 只有重启才生效。[root@centos7 html]#ss -ntlState Recv-Q Send-Q Local Address:Port Peer Address:Port LISTEN 0 50 :3306 : LISTEN 0 128 :111 : LISTEN 0 128 192.168.141.200:80 : [root@centos7 html]#vim /etc/httpd/conf/httpd.confListen 192.168.141.200:8080Listen 127.0.0.1:80 (注:listen命令不可注释掉)[root@centos7 html]#systemctl restart httpd[root@centos7 html]#ss -ntlState Recv-Q Send-Q Local Address:Port Peer Address:Port LISTEN 0 50 :3306 : LISTEN 0 128 :111 : LISTEN 0 128 127.0.0.1:80 : LISTEN 0 128 192.168.141.200:8080 : [root@centos7 html]#curl <h1>www.magedu.com</h1>3、持久连接设置:KeepAlive On|Off KeepAliveTimeout 15测试:telnet WEB_SERVER_IP PORT GET /URL HTTP/1.1 Host: WEB_SERVER_IP4、动态/静态模块设置[root@centos7 html]#cd /etc/httpd/conf.modules.d/[root@centos7 conf.modules.d]#ls00-base.conf 00-dav.conf 00-lua.conf 00-mpm.conf 00-proxy.conf 00-systemd.conf 01-cgi.conf[root@centos7 httpd]#httpd -l 静态模块Compiled in modules:core.cmod_so.chttp_core.c[root@centos7 ~]#cd /var/www/html[root@centos7 html]#lsindex.html test1.html[root@centos7 html]#mkdir /data/www[root@centos7 html]#cd /data/www[root@centos7 www]#echo /data/www/index.html > index.html[root@centos7 ~]#vim /etc/httpd/conf/httpd.conf<Directory "/data/www">AllowOverride NoneRequire all granted
</Directory>
[root@centos6 ~]#curl <h1>/data/www/index.html</h1>[root@centos7 www]#mkdir news/[root@centos7 www]#echo /data/www/news/index.html > news/index.html[root@centos7 www]#tree.├── index.html└── news└── index.html1 directory, 2 files[root@centos6 ~]#curl [root@centos6 ~]#vim /etc/hosts192.168.141.200 www.a.com www.b.com www.c.com[root@centos6 ~]#curl www.a.com<h1>/data/www/index.html</h1>[root@centos7 ~]#vim /etc/httpd/conf/httpd.conf<Directory "/data/www">Options indexes FollowSymLinksAllowOverride NoneRequire all granted</Directory><Directory "/data/www/news">AllowOverride none</Directory>[root@centos7 ~]#ls /var/log/httpd
access_log error_log[root@centos7 ~]#vim /etc/httpd/conf/httpd.confErrorLog: The location of the error log file. 错误日志If you do not specify an ErrorLog directive within a <VirtualHost>container, error messages relating to that virtual host will belogged here. If you do define an error logfile for a <VirtualHost>container, that host's errors will be logged there and not here.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 warnCustomLog "logs/ " common 访问日志
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined[root@centos7 ~]#vim /etc/httpd/conf/httpd.conf
LogFormat "%h %l %u %{%F %T}t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined[root@centos6 ~]#curl 192.168.141.200 <h1>/data/www/index.html</h1>12、定义路径别名 格式:Alias /URL/ "/PATH/"
[root@centos7 html]#mkdir /data/blog/[root@centos7 html]#echo /data/blog/index.html > /data/blog/index.html[root@centos7 ~]#vim /etc/httpd/conf/httpd.confIncludeOptional conf.d/*.conf<Directory /data/blog/>Require all granted</Directory>alias /bbs/ /data/blog/[root@centos7 html]#vim /etc/httpd/conf.d/auth.conf 指定用户访问
<Directory /var/www/html/admin>AuthType BasicAuthName "he is a nice boy"AuthUserFile "/etc/httpd/conf.d/httpuser”Require user huge</Directory>实验:实现用户家目录的http访问
[root@centos7 ~]#cd ~li[root@centos7 li]#cd /etc/httpd/conf.d/[root@centos7 conf.d]#vim userdir.conf[root@centos7 conf.d]#ll /home/total 0drwx------. 3 li li 78 Apr 11 2018 li[root@centos7 conf.d]#vim userdir.conf 需要在配置文件中修改的东西全在下面UserDir public_html
#<Directory "/home/*/public_html">#</Directory>
<directory /home/li/public_html>require all granted</directory>该实验步骤:1、vim /etc/httpd/conf.d/userdir.conf#UserDir disabledUserDir public_html2、<directory /home/wang/public_html>authtype basicauthname "wang home"authuserfile "/etc/httpd/conf.d/httpuser" require user tom</directory>3、mkdir /home/wang/public_html4、setfacl -m u:apache:x /home/wang/public_htm[root@centos7 conf.d]#vim /etc/httpd/conf.d/test.conf 编辑网站的状态信息:
<Location "/status">SetHandler server-status</Location>下图是详细的状态(截取部分):实验:基于IP的多个虚拟主机
mkdir /data/{a,b,c}siteecho www.a.com > /data/asite/index.htmlecho www.b.com > /data/bsite/index.htmlecho www.c.com > /data/csite/index.htmlvim /etc/httpd/conf.d/test.conf <VirtualHost 192.168.35.7:80>ServerName www.a.comDocumentRoot "/data/asite"ErrorLog "logs/a_error_log"CustomLog "logs/a_access_log" combined<directory /data/asite>require all granted</directory></VirtualHost><VirtualHost 192.168.35.8:80>ServerName www.b.comDocumentRoot "/data/bsite"ErrorLog "logs/b_error_log"CustomLog "logs/b_access_log" combined<directory /data/bsite>require all granted</directory></VirtualHost><VirtualHost 192.168.35.9:80>ServerName www.c.comDocumentRoot "/data/csite"ErrorLog "logs/c_error_log"CustomLog "logs/c_access_log" combined<directory /data/csite>require all granted</directory></VirtualHost>[root@centos6 ~]#vim /etc/hosts192.168.141.200 www.a.com192.168.141.201 www.b.com192.168.141.202 www.c.com[root@centos6 ~]#curl www.a.comwww.a.com[root@centos6 ~]#curl www.b.comwww.b.com[root@centos6 ~]#curl www.c.comwww.c.com实验:基于Port的多个虚拟主机
cat /etc/httpd/conf.d/test.conflisten 8001listen 8002listen 8003<VirtualHost :8001>ServerName www.a.comDocumentRoot "/data/asite"ErrorLog "logs/a_error_log"CustomLog "logs/a_access_log" combined<directory /data/asite>require all granted</directory></VirtualHost><VirtualHost :8002>ServerName www.b.comDocumentRoot "/data/bsite"ErrorLog "logs/b_error_log"CustomLog "logs/b_access_log" combined<directory /data/bsite>require all granted</directory></VirtualHost><VirtualHost :8003>ServerName www.c.comDocumentRoot "/data/csite"ErrorLog "logs/c_error_log"CustomLog "logs/c_access_log" combined<directory /data/csite>require all granted</directory></VirtualHost>[root@centos6 ~]#curl www.a.com:8001www.a.com[root@centos6 ~]#curl www.a.com:8002www.b.com[root@centos6 ~]#curl www.a.com:8003www.c.com实验:实现基于FQDN (full qualified domain name 完整主机名)的多虚拟主机[root@centos7 ~]# vim /etc/httpd/conf.d/test.confErrorLog "logs/a_error_log"CustomLog "logs/a_access_log" combined<directory /data/asite>require all granted</directory></VirtualHost><VirtualHost :80>ServerName www.b.comDocumentRoot "/data/bsite"ErrorLog "logs/b_error_log"CustomLog "logs/b_access_log" combined<directory /data/bsite>require all granted</directory></VirtualHost><VirtualHost :80>ServerName www.c.comDocumentRoot "/data/csite"ErrorLog "logs/c_error_log"CustomLog "logs/c_access_log" combined<directory /data/csite>require all granted</directory></VirtualHost>[root@centos6 ~]#curl www.a.comwww.a.com[root@centos6 ~]#curl www.b.comwww.b.com[root@centos6 ~]#curl www.c.comwww.c.comhttps:http over sslSSL会话的简化过程(1) 客户端发送可供选择的加密方式,并向服务器请求证书(2) 服务器端发送证书以及选定的加密方式给客户端(3) 客户端取得证书并进行证书验证如果信任给其发证书的CA(a) 验证证书来源的合法性;用CA的公钥解密证书上数字签名(b) 验证证书的内容的合法性:完整性验证(c) 检查证书的有效期限(d) 检查证书是否被吊销(e) 证书中拥有者的名字,与访问的目标主机要一致(4) 客户端生成/data/www/news/index.html临时会话密钥(对称密钥),并使用服务器端的公钥加密此数据发送给服务器,完成密钥交换(5) 服务用此密钥加密用户请求的资源,响应给客户端注意:SSL是基于IP地址实现,单IP的主机仅可以使用一个https虚拟主机实验:实现https[root@centos7 ~]#yum search ssl 搜索出需要安装的模块mod_ssl.x86_64 : SSL/TLS module for the Apache HTTP Server[root@centos7 ~]#yum install mod_ssl[root@centos7 ~]#systemctl restart httpd[root@centos7 ~]#ss -ntlLISTEN 0 128 :::80 ::: LISTEN 0 128 :::443 443代表现在可以去访问网站了 :::*http重定向https
重定向 Redirect [status] URL-path URLstatus状态:1、Permanent: 返回永久重定向状态码 301 2、Temp:返回临时重定向状态码302. 此为默认值[root@centos7 conf.d]#vim /etc/httpd/conf.d/test.confRedirect / (如果访问根目录就会跳转到百度) 效果如下: 重定向前:转载于:https://blog.51cto.com/14128387/2357269