搭建Apache
在Linux中通过命令
yum -y install httpd*
进行安装,一次不行多试几次
安装完成后根目录在/etc/httpd
apache 目录结构
服务目录 | /etc/httpd |
---|---|
主配置文件 | /etc/httpd/conf/httpd.conf |
网站数据目录 | /var/www/html |
访问日志 | /var/log/httpd/access_log |
错误日志 | /var/log/httpd/error_log |
在httpd 服务程序主配置文件中最为常见的参数如下:
ServerRoot | 服务目录 |
ServerAdmin | 管理员邮箱 |
User | 运行服务的用户 |
Group | 运行服务的用户组 |
ServerName | 网站服务器的域名 |
DocumentRoot | 网站数据的目录 |
Listen | 监听的IP地址与端口号 |
DirectoryIndex | 默认的索引页页面 |
ErrorLog | 错误日志文件 |
CustomLog | 访问日志文件 |
Timeout | 网页超时时间,默认300秒 |
Include | 需要加载的其他文件 |
apache 发布工程
Apache 的工程发布有两种方式,第一种方式是将工程放入、var/www/html中,或将$Apache_HOME\Apache24\htdocs中。第二种是配置http.conf,将conf/extra/httpd-vhosts.conf 前的’#'号去掉,然后在extra目录下的httpd-vhosts.conf文件中进行配置。
这里用第一种介绍,在 /var/www/html 目录下创建新目录,
mkdir demo
进入demo 创建新文件
vim test.html
在里面写一个HTML文件
这里写了一个计算器
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>计算器实现</title>
<style>
* {
margin: 0;
padding: 0;
}
.button {
width: 50px;
height: 50px;
font-size: 25px;
margin: 2px;
cursor: pointer;
background: #607d8b;
border: none;
color: white;
}
.textView {
width: 210px;
margin: 5px;
font-size: 25px;
padding: 5px;
border: none;
}
.main {
position: absolute;
top: 50%;
left: 50%;
transform: translateX(-50%) translateY(-50%);
}
html {
background: linear-gradient(to right, #ff0052, #0072ff);
height: 100%;
}
</style>
<script>
let a="1343.43245";
let b=a.indexOf(".");
console.log(b);
let c=a.substring(0,b+3);
console.log(c);
function insert(num) {
// TODO 插入
document.form.textView.value = document.form.textView.value + num;
}
function equal() {
// TODO 计算结果,并且结果保留两位小数
let exp = document.form.textView.value;
if (exp) {
let eval1 = eval(document.form.textView.value);
// eval() 执行括号内的语句 , 记录结果
let number = eval1.toString().indexOf(".");
if (number!==-1){
// 如果是小数,保留两位小数
eval1=eval1.toString().substring(0,number+3);
// 截取
document.form.textView.value=eval1;
}else {
// 如果不是小数,直接赋值
document.form.textView.value=eval1;
}
}
}
function Mclean() {
// TODO 清理输入框的文字
document.form.textView.value = null;
}
function back() {
// TODO 删除文本框的一个字符
let exp = document.form.textView.value;
document.form.textView.value = exp.substring(0, exp.length - 1);
// 截取字符串
}
</script>
</head>
<body>
<div class="main">
<form name="form">
<input class="textView" name="textView">
</form>
<table>
<tr>
<td><input type="button" class="button" value="C" onclick="Mclean()"></td>
<td><input type="button" class="button" value="<" onclick="back()"></td>
<td><input type="button" class="button" value="/" onclick="insert('/')"></td>
<td><input type="button" class="button" value="x" onclick="insert('*')"></td>
</tr>
<tr>
<td><input type="button" class="button" value="7" onclick="insert(7)"></td>
<td><input type="button" class="button" value="8" onclick="insert(8)"></td>
<td><input type="button" class="button" value="9" onclick="insert(9)"></td>
<td><input type="button" class="button" value="-" onclick="insert('-')"></td>
</tr>
<tr>
<td><input type="button" class="button" value="4" onclick="insert(4)"></td>
<td><input type="button" class="button" value="5" onclick="insert(5)"></td>
<td><input type="button" class="button" value="6" onclick="insert(6)"></td>
<td><input type="button" class="button" value="+" onclick="insert('+')"></td>
</tr>
<tr>
<td><input type="button" class="button" value="1" onclick="insert(1)"></td>
<td><input type="button" class="button" value="2" onclick="insert(2)"></td>
<td><input type="button" class="button" value="3" onclick="insert(3)"></td>
<td rowspan="2"><input style="height: 107px" type="button" class="button" value="=" onclick="equal()"></td>
</tr>
<tr>
<td colspan="2"><input style="width: 107px" type="button" class="button" value="0" onclick="insert(0)"></td>
<td><input type="button" class="button" value="." onclick="insert('.')"></td>
</tr>
</table>
</div>
</body>
</html>
完成后通过systemctl start httpd 来运行Apache,通过宿主机浏览器输入Linux的IP 访问刚刚编写的页面。
Apache 安全配置
重定向 404 页面
默认的404网页是非常不友好的。当一个用户访问到一些不存在或者错误的链接时,如果我们没有制作一个网页去引导用户访问该站点的其他页面时,会损失大量的用户。自定义404页面会告诉百度、谷歌等搜索器的爬虫,这条记录本站已经删除,请放弃搜索收录,利于seo优化。
具体方法先打开配置文件:/etc/httpd/conf/httpd.conf
并在里面添加如下语句:
在/var/www/html文件夹下创建一个404.html文件,文件内容如下;
此时,我们未找到网页时,就会显示我们设计好的内容了。
注意:
(1)不要将404错误直接转向网站首页,这将导致首页不被收录。
(2)/404.html 前面不要带域名,否则返回的状态码是302或者200状态码。
(3)自定义的404网页必须是大于512B,如果小于这个大小,浏览器就不会执行。
修改文件权限
现在大部分站点都存在文件上传,比如头像上传,附件上传等。如果在代码层对上传的文件限制得不够严格,很容易被上传Webshell。一旦被上传,会对服务器造成很大的威胁。
如果我们对存放上传文件的目录限制脚本的运行权限,上传的脚本就无法运行,在一定程度上能够减轻黑客攻击的危害,并且还不影响正常的业务,上传的图片不需要执行权限,也能正常打开。
代码如下:
<Directory /var/www/html/demo/upload>
php_flag engine off
</Directory>
将以上代码写入配置文件即可。
Apache日志
Apache会生成两个主要的日志文件,一个是Web访问日志access_log,一个是记录服务器运行时出错的日志error.log
通过配置文件/etc/httpd/conf/httpd.conf中定义了日志格式。
通过/var/log/httpd/error_log查看日志。
为 Apache 搭建 HTTPS
https 在http 的基础下加入了 SSL 层,https 的安全基础是 SSL ,因此加密的详细内容就需要 SSL。
OpenSSL
OpenSSL 是一个开放源代码的软件库包,应用程序可以使用这个包来进行安全通信,避免窃听,同时确认另一端连接者的身份。
里面包含了主要的密码算法、常用密匙、和证书封装管理及SSL协议。
OpenSSL 的安装与应用
直接在网上下载安装
yum -y install openssl
OpenSSL 一共实现了 4 中非对称加密算法,包括 DH 算法、RSA算法、DSA算法和ECC算法。DH 一般用于密匙交换,RSA 用于交换和数字签名,DSA 一般只用于数字签名。
OpenSSL的常见用法
1.对称加密,对称加密需要使用的标准命令为enc,用法如下:
加密:openssl enc -e -des3 -a -salt -in 1.txt -out 2.txt
通过des3对1.txt进行加密,将密文保存到2.txt中。
解密:openssl enc -d -des3 -a -salt -in 1.txt -out 2.txt
通过des3对1.txt进行解密,将明文保存到2.txt中。
2.哈希值提取
openssl dgst -md5 abcdefg
通过MD5提取”abcdfrg“的哈希值。
为Apache搭建HTTPS
本例中,通过OpenSSL创建主签证书来搭建HTTPS
1.创建CA和申请证书
使用openssl工具创建CA证书和申请证书时,需要先查看配置文件,因为配置文件中对证书的名称和存放位置等相关信息都做了定义,具体可以参考/etc/pki/tls/openssl.cnf文件。
2.创建自签证书
我们通过以下命令,创建为CA提供所需的目录及文件:
3.指名证书开始的编号,生成根证书私钥(注意:私钥的文件名与存放路径要与配置文件中的配置相匹配)
echo 01 >> serial
(umask 077;openssl genras -out private/ cakey.pem 2048)
4.生成自签证书,即根证书CA,自签证书的存放位置也要与配置文件中的设置相匹配,生成整数时需要填写相应的信息。
如下图:包括国家、省份、城市等。
openssl req -new -x509 -key /etc/pki/CA/private/cakey.pem -out cacert.pem -days 365
5.颁发证书
在需要使用证书的主机上生成证书请求时,以http服务为例,步骤如下:
第一步:在需要使用证书的主机上生成私钥,这个私钥文件的位置可以随意定
第二步:生成证书签署请求
第三步:将请求通过可靠方式发送给CA
(umask 077;openssl genrsa -out test.key 2048)
openssl req -new -key test.key -out test.csr -days 365
继续填写相关信息。
CA服务器拿到证书签署请求文件后颁发证书,这一步是在CA服务器上做的
openssl req -new -x509 -key test.key -out test.crt -days 365
依然需要填写相关信息。
6.安装mod_ssl
通过命令安装mod_ssl:
yum install mod_ssl
7.安装完成后,在/etc/httpd/conf.d目录下找到自动生成的文件ssl.conf,按照下图进行修改:
保存并重启httpd 服务,现在通过http 和 https 都可以访问Apache 主页了。