文章目录
1.apache下的http服务基本知识
- apache 是一款可以稳定同步阻塞的、提供http协议访问的软件动态资源服务器 ;与ngnix不同,ngnix无法处理动态页面
- apache软件名称:httpd
- http协议服务端口:80端口
- https服务端口:443端口
- 程序开启默认用户:apache
- 主配置文件:/etc/httpd/conf/httpd.conf
- 默认发布目录 :/var/www/html
- 默认发布页面 :/var/www/html/index.html
- http服务日志文件:/etc/httpd/logs/*
2.http服务的配置
(1)配置:
yum install -y httpd下载apache软件yum install -y httpd-manual下载http服务说明手册
systemctl start httpd开启服务firewall-cmd --permanent --add-service=http在火墙添加httpd服务
firewall-amd --reload重新加载
(2)排错:
当在其他主机浏览器中搜索httpd服务器ip时显示如下无法连接页面,从两方面排错:
- 防火墙未添加http服务
- httpd服务本身未开启

服务正常界面:

3.修改httpd服务默认属性
3.1修改默认端口
配置:
(1)vim /etc/httpd/conf/httpd.conf 编辑配置文件
42 Listen 8080 ##将默认服务端口修改为8080端口
(2)firewall-cmd --permanent --add-port=8080/tcp 在火墙开启8080端口,该端口是tcp协议下的
firewall-cmd --reload 重加载
(3)systemctl restart httpd 重启服务


测试:
在浏览器中访问http://172.25.254.201 将显示无法连接:

应该访问 http://172.25.254.201:8080 如下:

3.2修改默认发布文件
- 默认发布文件 即在浏览器中访问服务ip时默认访问的文件
- 修改默认发布文件 即为修改默认发布文件的顺序
(1)vim /etc/httpd/conf/httpd.conf 编辑配置文件
164 DirectoryIndex saturday index.html ##修改后默认访问文件为/var/www/html/saturday
(2)systemctl restart httpd 重启服务

在浏览器中访问:

3.3修改默认发布目录
配置:
(1)vim /etc/httpd/conf/httpd.conf 在配置文件中修改
DocumentRoot “/mnt/html”
< Diretory "/mnt">
Require all granted ##授权访问
< /Diretory >
(2)setenfirce 0 设置selinux为非Enforcing模式 否则加载了安全上下文 无法访问
(3)systemctl restart httpd 重启服务


测试:
在浏览器中访问 http://172.25.254.201/index.html 可以看到/mnt/html/index.html文件中内容:

4.http访问控制
4.1指定主机访问指定文件
vim /etc/httpd/conf/httpd.conf 编辑配置文件
-
黑名单 (不允许谁登录)
<Directory “/var/www/html/westos”> ##控制访问的文件
Order Allow,Deny ##服务读取顺序先读Allow 后读Deny
Allow from a
ll ##允许所有主机登录
Deny from 172.25.254.1 ##不允许172.25.254.1这台主机登录
</ Directory>
systemctl restart httpd重启服务



-
白名单 (允许谁登录)
<Directory “/var/www/html/westos”> ##控制访问的文件
Order Allow,Deny ##服务读取顺序先读Allow 后读Deny
Deny from all
Allow from 172.25.254.1 ##只允许172.25.254.1这台主机登录
</ Directory>
systemctl resatrt httpd重启服务



注:Order 表示读取顺序,后读取的文件会覆盖先读取的文件 故顺序十分重要!!
4.2 指定用户访问
4.2.1 用户的创建
(1)touch /etc/httpd/conf/user 建立用户存放文件
(2)htpasswd -cm /etc/http/conf/user user1 第一次创建用户需要加-c参数 表示在用户文件下创建用户user1 输入用户密码 存放在文件时为加密内容
4.2.2 用户访问控制
配置:
(1)vim /etc/httpd/conf/httpd.conf 编辑主配置文件
<Directory "/var/www/html/westos"> ##控制的访问文件
AuthUserFile /etc/httpd/conf/user ##服务验证的文件即为用户文件
AuthType basic ##基础验证类型
AuthName "Pleace input your username and password!" 验证模块自定义显示内容
Require user user1 ##允许访问的用户为user1用户
或者 Require valid-user ##允许所有有效用户
</Directory>
(2)systemctl restart httpd 重启服务

测试:
在浏览器中访问http服务器ip 会弹出输入密码对话框,输入密码方能访问:
user1被允许访问


user2不被允许访问 :输入密码之后会再次弹出输入密码对话框,不能正常访问


5. 一个http服务对应多个站点(以2个为例)
配置:
(1) 在默认发布目录下建立站点对应发布目录
mkdir /var/www/news/html -p 建立news站点发布目录
mkdir /var/www/music/html -p 建立music站点发布目录
vim /var/www/virtual/music/html/index.html 编辑站点发布内容
MUSIC
vim /var/www/virtual/news/html/index.html 编辑站点发布内容
NEWS

(2) 在子配置目录下建立不同站点配置文件
vim /etc/httpd/conf.d/default.conf 默认站点
<VirtualHost _default_:80> ##默认开放端口:80
DocumentRoot /var/www/html ##默认发布目录
CustomLog logs/default.log combined ##日志文件 combined表示混合日志
</VirtualHost>

vim /etc/httpd/conf.d/news.conf news站点
<VirtualHost *:80> ##开放所有端口:80
ServerName news.westos.com
DocumentRoot /var/www/virtual/news/html ##默认发布目录
CustomLog logs/news.log combined ##日志文件 combined表示混合日志
</VirtualHost>
<Directory "var/www/virtual/news/html">
Require all granted
</Directory>

vim /etc/httpd/conf.d/music.conf music站点
<VirtualHost *:80> ##开放所有端口:80
ServerName music.westos.com
DocumentRoot /var/www/virtual/music/html ##默认发布目录
CustomLog logs/music.log combined ##日志文件 combined表示混合日志
</VirtualHost>
<Directory "var/www/virtual/music/html">
Require all granted
</Directory>

(3) vim /etc/hosts 编辑本地域名解析文件
172.25.254.201 www.westos.com news.westos.com music.westos.com

测试:
在浏览器输入www.westos.com:

在浏览器中输入news.westos.com:

在浏览器中输入music.westos.com:

6. php+cgi脚本 动态网页访问
配置:
(1)配置 php动态插件
vim /var/www/html/index.php 编辑php发布文件
<?php ##用php语言编写
phpinfo();
?>
yum install -y php 安装php动态插件
systemctl restart httpd 重启apache服务php插件自动配置好
浏览器访问会出现如下界面:

(2)配置 cgi脚本
vim /var/www/html/index.cgi 编辑动态访问文件
!/usr/bin/perl ##perl语言编写
print "Content-type:text/html\n\n";
print `date`;
chmod +x /var/www/html/index.cgi 给该文件执行权力


(3)vim /etc/httpd/conf.d/default.conf 编辑动态访问配置文件
<Directory "/var/www/html/">
Options +ExecCGI
AddHandIer cgi-script .cgi
</Directory>

注: 编写配置文件可查看说明手册 http://172.25.254.201/manual 不易出错


(4)setenforce 0 设置selinux 为非强制模式
systemctl restart httpd 重启服务
测试:
在浏览器中访问http://172.25.254.201/index.cgi 可以看到动态变化:

7. 强制重定向
curl -I可以查看网页重定向和架构信息
| 302 | 临时重定向 |
|---|---|
| 301 | 永久重定向 |
| 200 | 最终定向 |

配置:
(1)apache中ssl安全认证书配置
yum install -y mod_ssl 重定向软件 下载后子配置目录下会自动生成ssl.conf文件
systemctl restart httpd 重启服务
在浏览器中搜索:https://172.25.254.201 出现如下图界面:

可查看默认证书:


为方便下一步配置 ,查看后应删除该证书 :

清空缓存记录 再次访问又会回到首次访问有黑金图标界面
(2)自签名证书配置
1)yum install -y crypto- utils 下载自签名书生成软件
2)genkey www.westos.com 生成自签名书






生成成功后会终端显示证书相关文件:

3)vim /etc/httpd/conf.d/ssl.conf 编辑ssl.conf 文件 让系统去读签名证书密码文件
101 SSLCertificateFile /etc/pki/tls/certs/www.westos.com.crt
108 SSLCertificateKeyFile /etc/pki/tls/private/www.westos.com.key
将文件改为自生成的证书文件

4)systemctl restrat httpd 重启服务
测试:
在浏览器中查看自签名证书:

(3)重写网页配置
1)编辑想要重定向的网页的配置文件 这里以news.westos.com 为例
vim /etc/httpd/conf.d/news.conf
<VirtualHost *:443> ##https网页配置
DocumentRoot /var/www/news/html
ServerName news.westos.com
CustomLog logs/news.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/news/html">
Require all granted
</Directory>
<VirtualHost *:80> #80端口重定向到443端口
ServerName news.westos.com
RewriteEngine On
RewriteRule ^(/.*)$ https://%{HTTP_HOST}$1 [redirect=301]
</VirtualHost>
^(/.*)$ https://%{HTTP_HOST}$1 [redirect=301] 含义
| 字符 | 含义 |
|---|---|
| ^(/.*)$ | 客户在浏览地址栏中输入的所有字符 |
| https:// | 强制客户加密访问 |
| %{HTTP_HOST} | 客户请求主机 |
| $1 | 表示^(/.*)$的指 |
| [redirect=301] | 永久重定向 |
2)systemctl restart httpd 重启服务

测试:
在浏览器中访问:news.westos.com 可以看到网页重定向到https

8.wsgi动态网页的访问
- WSGI=Web Service Gateway Interface 多线程的动态服务接口
- Python语言书写
配置:
(1)mkdir -p /var/www/wsgi/html单另创建wsgi的发布目录
cd /var/www/wsgi/html 编写发布文件这里是直接下载Python脚本
wget http://172.25.254.250/RHCEPCAGES/materials/script.wsgi


(2)cd /etc/httpd/conf.d 编辑httpd服务子配置目录文件
vim wsgi.conf
<VirtualHost *:80>
ServerName www.wsgi.com
WSGIScriptsAlias / /var/www/wsgi/html/script.wsgi ##让服务重导向阅读编写的脚本文件
</VirtualHost>

(3)yum install -y mod_wsgi下载wsgi动态模块

(4)systemctl restart httpd 重启服务,动态模块自动配置好
(5)vim /etc/hosts 编辑本地域名文件
172.25.254.201 www.wsgi.com
测试:
在浏览器上访问:www.wgsi.com

Apache HTTP 服务配置指南
本文详细介绍Apache HTTP服务的基础知识、配置方法、属性修改、访问控制、多站点配置、动态网页访问、重定向及WSGI服务等核心内容。
6690

被折叠的 条评论
为什么被折叠?



