apache

一、先做准备工作
1
)先配置网络
vim /etc/sysconfig/network-scripts/ifcfg-eth0
##
BOOTPROTO=none
IPADDR=172.25.254.120(真机id+100)
NETMASK=255.255.255.0
##
systemctl restart network
2
)改名
hostnamectl set-hostname apache.example.com
3
)配置yum
vim /etc/yum.repos.d/rhel_dvd.repo
##
baseurl=http://172.25.254.250/rhel(用老师的yum源)
##
yum clean all
init 3
关掉图形,用真机连上虚拟机做

apache
:提供了一个协议(超文本传输协议)(apache只是能提供这种协议的软件中的一种)

二、apache的安装部署:
yum install httpd httpd-manual -y ##
安装apache软件和手册
systemctl start httpd
systemctl enable httpd
firewall-cmd --list-all ##
列出火墙信息


apache


firewall-cmd --permanent --add-service=http ##
永久允许http服务
firewall-cmd --reload ##
火墙重新加载策略


apache


再次列出火墙信息,可以看到httpd服务被允许

apache


vim /var/www/html/index.html
##
<h1> hello world </h1>
##
systemctl restart httpd.service

测试:
http://172.25.254.120

http://172.25.254.120/manual

apache

apache


html超文本标记语言,
/var/www/html    ##apache
/目录,默认发布目录


apache


三、更改默认发布文件
vim /var/www/html/westos
##
<h1> /westos/html/index.html's page </h1>
##

vim /etc/httpd/conf/httpd.conf
##
164
行改成DirectoryIndex westos
##
systemctl restart httpd.service

如果在配置文件里写两个文件
vim /etc/httpd/conf/httpd.conf
##
164
行改成DirectoryIndex westos index.html
##
重启服务后显示的,是在第一个中所写的。如果删掉第一个,那么显示第二个。

apache

四、修改默认发布目录:
mkdir -p /westos/html
vim /westos/html/index.html
##
<h1> /westos/html/index.html's page </h1>
##
服务重启后没有显示这里所写内容,需要修改默认发布目录
vim /etc/httpd/conf/httpd.conf
(更改默认发布目录)
##
119
行注释掉
120
行重新写入DocumentRoot "/westos/html"
##
重启系统仍然访问不了
vim /etc/httpd/conf/httpd.conf
(使默认发布目录生效)
##
<Directory "/westos/html">
        Require all granted
谁都能访问这个
</Directory>
##
重启后成功

