Linux(红帽8)上搭建web服务

本文详细介绍Apache服务器的配置过程,包括软件仓库配置、Apache主程序安装、主配置文件解析及简单的helloworld界面展示。同时,还介绍了如何搭建基于域名的虚拟主机。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.配置软件仓库(与红帽7有区别)

 vim /etc/yum.repos.d/base.repo 

在这里插入图片描述
挂载光盘后检测

mount /dev/sr0 /mnt          //挂载
dnf repolist     //检测

2.安装Apache主程序,由httpd提供

dnf install httpd -y   //与红帽7不同的是dnf取代了yum

启动服务后在图形化界面检测是否安装成功

systemctl start httpd    //启动服务

在这里插入图片描述
3.主配置文件解析

[root@localhost ~]# tree /etc/httpd/
/etc/httpd/
├── conf
│   ├── httpd.conf     //主配置文件
│   └── magic
├── conf.d
│   ├── autoindex.conf
│   ├── README
│   ├── userdir.conf
│   └── welcome.conf   //测试界面的欢迎界面
├── conf.modules.d
│   ├── 00-base.conf
│   ├── 00-dav.conf
│   ├── 00-lua.conf
│   ├── 00-mpm.conf
│   ├── 00-optional.conf
│   ├── 00-proxy.conf
│   ├── 00-systemd.conf
│   ├── 01-cgi.conf
│   ├── 10-h2.conf
│   ├── 10-proxy_h2.conf
│   └── README
├── logs -> ../../var/log/httpd
├── modules -> ../../usr/lib64/httpd/modules
├── run -> /run/httpd
└── state -> ../../var/lib/httpd

