linux之apache的配置

什么是apache

Apache是世界使用排名第一的Web服务器软件。它可以运行在几乎所有广泛使用的计算机平台上,由于其跨平台和安全性被广泛使用,是最流行的Web服务器端软件之一。它快速、可靠并且可通过简单的API扩充,将Perl/Python等解释器编译到服务器中。同时Apache音译为阿帕奇,是北美印第安人的一个部落,叫阿帕奇族,在美国的西南部。也是一个基金会的名称、一种武装直升机等等。

为什么要使用apache

企业中常用来使用web服务,用来提供http服务

apache的特点

Apache:稳定性高,对客户端的响应是支持并发的,
nginx:快
在这里插入图片描述

apache的配置

配置服务环境
设置访问主机host
配置主机网络

安装软件以及策略放通

yum search http
yum install httpd -y            ##apache软件
yum install httpd-manual        ##apache的手册
systemctl start httpd			##启动Apache服务
systemctl enable httpd			##开机启动Apache服务
firewall-cmd --list-all                         ##列出火墙信息
firewall-cmd --permanent --add-service=http     ##永久允许http
firewall-cmd --reload                           ##火墙重新加载策略

配置文件内容

/etc/httpd/conf	主配置目录
/etc/httpd/conf/httpd.conf	主配置文件
/etc/httpd/conf.d	子配置目录
/etc/httpd/conf.d/*.conf	子配置文件
80	默认端口
/var/www/html	默认发布目录
index.html	默认发布文件
httpd_sys_content_t	默认安全上下文
apache	程序开启默认用户
/etc/httpd/logs/*	apache日志

在这里插入图片描述
在这里插入图片描述
打开apache服务 systemctl start httpd
设置服务开机自启 systemctl enable httpd
在这里插入图片描述
查看配置文件:
在这里插入图片描述
编辑测试页面文件:
默认访问路径为/var/www/html
访问文件 index.html
在这里插入图片描述
测试:
在这里插入图片描述

实验一、
修改默认端口
在这里插入图片描述
默认端口访问,无法访问通:
在这里插入图片描述
添加端口访问http:/192.168.181.140:8080
在这里插入图片描述
恢复配置文件

实验二、更改默认发布目录
关闭selinux

1.建立默认发布目录以及文件
mkdir -p /westos/html
vim/westos/html/index.html
	<h1>westos westos</h1>
2.修改配置文件:   
setenforce 0 #关闭selinux
vim /etc/httpd/conf/httpd.conf
119:  DocumentRoot"/westos/html" #更改默认发布目录,将原来的注释掉
124:   <Directory "/westos">
		Require all granted #授权,否则会访问页面会出现Forbidden
      </Directory>
systemctl restart httpd

在这里插入图片描述
在这里插入图片描述
selinux打开的情况,selinux=Enforcing
在这里插入图片描述
在这里插入图片描述
可以看到上图中,当selinux为enforcing时,是没有权限访问index.html文件中的内容,如果在此情况先需要访问的话,就需要配置文件的安全上下文或者bool值;
在这里插入图片描述
修改文件安全上下文:

semanage fcontext -a -t httpd_sys_content_t '/westos/html(/.*)?' 
restorecon -RvvF /westos/html

在这里插入图片描述

访问方式

在这里插入图片描述
实验3:更改默认发布文件

1.新建默认发布文件
需要将配置文件还原
vim /var/www/html/newdefaultpackage.html
	<h3>newdefaultpackage</h3>
systemctl restart httpd   

2.改配置文件
vim /etc/httpd/conf/httpd.conf
165:	DirectoryIndex newdefaultpackage.html index.html
systemctl restart httpd

发布目录和发布文件要保持路径一致,在查找文件的时候,会先进入发布目录,在发布目录中查找发布文件
在这里插入图片描述
在这里插入图片描述

apache的虚拟主机

apache虚拟机的安装

编辑配置文件

vim /etc/httpd/conf.d/vhost.conf

DocumentRoot /var/www/html 授权路径越靠近根,则apache的访问权限越大;
Customlog logs/default.log combined

<VirtualHost *:80> //如果指定访问的是news.westos.
org域名,则访问的是/var/webvirt/westos.org/news/html下面的文件news.html
ServerName news.westos.org
DocumentRoot /var/webvirt/westos.org/news/html
Customlog logs/default.log combined

<VirtualHost *:80>
ServerName music.westos.org ///如果指定访问的是music.westos.org域名,则访问的是/var/webvirt/westos.org/music/html下面的文件,music.html

DocumentRoot /var/webvirt/westos.org/music/html
Customlog logs/default.log combined

<Directory /var/webvirt> ///发布路径授权
Require all Granted

在这里插入图片描述

创建访问的文件

mkdir -p /var/webvirt/westos.org/{music,news}/html
vim /var/webvirt/westos.org/music/html/index.html
music
vim /var/webvirt/westos.org/news//html/index.html
news
解析在浏览器所在主机配置
vim /etc/hosts
192.168.181.140 www.westos.org music.westos.org news.westos.org
在这里插入图片描述

内部访问控制

分类:
基于ip
基于用户

基于ip访问控制:

<Directory “/var/www/html”>
<Directory “/var/www/html”>
Order deny,allow //deny在前,先执行deny的限制
Order allow,deny //allow在前,先执行allow
///后执行的会被后执行的限制所覆盖
Allow from 192.168.181.141 ///只允许141访问
Deny from all

在这里插入图片描述
在140主机进行访问,不通。可以看到设置生效
在这里插入图片描述
取消配置,再次测试:
在这里插入图片描述
先创建用户认证文件:

htpasswd	Manage user files for basic authentication
[root@node01 html]# htpasswd -cm /etc/httpd/.htpassfile admin
New password: 
Re-type new password: 
Adding password for user admin
[root@node01 html]#  htpasswd -m /etc/httpd/.htpassfile admin1
New password: 
Re-type new password: 
Adding password for user admin1
[root@node01 html]# cat /etc/httpd/.htpassfile
admin:$apr1$RxoaxA3e$fibSwdAvKz30ffE/hEa0j1
admin1:$apr1$JYX.qNP2$Msw1l7Tl.SHAwL9.WCDjc.
[root@node01 html]# 

编辑vhost文件:
基于用户访问控制:
<Directory “/var/www/html”>
I AuthUserFile /etc/httpd/.htpassfile
AuthType basic
AuthName “PLease input usename and password”
Require valid-user ///所有的用户都可以访问
或者 Require user admin //允许admin用户访问

在这里插入图片描述
使用没有授权的用户admin1登录,无法登录;
使用授权的用户admin,可以登录;
在这里插入图片描述
在这里插入图片描述
支持的语言:
1、html
2、php 安装
yum install php -y
编写php测试页
vim /var/www/html/index.php

<?php phpinfo(); ?>

重启服务
测试访问:192.268.181.140/index.php

在这里插入图片描述
3、cgi 通用网关接口 使用perl写的,安装httpd-manaul
开启的是动态:比如时间的变化,摄像头调用,
apache不支持动态,所以需要借用cgi显示;
cgi提供的功能:将perl语言实现的具体功能显示出来

安装httpd-manaual工具:
yum install httpd-manual -y
测试:
http://192.168.181.140/manual/
在这里插入图片描述
mkdir /var/www/html/cgi
cd /var/www/html/cgi
vim index.cgi

#!/usr/bin/perl
print “Content-type:text/html\n\n”;
print date;
其中为代码使用的,在~那个键盘上;
chmod +x index.cgi
./index/cgi
在这里插入图片描述
测试:
http://192.168.181.140/cgi/index.cgi
在这里插入图片描述

编辑apache虚拟机文件:
vim /etc/httpd/conf.d/vhost.conf
<Directory “/var/www/html/cgi”>
Options +ExecCGI
Ad systemctl restart httpddHandler cgi-script .cgi

重启服务 systemctl restart httpd
测试:
http://192.168.181.140/cgi/index.cgi
在这里插入图片描述
4、wsgi 支持python语言
yum install mod_wsgi.x86_64 -y 安装插件
编辑配置文件:
vim /etc/httpd/conf.d/vhost.conf

DocumentRoot /var/www/html
Customlog logs/default.log combined
WSGIScriptAlias /zhang /var/www/html/index.wsgi //其中/zhang是一个虚拟目录,可以任意起名字

编写python脚本:
#!/usr/bin/env python
import time
def application (environ, start_response):
response_body = ‘UNIX EPOCH time is now: %s\n’ % time.time()
status = ‘200 OK’
response_headers = [(‘Content-Type’, ‘text/plain’),
(‘Content-Length’, ‘1’),
(‘Content-Length’, str(len(response_body)))]
start_response(status, response_headers)
return [response_body]
重启服务: systemctl restart httpd
测试:http://192.168.181.140/zhang

HTTPS,apache加密模式

支持https安装的软件,安装的插件 yum install mod_ssl.x86_64 -y
genkey命令的使用,安装yum install crypto-utils.x86_64 -y
生成密钥 genkey www.westos.com
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

替换公钥和私钥:
vim ssl.conf
SSLCertificateKeyFile /etc/pki/tls/private/www.westos.com.key
SSLCertificateFile /etc/pki/tls/certs/www.westos.com.0.csr
在这里插入图片描述
重启httpd,
防火墙
添加https服务或者关闭防火墙

网页的重写:自动跳到https://172.25.254.111 http://www.westos.org
^(/.*)$ 浏览器浏览框写入的所有内容
vim /etc/httpd/conf.d/vhost.conf
<Directory “/var/www/htmli/virtual/login”>
Options +ExecCGI
AddHandler cgi-script .cgi
Require all granted

实现从80转到443的重写功能
<VirtualHost *:443>
ServerName login.westos.org
DocumentRoot /var/webvirt/login.org/login/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 :80>
ServerName login.westos.org
RewriteEngine On
RewriteRule ^(/.
)$ https://%{HTTP_HOST} 1 < / V i r t u a l H o s t > / / / / / / / H T T P H O S T 客 户 请 求 主 机 h t t p s : / / 强 制 客 户 加 密 访 问 ( / . ∗ ) 1 </VirtualHost> ///{HTTP_HOST} 客户请求主机 https:// 强制客户加密访问 ^(/.*) 1</VirtualHost>///////HTTPHOSThttps://访(/.) 在地址栏中输入的所有字符
1 表 示 ( / . ∗ ) 1 表示^(/.*) 1(/.) 的值
[redirect=301]
systemctl restart httpd
测试:

浏览器输入login.westos.com,默认为http://login.westos.org
自动转换为https://login.westos.org
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值