部署Apache
发布日期:2021-06-30 20:53:58 浏览次数:3 分类:技术文章

本文共 14656 字,大约阅读时间需要 48 分钟。

简介

整个配置文件总体上划分为三个部分,第一个部分为全局环境设置,主要用于设置ServerRoot 主进程号的保存文件,对进程的控制、服务器侦听的IP地址和端口以及装载的动态状态模块等。第二部分是服务器的主要配置指定位置;第三部分用于设置和创建虚拟主机。

配置文件

配置文件:/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 
for detailed information. 5 # In particular, see 6 #
7 # for a discussion of each configuration directive. 8 # 9 # Do NOT simply read the instructions in here without understanding 10 # what they do. They're here only as hints or reminders. If you are unsure 11 # consult the online docs. You have been warned. 12 # 13 # Configuration and logfile names: If the filenames you specify for many 14 # of the server's control files begin with "/" (or "drive:/" for Win32), the 15 # server will use that explicit path. If the filenames do *not* begin 16 # with "/", the value of ServerRoot is prepended -- so 'log/access_log' 17 # with ServerRoot set to '/www' will be interpreted by the 18 # server as '/www/log/access_log', where as '/log/access_log' will be 19 # interpreted as '/log/access_log'. 20 21 # 22 # ServerRoot: The top of the directory tree under which the server's 23 # configuration, error, and log files are kept. 24 # 25 # Do not add a slash at the end of the directory path. If you point 26 # ServerRoot at a non-local disk, be sure to specify a local disk on the 27 # Mutex directive, if file-based mutexes are used. If you wish to share the 28 # same ServerRoot for multiple httpd daemons, you will need to change at 29 # least PidFile. 30 # 31 ServerRoot "/etc/httpd" 32 33 # 34 # Listen: Allows you to bind Apache to specific IP addresses and/or 35 # ports, instead of the default. See also the
36 # directive. 37 # 38 # Change this to Listen on specific IP addresses as shown below to 39 # prevent Apache from glomming onto all bound IP addresses. 40 # 41 #Listen 12.34.56.78:80 42 Listen 80 43 44 # 45 # Dynamic Shared Object (DSO) Support 46 # 47 # To be able to use the functionality of a module which was built as a DSO you 48 # have to place corresponding `LoadModule' lines at this location so the 49 # directives contained in it are actually available _before_ they are used. 50 # Statically compiled modules (those listed by `httpd -l') do not need 51 # to be loaded here. 52 # 53 # Example: 54 # LoadModule foo_module modules/mod_foo.so 55 # 56 Include conf.modules.d/*.conf 57 58 # 59 # If you wish httpd to run as a different user or group, you must run 60 # httpd as root initially and it will switch. 61 # 62 # User/Group: The name (or #number) of the user/group to run httpd as. 63 # It is usually good practice to create a dedicated user and group for 64 # running httpd, as with most system services. 65 # 66 User apache 67 Group apache 68 69 # 'Main' server configuration 70 # 71 # The directives in this section set up the values used by the 'main' 72 # server, which responds to any requests that aren't handled by a 73 #
definition. These values also provide defaults for 74 # any
containers you may define later in the file. 75 # 76 # All of these directives may appear inside
containers, 77 # in which case these default settings will be overridden for the 78 # virtual host being defined. 79 # 80 81 # 82 # ServerAdmin: Your address, where problems with the server should be 83 # e-mailed. This address appears on some server-generated pages, such 84 # as error documents. e.g. admin@your-domain.com 85 # 86 ServerAdmin root@localhost 87 88 # 89 # ServerName gives the name and port that the server uses to identify itself. 90 # This can often be determined automatically, but we recommend you specify 91 # it explicitly to prevent problems during startup. 92 # 93 # If your host doesn't have a registered DNS name, enter its IP address here. 94 # 95 #ServerName www.example.com:80 96 97 # 98 # Deny access to the entirety of your server's filesystem. You must 99 # explicitly permit access to web content directories in other100 #
blocks below.101 #102
103 AllowOverride none104 Require all denied105
106 107 #108 # Note that from this point forward you must specifically allow109 # particular features to be enabled - so if something's not working as110 # you might expect, make sure that you have specifically enabled it111 # below.112 #113 114 #115 # DocumentRoot: The directory out of which you will serve your116 # documents. By default, all requests are taken from this directory, but117 # symbolic links and aliases may be used to point to other locations.118 #119 DocumentRoot "/var/www/html"120 121 #122 # Relax access to content within /var/www.123 #124
125 AllowOverride None126 # Allow open access:127 Require all granted128
129 130 # Further relax access to the default document root:131
132 #133 # Possible values for the Options directive are "None", "All",134 # or any combination of:135 # Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews136 #137 # Note that "MultiViews" must be named *explicitly* --- "Options All"138 # doesn't give it to you.139 #140 # The Options directive is both complicated and important. Please see141 # http://httpd.apache.org/docs/2.4/mod/core.html#options142 # for more information.143 #144 Options Indexes FollowSymLinks145 146 #147 # AllowOverride controls what directives may be placed in .htaccess files.148 # It can be "All", "None", or any combination of the keywords:149 # Options FileInfo AuthConfig Limit150 #151 AllowOverride None152 153 #154 # Controls who can get stuff from this server.155 #156 Require all granted157
158 159 #160 # DirectoryIndex: sets the file that Apache will serve if a directory161 # is requested.162 #163
164 DirectoryIndex index.html165
166 167 #168 # The following lines prevent .htaccess and .htpasswd files from being169 # viewed by Web clients.170 #171
172 Require all denied173
174 175 #176 # ErrorLog: The location of the error log file.177 # If you do not specify an ErrorLog directive within a
178 # container, error messages relating to that virtual host will be179 # logged here. If you *do* define an error logfile for a
180 # container, that host's errors will be logged there and not here.181 #182 ErrorLog "logs/error_log"183 184 #185 # LogLevel: Control the number of messages logged to the error_log.186 # Possible values include: debug, info, notice, warn, error, crit,187 # alert, emerg.188 #189 LogLevel warn190 191
192 #193 # The following directives define some format nicknames for use with194 # a CustomLog directive (see below).195 #196 LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined197 LogFormat "%h %l %u %t \"%r\" %>s %b" common198 199
200 # You need to enable mod_logio.c to use %I and %O201 LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %I %O" com binedio202
203 204 #205 # The location and format of the access logfile (Common Logfile Format).206 # If you do not define any access logfiles within a
207 # container, they will be logged here. Contrariwise, if you *do*208 # define per-
access logfiles, transactions will be209 # logged therein and *not* in this file.210 #211 #CustomLog "logs/access_log" common212 213 #214 # If you prefer a logfile with access, agent, and referer information215 # (Combined Logfile Format) you can use the following directive.216 #217 CustomLog "logs/access_log" combined218
219 220
221 #222 # Redirect: Allows you to tell clients about documents that used to223 # exist in your server's namespace, but do not anymore. The client224 # will make a new request for the document at its new location.225 # Example:226 # Redirect permanent /foo http://www.example.com/bar227 228 #229 # Alias: Maps web paths into filesystem paths and is used to230 # access content that does not live under the DocumentRoot.231 # Example:232 # Alias /webpath /full/filesystem/path233 #234 # If you include a trailing / on /webpath then the server will235 # require it to be present in the URL. You will also likely236 # need to provide a
section to allow access to237 # the filesystem path.238 239 #240 # ScriptAlias: This controls which directories contain server scripts.241 # ScriptAliases are essentially the same as Aliases, except that242 # documents in the target directory are treated as applications and243 # run by the server when requested rather than as documents sent to the244 # client. The same rules about trailing "/" apply to ScriptAlias245 # directives as to Alias.246 #247 ScriptAlias /cgi-bin/ "/var/www/cgi-bin/"248 249
250 251 #252 # "/var/www/cgi-bin" should be changed to whatever your ScriptAliased253 # CGI directory exists, if you have that configured.254 #255
256 AllowOverride None257 Options None258 Require all granted259
260 261
262 #263 # TypesConfig points to the file containing the list of mappings from264 # filename extension to MIME-type.265 #266 TypesConfig /etc/mime.types267 268 #269 # AddType allows you to add to or override the MIME configuration270 # file specified in TypesConfig for specific file types.271 #272 #AddType application/x-gzip .tgz273 #274 # AddEncoding allows you to have certain browsers uncompress275 # information on the fly. Note: Not all browsers support this.276 #277 #AddEncoding x-compress .Z278 #AddEncoding x-gzip .gz .tgz279 #280 # If the AddEncoding directives above are commented-out, then you281 # probably should define those extensions to indicate media types:282 #283 AddType application/x-compress .Z284 AddType application/x-gzip .gz .tgz285 286 #287 # AddHandler allows you to map certain file extensions to "handlers":288 # actions unrelated to filetype. These can be either built into the server289 # or added with the Action directive (see below)290 #291 # To use CGI scripts outside of ScriptAliased directories:292 # (You will also need to add "ExecCGI" to the "Options" directive.)293 #294 #AddHandler cgi-script .cgi295 296 # For type maps (negotiated resources):297 #AddHandler type-map var298 299 #300 # Filters allow you to process content before it is sent to the client.301 #302 # To parse .shtml files for server-side includes (SSI):303 # (You will also need to add "Includes" to the "Options" directive.)304 #305 AddType text/html .shtml306 AddOutputFilter INCLUDES .shtml307
308 309 #310 # Specify a default charset for all content served; this enables311 # interpretation of all content as UTF-8 by default. To use the312 # default browser choice (ISO-8859-1), or to allow the META tags313 # in HTML content to override this choice, comment out this314 # directive:315 #316 AddDefaultCharset UTF-8317 318
319 #320 # The mod_mime_magic module allows the server to use various hints from the321 # contents of the file itself to determine its type. The MIMEMagicFile322 # directive tells the module where the hint definitions are located.323 #324 MIMEMagicFile conf/magic325
326 327 #328 # Customizable error responses come in three flavors:329 # 1) plain text 2) local redirects 3) external redirects330 #331 # Some examples:332 #ErrorDocument 500 "The server made a boo boo."333 #ErrorDocument 404 /missing.html334 #ErrorDocument 404 "/cgi-bin/missing_handler.pl"335 #ErrorDocument 402 http://www.example.com/subscription_info.html336 #337 338 #339 # EnableMMAP and EnableSendfile: On systems that support it,340 # memory-mapping or the sendfile syscall may be used to deliver341 # files. This usually improves server performance, but must342 # be turned off when serving from networked-mounted343 # filesystems or if support for these functions is otherwise344 # broken on your system.345 # Defaults if commented: EnableMMAP On, EnableSendfile Off346 #347 #EnableMMAP off348 EnableSendfile on349 350 # Supplemental configuration351 #352 # Load config files in the "/etc/httpd/conf.d" directory, if any.353 IncludeOptional conf.d/*.conf

