1、安装
yum install httpd -y ##apache软件
yum install httpd-manual ##apache的手册
systemctl start httpd
systemctl enable httpd
systemctl start firewalld.service
firewall-cmd --permanent --add-service=http ##火墙允许http
firewall-cmd --reload ##火墙重新加载策略
vim /var/www/html/index.html ##写一个发布页面,便于实验效果查看
2、基础配置(以下实验在防火墙关闭,selinux开启情况下进行的)
/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 ##程序开启默认用户
/etc/httpd/logs/ ##apache日志
1)修改默认端口、修改默认发布文件
vim /etc/httpd/conf/httpd.conf ##修改主配置文件
164 DirectoryIndex text.html index.html ##修改发布文件(哪个发布文件在前,先读取哪个,当前一个不存在时,读取后一个)
42 Listen 8080 ##修改默认端口(如果防火墙开,端口修改后访问时需要加端口号,且防火墙可能会阻拦此端口)
cd /var/www/html/
vim text.html
hello text
systemctl restart httpd
测试:浏览器搜索172.25.254.147:8080
防火墙开启时,修改端口,可执行下面命令允许修改后端口通过
firewall-cmd --permanent --add-port=8080/tcp ##修改防火墙允许8080端口通过
firewall-cmd --reload
实验: 关掉防火墙(除去防火墙对实验干扰),修改端口和发布文件 访问查看







