萌新的linux之旅23

Apache 简介
Apache HTTP Server(简称Apache)是Apache软件基金会的一个开放源码的网页服务器,可以在大多数计算机操作系统中运行,由于其多平台和安全性被广泛使用,是最流行的Web服务器端软件之一。它快速、可靠并且可通过简单的API扩展,将Perl/Python等解释器编译到服务器中。[2]
Apache HTTP服务器是一个模块化的服务器,源于NCSAhttpd服务器,经过多次修改,成为世界使用排名第一的Web服务器软件。
它可以运行在几乎所有广泛使用的计算机平台上。
Apache
安装
yum install -y httpd
systemctl start httpd
systemctl enable httpd
/var/www/html 默认发布目录
/var/www/html/index.html 默认发布文件

更改默认发布文件
vim /etc/httpd/conf/httpd.conf apache主配置文件
164 DirectoryIndex index.html ===> DirectoryIndex westos
systemctl restart httpd 重启服务

萌新的linux之旅23

更改默认发布目录
mkdir /haha/html -p
touch /haha/html/index.html
vim /etc/httpd/conf/httpd.conf
120 DocumentRoot “/haha/html”
121 < Directory “/haha/html” >
122 Require all granted
123
如果selinux 是处于开启状态,那么需要更改新建的/westos 目录的安全上下文
semanage fcontext -a -t httpd_sys_content_t ‘/haha(/.*)?’
restorecon -RvvF /haha
如果selinux 是处于关闭状态,则不需要更改安全上下文
systemctl restart httpd
萌新的linux之旅23

vim /etc/httpd/conf/httpd.conf 编辑主配置文件
萌新的linux之旅23

vim /haha/html/index.html 编辑默认发布文件
萌新的linux之旅23

测试,用浏览器打开
萌新的linux之旅23

设置黑白名单
vim /etc/httpd/conf/httpd.conf
120 DocumentRoot “/haha/html”
121 < Directory “/haha/html” >
122 Require all granted
123 Order Deny,Allow
124 Deny from ALL
125 Allow from 172.25.254.10
126 < /Directory >

建立用户
htpasswd -cm authfile haha
htpasswd -m authfile hehe
注意,建立第一个用户时是用 -cm ,而之后建立用户则用 -m
- c creat 建立 ,-cm 建立authfile 这个文件,并往里面添加 ,第二次,建立用户时,已有此表,不需要再建立一个

萌新的linux之旅23

设置登陆密码
vim /etc/httpd/conf/httpd.conf
120 DocumentRoot “/haha/html”
121 < Directory “/haha/html” >
122# Require all granted 授权所有人
123 AuthUserFile /etc/httpd/conf/authfile 指定文件
124 AuthName “please input your name and password.3q”
125 AuthType basic
126# Require user haha 指定用户
127 Require valid-user 指定文件中的用户
128 < /Directory >

萌新的linux之旅23

打开浏览器测试
萌新的linux之旅23

不输入密码则登陆不了

萌新的linux之旅23

apache 的虚拟主机
mkdir /var/www/haha/news.haha.com -p
mkdir /var/www/haha/game.haha.com -p
touch /var/www/haha/news.haha.com/index.html
touch /var/www/haha/game.haha.com/index.html

萌新的linux之旅23

cd /etc/httpd/conf.d/
vim default.conf
< Virtualhost default:80 > 80 ,apache 服务默认打开的端口
DocumentRoot “/var/www/html”
CustomLog “logs/default.log” combined
< /Virtualhost >

vim news.conf
< Virtualhost *:80 >
ServerName news.haha.com
DocumentRoot /var/www/haha/news.haha.com
CustomLog “logs/news.log” combined
< /Virtualhost >
< Directory “/var/www/haha/news.haha.com” >
Require all granted
< /Directory >

vim game.conf
< Virtualhost *:80 >
ServerName game.haha.com
DocumentRoot /var/www/haha/game.haha.com
CustomLog “logs/game.log” combined
< /Virtualhost >
< Directory “/var/www/haha/gaem.haha.com” >
Require all granted
< /Directory >

萌新的linux之旅23

测试
萌新的linux之旅23

萌新的linux之旅23
这是default.conf 文件指定的域名

萌新的linux之旅23

萌新的linux之旅23
这是news.conf 文件指定的域名

萌新的linux之旅23

萌新的linux之旅23
这是game.conf指定的域名

apache安全证书

yum install mod_ssl -y 安装这个软件
yum install crypto-utils.x86_64 -y
重新加载apache,所安装的软件会自动开启apache的443端口

萌新的linux之旅23

genkey www.westos.com 去给www.westos.com生成证书
512 最短的
1024是最快
2048标准
需要敲键盘和移动鼠标,生成**,2048花费时间太长,所以选择1024