性能配置命令

TimeOut --用于设置连接请求超时的时间
KeepAlive --用于启用持续的连接或者禁用持续的连接。 KeepAlive On|Off 配置文件中的默认设置为On
MaxKeepAliveRequests --用于设置一个在持续连接期间允许的最大HTTP请求数目。将数值加大可以提高服务器性能。
KeepAliveTimeout --用于设置在关闭TCP连接之前,等待后续请求的秒数。
控制Apache进程
对于使用prefork多道处理模块的Apache服务器,对进程的控制,可在prefork.c模块中进行设置或修改。

StartServers 8 #用于设置服务器启动时启动的子进程的个数MinSpareServers 5 #用于设置服务器中空闲子进程MaxSpareServers 20 #用于设置服务器中空闲子进程数目的上限ServerLimit 256 #默认的MAXClient最大是256线程MaxClients 256 限定同一时间客户端最大接入请求的数量MaxRequestsPerChild 400 每个子进程在其生存期内允许处理的最大请求数量,默认为400

容器与访问控制指令

容器指令通常用于封装一组指令,使其在容器条件成立时有效,或者用于改变指令的作用域。

#用于封装一组指令,使指令的作用域限制在容器指定的目录。
#用于定义虚拟主机

访问控制指令

访问控制指令能实现基于internal主机名的访问控制,其主机名就是域名,也是一个IP地址,建议尽量使用IP地址,以减少DNS域名解析。
Allow
命令用法:Allow form host-list
命令功能:指定允许访问的主机。
Deny
命令用法:Deny form host-list
命令功能:该命令与Allow刚好相反,用于禁止访问的主机名
Order
命令用法:Order allow,deny | deny,allow | mutual-failure
命令功能:用于指定allow和deny语句,哪一个被先执行。
.htaccess文件
.htaccess文件也称为分布式配置文件,在该文件中也可以放置一些配置指令,以作用于该文件所在的目录及其下的所有子目录。

