实验前配置:
需要一台全新的虚拟机
将ip地址改为真机ip+100并配置好yum源
1.apchece
企业中常用的web服务,用来提供http://(超文本传输协议)
curl -I xxxxxxxx(网址) #查看该网址的头信息
2.apache的安装部署
yum install httpd httpd-manual -y ##安装http服务和使用手册
systemctl start httpd ##启动服务
systemctl enable httpd.service ##开机自启
ss -antlupe |grep httpd ##查看httpd的端口号
firewall-cmd --list-all ##列出火墙信息
firewall-cmd --permanent --add-service=http ##永久允许http服务
firewall-cmd --reload ##火墙重新加载策略
/var/www/html/ ##apache的/目录,默认发布目录
vim /var/www/html/index ##默认发布文件编辑
<h1>your first lesson of apache</h1>
3.http服务信息
测试:http://172.25.254.117
http://172.25.254.117/maunal
主配置文件: /etc/httpd/conf/httpd.confyum
子配置目录:/etc/httpd/conf.d
子配置文件:/etc/httpd/conf.d/*.conf
默认发布目录:/var/www/html
默认发布文件:index.html
默认端口:80
默认安全上下文:httpd_sys_content_t
程序开启默认用户:apache
apache日志: /etc/httpd/logs/*
如果开启了selinux需要修改安全上下文为httpd_sys_content_t
4.修改默认配置文件
修改默认发布文件
vim /etc/httpd/conf/httpd.conf
DirectoryIndex westos
:wq
vim /var/www/html/westos
<h1>change file success</h1>
:wq
systemctl restart httpd
修改默认发布目录
vim /etc/httpd/conf/httpd.conf
DocumentRoot "/westos/html"
<Directory "/westos/html">
Require all granted
</Directory>
:wq
mkdir /westos/html -p
vim /westos/html/westos
<h1>change DocumentRoot well!</h1>
systemctl restart httpd
修改默认端口号
vim /etc/httpd/conf/httpd.conf
Listen=8080
:wq
然后在防火墙中添加修改的端口号:
firewall-cmd --permanent --add-port=8080/tcp
firewall-cmd --reload
systemctl restart httpd
#需要在ip地址后面添加端口号:8080
172.25.254.117:8080
5.apache访问控制
1.针对主机的访问控制
mkdir /var/www/html/admin
vim /etc/httpd/conf/httpd.conf
<Directory "/var/www/html/admin">
Require all granted
Order deny,allow #用户读取顺序,后读取的列表覆盖前面的
Allow from 172.25.254.17
Deny from all
2.针对用户的访问控制
htpasswd -cm /etc/httpd/userpass admin
<Directory "/var/www/html/admin">
#Require all granted
AuthUserFile /etc/httpd/userpass
AuthName "name and password"
AuthType basic
#Ruquire user admin ##下面两种方法用一个就可以
Require valid-user
3.http解析
在浏览的一端上添加解析:
vim /etc/hosts
172.25.254.17 www.westos.com music.westos.com news.westos.com
:wq
在服务端上添加配置:
cd /etc/httpd/conf.d ##配置文件中路径必须是已经存在的
vim a_default.conf
<VirtualHost _default_:80>
DocumentRoot /var/www/html
CustomLog logs/default.log combined
</VirtualHost>
:wq
vim news.conf
<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>
:wq
vim music.conf
<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>
:wq
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/westos
vim /var/www/virtual/music.westos.com/html/westos
systemctl restart httpd.service
6.php服务
1,安装并测试php服务
yum install -y php
cd /var/www/html
vim info.php
<?php
phpinfo();
?>
:wq
systemctl restart httpd
测试:打开 172.25.254.117/info.php 可以看到php的信息
2.第一个cgi插件
mkdir /var/www/html/cgi
vim index.cgi
#!/usr/bin/perl
print "Content-type: text/html\n\n";
print `date`;
:wq
chmod +x index.cgi
./index.cgi ##如果不能执行说明代码有问题
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/cgi>
Options +ExecCGI
AddHandler cgi-script .cgi
</Directory>
:wq
systemctl restart httpd
4)https服务
yum install mod_ssl -y
systemctl restart httpd
firewall-cmd --list-all ##列出火墙信息
firewall-cmd --permanent --add-service=https ##永久允许https服务
firewall-cmd --reload ##火墙重新加载策略
测试:打开 https://172.25.254.117然后获得证书
如果想要自己修改关于安全证书的信息:
yum install -y crypto-utils.x86_64
genkey www.westos.com ##进入界面中选择并安装证书
安装完成后将证书和钥匙地址复制到/etc/httpd/conf.d/ssl.conf中(100和107行)
systemctl restart httpd
测试:
如果在下图这个界面卡住不动请晃动鼠标或者输入字符,因为是通过获取字符随机生成证书和钥匙
是否将证书发送给CA组织认证,企业认证都是收费的,这里我们选择NO
这里不要勾选Encrypt the private key,不然无法设置自启动
这里自己填写证书信息
修改ssl.conf配置重启后即可看到
@@注意因为之前测试过一次需要在edit-perference-advanced-certificates里查看并删除掉之前测试下载的证书@@
打开https://172.25.254.117获得证书后查看到信息是已经自己修改过的了
5)https重写
mkdir /var/www/virtual/login.westos.com/html -p
vim /var/www/virtual/login.westos.com/html/index.html
rewrite success
vim /etc/httpd/conf.d/login.conf
<VirtualHost *:443>
ServerName login.westos.com
DocumentRoot /var/www/virtual/login.westos/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/html">
Require all granted
</Directory>
<Virtualhost *:80>
ServerName login.westos.com
RewriteEngine on
RewriteRule ^(/.*)$ https://%{HTTP_HOST}$1[redirect=301]
</Virtualhost>
^(/.*)$ ##客户在浏览器地址中输入的所有字符
https:// ##强制用户加密访问
%{HTTP_HOST} ##客户请求主机
$1 ##"$1"标示^(/.*)$的值
[redirect=301] ##临时重写 302永久转换
systemctl restart httpd
测试:输入https://login.westos.com成功
删除掉“https://”回车打开,发现会自动加上“https://“