2)修改默认发布目录
mkdir /westos/html -p ##新建一个发布目录
vim /westos/html/index.html ##新建一个发布文件
hello wang
vim /etc/httpd/conf/httpd.conf ##修改主配置文件
119 #DocumentRoot “/var/www/html”
120 DocumentRoot “/westos/html” ##指定发布目录
124 <Directory “/westos/html”> ##对新指定的发布目录授权
125 Require all granted
126
semanage fcontext -a -t httpd_sys_content_t ‘/westos(/.*)?’ ##修改安全上下文
restorecon -RvvF /westos/ ##刷新
systemctl restart httpd ##重启服务
测试:修改发布目录后访问,与修改前对比
实验结束后恢复原来设置
实验:
3、apache的虚拟主机
在真实主机
vim /etc/hosts中
172.25.254.137 www.westos.com news.westos.com music.westos.com
apache服务端:
mkdir /var/www/virtual/westos.com/music -p
mkdir /var/www/virtual/westos.com/news -p
vim /var/www/virtual/westos.com/music/index.html
music page
vim /var/www/virtual/westos.com/news/index.html
news page
cd /etc/httpd/conf.d/
vim adefault.conf
1 ##当没有指定时查看/var/www/html默认发布文件
2 DocumentRoot “/var/www/html”
3
vim news.conf
1 <VirtualHost *:80>
2 ServerName “news.westos.com”
3 DocumentRoot “/var/www/virtual/westos.com/news” ##访问news时的发布文件
4 CustomLog logs/news.log combined
5
6 <Directory “/var/www/virtual/westos.com/news”> ##授权
7 Require all granted
8
cp news.conf music
vim music.conf
1 <VirtualHost *:80>
2 ServerName “music.westos.com”
3 DocumentRoot “/var/www/virtual/westos.com/music” ##访问news时的发布文件
4 CustomLog logs/music.log combined
5
6 <Directory “/var/www/virtual/westos.com/music”> ##授权
7 Require all granted
8
systemctl restart httpd
实验:
真实主机
apache服务端
4、apache内部的访问控制
1)黑白名单
vim adefault.conf
1
2 DocumentRoot “/var/www/html”
3
4 <Directory “/var/www/html”>
5 Require all granted
6 Order Deny,Allow ##谁在前先读谁,此处为白名单(Allow,Deny则为黑名单)
7 Allow from 172.25.254.37 ##允许主机登陆
8 Deny from all ##不允许所有主机登陆
9
systemctl resatrt httpd
测试:分别设置不同黑白名单登陆
实验:
白名单
黑名单
2)用户的访问控制
mkdir /var/www/html/admin
vim /var/www/html/admin/index.html
admin
htpasswd -cm /etc/httpd/htuser admin ##建立admin用户
htpasswd -m /etc/httpd/htuser admin1 ##建立admin1用户,建立第二个用户时 是-m, -cm会覆盖之前的用户
cat /etc/httpd/htuser ##查看生成用户
cd /etc/httpd/conf.d/
ls
vim adefault.conf ##修改配置文件
9 <Directory “/var/www/html/admin”>
10 AuthUserFile “/etc/httpd/htuser” ##查看用户
11 AuthName “Please input username and password” ##请输入帐号密码
12 AuthType Basic ##基本检测,即检测帐号和密码
13 Require user admin ##允许admin用户登陆
14 # Require valid-user ##允许所有用户登陆
15
systemctl resatrt httpd ##重启服务
测试: 真机浏览器访问172.25.254.xxx/admin
注意及时清理缓存 ctrl+shift+delete
5、系统支持的语言
1)html ##系统默认支持
2)php
vim /var/www/html/index.php
1 <?php
2 phpinfo();
3 ?>
yum install php -y
systemctl restart httpd
测试
3)cgi
mkdir -p /var/www/html/cgi
semanage fcontext -a -t httpd_sys_script_exec_t ‘/var/www/html/cgi(/.*)?’ ##修改上下文权限
restorecon -RvvF /var/www/html/cgi/
vim /var/www/html/cgi/index.cgi ##cgi语言写的一段指令
1 #!/usr/bin/perl
2 print “Content-type: text/html\n\n”;
3 print date
;
chmod +x /var/www/html/cgi/index.cgi ##给一个操作的权限
vim adefault.conf
16
17 <Directory “/var/www/html/cgi”>
18 Options +ExecCGI ##增添执行cgi权力
19 AddHandler cgi-script .cgi
20
systemctl restart httpd
测试
4)wsgi
yum install mod_wsgi -y ##安装一个软件
vim /var/www/html/cgi/westos.wsgi ##此文件中写的内容模板————westos.wsgi(此出是python写的一段命令)
1 #!/usr/bin/env python
2 import time
3
4 def application (environ, start_response):
5 response_body = ‘UNIX EPOCH time is now: %s\n’ % time.time()
6 status = ‘200 OK’
7 response_headers = [(‘Content-Type’, ‘text/plain’),
8 (‘Content-Length’, ‘1’),
9 (‘Content-Length’, str(len(response_body)))]
10 start_response(status, response_headers)
11 return [response_body]
12
vim /etc/httpd/conf.d/adefault.conf
1
2 DocumentRoot “/var/www/html”
3 WSGIScriptAlias /WSGI /var/www/html/cgi/westos.wsgi ##查询WSGI时执行westos.wsgi
4
systemctl restart httpd
测试
6、https
yum install mod_ssl -y ##按装软件使可以使用https
yum install crypto-utils -y ##安装软件使可以修改安全证书
genkey www.westos.com ##修改安全证书
注意不要上传
vim /etc/httpd/conf.d/ssl.conf
100 #SSLCertificateFile /etc/pki/tls/certs/localhost.crt
101
102 SSLCertificateFile /etc/pki/tls/certs/www.westos.com.crt ##安全认证
108 #SSLCertificateKeyFile /etc/pki/tls/private/localhost.key
109
110 SSLCertificateKeyFile /etc/pki/tls/private/www.westos.com.key ##钥匙存放的位置
systemctl restart httpd
7、设定https虚拟主机并设定网页重写
mkdir /var/www/virtual/westos.com/login
cd /var/www/virtual/westos.com/login/
vim index.html
login page
cd /etc/httpd/conf.d/
cp -p music.conf zlogin.conf
vim zlogin.conf
1 <VirtualHost *:443> ##修改端口443
2 ServerName “login.westos.com”
3 DocumentRoot “/var/www/virtual/westos.com/login”
4 CustomLog logs/login.log combined
5 SSLEngine on ##开启设置
6 SSLCertificateFile /etc/pki/tls/certs/www.westos.com.crt ##看/etc/httpd/conf.d/ssl.conf
7 SSLCertificateKeyFile /etc/pki/tls/private/www.westos.com.key
8
9 <Directory “/var/www/virtual/westos.com/login”>
10 Require all granted
11
12 <VirtualHost :80>
13 ServerName “login.westos.com”
14 RewriteEngine on
15 RewriteRule ^(/.)$ https://%{HTTP_HOST}KaTeX parse error: Expected 'EOF', got '#' at position 19: …[redirect=301] #̲#地址栏输入的地址专成加密
… ##客户在浏览器地址栏中输入的所有字符
https:// ##强制客户加密访问
%{HTTP_HOST} ##客户请求主机
$1 ##“1”表示(/.∗)1”表示^(/.*)1”表示(/.∗)的值
[redirect=301] ##永久重写 302表示临时
测试: 在真机中 vim /etc/hosts
login.westos.com
浏览器搜索login.westos.com查看效果