大家好,我是互联网老辛,这是我参与更文挑战的第20天, 活动详情查看:更文挑战
安装apache并启动
一般情况下,http系统里都已经安装好了,本文使用的是centos7操作系统,系统信息如下:
[root@itlaoxin-17 ~]# cat /etc/redhat-release
CentOS Linux release 7.7.1908 (Core)
复制代码
内核版本:
root@itlaoxin-17 ~]# uname -r
3.10.0-1062.el7.x86_64
复制代码
在安装之前,我们可以通过qi来查看包是否安装:
[root@itlaoxin-17 ~]# rpm -qi httpd
未安装软件包 httpd
复制代码
如果显示未安装则使用下面的方式进行安装
[root@gaosh-64 ~]# yum install httpd
[root@gaosh-64 ~]# systemctl restart httpd
[root@gaosh-64 ~]# systemctl enable httpd
Created symlink from /etc/systemd/system/multi-user.target.wants/httpd.service to /usr/lib/systemd/system/httpd.service.
复制代码
此时我们已经安装好httpd,如果这时候在使用qi,就不再显示未安装
[root@itlaoxin-17 ~]# rpm -qi httpd
Name : httpd #包的名字
Version : 2.4.6 ## 包的版本
Release : 97.el7.centos
Architecture: x86_64
Install Date: 2021年06月21日 星期一 06时20分37秒 ## 包的安装时间
Group : System Environment/Daemons
Size : 9821064
License : ASL 2.0
Signature : RSA/SHA256, 2020年11月18日 星期三 22时17分43秒, Key ID 24c6a8a7f4a80eb5
Source RPM : httpd-2.4.6-97.el7.centos.src.rpm
Build Date : 2020年11月17日 星期二 00时21分17秒
Build Host : x86-02.bsys.centos.org
Relocations : (not relocatable)
Packager : CentOS BuildSystem <http://bugs.centos.org>
Vendor : CentOS
URL : http://httpd.apache.org/
Summary : Apache HTTP Server
Description :
The Apache HTTP Server is a powerful, efficient, and extensible
web server.
复制代码
可以看到整个httpd包的详细信息
然后我们来探讨apache的常见配置和参数
常见配置及参数
vim /etc/httpd/conf/httpd.conf
ServerRoot "/etc/httpd" #用于指定Apache运行的根目录
Listen 80 #监听80端口
MaxClients 256 #指定同时能访问服务器的客户机数量为256
DocumentRoot "/var/www/html" #网页文件存放的目录
DirectoryIndex index.html index.html.var #默认网站主页
Include conf.d/*.conf #读取/etc/httpd/conf/conf.d/目录中所有以.conf结尾的文件
ServerName www.wg.com #域名
ServerAdmin #设置管理员的邮箱
Include conf.d/*.conf #包含的子配置文件
User apache #用户是apache
Group apache #用户组是apache
Directory #认证授权和访问控制
##################################
<IfModule prefork.c> #当httpd服务使用的profork模型的时候:
StartServers 10 #默认启动10个作业进程
MinSpareServers 10 #空闲进程数不低于10个
MaxSpareServers 20 #空闲进程数最大20个
ServerLimit 256 #最多可以启动256个进程
MaxClients 256 #最大并发客户端数为256个
MaxRequestsPerChild 4000 #每个进程可以处理4000个请求,超过此数目进程被杀死并重新创建
</IfModule>
复制代码
需要注意的是:ServerLimit最大值为20000个,并且:由于profork是单一线程的进程,所以每个进程在同一时间里仅能处理一个请求(也就是一个请求一个进程),所以MaxClients的值要和ServerLimit一致。而且,profork的开销比较大,不过稳定性比较强。
使用案例:
搭建一台测试web服务器
要求如下:
- 首页为index.html,访问时候显示为“大家好,我是高胜寒,我正在设置测试机”
- 管理员的Email地址为:gaosh@yeah.net
- 网页编码采用UTF-8
- 所有网页均存放在 /var/www/html/目录下
- apache的配置文件根目录设置为/etc/httpd目录
注: 这里有很多都是默认的,我们来看一下如何配置。
切记: 修改配置文件前一定要备份
1. 备份配置文件
[root@gaosh-64 ~]# cp /etc/httpd/conf/httpd.conf httpd.bak
复制代码
2. 修改配置文件
[root@gaosh-64 ~]# vim /etc/httpd/conf/httpd.conf
31 ServerRoot "/etc/httpd" #apache配置文件的根目录
32 Timeout 60 #添加此项,超时时间
42 Listen 80 #监听的端口
复制代码
改:86 ServerAdmin root@localhost
为:86 ServerAdmin gaosh@yeah.net #设置管理员,e-mail 地址
改:95 #ServerName www.example.com:80
为:95 ServerName 192.168.1.64:80 #服务器主机名
119 DocumentRoot “/var/www/html” #网站页面根目录
144 Options Indexes FollowSymLinks #当一个目录没有默认首页时,允许显示此目录列表
改:164 DirectoryIndex index.html
为:164 DirectoryIndex index.html index.php #指定默认首页
316 AddDefaultCharset UTF-8 #设置服务器的默认编码为: UTF-8
3. 取消apache默认欢迎页
给文件加#号, 我这里使用的是ctrl +v –I –#–两下ECS
还记得这个方法吗? 看图:
[root@gaosh-64 ~]# vim /etc/httpd/conf.d/welcome.conf
此处使用的是动图演示,请仔细查看哦
4. 修改网页内容(index.html)
[root@gaosh-64 ~]# cat /var/www/html/index.html
大家好,我叫高胜寒,我正在设置测试页面
[root@gaosh-64 ~]#
5. 启动并测试
[root@gaosh-64 ~]# curl 192.168.1.64
大家好,我叫高胜寒,我正在设置测试页面
复制代码
总结:
apache 使我们常用的web容器之一,需要下苦功夫研究。