vim /etc/httpd/conf/httpd.conf   //查看主配置文件
  1 #
  2 # This is the main Apache HTTP server configuration file.  It contains the
  3 # configuration directives that give the server its instructions.
  4 # See <URL:http://httpd.apache.org/docs/2.4/> for detailed information.
  5 # In particular, see 
  6 # <URL:http://httpd.apache.org/docs/2.4/mod/directives.html>
  7 # for a discussion of each configuration directive.
  8 #
  9 # See the httpd.conf(5) man page for more information on this configuration,
 10 # and httpd.service(8) on using and configuring the httpd service.
 11 #
 12 # Do NOT simply read the instructions in here without understanding
 13 # what they do.  They're here only as hints or reminders.  If you are unsure
 14 # consult the online docs. You have been warned.  
 15 #
 16 # Configuration and logfile names: If the filenames you specify for many
 17 # of the server's control files begin with "/" (or "drive:/" for Win32), the
 18 # server will use that explicit path.  If the filenames do *not* begin
 19 # with "/", the value of ServerRoot is prepended -- so 'log/access_log'
 20 # with ServerRoot set to '/www' will be interpreted by the
 21 # server as '/www/log/access_log', where as '/log/access_log' will be
 22 # interpreted as '/log/access_log'.
 23 
 24 #
 25 # ServerRoot: The top of the directory tree under which the server's
 26 # configuration, error, and log files are kept.
 27 #
 28 # Do not add a slash at the end of the directory path.  If you point
 29 # ServerRoot at a non-local disk, be sure to specify a local disk on the
 30 # Mutex directive, if file-based mutexes are used.  If you wish to share the
 31 # same ServerRoot for multiple httpd daemons, you will need to change at
 32 # least PidFile.
 33 #
 34 ServerRoot "/etc/httpd"       //http服务的顶级目录
 35 
 36 #
 37 # Listen: Allows you to bind Apache to specific IP addresses and/or
 38 # ports, instead of the default. See also the <VirtualHost>
 39 # directive.
 40 #
 41 # Change this to Listen on specific IP addresses as shown below to 
 42 # prevent Apache from glomming onto all bound IP addresses.
 43 #
 44 #Listen 12.34.56.78:80
 45 Listen 80      //监听端口  默认为80
 46 
 47 #
 48 # Dynamic Shared Object (DSO) Support
 49 #
 50 # To be able to use the functionality of a module which was built as a DSO you
 51 # have to place corresponding `LoadModule' lines at this location so the
 52 # directives contained in it are actually available _before_ they are used.
 53 # Statically compiled modules (those listed by `httpd -l') do not need
 54 # to be loaded here.
 55 #
 56 # Example:
 57 # LoadModule foo_module modules/mod_foo.so
 58 #
 59 Include conf.modules.d/*.conf   // 包括/etc/httpd/conf.modules.d/*.conf的所有文件
 60 
 61 #
 62 # If you wish httpd to run as a different user or group, you must run
 63 # httpd as root initially and it will switch.  
 64 #
 65 # User/Group: The name (or #number) of the user/group to run httpd as.
 66 # It is usually good practice to create a dedicated user and group for
 67 # running httpd, as with most system services.
 68 #
 69 User apache    //服务的用户  先以root把服务启动起来,再切换为Apache用户
 70 Group apache    //服务的组
 71 
 72 # 'Main' server configuration
 73 #
 74 # The directives in this section set up the values used by the 'main'
 75 # server, which responds to any requests that aren't handled by a
 76 # <VirtualHost> definition.  These values also provide defaults for
 77 # any <VirtualHost> containers you may define later in the file.
 78 #
 79 # All of these directives may appear inside <VirtualHost> containers,
 80 # in which case these default settings will be overridden for the
 81 # virtual host being defined.
 82 #
 83 
 84 #
 85 # ServerAdmin: Your address, where problems with the server should be
 86 # e-mailed.  This address appears on some server-generated pages, such
 87 # as error documents.  e.g. admin@your-domain.com
 88 #
 89 ServerAdmin root@localhost   //本地的root用户,可以发送邮箱。
 90 
 91 #
 92 # ServerName gives the name and port that the server uses to identify itself.
 93 # This can often be determined automatically, but we recommend you specify
 94 # it explicitly to prevent problems during startup.
 95 #
 96 # If your host doesn't have a registered DNS name, enter its IP address here.
 97 #
 98 #ServerName www.example.com:80     // ServerName 0.0.0.0:80匹配任意IP地址,监听端口在80端口   
 99 
100 #
101 # Deny access to the entirety of your server's filesystem. You must
102 # explicitly permit access to web content directories in other 
103 # <Directory> blocks below.
104 #
105 <Directory />        //目录为根,起始标签
106     AllowOverride none   //权限
107     Require all denied    //权限
108 </Directory>      //结束标签
109 
110 #
111 # Note that from this point forward you must specifically allow
112 # particular features to be enabled - so if something's not working as
113 # you might expect, make sure that you have specifically enabled it
114 # below.
115 #
116 
117 #
118 # DocumentRoot: The directory out of which you will serve your
119 # documents. By default, all requests are taken from this directory, but
120 # symbolic links and aliases may be used to point to other locations.
121 #
122 DocumentRoot "/var/www/html"  //网页文件存放的目录
123 
124 #
125 # Relax access to content within /var/www.
126 #
127 <Directory "/var/www">
128     AllowOverride None
129     # Allow open access:
130     Require all granted
131 </Directory>
132 
133 # Further relax access to the default document root:
134 <Directory "/var/www/html">
135     #
136     # Possible values for the Options directive are "None", "All",
137     # or any combination of:
138     #   Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews
139     #
140     # Note that "MultiViews" must be named *explicitly* --- "Options All"
141     # doesn't give it to you.
142     #
143     # The Options directive is both complicated and important.  Please see
144     # http://httpd.apache.org/docs/2.4/mod/core.html#options
145     # for more information.
146     #
147     Options Indexes FollowSymLinks     //索引,跟踪软连接
148 
149     #
150     # AllowOverride controls what directives may be placed in .htaccess files.
151     # It can be "All", "None", or any combination of the keywords:
152     #   Options FileInfo AuthConfig Limit
153     #
154     AllowOverride None
155 
156     #
157     # Controls who can get stuff from this server.
158     #
159     Require all granted
160 </Directory>
161 
162 #
163 # DirectoryIndex: sets the file that Apache will serve if a directory
164 # is requested.
165 #
166 <IfModule dir_module>         //加载一个目录模块
167     DirectoryIndex index.html
168 </IfModule>
169 
170 #
171 # The following lines prevent .htaccess and .htpasswd files from being 
172 # viewed by Web clients. 
173 #
174 <Files ".ht*">   //不能访问
175     Require all denied
176 </Files>
177 
178 #
179 # ErrorLog: The location of the error log file.
180 # If you do not specify an ErrorLog directive within a <VirtualHost>
181 # container, error messages relating to that virtual host will be
182 # logged here.  If you *do* define an error logfile for a <VirtualHost>
183 # container, that host's errors will be logged there and not here.
184 #
185 ErrorLog "logs/error_log"
186 
187 #
188 # LogLevel: Control the number of messages logged to the error_log.
189 # Possible values include: debug, info, notice, warn, error, crit,
190 # alert, emerg.
191 #
192 LogLevel warn
193 
194 <IfModule log_config_module>         //日志配置模块
195     #
196     # The following directives define some format nicknames for use with
197     # a CustomLog directive (see below).
198     #
199     LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
200     LogFormat "%h %l %u %t \"%r\" %>s %b" common
201 
202     <IfModule logio_module>
203       # You need to enable mod_logio.c to use %I and %O
204       LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %I %O" combinedio
205     </IfModule>
206 
207     #
208     # The location and format of the access logfile (Common Logfile Format).
209     # If you do not define any access logfiles within a <VirtualHost>
210     # container, they will be logged here.  Contrariwise, if you *do*
211     # define per-<VirtualHost> access logfiles, transactions will be
212     # logged therein and *not* in this file.
213     #
214     #CustomLog "logs/access_log" common
215 
216     #
217     # If you prefer a logfile with access, agent, and referer information
218     # (Combined Logfile Format) you can use the following directive.
219     #
220     CustomLog "logs/access_log" combined
221 </IfModule>
222 
223 <IfModule alias_module>      //别名模块
224     #
225     # Redirect: Allows you to tell clients about documents that used to 
226     # exist in your server's namespace, but do not anymore. The client 
227     # will make a new request for the document at its new location.
228     # Example:
229     # Redirect permanent /foo http://www.example.com/bar
230 
231     #
232     # Alias: Maps web paths into filesystem paths and is used to
233     # access content that does not live under the DocumentRoot.
234     # Example:
235     # Alias /webpath /full/filesystem/path  //别名示例
236     #
237     # If you include a trailing / on /webpath then the server will
238     # require it to be present in the URL.  You will also likely
239     # need to provide a <Directory> section to allow access to
240     # the filesystem path.
241 
242     #
243     # ScriptAlias: This controls which directories contain server scripts. 
244     # ScriptAliases are essentially the same as Aliases, except that
245     # documents in the target directory are treated as applications and
246     # run by the server when requested rather than as documents sent to the
247     # client.  The same rules about trailing "/" apply to ScriptAlias
248     # directives as to Alias.
249     #
250     ScriptAlias /cgi-bin/ "/var/www/cgi-bin/"
251 
252 </IfModule>
253 
254 #
255 # "/var/www/cgi-bin" should be changed to whatever your ScriptAliased
256 # CGI directory exists, if you have that configured.
257 #
258 <Directory "/var/www/cgi-bin">          //cgi(通用网关接口)是web服务器运行时外部程序的规范,按cgi编写的程序可以扩展服务器的功能。cgi应用程序能与浏览器进行交互,还可通过数据库API与数据库服务器等外部数据源进行通信,从数据库服务器中获取数据
259     AllowOverride None
260     Options None
261     Require all granted
262 </Directory>
263 
264 <IfModule mime_module>
265     #
266     # TypesConfig points to the file containing the list of mappings from
267     # filename extension to MIME-type.
268     #
269     TypesConfig /etc/mime.types
270 
271     #
272     # AddType allows you to add to or override the MIME configuration
273     # file specified in TypesConfig for specific file types.
274     #
275     #AddType application/x-gzip .tgz
276     #
277     # AddEncoding allows you to have certain browsers uncompress
278     # information on the fly. Note: Not all browsers support this.
279     #
280     #AddEncoding x-compress .Z
281     #AddEncoding x-gzip .gz .tgz
282     #
283     # If the AddEncoding directives above are commented-out, then you
284     # probably should define those extensions to indicate media types:
285     #
286     AddType application/x-compress .Z
287     AddType application/x-gzip .gz .tgz
288 
289     #
290     # AddHandler allows you to map certain file extensions to "handlers":
291     # actions unrelated to filetype. These can be either built into the server
292     # or added with the Action directive (see below)
293     #
294     # To use CGI scripts outside of ScriptAliased directories:
295     # (You will also need to add "ExecCGI" to the "Options" directive.)
296     #
297     #AddHandler cgi-script .cgi
298 
299     # For type maps (negotiated resources):
300     #AddHandler type-map var
 301 
302     #
303     # Filters allow you to process content before it is sent to the client.
304     #
305     # To parse .shtml files for server-side includes (SSI):
306     # (You will also need to add "Includes" to the "Options" directive.)
307     #
308     AddType text/html .shtml
309     AddOutputFilter INCLUDES .shtml
310 </IfModule>
311 
312 #
313 # Specify a default charset for all content served; this enables
314 # interpretation of all content as UTF-8 by default.  To use the 
315 # default browser choice (ISO-8859-1), or to allow the META tags
316 # in HTML content to override this choice, comment out this
317 # directive:
318 #
319 AddDefaultCharset UTF-8      //字符集
320 
321 <IfModule mime_magic_module>
322     #
323     # The mod_mime_magic module allows the server to use various hints from the
324     # contents of the file itself to determine its type.  The MIMEMagicFile
325     # directive tells the module where the hint definitions are located.
326     #
327     MIMEMagicFile conf/magic
328 </IfModule>
329 
330 #
331 # Customizable error responses come in three flavors:
332 # 1) plain text 2) local redirects 3) external redirects
333 #
334 # Some examples:
335 #ErrorDocument 500 "The server made a boo boo."
336 #ErrorDocument 404 /missing.html
337 #ErrorDocument 404 "/cgi-bin/missing_handler.pl"
338 #ErrorDocument 402 http://www.example.com/subscription_info.html
339 #
340 
341 #
342 # EnableMMAP and EnableSendfile: On systems that support it, 
343 # memory-mapping or the sendfile syscall may be used to deliver
344 # files.  This usually improves server performance, but must
345 # be turned off when serving from networked-mounted 
346 # filesystems or if support for these functions is otherwise
347 # broken on your system.
348 # Defaults if commented: EnableMMAP On, EnableSendfile Off
349 #
350 #EnableMMAP off
351 EnableSendfile on
352 
353 # Supplemental configuration
354 #
355 # Load config files in the "/etc/httpd/conf.d" directory, if any.
356 IncludeOptional conf.d/*.conf
                                                                                                             

以上为配置文件解析,具体行数为:34 45 59 69 70 89 98 105 122 147 166 174 194 223 235 258 319
4.简单的hello world界面
在/var/www/html/下创建一个主页文件

echo hello world > /var/www/html/index.html

修改完参数后重启服务验证

systemctl restart httpd

在这里插入图片描述
要求搭建web服务器,能够访问到网页内容为“小胖,你咋这么胖!”
①修改主页文件

vim /var/www/html/index.html

②重启服务验证

systemctl restart httpd

在这里插入图片描述
要求搭建web服务器,创建基于域名的虚拟主机,能够使用www.xiaopang.com和www.dapang.com访问各自的网站存放路径分别为/xiaopang和/dapang,内容自定
①创建虚拟主机文件

 vim /etc/httpd/conf.d/vhost.conf


<directory /dapang>
        allowoverride none
        require all granted
</directory>
<directory /xiaopang>
        allowoverride none
        require all granted
</directory>
<virtualhost *:80>
        documentroot /xiaopang
        servername www.xiaopang.com
</virtualhost>
<virtualhost *:80>
        documentroot /dapang
        servername www.dapang.com
</virtualhost>

②创建网页文件及内容

  mkdir /xiaopang /dapang
  echo this is xiaopang > /xiaopang/index.html
  echo this is dapang > /dapang/index.html

③由于是基于域名访问,所以添加在/etc/hosts中添加IP与域名的映射关系

vim /etc/hosts
192.168.17.134 www.xiaopang.com www.dapang.com

④重启服务验证

[root@localhost ~]# systemctl restart httpd
[root@localhost ~]# curl www.xiaopang.com
this is xiaopang
[root@localhost ~]# curl www.dapang.com
this is dapang
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值