实验:

安装

yum install -y httpd*

启动服务

systemctl start httpd

测试服务

netstat -tnlp |grep 80

或者

在Windows上打开网页 http://服务器的IP地址

配置简单的Web站点

进入web服务的站点根目录

cd /var/www/html

在站点根目录下编辑主页文件

vim index.htmlHell  Welcome to my  web page

重启服务

systemctl restart httpd

修改web读取网页的路径

将119行:DocumentRoot “/var/www/html”

124行: <Directory “/var/www”>
修改为自己定义目录即可

119 DocumentRoot "/var/www/html"120 121 #122 # Relax access to content within /var/www.123 #124 

实验举例:

[root@localhost ~]# mkdir  /root/www[root@localhost ~]# echo "111111111" >  /home/www/index.html[root@localhost ~]# vim /etc/httpd/conf/httpd.conf119 DocumentRoot "/home/www"120 121 #122 # Relax access to content within /var/www.123 #124 
[root@localhost ~]# systemctl restart httpd

在这里插入图片描述

注意:修改的路径最好不要是/root目录下的,明明关闭selinux和防火墙后仍然不能访问,不是/root/目录下自定义的路径没有这个情况。

基于IP地址虚拟主机访问

[root@localhost ~]# vim /etc/httpd/conf/httpd.conf