五、修改默认端口:
ss -antlupe | grep httpd
查看端口(80


apache


vim /etc/httpd/conf/httpd.conf
(更改默认发布目录)
##
42
Listen 8080
##
重启服务
ss -antlupe | grep httpd
查看端口(8080
firewall-cmd --permanent --add-port=8080/tcp
firewall-cmd --reload
访问172.25.254.120:8080访问成功

apache

六、访问控制:(后台登陆)
先恢复之前设置,之后
vim /etc/httpd/conf/httpd.conf
##
<Directory "/var/www/html/">
        Require all granted
授权
        Order Allow,Deny ##
两个名单,先读白名单,后读黑名单
        Allow from all ##
允许所有人访问
        Deny from 172.25.254.20 ##
不允许20这台主机访问
</Directory>
##
重启后20无法访问


apache


vim /etc/httpd/conf/httpd.conf
##
<Directory "/var/www/html/">
        Require all granted
        Order Deny,Allow
        Allow from 172.25.254.20
        Deny from all
</Directory>
##
重启后只有20可以登陆


apache


cd /etc/httpd/
htpasswd -cm webuser zhao
c创建htpasswd文件,m指定用户)
htpasswd -m webuser zhang
(第二次不要加c,不然会覆盖第一次)


apache


在一个新建的地址目录中做,不然配置文件中的其他信息可能导致实验失败

apache


在配置文件中
#
<Directory "/var/www/html/test">
AuthUserfile /etc/httpd/webuser ##
文件(绝对路径)
AuthName "Please input username and password~~" ##
显示的话
AuthType basic ##
类型
下面两条命令只能有一个
Require user zhao ##
所有webuser里的其他人(zhang)输入密码也进不去,zhao进去需要密码
#Require valid-user ##
所有webuser中的用户登陆需要输入用户密码即可登录
</Directory>
#
重启后查看效果

apache

apache

七、设置虚拟主机地址
解析
在真机中用root身份vim /etc/hosts(在真机中检验所以在真机中配置解析)
##
172.25.254.120 www.westos.com news.westos.com music.westos.com
##
ping www.westos.com
(能ping通说明解析设置好了)

cd /etc/httpd/conf.d
vim a_default.conf
(编辑默认解析文件)
##
<VirtualHost _default_:80>
        DocumentRoot /var/www/html ##
解析地址
        CustomLog logs/default.logcombined ##
日志控制(混合型日志:登陆警告报错),文件地址为默认地址(在主配置文件中有写43/etc/httpd
</VirtualHost>
##

mkdir /var/www/virtual/news.westos.com/html -p
(递归建立)
mkdir /var/www/virtual/music.westos.com/html -p
vim /var/www/virtual/news.westos.com/html/index.html
vim /var/www/virtual/music.westos.com/html/index.html

apache

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>
##

vim music.conf
(编辑music.westos.com解析文件)
##
<VirtualHost *:80>
        ServerName music.westos.com ##
域名
        DocumentRoot/var/www/virtual/music.westos.com/html ##
默认发布目录
        CustomLog logs/music.log combined##
日志控制(混合型日志)
</VirtualHost>
<Directory "/var/www/virtual/music.westos.com/html">
        Require all granted ##
授权
</Directory>
##
重启服务显示如图

apache

apache

apache


八、php的发布
yum install php -y ##
安装php
vim /var/www/html/index.php
##
<?php
        phpinfo();
?>
##
systemctl restart httpd.service

测试:访问172.25.254.120/index.php


apache

九、cgi
的发布
mkdir /var/www/html/cgi
vim /var/www/html/cgi/index.cgi
(脚本内容在172.25.254.120/manual中查找)
##
#!/usr/bin/perl
print "Content-type: text/html\n\n";
print `date`;
##

chmod +x index.cgi
(给这个脚本一执行权限)

vim /etc/httpd/conf.d/a_default.conf
(编辑默认解析文件)
##
<VirtualHost _default_:80>
        DocumentRoot /var/www/html
        CustomLog logs/default.logcombined
</VirtualHost>
<Directory /var/www/html/cgi>
        Options +ExecCGI
        AddHandler cgi-script .cgi
(文件后坠是.cgi
</Directory>
##
./index.cgi ##
执行文件,看看是否成功
systemctl restart httpd.service

访问http://172.25.254.120/cgi/index.cgi
成功,每次刷新时间不一样


apache

十、https
:把输入字符加密

cd /etc/httpd/conf.d
yum install mod_ssl -y
systemctl restart httpd.service
firewall-cmd --permanent --add-service=https
firewall-cmd --reload

apache

但是钥匙不是自己的


apache


想要key是自己的
钥匙和证书,钥匙开证书的锁

yum install crypto-utils.x86_64 -y(就会多了genkey命令,用来给钥匙)
genkey www.westos.com


apache

apache

apache


apache


如果发给CA认证需要花钱,所以在做实验的时候选择NO

apache


apache


apache


完成后,写了证书和钥匙的地址


apache


vim ssl.conf
##
100
行改成/etc/pki/tls/certs/www.westos.com.crt
107
行改成/etc/pki/tls/private/www.westos.com.key
##


apache


systemctl restart httpd.service
访问https://172.25.254.120/
查看


apache



十一、做一个加密的虚拟主机(443端口)

mkdir /var/www/virtual/login.westos.com/html -p
vim /var/www/virtual/login.westos.com/html/index.html(写入要显示的值)
##
<h1>login.westos.com's page</h1>
##
vim /etc/httpd/conf.d/ssl.conf
##
<VirtualHost *:443>(在主配置文件/etc/httpd/conf.d/ssl.conf中56行写了https访问端口为443)
        ServerName login.westos.com(域名)
        DocumentRoot /var/www/virtual/login.westos.com/html(默认访问目录)
        CustomLog logs/login.log combined(日志)
        SSLEngine on(加密功能开启)
        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>
##
重启后可以访问https://login.westos.com


apache


但是访问login.westos.com时默认访问http://login.westos.com,
我想改成访问login.westos.com时默认访问https://login.westos.com,就需要用到网页重写功能

二、网页重写功能
在上面的基础上
vim /etc/httpd/conf.d/ssl.conf
##(加入)
<Virtualhost *:80>(因为http是80端口)
        ServerName login.westos.com
        RewriteEngine on
        RewriteRule ^(/.*)$ https://%{HTTP_HOST}$1 [redirect=301]
</Virtualhost>
##
PS:
{
^(/.*)$ ##客户在浏览器地址栏中输入的所有字符
https:// ##强是客户加密访问
%{HTTP_HOST} ##客户请求主机
$1 ##“$1”标示^(/.*)$的值
[redirect=301] ##301临时重写(先转换在访问) 302永久转换(直接访问)
}

重启后成功

PS:
> /var/log/messages
cat /var/log/messages
查看文件报错