1.部署
yum install httpd -y ##下载httpd
yum install httpd-manual -y ##下载httpd的手册
systemctl stop firewalld ##关闭防火墙
systemctl disable firewalld ##设置开机不启动防火墙
systemctl start httpd ##启动httpd
systemctl enable httpd ##设置开机自动启动httpd
html 超文本标记语言
xml 可扩展标记语言,封装
测试:
打开firefox
http://172.25.254.116 ##172.25.254.116为服务端主机ip
http://172.25.254.116/manual ##查看manual
2.apache的基础信息:
主配置目录:/etc/httpd/conf
主配置文件:/etc/httpd/conf/httpd.conf
子配置目录:/etc/httpd/conf.d
子配置文件:/etc/httpd/conf.d/*.conf
默认发布目录:/var/www/html
默认发布文件:index.html
默认端口:80
默认安全上下文:httpd_sys_content_t
程序开启默认用户:apache
apache日志:/etc/httpd/logs/*
3.端口修改:
vim /etc/httpd/conf/httpd.conf
41 #Listen 12.34.56.78:80
42 Listen 80 ##80即为httpd服务的网络端口
测试:
4.默认发布文件
vim /var/www/html/test ##新建默认发布文件test
<h1>hello world</h1> ##填写默认发布文件的内容
vim /etc/httpd/conf/httpd.conf
163 <IfModule dir_module>
164 DirectoryIndex test index.html ##新建的默认发布文件test放在index.html前面,则优先读取test.
165 </IfModule>
测试:
##将需要改的文件放在index.html前面则优先被访问。若不修改默认发布文件想要访问/var/www/html/下的file,则应http://172.25.254.116/file进行访问。修改主配置文件后应systemctl restart httpd.service
ss -antlupe | grep httpd ##端口查询
curl -I http://www.baidu.com ##百度服务查询
rpm -qc httpd ##软件主配置文件查询
5.修改默认发布目录
mkdir /www/html -p ##新建一个默认发布目录
vim /www/html/index.html ##添加默认发布文件,并写入内容
<h1>/www/html 's page</h1>
vim /etc/httpd/conf/httpd.conf
120 DocumentRoot "/www/html" ##添加新建默认发布目录
121 <Directory "/www/html">
122 Require all granted ##允许所有人访问
123 </Directory>
semanage fcontext -a -t httpd_sys_content_t '/www(/.*)?' ##修改/www/的安全上下文
restorecon -RvvF /www/ ##刷新/www/的安全上下文
测试:
6.apache的虚拟主机
server主机:
mkdir -p /var/www/virtual/news/html ##创建虚拟主机的默认发布目录
mkdir -p /var/www/virtual/music/html
vim /var/www/html/index.html ##修改虚拟主机的默认发布文件
<h1>www.westos.com</h1>
vim /var/www/virtual/news/html/index.html ##修改虚拟主机news的默认发布文件内容
news.westos.com
vim /var/www/virtual/music/html/index.html ##修改虚拟主机nmusic的默认发布文件内容
music.westos.com
vim /etc/httpd/conf.d/a_default.conf
##位指定域名的访问都访问default
<VirttualHost _default_:80> ##虚拟主机开启的端口
DocumentRoot /var/www/html ##虚拟主机的默认发布目录
CustomLog "logs/default.log" combined ##虚拟主机日志
</VirttualHost>
vim /etc/httpd/conf.d/news.conf ##指定域名news.westos.com的访问到指定默认发布目录中
<VirtualHost *:80>
Servername news.westos.com ##指定站点名称
DocumentRoot /var/www/virtual/news/html ##站点默认发布目录,即网页文件存放位置
CustomLog "logs/news.log" combined ##站点日志combined表示四种日志
</VirtualHost>
<Directory "/var/www/virtual/news/html"> ##默认发布目录的访问授权
Require all granted ##允许所有主机访问
</Directory>
vim /etc/httpd/conf.d/music.conf ##指定域名music.westos.com的访问到指定默认发布目录中
<VirtualHost *:80>
Servername music.westos.com ##指定站点名称
DocumentRoot /var/www/virtual/music/html ##指定站点的默认发布目录
CustomLog "logs/music.log" combined ##站点日志
</VirtualHost>
<Directory "/var/www/virtual/music/html"> ##默认发布目录的访问授权
Require all granted
</Directory>
浏览器所在主机(客户端):
vim /etc/hosts
172.25.254.116 www.westos.com news.westos.com music.westos.com##三个主机的ip均为172.25.254.116
测试:
7.apache内部的访问机制
(1)IP访问方式的控制
mkdir /var/www/html/westos ##创建实验用的westos目录
vim /var/www/html/westos/index.html ##编辑默认发布文件
<h1>westos 's page</h1>
vim /etc/httpd/conf.d/a_default.conf
<VirtualHost _default_:80>
DocumentRoot /var/www/html
CustomLog "logs/default.log" combined
</VirtualHost>
<Directory "/var/www/html/westos">
Order Deny,Allow ##列表读取顺序,后读取的列表内容会覆盖先读取的列表内容。Allow在后面则会覆盖Deny,只有172.25.254.16可以访问。反之亦然。
Allow from 172.25.254.16 ##允许172.25.254.16访问
Deny from All ##拒绝所有主机访问
</Directory>
(2)用户访问方式的控制
mkdir /var/www/html/admin
vim /var/www/html/admin/index.html
<h1>admin 's page</h1>
htpasswd -cm /etc/httpd/userpass admin ##生成用户admin的密码文件,cm会覆盖原文件的内容,m会追加在原文件内容的后面。admin和admin1不是真实存在的用户。
htpasswd -m /etc/httpd/userpass admin1
cat /etc/httpd/userpass ##查看生成的用户密码文件内容
vim /etc/httpd/conf.d/a_default.conf
<VirtualHost _default_:80>
DocumentRoot /var/www/html
CustomLog "logs/default.log" combined
</VirtualHost>
<Directory "/var/www/html/westos">
Order Deny,Allow ##访问顺序,Allow在后面则会覆盖Deny,只有172.25.254.16可以访问.反之亦然.
Allow from 172.25.254.16 ##允许172.25.254.16访问
Deny from All ##拒绝所有主机访问
</Directory>
<Directory "/var/www/html/admin">
AuthUserFile /etc/httpd/userpass ##认证的用户文件
AuthName "Please input your name and passwd"
AuthType basic ##基础认证类型
Require user admin ##允许用户admin访问
#Require valid-user ##允许所有用户访问
</Directory>
8.apache支持的语言
(1)html语言默认支持
(2)php语言
vim /var/www/html/index.php ##新建默认发布文件index.php并添加内容
<?php
phpinfo();
?>
yum install php -y ##安装php
vim /etc/httpd/conf/httpd.conf
166 <IfModule dir_module>
167 DirectoryIndex index.php index.html ##在主配置文件中添加新建的默认发布文件index.php
168 </IfModule>
systemctl restart httpd
测试:
http://172.25.254.116/index.php
(3)cgi语言
mkdir /var/www/html/cgi
semanage fcontext -a -t httpd_sys_script_exec_t '/var/www/html/cgi(/.*)?' ##修改/var/www/html/cgi的安全上下文
restorecon -RvvF /var/www/html/cgi ##刷新/var/www/html/cgi的安全上下文
ls -dZ /var/www/html/cgi ##查看/var/www/html/cgi的安全上下文
vim /var/www/html/cgi/index.cgi ##编写脚本
#!/usr/bin/perl
print "Content-type: text/html\n\n"; ##输出Content-type: text/html并执行两次换行
print `date`; ##优先执行反单引号内的内容,并将执行结果作为外部的输入信息
chmod +x /var/www/html/cgi/index.cgi ##给脚本添加x权限
/var/www/html/cgi/index.cgi ##执行下脚本确保脚本运行正常
测试脚本/var/www/html/cgi/index.cgi
vim /etc/httpd/conf.d/a_default.conf
alias /cgi /var/www/html/cgi ##设置/var/www/html/cgi的别名(快捷方式)为/cgi
<Directory "/var/www/html/cgi"> ##执行默认发布文件内的脚本内容
Options +ExecCGI
AddHandler cgi-script .cgi
</Directory>
点击index.cgi
9.网页加密
yum install mod_ssl -y ##下载ssl模块
yum install crypto-utils -y ##下载加密软件
systemctl restart httpd
genkey www.westos.com ##加密网页www.westos.com
当出现该图时,需要在同一主机另外打开窗口进行不间断的输入
CA指网页加密授权认证
vim /etc/httpd/conf.d/ssl.conf ##修改模块ssl的配置文件,下面两行的内容可在执行genkey www.westos.com后恢复的内容中找到
100 SSLCertificateFile /etc/pki/tls/certs/www.westos.com.crt
107 SSLCertificateKeyFile /etc/pki/tls/private/www.westos.com.key
systemctl restart httpd
测试:
https://172.25.254.142
10.网页重写
mkdir -p /var/www/virtual/login
vim /var/www/virtual/login/index.html
<h1>login.wesstos.com</h1>
vim /etc/httpd/conf.d/login.conf
<VirtualHost *:443>
Servername login.westos.com
DocumentRoot /var/www/virtual/login
CustomLog "logs/default.log" combined
SSLEngine on
SSLCertificateKeyFile /etc/pki/tls/private/www.westos.com.key
SSLCertificateFile /etc/pki/tls/certs/www.westos.com.crt
</VirtualHost>
<VirtualHost *:80>
Servername login.westos.com
RewriteEngine on
RewriteRule ^(/.*)$ https://%{HTTP_HOST}$1 [redirect=301]
</VirtualHost>
^(/.*)$ ##客户在浏览器地址栏输入的所有字符
https:// ##强制客户加密访问
%{HTTP_HOST} ##客户强求主机
$1 ##表示^(/.*)$ 的值
[redirect=301] ##临时重写,302表示永久重写