虚拟主机,可以在一台机器上运行多个站点,标记多个域名。如果没有标记的域名也指向这台服务器,则指向默认虚拟主机。
[root@ligenkelong ~]# vim /usr/local/apache2.4/conf/httpd.conf
# Virtual hosts
Include conf/extra/httpd-vhosts.conf 把行首#号去掉,打开虚拟主机功能
DocumentRoot "/data/wwwroot/abc.com"
ServerName abc.com
ServerAlias www.abc.com www.123.com
ErrorLog "logs/abc.com-error_log"
CustomLog "logs/abc.com-access_log" common
DocumentRoot "/data/wwwroot/111.com" 虚拟主机站点根目录
ServerName 111.com 网站的域名
ServerAlias www.example.com 网站的第二域名
AllowOverride AuthConfig
AuthName "111.com user auth"
AuthType Basic
AuthUserFile /data/.htpasswd
require valid-user
ErrorLog "logs/111.com-error_log"
CustomLog "logs/111.com-access_log" common
创建目录:
[root@ligenkelong ~]# /usr/local/apache2.4/bin/apachectl -t 验证配置文件是否出错
[root@ligenkelong ~]# /usr/local/apache2.4/bin/apachectl graceful 重新加载配置
[root@ligenkelong ~]# mkdir /data/wwwroot/abc.com/
[root@ligenkelong ~]# mkdir /data/wwwroot/111.com/
[root@ligenkelong ~]# vim /data/wwwroot/abc.com/index.php
[root@ligenkelong ~]# vim /data/wwwroot/111.com/index.php
[root@ligenkelong ~]# vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf
DocumentRoot "/data/wwwroot/111.com"
ServerName 111.com
ServerAlias www.example.com
AllowOverride AuthConfig #打开认证的开关
AuthName "111.com user auth" #自定义认证的名字,作用不大
AuthT ype Basic 认证类型 basic
AuthUserFile /data/.htpasswd 密码文件存放位置
require valid-user #需要认证的用户为全部可用用户
ErrorLog "logs/111.com-error_log"
CustomLog "logs/111.com-access_log" common
创建密码文件:
[root@ligenkelong ~]# /usr/local/apache2.4/bin/htpasswd -cm /data/.htpasswd root #第二次创建不用加-c否则文件会被清空
[root@ligenkelong ~]# /usr/local/apache2.4/bin/apachectl -t
Syntax OK
[root@ligenkelong ~]# /usr/local/apache2.4/bin/apachectl gracefu
在windows上修改域名解析
hosts文件在c\windows\system32\drivers\etc\hosts增加一行:
IP地址 www.111.com
针对文件设置用户认证:
编辑配置文件:
[root@ligenkelong ~]# vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf
[root@ligenkelong ~]# /usr/local/apache2.4/bin/apachectl -t
一个网站可能有多个域名,不同的域名跳转到同一个站点,这就是域名跳转。
例如abc.com域名跳转到www.abc.com,配置如下:
DocumentRoot "/data/wwwroot/abc.com"
ServerName abc.com
ServerAlias www.abc.com
#需要支持mod_rewrite模块
RewriteEngine on #开启rewrite模块
RewriteCond %{HTTP_HOST} !^www.123.com$ #定义rewrite的条件
RewriteRule ^/(.*)$ http://www.123.com/$1 [R=301,L] #满足上面条件,执行这条
ErrorLog "logs/abc.com-error_log"
CustomLog "logs/abc.com-access_log" common
查看httpd是否加载rewrite模块:
[root@ligenkelong ~]# /usr/local/apache2.4/bin/apachectl -M|grep -i rewrite #如果没有输出,则要配置
[root@ligenkelong ~]# vim /usr/local/apache2.4/conf/httpd.conf
LoadModule rewrite_module modules/mod_rewrite.so #把#号去掉
[root@ligenkelong ~]# /usr/local/apache2.4/bin/apachectl -M|grep -i rewrite
rewrite_module (shared)
测试:
[root@ligenkelong ~]# curl -x127.0.0.1:80 -I 123.com
HTTP/1.1 301 Moved Permanently
Date: Fri, 10 May 2019 04:55:45 GMT
Server: Apache/2.4.38 (Unix) PHP/7.1.25
Location: http://www.abc.com/
Content-Type: text/html; charset=iso-8859-1
状态码类型说明:
https://blog.csdn.net/menggx1/article/details/81071569
200:正常请求已完成
301:永久重定向
403:禁止访问。
404:找不到服务器给定的资源,文件不存在
304 Not Modified:服务端的资源与客户端上一次请求的一致,不需要重新传输,客户端使用本地缓存的即可
[root@ligenkelong extra]# vim /usr/local/apache2.4/conf/httpd.conf
Require all granted #改成denied就会出现403
访问日志记录网站的访问情况。
[root@ligenkelong ~]# vim /usr/local/apache2.4/conf/httpd.conf
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
LogFormat "%h %l %u %t \"%r\" %>s %b" common
%h:访问网站的ip
%l:远程登入名
%u:用户名
%t:时间
%r:请求的动作方式(curl -I 显示HEADE)
%s:请求的状态码 %>s为最后的状态码
%b:传输的数据大小
%{Referer}i为referer信息(请求本地地址的上一次地址就是referer
%{User-Agent}i为浏览器标识
修改主机配置:
[root@ligenkelong ~]# vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf
DocumentRoot "/data/wwwroot/abc.com"
ServerName www.abc.com
ServerAlias abc.com
#mod_rewrite mo kuai zhichi
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www.abc.com$
RewriteRule ^/(.*)$ http://www.abc.com/$1 [R=301,L]
CustomLog "logs/abc.com-access_log" combined
[root@ligenkelong ~]# /usr/local/apache2.4/bin/apachectl -t
Syntax OK
[root@ligenkelong ~]# /usr/local/apache2.4/bin/apachectl graceful
测试:
[root@ligenkelong ~]# curl -x127.0.0.1:80 -I abc.com
HTTP/1.1 301 Moved Permanently
Date: Fri, 10 May 2019 05:36:13 GMT
Server: Apache/2.4.38 (Unix) PHP/7.1.25
Location: http://www.abc.com/
Content-Type: text/html; charset=iso-8859-1
**httpd日志位置:**
[root@ligenkelong ~]# tail /usr/local/apache2.4/logs/abc.com-access_log
[root@ligenkelong ~]# vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf
DocumentRoot "/data/wwwroot/abc.com"
ServerName www.abc.com
ServerAlias abc.com
#mod_rewrite mo kuai zhichi
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www.abc.com$
RewriteRule ^/(.*)$ http://www.abc.com/$1 [R=301,L]
SetEnvIf Request_URI ".*\.gif$" image-request
SetEnvIf Request_URI ".*\.jpg$" image-request
SetEnvIf Request_URI ".*\.png$" image-request
SetEnvIf Request_URI ".*\.bmp$" image-request
SetEnvIf Request_URI ".*\.swf$" image-request
SetEnvIf Request_URI ".*\.js$" image-request
SetEnvIf Request_URI ".*\.css$" image-request
CustomLog "|/usr/local/apache2.4/bin/rotatelogs -l logs/abc.com-access_%Y%m%d.log 86400" combined env=!image-request
[root@ligenkelong ~]# /usr/local/apache2.4/bin/apachectl -t
Syntax OK
[root@ligenkelong ~]# /usr/local/apache2.4/bin/apachectl graceful
测试:
[root@ligenkelong ~]# curl -x127.0.0.1:80 123.com
301 Moved Permanently
Moved Permanently
The document has moved here.
[root@ligenkelong ~]# ls /usr/local/apache2.4/logs/
111.com-access_log abc.com-access_log error_log
111.com-error_log abc.com-error_log httpd.pid
abc.com-access_20190510.log access_log
问题:
中途因为少了个/导致httpd无法启动,httpd报错403
http://ask.apelearn.com/question/10261
状态码304表示该文件已经缓存到用户的电脑上了,再次请求它的时候不用从服务器下载,二十直接访问用户电脑里面的缓存,这样做的目的降低服务器资源的消耗,提升网站访问速度。
控制缓存过期时间:
编辑虚拟主机配置:
[root@ligenkelong ~]# vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf
DocumentRoot "/data/wwwroot/abc.com"
ServerName www.abc.com
ServerAlias abc.com
#mod_rewrite mo kuai zhichi
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www.abc.com$
RewriteRule ^/(.*)$ http://www.abc.com/$1 [R=301,L]
SetEnvIf Request_URI ".*\.gif$" image-request
SetEnvIf Request_URI ".*\.jpg$" image-request
SetEnvIf Request_URI ".*\.png$" image-request
SetEnvIf Request_URI ".*\.bmp$" image-request
SetEnvIf Request_URI ".*\.swf$" image-request
SetEnvIf Request_URI ".*\.js$" image-request
SetEnvIf Request_URI ".*\.css$" image-request
CustomLog "|/usr/local/apache2.4/bin/rotatelogs -l logs/abc.com-access_%Y%m%d.log 86400" combined env=!image-request
ExpiresActive on
ExpiresByType image/gif "access plus 1 days"
ExpiresByType image/jpeg "access plus 24 hours"
ExpiresByType image/png "access plus 24 hours"
ExpiresByType text/css "now plus 2 hours"
ExpiresByType application/x-javascript "now plus 2 hours"
ExpiresByType application/javascript "now plus 2 hours"
ExpiresByType application/x-shockwave-flash "now plus 2 hours"
ExpiresDefault "now plus 0 min"
[root@ligenkelong ~]# /usr/local/apache2.4/bin/apachectl -t
Syntax OK
[root@ligenkelong ~]# /usr/local/apache2.4/bin/apachectl graceful
检查httpd是否加载expires模块:
[root@ligenkelong ~]# /usr/local/apache2.4/bin/apachectl -M|grep -
i expires
[root@ligenkelong ~]# vim /usr/local/apache2.4/conf/httpd.conf
LoadModule expires_module modules/mod_expires.so
[root@ligenkelong ~]# /usr/local/apache2.4/bin/apachectl -t
Syntax OK
[root@ligenkelong ~]# /usr/local/apache2.4/bin/apachectl graceful
[root@ligenkelong ~]# /usr/local/apache2.4/bin/apachectl -M|grep -i expires
expires_module (shared)
测试:
[root@ligenkelong ~]# curl -x127.0.0.1:80 -I www.abc.com/index.phpHTTP/1.1 200 OK
Date: Fri, 10 May 2019 08:34:37 GMT
Server: Apache/2.4.38 (Unix) PHP/7.1.25
X-Powered-By: PHP/7.1.25
Cache-Control: max-age=0 #说明没有缓存该类型的文件
Expires: Fri, 10 May 2019 08:34:37 GMT
Content-Type: text/html; charset=UTF-8