一、源码包安装
将依赖包传到虚拟机
rz -E
#安装基础依赖
$ yum -y install openssl openssl-devel zlib zlib-devel pcre pcre-devel
#解压源码包并解决依赖关系
$ tar -zxf httpd-2.4.7.tar.gz
$ tar -zxf apr-1.4.6.tar.gz
$ tar -zxf apr-util-1.4.1.tar.gz
#解决依赖关系
$ cp -r apr-1.4.6 httpd-2.4.7/srclib/apr
$ cp -r apr-util-1.4.1 httpd-2.4.7/srclib/apr-util
#编译、安装,每步执行完用echo $?检测
#--prefix:指定安装路径, enable-rewrite:开启地址重写, enable-so:开启dso(动态共享对象), enable-headers:允许修改http的请求头, enable-expires:允许客户端缓存, enable-modules=most:尽可能安装更多模块, enable-deflate:开启对压缩的支持, enable-ssl:开启https的支持
$ cd httpd-2.4.7
$ ./configure --prefix=/usr/local/apache2 --enable-rewrite --enable-so --enable-headers --enable-expires --enable-modules=most --enable-deflate --enable-ssl
$ make && make install
#检测配置文件、启停服务
$ /usr/local/apache2/bin/apachectl -t
$ /usr/local/apache2/bin/apachectl -k stop
$ /usr/local/apache2/bin/apachectl -k start
#添加或修改网页
vim /usr/local/apache2/htdocs/index.html
hello~
————————————————————————————————————————————————————————
客户端
#客户端界面访问测试:
http://服务器IP/index.html
二、用户认证功能搭建
#指定目录下创建权限文件,本例子是加在网页根目录,访问任何页面都要求验证用户
$ cd /usr/local/apache2/htdocs
$ vim .htaccess
AuthName "Welcome to kernel"
#提示信息
AuthType basic
#加密类型
AuthUserFile /usr/local/apache2/htdocs/apache.passwd
#密码文件,文件名自定义。(使用绝对路径)
require valid-user
#允许密码文件中所有用户访问
#创建指定的密码文件、添加允许访问的用户(与系统用户无关)
#注:-c创建密码文件和添加第一个用户、-m添加更多用户;htpasswd是httpd的命令、需要使用绝对路径
$ /usr/local/apache2/bin/htpasswd -c /usr/local/apache2/htdocs/apache.passwd username1
$ /usr/local/apache2/bin/htpasswd -m /usr/local/apache2/htdocs/apache.passwd username2
#编辑配置文件,在需要登录认证的目录标签中添加:
$ vim /usr/local/apache2/conf/httpd.conf
<Directory "/usr/local/apache2/htdocs">
#声明被保护目录,没有开启虚拟主机时默认是htdocs目录即可,只要在对应的权限控制目录下创建.htaccess文件即可
Options Indexes FollowSymLinks
AllowOverride All #开启权限认证文件.htaccess
Require all granted
</Directory>
#重启服务、验证(访问界面后输入用户名和密码才能看到内容)
$ /usr/local/apache2/bin/apachectl -t
$ /usr/local/apache2/bin/apachectl stop
$ /usr/local/apache2/bin/apachectl start
------------------
实验:添加在网页目录下的子目录下,则界面只访问该目录下页面时需要验证用户;其他页面可以直接访问
三、虚拟主机功能搭建(基于域名)
#主配置文件开启虚拟主机模块的关联配置文件
$ vim /usr/local/apache2/conf/httpd.conf
Include conf/extra/httpd-vhosts.conf #取消注释
#编辑虚拟主机配置文件、添加相应的标签
$ vim /usr/local/apache2/conf/extra/httpd-vhosts.conf
#有几个虚拟机主机添加几组下面的标签
#配置sina虚拟主机
<VirtualHost *:80> #虚拟主机标签(ip、端口)
ServerAdmin webmaster@sina.com #管理员邮箱
DocumentRoot "/usr/local/apache2/htdocs/sina" #网站主目录
ServerName www.sina.com #完整域名
ErrorLog "logs/sina-error_log" #错误日志
CustomLog "logs/sina-access_log" common #访问日志
</VirtualHost>
<Directory "/usr/local/apache2/htdocs/sina">
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
#配置souhu虚拟主机
<VirtualHost *:80> #虚拟主机标签(ip、端口)
ServerAdmin webmaster@souhu.com #管理员邮箱
DocumentRoot "/usr/local/apache2/htdocs/souhu"#网站主目录
ServerName www.souhu.com #完整域名
ErrorLog "logs/sina-error_log" #错误日志
CustomLog "logs/sina-access_log" common #访问日志
</VirtualHost>
#重启服务、验证(访问界面后输入用户名和密码才能看到内容)
$ /usr/local/apache2/bin/apachectl -t
$ /usr/local/apache2/bin/apachectl stop
$ /usr/local/apache2/bin/apachectl start
---------------------
#客户端配置
1.更改hosts文件,添加服务器IP 指定的域名
2.界面访问指定域名
网站配置
$ vim /usr/local/apache2/conf/httpd.conf
#取消此模块注释、开启协商模块
LoadModule negotiation_module modules/mod_negotiation.so
#
<Directory "/usr/local/apache2/htdocs">
#若访问目录下有多个名字相近的网页,协商匹配最合适的返回
Options MultiViews
#若访问目录下没有默认网页,允许该目录下所有文件以软链接方式访问
#Options Indexes FollowSymLinks
#若上述两种情况都支持
#Options Indexes FollowSymLinks MultiViews
#允许改directory目录下配置的.htaccess文件生效
AllowOverride All
Require all granted
</Directory>
$ /usr/local/apache2/bin/apachectl stop
$ /usr/local/apache2/bin/apachectl start
------------------
客户端测试
#在指定目录下添加网页
$ cd /usr/local/apache2/htdocs
$ echo "123 from txt" > 123.txt
$ echo "123 from html" > 123.html
$ echo "123 from php" > 123.php
注释上实验3的那个开启的注释
#在浏览器访问: http://serverIP/123
四、地址跳转(基于域名的虚拟主机基础上,www.souhu.com --> www.sina.com)
#主配置文件开启重定向模块
$ vim /usr/local/apache2/conf/httpd.conf
LoadModule rewrite_module modules/mod_rewrite.so #取消注释
#在需要进行跳转的目录下添加权限文件
#地址跳转的可用参数,参考:http://httpd.apache.org/docs/2.4/mod/mod_rewrite.html#rewriterule
$ cd /usr/local/apache2/htdocs/souhu
$ vim .htaccess
RewriteEngine on
RewriteCond %{HTTP_HOST} www.souhu.com
RewriteRule .* http://www.sina.com/$1 [R=permanent,L]
#重启服务、验证(访问界面后输入用户名和密码才能看到内容)
$ /usr/local/apache2/bin/apachectl -t
$ /usr/local/apache2/bin/apachectl stop
$ /usr/local/apache2/bin/apachectl start
--------------
#客户端访问
客户端访问www.souhu.com自行跳转到www.sina.com界面;
此跳转能在浏览器中正常体现;在字符终端访问返回301网页;
五、结合openssl实现https
#前提:安装apache的时候需要添加选项(--enable-ssl)、安装了基础依赖(openssl、openssl-devel)
#修改主配置文件开启ssl模块、开启加载ssl关联的配置文件
$ vim /usr/local/apache2/conf/httpd.conf
LoadModule ssl_module modules/mod_ssl.so #取消注释
Include conf/extra/httpd-ssl.conf #取消注释
#方法一:开启缓存模块
LoadModule socache_shmcb_module modules/mod_socache_shmcb.so
#修改ssl关联的配置文件(改域名、关缓存)
$ vim /usr/local/apache2/conf/extra/httpd-ssl.conf
<VirtualHost _default_:443>
#改域名
DocumentRoot "/usr/local/apache2/htdocs"
ServerName 要访问的域名:443
ServerAdmin webmaster@sina.com
ErrorLog "logs/sina-https-error_log"
CustomLog "logs/sina-https-access_log" common
#方法二:不开模块、注释掉相关的配置
#关缓存或加载缓存模块、因为需要加载额外的模块,但与本实验无关
#SSLSessionCache "dbm:/usr/local/apache2/logs/ssl_scache"
#SSLSessionCache "shmcb:/usr/local/apache2/logs/ssl_scache(512000)"
#SSLSessionCacheTimeout 300
</VirtualHost>
#创建证书
$ cd /usr/local/apache2/conf #可以先进入该存放证书的目录位置
$ openssl genrsa -out server.key 1024
$ openssl req -new -key server.key -out server.csr
$ openssl x509 -req -days 365 -sha256 -in server.csr -signkey server.key -out server.crt
#重启服务、验证(访问界面后输入用户名和密码才能看到内容)
$ /usr/local/apache2/bin/apachectl -t
$ /usr/local/apache2/bin/apachectl stop
$ /usr/local/apache2/bin/apachectl start
-----------------------
#客户端访问
浏览器地址栏输入:https://www.sina.com - 高级-继续访问
六、地址跳转(端口跳转,www.sina.com的80 --> 443)
#主配置文件开启重定向模块
$ vim /usr/local/apache2/conf/httpd.conf
LoadModule rewrite_module modules/mod_rewrite.so #取消注释
#在需要进行跳转的目录下添加权限文件
$ cd /usr/local/apache2/htdocs/sina
$ vim .htaccess
RewriteEngine on
#判断站点访问端口,不是443的时候,进行处理;HTTP_PORT是80
RewriteCond %{SERVER_PORT} !443
RewriteRule .* https://www.sina.com/$1 [R=permanent,L]
#重启服务、验证(访问界面后输入用户名和密码才能看到内容)
$ /usr/local/apache2/bin/apachectl -t
$ /usr/local/apache2/bin/apachectl stop
$ /usr/local/apache2/bin/apachectl start
~~~~~~~~第二种写法:上面在对应目录下新建htaccess文件.也可以使用下面在配置文件中对应目录下写入.htaccess的内容~~~~~~~
<Directory "/usr/local/apache2/htdocs/sina">
RewriteEngine on #开启转发规则
RewriteCond %{SERVER_PORT} !443 #检查访问端口只要目标不是443的
RewriteRule .* https://%{SERVER_NAME}/$1 [R=301,L] #全都使用https重新访问
</Directory>
七、个人主页
$ vim /usr/local/apache2/conf/httpd.conf
#开启个人主页模块、加载个人主页配置
LoadModule userdir_module modules/mod_userdir.so
Include conf/extra/httpd-userdir.conf
$ vim /usr/local/apache2/conf/extra/httpd-userdir.conf
#用户家目录下默认的网页目录
UserDir public_html
#家目录下网页目录的权限
<Directory "/home/*/public_html">
AllowOverride FileInfo AuthConfig Limit Indexes
Options MultiViews Indexes SymLinksIfOwnerMatch IncludesNoExec
Require method GET POST OPTIONS
</Directory>
$ /usr/local/apache2/bin/apachectl stop
$ /usr/local/apache2/bin/apachectl start
#创建用户
$ useradd 用户名
$ password 用户名
$ cd /home/用户名
$ mkdir public_html
$ cd public_html
$ echo "XXX's home~" > index.html
#apache用户运行apache服务、需要设置访问用户的权限
$ chomd o+rx /home/用户名
----------------------
客户端访问: http://serverIP/~用户名
八、配置缓存
$ vim /usr/local/apache2/conf/httpd.conf
#此模块默认未启用,请手动启用
LoadModule expires_module modules/mod_expires.so
#添加各文件类型的缓存时间
<IfModule mod_expires.c>
ExpiresActive on
ExpiresByType image/gif "access plus 1 days"
ExpiresByType image/jpeg "access plus 4 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"
</IfModule>
$ /usr/local/apache2/bin/apachectl stop
$ /usr/local/apache2/bin/apachectl start
--------------
客户端测试
#界面访问,浏览器中的开发者工具中查看Cache-Control: max-age=值
九、HTTP2
用另一台虚拟机
#环境搭建:
1.httpd软件包在2.4.17以上,否则不支持mod_http2
2.openssl软件包在1.0.2以上,否则不支持HTTP/2.0
#解压源码包并解决依赖关系
$ tar -zxf httpd-2.4.47.tar.gz
$ tar -zxf apr-1.4.6.tar.gz
$ tar -zxf apr-util-1.4.1.tar.gz
#解决aprhr apr-util依赖关系
$ cp -r apr-1.4.6 httpd-2.4.47/srclib/apr
$ cp -r apr-util-1.4.1 httpd-2.4.47/srclib/apr-util
#安装基础依赖
$ yum -y install gcc gcc-c++ openssl openssl-devel zlib zlib-devel pcre pcre-devel
#支持HTTP/2.0的依赖
$ yum -y install libnghttp2-devel
#编译、安装,每步执行完用echo $?检测
#--prefix:指定安装路径, enable-rewrite:开启地址重写, enable-so:开启dso(动态共享对象), enable-headers:允许修改http的请求头, enable-expires:允许客户端缓存, enable-modules=most:尽可能安装更多模块, enable-deflate:开启对压缩的支持, enable-ssl:开启https的支持
$ cd httpd-2.4.47
$ ./configure --prefix=/usr/local/apache2 --enable-rewrite --enable-so --enable-headers --enable-expires --enable-modules=most --enable-deflate --enable-ssl --enable-http2
$ make && make install
#结合ssl、配置端口跳转
$ vim /usr/local/apache2/conf/httpd.conf
#开启HTTP/2.0模块、SSL模块
LoadModule http2_module modules/mod_http2.so
LoadModule ssl_module modules/mod_ssl.so
Include etc/extra/httpd-ssl.conf
#设置域名、协议
ServerName www.linuxlc.com:80
Protocols h2 http/1.1
#配置ssl子文件
$ vim /usr/local/apache2/conf/extra/httpd-ssl.conf
DocumentRoot "/usr/local/apache2/htdocs"
Protocols h2 http/1.1
ServerName www.linuxlc.com:443
ServerAdmin you@example.com
ErrorLog "/usr/local/apache2/logs/error_log"
TransferLog "/usr/local/apache2/logs/access_log"
SSLEngine on
#注释相关session_cache的配置
#SSLSessionCache "dbm:/usr/local/apache2/logs/ssl_scache"
#SSLSessionCache "shmcb:/usr/local/apache2/logs/ssl_scache(512000)"
#SSLSessionCacheTimeout 300
#创建证书
$ cd /usr/local/apache2/conf/
$ openssl genrsa -out server.key 1024
$ openssl req -new -key server.key -out server.csr
$ openssl x509 -req -days 365 -sha256 -in server.csr -signkey server.key -out server.crt
#重启服务
$ /usr/local/apache2/bin/apachectl stop
$ /usr/local/apache2/bin/apachectl start
-----------------
客户端测试;
https://IP
http://IP/xx.html -- 跳转到https,查看开发者工具,右键勾选Protocol,看到h2
#添加80-443跳转:
$ vim /usr/local/apache2/conf/httpd.conf
#开启跳转
LoadModule rewrite_module modules/mod_rewrite.so
#端口跳转(80-443)
<Directory "/usr/local/apache2/htdocs">
RewriteEngine on
RewriteCond %{SERVER_PORT} !443
RewriteRule .* https://%{SERVER_NAME}/$1 [R=301,L]
</Directory>
#重启服务
$ /usr/local/apache2/bin/apachectl stop
$ /usr/local/apache2/bin/apachectl start