##############################apache的安装#######################
yum install httpd -y
systemctl start httpd
systemctl stop firewalld
systemctl enable httpd
systemctl disable httpd
#############################apache基本信息########################
1.apache的默认发布文件
index.html
2.apache的配置文件
/etc/http/conf/http.conf
/etc/httpd/conf.d/*.conf
3.apache 的默认发布目录
/var/www/html
4.apache的默认端口
80
#######################apache的基本配置############################
1,修改默认发布文件
vim /etc/httpd/conf/httpd.conf
168 <IfModule dir_module>
169 DirectoryIndex westos.html index.html
170 </IfModule>
systemctl restart httpd
测试:
在真机上,如图一
2,修改配置发布目录
[当selinux为disable状态] <-----------
vim /etc/httpd/conf/httpd.conf
119 #DocumentRoot "/var/www/html"
120 DocumentRoot "/westos/www/test" ##更改apache的默认发布目录为/westos/www/test
121 <Directory "/westos/www/test"> ##允许访问
122 Require all granted
123 </Directory>
systemctl restart httpd
[当selinux为enforcing状态] <-----------------
vim /etc/httpd/conf/httpd.conf
119 #DocumentRoot "/var/www/html"
120 DocumentRoot "/westos/www/test" ##更改apache的默认发布目录为/westos/www/test
121 <Directory "/westos/www/test"> ##允许所有人访问/westos/www/test目录
122 Require all granted
123 </Directory>
systemctl restart httpd
semanage fcontext -a -t httpd_sys_content_t '/westos(/.*)?' ##更改安全上下文
restorecon -RvvF /westos ##刷新,使更改生效
测试:
在真机上,如圖二
3,apache的访问控制
vim /etc/httpd/conf/httpd.conf
125 <Directory "/var/www/html/admin"> ##允许90主机访问admin目录
126 Order Deny,Allow
127 Allow from 172.25.254.90
128 Deny from All
129 </Directory>
125 <Directory "/var/www/html/admin"> ##允许所有人访问admin目录
126 Order Allow,Deny
127 Allow from All
128 Deny from 172.25.254.90
129 </Directory>
systemctl restart httpd
如图三,主配置文件的更改
测试,如图四
htpasswd -cm /etc/httpd/accessuser admin
vim /etc/httpd/conf/httpd.conf
<Directory "/var/www/html/admin">
AuthUserFile /etc/httpd/accessuser ##用户认证文件
AuthName "please input your name and password !!" ##用户认证提示信息
AuthType basic ##认证类型
Require valid-user ##认证用户,认证文件中所有用户都可以通过
[Require user admin] ##只允许认证文件中admin用户访问
</Directory>
systemctl restart httpd
##认证用户的生成,admin和tom 如图六
##设定认证用户的访问,如图五
测试:如图七
结果显示,如图八
4,apache语言支持
html语言默认支持
php语言
yum install php -y
systemctl restart httpd
cgi语言支持
mkdir /var/www/html/cgi ##建立发布目录
cd /var/www/html/cgi
vim index.cgi ##编写cgi
#!/usr/bin/perl
print "Content-type: text/html\n\n";
print `date`;
vim /etc/httpd/conf/httpd.conf
<Directory "/var/www/html/cgi"> ##访问cgi目录
Options +ExecCGI
AddHandler cgi-script .cgi
</Directory>
systemctl restart httpd
修改apache的主配置文件:如图九
测试,如图十
###################apache的虚拟主机#######################
1,定义
可以让我们的一台apache服务器在被访问不同域名的时候现实不同的主页
2,建立测试页
mkdir virtual/news.westos.com/html -p
mkdir virtual/money.westos.com/html -p
echo "<h1>money.westos.com's page</h1>" >virtual/money.westos.com/html/index.html
echo "<h1>news.westos.com's page</h1>" >virtual/news.westos.com/html/index.html
3,配置
(1)
cd /etc/httpd/conf.d/
vim default.conf ##未指定域名的访问都访问default
<Virtualhost _default_:80> ##虚拟主机开启的端口80
DocumentRoot "/var/www/html" ##虚拟主机的默认发布目录
CustomLOg "logs/default.log" combined ##虚拟主机日志
</Virtualhost>
systemctl restart httpd.service ##重启服务
##编写未指定域名的访问default,如图十一
[root@mariadb conf.d]# ls
autoindex.conf default.conf php.conf README userdir.conf welcome.conf
(2)在apache的子策略中编写news.conf 和 money.conf
vim news.conf ---> ##指定域名news.westos.com的访问到指定默认发布目录中
<Virtualhost *:80>
ServerName "news.westos.com"
DocumentRoot "/var/www/virtual/news.westos.com/html"
CustomLog "logs/news.log" combined
</Virtualhost>
<Directory "/var/www/virtual/news.westos.com/html"> ##默认发布目录的访问授权
Require all granted
</Directory>
如图十二
cp news.conf money.conf
vim money.conf ---> ##指定域名money.westos.com的访问到指定默认发布目录中
<Virtualhost *:80>
ServerName "money.westos.com"
DocumentRoot "/var/www/virtual/money.westos.com/html"
CustomLog "logs/money.log" combined
</Virtualhost>
<Directory "/var/www/virtual/money.westos.com/html"> ##默认发布目录的访问授权
Require all granted
</Directory>
systemctl restart httpd.service
4,测试
在浏览器所在主机中
vim /etc/hosts
172.25.254.135 www.westos.com news.westos.com money.westos.com ##在本地解析中,加入访问的主机ip及域名
如图十三,十四,十五
##########################################https########################################
1,https定义
Hyper text transfer protocol over Secure socker layer
通过ssl进行超文本加密传输
2,配置
yum install mod_ssl -y
yum install crypto-utils -y
genkey www.westos.com ##生成加密文件
vim /etc/httpd/conf.d/login.conf
<Virtualhost *:443>
ServerName "login.westos.com"
DocumentRoot "/var/www/virtual/login.westos.com/html"
CustomLog "logs/login.log" combined
SSLEngine on ##开启https功能
SSLCertificateFile /etc/pki/tls/certs/www.westos.com.crt ##证书
SSLCertificateKeyFile /etc/pki/tls/private/www.westos.com.key ##密钥
</Virtualhost>
<Directory "/var/www/virtual/login.westos.com/html">
Require all granted
</Directory>
<Virtualhost *:80> ##网页重写,实现自动访问httos
ServerName "login.westos.com"
RewriteEngine on
RewriteRule ^(/.*)$ https://%{HTTP_HOST}$1 [redirect=301]
</Virtualhost>
####
^(/.*)$ https://%{HTTP_HOST}$1 [redirect=301]
^(/.*)$ ##客户主机在地址栏中写入的所有字符(不包括换行符)
https:// ##定义成为主机的访问协议
%{HTTP_HOST} ##客户请求主机
$1 $1的值表示^(/.*)$的值
[redirect=301] ##临时重定向,302永久重定向
如图十六
mkdir /var/www/virtual/login.westos.com/html -p
vim /var/www/virtual/login.westos.com/html/index.htm
<h1>virtual.login.westos.com's page</h1>
systemctl restart httpd
测试,在客户主机中添加解析
172.25.254.135 login.westos.com
访问http://login.westos.com 会自动跳转到https://login.westos.com 实现网页数据加密传输
如图十七