发给CA认证机构是要收取认证费用的,所以在实验环境,可以选择不发,对环境没有影响
Protecting your private key
不要去填,直接next
后面要自己写,不然每次进入阿帕奇都要输入密码

输入国家,省会,城市,公司,名称,网址
NEXT之后就完成了
出现一些信息提示,发现有钥匙和证书的存放地方
还会在/etc/httpd/conf.d下生成ssl.conf,这里面的文件就是默认443端口的配置
按照提示中的地址,修改ssl.conf中的所示部分

萌新的linux之旅23

萌新的linux之旅23

萌新的linux之旅23

萌新的linux之旅23

萌新的linux之旅23

完成后重启httpd

然后关闭火墙,或者给http和https做火墙设略

[[email protected] conf.d]# firewall-cmd –permanent –add-service=http
success
[[email protected] conf.d]# firewall-cmd –permanent –add-service=https
success
[[email protected] conf.d]# systemctl restart firewalld.service
[[email protected] conf.d]# firewall-cmd –list-all
public (default, active)
interfaces: eth0 eth1 team0
sources:
services: dhcpv6-client dns http https ssh
ports:
masquerade: no
forward-ports:
icmp-blocks:
rich rules:

出现网警的界面
在 I Understand the Risks这个选项中选择添加,获取证书
就可以进入你的界面了
点击网址前面的锁的图标,选择更多信息
出来的界面选择View 证书,查看证书信息,这时显示的就是我们手动制作的证书
注意本地解析,加密的端口是443

萌新的linux之旅23

自动跳转加密网页
新建网页login.westos.com,创建在/var/www/westos/login/html中
写入:
vim /var/www/westos/login/html/index.html
在index.html 中编辑网页内容,注意格式
在/etc/httpd/conf.d下新建文件login.conf

萌新的linux之旅23

vim 编辑双文件
:sp /etc/httpd/conf.d/ssl.conf
添加:
将SSL功能开启
并且添加钥匙,锁
SSLEngine on
SSLCertificateFile /etc/pki/tls/certs/www.westos.com.crt
SSLCertificateKeyFile /etc/pki/tls/private/www.westos.com.key

萌新的linux之旅23

将初始端口变成443
新建虚拟主机端口80,将80中的引导到443端口
RewriteEngine on 重写功能开启
RewriteRule ^(/.*)https://访1 匹配后面的内容
[redirect=301]
301 临时重定向,访问一次,定向一次,因为网页后台会有刷新,所以一般用301
302 永久重定向

萌新的linux之旅23

重启服务
因为用主机浏览器测试,主机添加本地解析
172.25.254.110 www.westos.com news.westos.com gongyi.westos.com login.westos.com

测试,输入http://login.westos.com
浏览器会直接跳转访问到https://login.westos.com

萌新的linux之旅23

php编写网页
yum install php -y 安装php软件
在/var/www/html下编写index.php
进入/etc/httpd/conf/httpd.conf
171
172 DirectoryIndex index.php index.html westos
173
优先访问index.php ,重启apache,打开网页,你会发现已经变成php的页面

萌新的linux之旅23

萌新的linux之旅23

萌新的linux之旅23

[[email protected] conf.d]# yum install httpd-manual.noarch -y
安装httpd.conf 文件的帮助手册
重启httpd
查看用网址 172.25.254.109/manual
进入后点击CGI:Dynamic Content
在里面发现了cgi格式的文件写法,和涉及到相关文件的书写规范
下面设置cgi格式网页(perl语言)
vim /var/www/html/cgi/index.cgi
1 #!/usr/bin/perl
2 print “Content-type: text/html\n\n”;
3 print date;

萌新的linux之旅23

萌新的linux之旅23

chmod +x index.cgi
此时可以使用,但是不能显示网页,只能把文件显示出来,这是因为没有网页文件的支持
所以修改/etc/httpd/conf.d/default.conf
添加
5 < Directory /var/www/html/cgi >
6 Options +ExecCGI
7 AddHandler cgi-script .cgi
8 < /Directory >
systemctl restart httpd

萌新的linux之旅23

如果还是显示不出来,查看一下安全上下文,查看安全上下文是否配对,如果不配对,那就不能显示出来
semanage fcontext -l | grep cgi
/var/www/cgi-bin(/.*)? all files system_u:object_r:httpd_sys_script_exec_t:s0
而ls -Zd /var/www/html/cgi
安全上下文是unconfined_u:object_r:httpd_sys_content_t:s0
所以更改
先更改selinux模式
setenforcing 0 警告
修改:
semanage fcontext -a -t httpd_sys_script_exec_t ‘/var/www/html/cgi(/.*)?’
restorecon -RvvF /var/www/html/cgi
setenforcing 1 变回强制模式
也可以直接关掉 selinux
再次进入地址172.25.254.109/cgi/index.cgi

萌新的linux之旅23

萌新的linux之旅23