linux中web服务--Apache

Apache是web服务器。开源,支持二次开发,稳定性好,支持多种操作系统,支持多种编程语言。

软件名叫apache,软件的文件名是httpd ,软件包名也是httpd

默认端口及协议:tcp 80

web服务器的工作原理:
1)连接过程
2)请求过程
3)应答过程
4)关闭连接

apache的工作模式:
进程模式 prefork:用于用户访问量比较平稳,系统资源较为丰富时。
线程模式 worker:用于用户访问波动很大,与进程模式相比,资源占用较少。
事件模式 event:2.4以上才有,和进程模式类似,只是多出一个事件进程来管理这些请求。

进程 > 线程

一个进程可以包含多个线程。进程是申请资源的最小单位,线程是进程里的一个组成部分,线程模式时,会多个线程共享一个进程所申请的资源。

使用进程模式会产生很多apache的子进程,每个子进程产生1个线程,1个线程响应1个请求,响应结束后资源回收,杀死子进程。进程模式默认产生5个进程。不够再加。当访问量不稳定时,会不停地生成杀死进程。所以上文说进程模式最好用于访问量稳定的时候。

使用线程模式会产生少量的子进程,这个子进程会产生多个线程,每个线程响应1个请求,响应结束后,资源回收,杀死线程。但是其中只要1个线程坏掉,整个进程都会死掉。但是线程去管理请求,但没有线程有一定的时限,防止占用过多的资源。

MPM多路处理模块。apache的工作模式也可以写作,prefork MPM等等。

模式切换:

[root@localhost ~]# vim /etc/httpd/conf.modules.d/00-mpm.conf

修改这个配置文件,将注释打开或者关闭就OK了。

在主配置文件httpd.conf中。有这样一条。
在这里插入图片描述
这个就是指定了模式控制的配置文件的位置。

模式的选项:

Prefork MPM

<IfModule mpm_prefork_module>
    StartServers             5    //服务器启动时建立的子进程数量,prefork默认是5
    MinSpareServers          5    //空闲子进程的最小数量,默认5;如果当前空闲子进程数少于MinSpareServers ,那么Apache将以最大每秒一个的速度产生新的子进程
    MaxSpareServers         10    //空闲子进程的最大数量,默认10;如果当前有超过MaxSpareServers数量的空闲子进程,那么父进程会杀死多余的子进程
    MaxRequestWorkers      256    //限定服务器同一时间内客户端最大接入的请求数量,默认是256;任何超过了MaxRequestWorkers限制的请求都要进入等待队列,一旦一个个连接被释放,队列中的请求才将得到服务
    MaxConnectionsPerChild   1000    //每个子进程在其生命周期内允许最大的请求数量,如果请求总数已经达到这个数值,子进程将会结束,如果设置为0,子进程将永远不会结束。
</IfModule>

Worker MPM

<IfModule mpm_worker_module>
    StartServers             3    
    ServerLimit             16    //系统配置的最大进程数量
    MinSpareThreads         75    //空闲子进程的最小数量,默认75
    MaxSpareThreads        256
    ThreadsPerChild         64    //每个子进程产生的线程数量,默认是64
    MaxRequestWorkers      400    //限定服务器同一时间内客户端最大接入的请求数量
    MaxConnectionsPerChild   1000   //每个子进程在其生命周期内允许最大的请求数量,如果请求总数已经达到这个数值,子进程将会结束,如果设置为0,子进程将永远不会结束
</IfModule>

Event MPM

<IfModule mpm_event_module>
    StartServers             3
    ServerLimit             16
    MinSpareThreads         75
    MaxSpareThreads        250
    ThreadsPerChild         25
    MaxRequestWorkers      400
    MaxConnectionsPerChild   1000
</IfModule>

配置思路:
1、安装软件
2、启动服务,设置开机自启。

1、安装服务,这个非常简单。

[root@localhost named]# yum install httpd -y

2、启动服务

[root@localhost named]# systemctl start httpd
[root@localhost named]# systemctl enable httpd
Created symlink from /etc/systemd/system/multi-user.target.wants/httpd.service to /usr/lib/systemd/system/httpd.service.


[root@localhost named]# systemctl status httpd
● httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled)
   Active: active (running) since Tue 2019-10-29 19:09:27 CST; 40s ago
     Docs: man:httpd(8)
           man:apachectl(8)
running就是启动了。

这时可以打开网页,输入 http://IP 来测试下,能看到Apache的测试页。在这里插入图片描述
Ok 啦。

其中可以找到,/var/www/html。这个目录,这个目录就是放网页源码的目录。可以在这里简单的编辑下。

index.html里的东西。

下面是我随便写得东西。

这里涉及到html编程,不过多介绍了。

[root@geili html]# ls /var/www/html
helloworld.html  index.html  name.html  web-photo.JPG  网卡.JPG  阿里云.JPG

<h1><center size=color='red'>Welcome To Gsc Web Service!!!!!</center></h1>
<center><font size='+2' color='blue'>这是网站的名字</font></center>
<hr>
<br>
<center>看看我都写了什么吧!!!</center>
<br>
<br>
<center>当然是随便敲了几行代码。。。。。QAQ</center>
<br>
<br>
<p><center>这下边的链接都是闹着玩的。。。</center>

<br><center><a href='helloworld.html'>千万别点这里!!</a></center>

<br><center><a href='阿里云.JPG'>这个点了也没啥用QWQ</a></center>

