写在前面:
参考来源:Apache服务的搭建与配置(超详细版)
目标:完成基于httpd应用的web服务搭建和httpd主要配置修改的记录存档;
快速回顾:
关闭防火墙软件,通过软件包管理工具yum在centos7上安装httpd应用;
常见的httpd应用的配置文件路径有两个,#主配置文件路径 /etc/httpd/conf/httpd.conf #默认网网站家目录 /var/www/html ;
常见的web服务配置场景有:指定首页展示内容、指定网站家目录、指定主页类型或主页名;
一个彩蛋:重载和重启服务的区别,重载时主进程号不会改变,重启后会生成新的主进程号;
另一个彩蛋:使用elinks工具进行http页面内容访问的体验;
-----------------分隔符------------------------------------------
步骤一、关闭防火墙等安全软件
将config配置文件第七行的selinux配置为disabled
[root@centos7 ~]# vim /etc/selinux/config
7 SELINUX=disabled
[root@centos7 ~]# setenforce 0
步骤二、通过软件包管理器yum安装httpd应用,运行httpd应用软件
[root@centos7 ~]# yum -y install httpd
已安装:
httpd.x86_64 0:2.4.6-99.el7.centos.1
作为依赖被安装:
mailcap.noarch 0:2.1.41-2.el7
完毕!
安装完成后检查服务状态和配置文件路径
[root@centos7 ~]# systemctl status httpd
● httpd.service - The Apache HTTP Server
Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled)
Active: inactive (dead)
Docs: man:httpd(8)
man:apachectl(8)
发现服务未激活,尝试重启服务,然后重新检查服务状态,httpd应用服务的主进程号1755,
[root@centos7 ~]# systemctl restart httpd #重启httpd服务
[root@centos7 ~]# systemctl status httpd
● httpd.service - The Apache HTTP Server
Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled)
Active: active (running) since 一 2024-07-22 23:05:27 CST; 1min 58s ago
Docs: man:httpd(8)
man:apachectl(8)
Main PID: 1755 (httpd)
Status: "Total requests: 0; Current requests/sec: 0; Current traffic: 0 B/sec"
CGroup: /system.slice/httpd.service
├─1755 /usr/sbin/httpd -DFOREGROUND
├─1757 /usr/sbin/httpd -DFOREGROUND
├─1758 /usr/sbin/httpd -DFOREGROUND
├─1759 /usr/sbin/httpd -DFOREGROUND
├─1760 /usr/sbin/httpd -DFOREGROUND
└─1761 /usr/sbin/httpd -DFOREGROUND
7月 22 23:05:12 centos7 systemd[1]: Starting The Apache HTTP Server...
7月 22 23:05:22 centos7 httpd[1755]: AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using fe80::7f9:9a7d:e440:9c3b. ...his message
7月 22 23:05:27 centos7 systemd[1]: Started The Apache HTTP Server.
Hint: Some lines were ellipsized, use -l to show in full.
本来想通过lsof -c httpd 看是否能看到配置文件的路径,结果没发现(今天在刷linux技能树的勋章,关于lsof的命令使用)
安装成功后,会产生下面两个文件:
#主配置文件 /etc/httpd/conf/httpd.conf
#默认网网站家目录 /var/www/html
httpd应用默认配置的端口为80,通过lsof工具查看端口状态,得到信息(tcp协议,监听状态)
[root@centos7 ~]# lsof -i :80
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
httpd 1755 root 4u IPv6 25867 0t0 TCP *:http (LISTEN)
httpd 1757 apache 4u IPv6 25867 0t0 TCP *:http (LISTEN)
httpd 1758 apache 4u IPv6 25867 0t0 TCP *:http (LISTEN)
httpd 1759 apache 4u IPv6 25867 0t0 TCP *:http (LISTEN)
httpd 1760 apache 4u IPv6 25867 0t0 TCP *:http (LISTEN)
httpd 1761 apache 4u IPv6 25867 0t0 TCP *:http (LISTEN)
步骤三、访问验证提供的web服务是否可用(三种方式验证)
通过curl工具访问本地80端口服务,可以正常显示页面内容
curl http://localhost:80
通过浏览器访问,可以正常显示页面内容
根据参考文档,安装了一个elinks的文本浏览器,发现在命令行模式也比较友好的获取页面内容
[root@centos7 ~]# yum -y install elinks
[root@centos7 ~]# elinks http://localhost
步骤四、基于httpd应用的web服务的场景化实现
场景一:创建一个默认登录页面,页面内展示指定的信息,重启服务生效,访问验证效果
[root@centos7 html]# ls -l
总用量 0
[root@centos7 html]# pwd
/var/www/html
[root@centos7 html]# echo 'mortalz7' > /var/www/html/index.html
[root@centos7 html]# ls
index.html
验证首页实现效果
elinks方式
curl方式
[root@centos7 ~]# curl http://localhost
mortalz7
[root@centos7 ~]# curl http://localhost -I #仅显示响应头字段信息
HTTP/1.1 200 OK
Date: Mon, 22 Jul 2024 15:33:42 GMT
Server: Apache/2.4.6 (CentOS)
Last-Modified: Mon, 22 Jul 2024 15:30:53 GMT
ETag: "9-61dd7bb04b548"
Accept-Ranges: bytes
Content-Length: 9
Content-Type: text/html; charset=UTF-8
[root@centos7 ~]#
对场景一的优化,使用html语言标准语法样式,实现页面效果,
index.html内容修改为
[root@centos7 html]# cat index.html
<html>
<head>
<title>测试</title>
</head>
<body>
<h1 align="center">mortalz7</h1>
</body>
</html>
访问验证效果
elinks方式
curl方式
[root@centos7 ~]# curl http://localhost
<html>
<head>
<title>测试</title>
</head>
<body>
<h1 align="center">mortalz7</h1>
</body>
</html>
场景二、指定网站家目录位置从默认位置/var/www/html改为/www
修改httpd配置文件相关内容,具体如下
创建指定网站家目录www
mkdir /www
编辑httpd应用主配置文件
vim /etc/httpd/conf/httpd.conf
vim的相关操作
:set nu #显示行号
:119 #跳转到指定行
修改两处位置
119 DocumentRoot "/var/www/html" 修改为 DocumentRoot "/www"
131 <directory “var/www/html”>修改为<directory ”/www”>
使用:wq 保存退出,
修改httpd配置文件之后,重启httpd服务使配置生效
[root@centos7 www]# systemctl restart httpd
在指定家目录添加首页内容信息,从原路径复制html文件,修改页面内容
[root@centos7 www]# cp /var/www/html/index.html index.html
[root@centos7 www]# cat -n index.html
1 <html>
2 <head>
3 <title>测试/www路径</title>
4 </head>
5 <body>
6 <h1 align="center">mortalz7</h1>
7 </body>
8 </html>
访问页面内容验证,验证通过
curl方式验证
[root@centos7 www]# curl http://localhost
<html>
<head>
<title>测试/www路径</title>
</head>
<body>
<h1 align="center">mortalz7</h1>
</body>
</html>
[root@centos7 www]# curl http://localhost -I
HTTP/1.1 200 OK
Date: Mon, 22 Jul 2024 15:55:19 GMT
Server: Apache/2.4.6 (CentOS)
Last-Modified: Mon, 22 Jul 2024 15:53:46 GMT
ETag: "7e-61dd80ce89f0d"
Accept-Ranges: bytes
Content-Length: 126
Content-Type: text/html; charset=UTF-8
场景三、指定主页类型或者主页名
通过修改配置文件方式实现,具体如下
vim /etc/httpd/conf/httpd.conf
在 dir_module模块中修改,或者指定行号:164 (我没有删除配置,在将原配置基础上先复制修改,然后将注释原配置,所以多了两行)
将index.html修改为indede.php,保存退出;
重新加载服务(重启服务也可以),或者重启服务使配置生效【一个彩蛋:观察重新加载后主进程号不会变,重启服务之后主进程号发生了改变】
[root@centos7 www]# systemctl status httpd
● httpd.service - The Apache HTTP Server
Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled)
Active: active (running) since 一 2024-07-22 23:50:46 CST; 11min ago
Docs: man:httpd(8)
man:apachectl(8)
Process: 2091 ExecStop=/bin/kill -WINCH ${MAINPID} (code=exited, status=0/SUCCESS)
Main PID: 2096 (httpd)
Status: "Total requests: 3; Current requests/sec: 0; Current traffic: 0 B/sec"
CGroup: /system.slice/httpd.service
├─2096 /usr/sbin/httpd -DFOREGROUND
├─2098 /usr/sbin/httpd -DFOREGROUND
├─2099 /usr/sbin/httpd -DFOREGROUND
├─2100 /usr/sbin/httpd -DFOREGROUND
├─2101 /usr/sbin/httpd -DFOREGROUND
├─2102 /usr/sbin/httpd -DFOREGROUND
└─2164 /usr/sbin/httpd -DFOREGROUND
7月 22 23:50:31 centos7 systemd[1]: Starting The Apache HTTP Server...
7月 22 23:50:41 centos7 httpd[2096]: AH00558: httpd: Could not reliably determ...ge
7月 22 23:50:46 centos7 systemd[1]: Started The Apache HTTP Server.
Hint: Some lines were ellipsized, use -l to show in full.
[root@centos7 www]# systemctl reload httpd # 重载方式使配置文件生效
[root@centos7 www]# systemctl status httpd
● httpd.service - The Apache HTTP Server
Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled)
Active: active (running) since 一 2024-07-22 23:50:46 CST; 13min ago
Docs: man:httpd(8)
man:apachectl(8)
Process: 2091 ExecStop=/bin/kill -WINCH ${MAINPID} (code=exited, status=0/SUCCESS)
Process: 2226 ExecReload=/usr/sbin/httpd $OPTIONS -k graceful (code=exited, status=0/SUCCESS)
Main PID: 2096 (httpd)
Status: "Total requests: 3; Current requests/sec: 0; Current traffic: 0 B/sec"
CGroup: /system.slice/httpd.service
└─2096 /usr/sbin/httpd -DFOREGROUND
7月 22 23:50:31 centos7 systemd[1]: Starting The Apache HTTP Server...
7月 22 23:50:41 centos7 httpd[2096]: AH00558: httpd: Could not reliably determ...ge
7月 22 23:50:46 centos7 systemd[1]: Started The Apache HTTP Server.
7月 23 00:03:40 centos7 systemd[1]: Reloading The Apache HTTP Server.
7月 23 00:03:51 centos7 httpd[2226]: AH00558: httpd: Could not reliably determ...ge
7月 23 00:03:51 centos7 systemd[1]: Reloaded The Apache HTTP Server.
Hint: Some lines were ellipsized, use -l to show in full.
[root@centos7 www]# systemctl restart httpd #重启方式使配置文件生效
[root@centos7 www]# systemctl status httpd
● httpd.service - The Apache HTTP Server
Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled)
Active: active (running) since 二 2024-07-23 00:04:51 CST; 3s ago
Docs: man:httpd(8)
man:apachectl(8)
Process: 2243 ExecStop=/bin/kill -WINCH ${MAINPID} (code=exited, status=0/SUCCESS)
Process: 2226 ExecReload=/usr/sbin/httpd $OPTIONS -k graceful (code=exited, status=0/SUCCESS)
Main PID: 2248 (httpd)
Status: "Processing requests..."
CGroup: /system.slice/httpd.service
├─2248 /usr/sbin/httpd -DFOREGROUND
├─2249 /usr/sbin/httpd -DFOREGROUND
├─2250 /usr/sbin/httpd -DFOREGROUND
├─2251 /usr/sbin/httpd -DFOREGROUND
├─2252 /usr/sbin/httpd -DFOREGROUND
└─2253 /usr/sbin/httpd -DFOREGROUND
7月 23 00:04:36 centos7 systemd[1]: Starting The Apache HTTP Server...
7月 23 00:04:46 centos7 httpd[2248]: AH00558: httpd: Could not reliably determ...ge
7月 23 00:04:51 centos7 systemd[1]: Started The Apache HTTP Server.
Hint: Some lines were ellipsized, use -l to show in full.
同样创建一个php的首页内容,用于验证
root@centos7 www]# cat indede.php
<html>
<head>
<title>简单的PHP页面</title>
</head>
<body>
<?php
echo "Hello, World!";
?>
</body>
</html>
通过curl工具进行访问验证,验证通过
[root@centos7 www]# curl http://localhost
<html>
<head>
<title>简单的PHP页面</title>
</head>
<body>
<?php
echo "Hello, World!";
?>
</body>
</html>
[root@centos7 www]# curl http://localhost -I
HTTP/1.1 200 OK
Date: Mon, 22 Jul 2024 16:12:40 GMT
Server: Apache/2.4.6 (CentOS)
Last-Modified: Mon, 22 Jul 2024 16:11:19 GMT
ETag: "7f-61dd84ba820e7"
Accept-Ranges: bytes
Content-Length: 127
Content-Type: text/html; charset=UTF-8
[root@centos7 www]#
其他常见httpd应用命令 (类比nginx应用)
#/usr/local/apache2/bin/apachectl -M #查看常见的模块(包括动态和静态)
#/usr/local/apache2/bin/apachectl -l #查看加载的静态模块
#/usr/local/apache2/bin/apachectl -t #检查配置文件有无语法错误
#/usr/local/apache2/bin/apachectl graceful #加载配置文件,但不重启
#/usr/local/apache2/bin/apachectl start/restart/stop #启动/重启/停止Apache服务
``
```bash
[root@centos7 httpd]# httpd ?
Usage: httpd [-D name] [-d directory] [-f file]
[-C "directive"] [-c "directive"]
[-k start|restart|graceful|graceful-stop|stop]
[-v] [-V] [-h] [-l] [-L] [-t] [-T] [-S] [-X]
Options:
-D name : define a name for use in <IfDefine name> directives
-d directory : specify an alternate initial ServerRoot
-f file : specify an alternate ServerConfigFile
-C "directive" : process directive before reading config files
-c "directive" : process directive after reading config files
-e level : show startup errors of level (see LogLevel)
-E file : log startup errors to file
-v : show version number
-V : show compile settings
-h : list available command line options (this page)
-l : list compiled in modules
-L : list available configuration directives
-t -D DUMP_VHOSTS : show parsed vhost settings
-t -D DUMP_RUN_CFG : show parsed run settings
-S : a synonym for -t -D DUMP_VHOSTS -D DUMP_RUN_CFG
-t -D DUMP_MODULES : show all loaded modules
-M : a synonym for -t -D DUMP_MODULES
-t : run syntax check for config files
-T : start without DocumentRoot(s) check
-X : debug mode (only one worker, do not detach)
[root@centos7 httpd]# httpd -v
Server version: Apache/2.4.6 (CentOS)
Server built: May 30 2023 14:01:11