Apache学习笔记(B站自学)

1.概念

网站服务程序,也叫Web服务,指能够通过浏览器访问到互联网中文档资源的服务Apache Nginx IIS等,Web服务是被动程序,只有接受到互联网中其他电脑的请求后才会响应,然后使用HTTP(超文本传输协议)或HTTPS(超文本安全传输协议)将指定文件传送到客户的浏览器上

Apache是Apache软件基金会的一个开源网页服务器,是世界使用排名第一的Web服务器软件。其跨平台和安全性被广泛使用

支持基于IP、端口或域名的虚拟主机

支持多种方式的HTTP认证

多种模块的支持

2.安装

yum -y install httpd(在centos7安装成功,centos8安装失败后用宝塔安装)

3.管理

systemctl start|stop|status|restart httpd

systemctl disable|enable httpd

测试 127.0.0.1 | localhost

4.配置

/etc/httpd 服务目录

/etc/httpd/conf/httpd.conf 配置文件(重点学习)

/var/www/html 默认网站数据目录

(宝塔安装很多路径不同,默认网站数据/www/server/apache/htdocs标记一下)

/var/log/httpd/ 日志

默认监听80端口

启动一个(pid在/etc/httpd/run/httpd.pid)主进程(控制进程)和多个子进程

Apache是一个模块化设计的服务,核心只包含主要功能,扩展功能通过模块化实现(可扩展性强,各功能依赖性低)不同模块可以被静态编译进程序,也可以动态加载(通过DSO,Dynamic shared Object)实现

httpd -M 查看模块

配置详解/etc/httpd/conf/httpd.conf

Apache安装成功后自动创建的用户可以在文件中查看

ServerRoot 服务目录 /etc/httpd

Listen [ip]:80 [协议]   监听端口,也可改成192.168.200.130:80后localhost无法访问,也可以多加一行用于多监听一个端口

httpd -t 用于查看配置语法有无问题

Include 加载模块  conf.modules.d/*.conf 

IncludeOptional 加载模块(不报错) conf.d/*.conf  自定义模块

ServerAdmin 管理员邮箱 root@localhost

ServerName 默认主机域名 www.example.com:80      ServerName 需要打开(不用#注释)

DocumentRoot 站点根目录  "/var/www/html"


    DirectoryIndex index.html
     如果有dir_module就运行设定服务文件为index.html


    Require all denied
  所有.ht*文件被禁止

LogLevel warn 错误日志记录级别:依次有debug, info, notice, warn, error, crit,alert, emerg大于等于warn会记录)

LogFormat 格式命名 "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" 格式combined 名字

CustomLog "logs/access_log" combined  连接日志 地址 选定格式(上条命名的)

下两条用两种工具分割连接记录为每天 后三条为错误日志记录
Customlog "|/usr/sbin/rotatelogs -l logs/access_%Y%m%d.log 86400" combined
Customlog "|/usr/local/sbin/cronolog logs/access_%Y%m%d.log" combined

ErrorLog "logs/error_log"
ErrorLog "|/usr/sbin/rotatelogs -l logs/error_%Y%m%d.log 86400"
ErrorLog "|/usr/local/sbin/cronolog logs/error_%Y%m%d.log"

alias_module

Redirect /itcast http://www.itcast.cn    外部映射

Alias /images /var/images  本地文件映射 以下是本地文件相关配置


    AllowOverride None
    Options Indexes                  开启目录结构           
    Require all granted              开启权限


ScriptAlias /cgi-bin/ "/var/www/cgi-bin/"    在网址运行脚本 localhost/cgi-bin/hello.py

模块配置详解/etc/httpd/conf.d

基于域名的虚拟主机

/etc/hosts      (服务器上访问的服务器配置,如果用window访问改为IP地址)

127.0.0.1 www.linux1.org
127.0.0.1 www.linux2.org
127.0.0.1 www.mylinux.org

mkdir -p /var/wwwroot/linux1 创建网站数据目录

echo linux1 > /var/wwwroot/linux1/index.html  写入首页文件

/etc/httpd/conf.d/linux1.conf   配置模块文件


        ServerName www.mylinux.org
        ServerAlias www.linux1.org
        ServerAlias www.linux2.org
        DocumentRoot "/var/wwwroot/linux1"
       
                AllowOverride None
                Require all granted
       

        RewriteEngine on                                                                  开启重写功能
        RewriteCond %{HTTP_HOST} ^www.linux1.org$ [OR]         重写条件
        RewriteCond %{HTTP_HOST} ^www.linux2.org$
        RewriteRule ^/(.*)$ http://www.mylinux.org/$1 [R=301,L]        重写规则    (实现域名跳转)

rewrite重写功能用于SEO优化或是url路径的简洁 、防盗链、域名跳转,需要开启mod_rewrite模块

RewriteCond:

HTTP_HOST       

HTTP_REFERER 判断访问者来源 

REQUSET_FILENAME 匹配当前访问的文件 -d -f

RewriteRule Pattern正则匹配 Substitution 匹配的替换内容 [flags]参数限制

参数限制:

NC nocase正则匹配时忽略大小写

R redirect强制重定向,适合匹配pattern后substitute是一个http地址url的情况,就跳转出去了

L last结尾规则 匹配到了就立即停止,类似编程语言中的break

#       RewriteEngine on                            
#       RewriteCond %{HTTP_REFERER} !^http://192.168.200.130/.*$ [NC]
#       RewriteRule .*\.(gif|jpg|png)$ - [F]

若有图片则为 RewriteRule .*\.(gif|jpg|png)$  http://192.168.200.130/other/nolink.png [R,NC,L]
#       SetEnvifNoCase Referer "^$" local_ref               直接输入网址允许访问
#       SetEnvifNoCase Referer "192.168.200.130/.*$" local_ref     允许访问的网址 白名单
#      
#               Require all denied
#               Require env local_ref
#      
                                   /etc/httpd/conf/httpd.conf  (实现防盗链)

遇到没有权限问题是使用  setenforce 0 解决


 

你可能感兴趣的:(apache,学习,笔记)