<br><center><a href='web-photo.JPG'>这个随便。。</a></center>

<br><center><a href='网卡.JPG'>这个还有点用。。</a></center>

<br><center><a href='http://172.16.12.10/apache-yum/'>这个应该很有用(yum)</a></center>

<br><center><a href='name.html'>这个一定要进去。。。</a></center>

</html>
~          

页面是这样的。

在这里插入图片描述
这就是一个简单的网站了。。。。。。。。。。。。。。。

#ServerName www.example.com:80 //服务器的名字
可以按自己的喜好修改

Apache的一些功能:

1、配置局域网范围内的网络yum源共享源

创建一个,测试目录名字随便起

挂载。
[root@localhost html]# mkdir cdrom-CentOS7iso
[root@localhost html]# mount -o loop /dev/sr0 /var/www/html/cdrom-CentOS7iso/
[root@localhost html]# mount | grep iso
/dev/sr0 on /mnt/cdrom-CentOS7iso type iso9660 (ro,relatime)
/dev/sr0 on /var/www/html/cdrom-CentOS7iso type iso9660 (ro,relatime)

然后在浏览器地址栏中输入http://IP/cdrom-CentOS7iso 后边的就是挂载点。http://IP 也就等价于 主机中的 /var/www/html/ 。

就能看到一个源了,当然你也可以把这个直接写到index.html里作为连接访问。总之这个就和网络yum源一样,baseurl写浏览器上的有repodata这个目录的链接。

在这里插入图片描述
自动生成yum源。

[root@localhost ~]# yum-config-manager --add-repo http://172.16.12.11/cdrom-CentOS7iso/
Loaded plugins: fastestmirror
adding repo from: http://172.16.12.11/cdrom-CentOS7iso/

[172.16.12.11_cdrom-CentOS7iso_]
name=added from: http://172.16.12.11/cdrom-CentOS7iso/
baseurl=http://172.16.12.11/cdrom-CentOS7iso/
enabled=1

yum clean all ; yum list ;验证。

ok

2、简单的目录访问控制。
Indexes属性。有Options Indexes 时表示没有index.html则显示文件目录。有index.html则显示index.html内容。
有 Options -Indexes 时表示这个不生效。

修改配置文件,这里是编辑了一个配置文件,是子配置文件,按照功能分的,在/etc/httpd/conf.d/ 这里 配置文件名叫dog.conf 只要在配置文件中指向正确的位置就好,名字不重要。

[root@localhost html]# mkdir dog
[root@localhost html]# cp /etc/passwd ./dog/
[root@localhost html]# cp /etc/group ./dog/
[root@localhost html]# cp /etc/shadow ./dog/
[root@localhost html]# ls dog/
group  passwd  shadow
[root@localhost html]# vi /etc/httpd/conf.d/dog.conf
[root@localhost html]# systemctl restart httpd
[root@localhost html]# cat /etc/httpd/conf.d/dog.conf 
<Directory "/var/www/html/dog">
	Options Indexes 
	AllowOverride none
</Directory>

开浏览器测试。

没有index.html文件时显示的是目录的内容。
在这里插入图片描述
有index.html时,显示的是index.html的内容。

在这里插入图片描述修改配置文件:

改为 -indexes

有index.html
在这里插入图片描述没有index.html:
报错
在这里插入图片描述3、是否支持追踪符号链接

FollowSymLinks

[root@localhost dog]# ln -s passwd pp.txt
[root@localhost dog]# ls
group  passwd  pp.txt  shadow
[root@localhost conf.d]# cat dog.conf
<Directory "/var/www/html/dog">
	
	Options Indexes FollowSymLinks
	AllowOverride none

</Directory>

pp.txt是一个软链接链接,指向passwd这个文件。
在配置文件中有FollowSymLinks选项。

此时重启服务,进入查看。

在这里插入图片描述
并且文件能够点击进去,看到链接到的文件内容。
在这里插入图片描述
删除FollowSymLinks后,不显示符号链接文件。

在这里插入图片描述
4、黑白名单

在这里插入图片描述

<Directory "/var/www/html/black-list">
        <RequireAll>
                Require all granted   //让所有的IP能访问
                Require not ip 172.16.12.10   //这个ip不能访问
        </RequireAll>
</Directory>
黑名单的配置文件。

白名单和黑名单类似

<Directory "/var/www/html/white">
		Require  ip 172.16.0.252  //这个IP能访问 别的都不能访问。
</Directory>

5、网页控制

其实这个基本用不到。。。。。。。

在访问网页时要先输入用户和密码能访问。

[root@localhost html]# mkdir access
[root@lo[root@localhost html]# cat /etc/httpd/conf.d/access.conf 
<Directory "/var/www/html/access">
	Options Indexes FollowSymLinks   
	AllowOverride AuthConfig
</Directory>
[root@localhost html]# cat /var/www/html/access/.htaccess 
AuthType Basic   //使用Basic模块
AuthName "Restricted Files"   //控制的名字
AuthBasicProvider file   //
AuthUserFile "/var/www/html/access/.htpasswd"   //设置密码的文件 ,加密后的
Require user kkk  //用户名

[root@localhost html]# htpasswd -c /var/www/html/access/.htpasswd kkk   //设置kkk用户的密码
New password: 
Re-type new password: 
Adding password for user kkk

然后再进入网页时就要先输入账户密码。

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值