添加:

DocumentRoot /home/www/13
DocumentRoot /home/www/14

添加文件

[root@localhost www]# echo "13" > /home/www/13/index.html[root@localhost www]# echo "14" > /home/www/14/index.html

给网卡临时添加IP地址

[root@localhost www]# ifconfig    eno16777736  add   10.1.1.13 netmask 255.255.255.0[root@localhost www]# ifconfig    eno16777736  add   10.1.1.14 netmask 255.255.255.0
[root@localhost www]# ping 10.1.1.13PING 10.1.1.13 (10.1.1.13) 56(84) bytes of data.64 bytes from 10.1.1.13: icmp_seq=1 ttl=64 time=0.015 ms[root@localhost www]# ping 10.1.1.14PING 10.1.1.14 (10.1.1.14) 56(84) bytes of data.64 bytes from 10.1.1.14: icmp_seq=1 ttl=64 time=0.014 ms64 bytes from 10.1.1.14: icmp_seq=2 ttl=64 time=0.023 ms

重启网卡

[root@localhost www]# systemctl restart httpd

在这里插入图片描述

在这里插入图片描述

转载地址:https://liushiya.blog.csdn.net/article/details/90033014 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:有哪些通俗易懂的例子可以解释 IaaS、PaaS、SaaS 的区别?
下一篇:centos7 pptp

发表评论

最新留言

做的很好,不错不错
[***.243.131.199]2024年05月01日 05时38分49秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章