Linux搭建Apache
Posted 南岸青栀*
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Linux搭建Apache相关的知识,希望对你有一定的参考价值。
1.配置yum仓库:
注:RedHat8中dnf为yum的新版本;并且8中在yum源中增加大量的资源包。分为APPstream,BaseOS
[root@localhost 100]# vim /etc/yum.repos.d/base.repo
[base]
name=base
baseurl=file:///mnt/BaseOS
enable=1
gpgcheck=0
[Appstream]
name=appstream
baseurl=file:///mnt/AppStream
enable=1
gpgcheck=0
2.安装httpd资源包
3.启动httpd服务
[root@localhost 100]# systemctl start httpd
[root@localhost 100]# systemctl status httpd -l
4.http的基本结构
1./etc/httpd/conf/httpd.conf 主要的配置文件 。
2./etc/httpd/conf.d/*.conf 额外的参数文件。
- 如果你不想要修改原始配置文件httpd.conf的话,那么你可以将你自己的额外参数文件独立出来,例如你想要有自己的额外设置值,可以将它写入/etc/httpd/conf.d/zhuji.conf(注意,扩展名一定是.conf),而启动Apache时,这个文件就会被读入主要配置文件当中了。
3./var/www/html/
- 这就是默认的首页所在目录,当输入网址时所显示的数据,就是放在这个目录当中的首页文件(默认为index.html)。
4./var/www/cgi-bin/
- 默认给一些可执行的CGI(网页程序)程序放置的目录,当输入网址/cgi-bin/时所显示的数据所在。
5./var/log/httpd/
- 默认的Apache日志文件都放在这里,对于流量比较大的网站来说,一个星期的日志文件的数据可以达到1GB左右。
主配置文件内容**😗*[root@localhost ~]# vim /etc/httpd/conf/httpd.conf
31 ServerRoot "/etc/httpd" http服务的顶级目录为/etc/httpd
42 Listen 80监听在80端口,80为web服务器的默认端口
56 Include conf.modules.d/*.conf 包括/etc/httpd/conf.modules.d/*.conf的所有文件
66 User apache服务的用户(ps -ef | grep httpd,先以root用户把/usr/sbin/httpd服务启动起来)。启动服务后转换的身份,在启动服务时通常以root身份,然后转换身份,这样增加系统安全
67 Group apache
86 ServerAdmin root@localhost你的邮箱,有事的时候给你发邮件
95 #ServerName [www.example.com:80](www.example.com:80) ServerName 0.0.0.0:80匹配任意IP地址,监听端口在80端口
默认是不需要指定的,服务器通过名字解析过程来获得自己的名字,但如果解析有问题(如反向解析不正确),或者没有DNS名字,也可以在这里指定ip地址,当这项不正确的时候服务器不能正常启动。解决办法就是启动该项把www.example.com:80修改为自己的域名或者直接修改为localhost
102 <Directory /> 目录为根,<>为起始标志,</>为结束标志
103 AllowOverride none 不允许这个目录下的访问控制文件来改变这里的配置,这也意味着不用查看这个目录下的访问控制文件。
104 Require all denied 拒绝访问根
105 </Directory> 和<Directory />是一组标签,目录控制容器
119 DocumentRoot "/var/www/html"网页文件存放的目录
124 <Directory "/var/www">
125 AllowOverride None
126 # Allow open access:
127 Require all granted
128 </Directory>
131 <Directory "/var/www/html">
144 Options Indexes FollowSymLinks 索引,跟踪软链接
151 AllowOverride None
156 Require all granted
157 </Directory>
163 <IfModule dir_module>加载一个目录模块
164 DirectoryIndex index.html
165 </IfModule>
171 <Files ".ht*">不能访问
172 Require all denied
173 </Files>
182 ErrorLog "logs/error_log"
189 LogLevel warn
191 <IfModule log_config_module>日志配置模块 /var/log/httpd,日志模块:通过时间节点去记录(man date)
196 LogFormat "%h %l %u %t \\"%r\\" %>s %b \\"%{ Referer}i\\" \\"%{User-Agent}i\\"" combined
197 LogFormat "%h %l %u %t \\"%r\\" %>s %b" com mon
198
199 <IfModule logio_module>
201 LogFormat "%h %l %u %t \\"%r\\" %>s %b \\" %{Referer}i\\" \\"%{User-Agent}i\\" %I %O" combi nedio
202 </IfModule>
217 CustomLog "logs/access_log" combined
218 </IfModule>
220 <IfModule alias_module>别名模块
247 ScriptAlias /cgi-bin/ "/var/www/cgi-bin/"
248
249 </IfModule>
cgi(通用网关接口)是web服务器运行时外部程序的规范,按cgi编写的程序可以扩展服务器的功能。cgi应用程序能与浏览器进行交互,还可通过数据库API与数据库服务器等外部数据源进行通信,从数据库服务器中获取数据
255 <Directory "/var/www/cgi-bin">
256 AllowOverride None
257 Options None
258 Require all granted
259 </Directory>
261 <IfModule mime_module> 多用途互联网邮件扩展模块
266 TypesConfig /etc/mime.types
283 AddType application/x-compress .Z
284 AddType application/x-gzip .gz .tgz
305 AddType text/html .shtml
306 AddOutputFilter INCLUDES .shtml
307 </IfModule>
mime多用途互联网邮件扩展类型,是设定某种扩展名的文件用一种应用程序来打开的方式类型,当该扩展名文件被访问时,浏览器会自动使用指定应用程序来打开。多用于指定一些客户端自定义的文件名,以及一些媒体文件打开方式。
316 AddDefaultCharset UTF-8 默认字符集
318 <IfModule mime_magic_module>
324 MIMEMagicFile conf/magic
325 </IfModule>
348 EnableSendfile on
353 IncludeOptional conf.d/*.conf
实验一:搭建一个web服务器,访问该服务器时显示“hello world”欢迎界面。
[root@localhost 100]# echo Hello world! > /var/www/html/index.html
[root@localhost 100]# echo 你好! >> /var/www/html/index.html
[root@localhost 100]# curl 192.168.10.136
实验二:建立两个基于ip地址访问的网站
1、该网站ip地址的主机位为100,设置DocumentRoot为/www/ip/100,网页内容为:this is 100。
2、该网站ip地址主机位为200,设置DocumentRoot为/www/ip/200,网页内容为:this is 200。
在图形化界面中给网卡添加IP地址
查看添加的IP地址
#查看命令
ip a
ifconfig
定义基于不同ip地址来访问网站的配置文件
示例文件[root@localhost ~]# vim /usr/share/doc/httpd-2.4.6/httpd-vhosts.conf
root@localhost 100]# vim /etc/httpd/conf.d/vhost.conf
<Directory /www>
AllowOverride none
Require all granted
</Directory>
<VirtualHost 192.168.10.100:80>
ServerAdmin root@Localhost
DocumentRoot /www/100
ServerName 192.168.10.100
</VirtualHost>
<VirtualHost 192.168.10.200:80>
ServerAdmin root@Localhost
DocumentRoot /www/200
ServerName 192.168.10.200
</VirtualHost>
创建文件目录并定义网页内容
[root@localhost /]# mkdir /www/{100,200} -pv
mkdir: created directory '/www'
mkdir: created directory '/www/100'
mkdir: created directory '/www/200'
[root@localhost /]# man mkdir
[root@localhost 100]# echo This is 100 > /www/100/index.html
[root@localhost 100]# echo This is 200 > /www/200/index.html
关闭防火墙,重启http服务
[root@localhost ~]# setenforce 0
[root@localhost ~]# systemctl stop firewalld.service
[root@localhost 100]# systemctl restart httpd.service
进行测试:
[root@localhost 100]# curl 192.168.10.100
This is 100
[root@localhost 100]# curl 192.168.10.200
This is 200
实验三:建立两个基于不同端口访问的网站,要求如下:
1、建立一个使用web服务器默认端口的网站,设置DocumentRoot为/www/port/80,网页内容为:the port is 80。
2、建立一个使用10000端口的网站,设置DocumentRoot为/www/port/10000,网页内容为:the port is 10000。
实验四:建立两个基于域名访问的网站,要求如下:
1、新建一个网站,域名为www.ceshi.com,设置DocumentRoot为/www/name,网页内容为this is test。
2、新建一个网站,域名为rhce.first.day,同时可通过ce.first.day访问,设置DocumentRoot为/www/ce,网页内容为:today is first day of class。
配置本地DNS缓存
Linux:
windows:
windows下的hosts文件路径:C:\\Windows\\System32\\drivers\\etc\\hosts
测试:
HTTP协议(超文本传输协议)
组成:
请求报文:
(1)请求行:包含请求方法,URI,HTTP版本协议
(2)请求首部字段
(3)请求内容实体
响应报文:
(1)状态行:包含HTTP版本,状态码,状态码原因短语
(2)响应首部字段
(3)响应内容实体
HTTP常见的请求方法有哪些?
方法 | 描述 | 是否包含主体 |
---|---|---|
GET | 从服务端获取指定信息 | 否 |
POST | 向服务端发送待处理的数据 | 是 |
HEAD | 从服务端获取指定信息的头部 | 否 |
PUT | 向服务端发送数据并替换服务端上指定的数据 | 是 |
OPTIONS | 查询针对请求URL指定的资源支持 | 否 |
DELETE | 从服务端删除指定数据 | 否 |
TRACE | 沿着目标资源的路径执行消息环回测试 | 否 |
常见的HTTP相应状态码
- 200:请求被正常处理( OK )
- 204:请求被受理但没有资源可以返回
- 206:客户端只是请求资源的一部分,服务器只对请求的部分资源执行GET方法,相应报文中通过Content-Range指定范围的资源。
- 301:永久性重定向
- 302:临时重定向
- 303:与302状态码有相似功能,只是它希望客户端在请求一个URI的时候,能通过GET方法重定向到另一个URI上
- 304:发送附带条件的请求时,条件不满足时返回,与重定向无关( Not Modified )
- 307:临时重定向,与302类似,只是强制要求使用POST方法
- 400:请求报文语法有误,服务器无法识别
- 401:请求需要认证
- 403:请求的对应资源禁止被访问
- 404:服务器无法找到对应资源( Not Found )
- 500:服务器内部错误( Internal Server Error )
- 502:前面代理服务器联系不到后端的服务器出现( Bad Gateway )
- 503:服务器正忙
- 504:这个是代理能联系到后端的服务器,但是后端的服务器在规定的时间内没有给代理服务器响应( Gateway Timeout )
源。
- 301:永久性重定向
- 302:临时重定向
- 303:与302状态码有相似功能,只是它希望客户端在请求一个URI的时候,能通过GET方法重定向到另一个URI上
- 304:发送附带条件的请求时,条件不满足时返回,与重定向无关( Not Modified )
- 307:临时重定向,与302类似,只是强制要求使用POST方法
- 400:请求报文语法有误,服务器无法识别
- 401:请求需要认证
- 403:请求的对应资源禁止被访问
- 404:服务器无法找到对应资源( Not Found )
- 500:服务器内部错误( Internal Server Error )
- 502:前面代理服务器联系不到后端的服务器出现( Bad Gateway )
- 503:服务器正忙
- 504:这个是代理能联系到后端的服务器,但是后端的服务器在规定的时间内没有给代理服务器响应( Gateway Timeout )
以上是关于Linux搭建Apache的主要内容,如果未能解决你的问题,请参考以下文章