Apache和Nginx对比,其实我跟青睐轻量级的Nginx一点,无奈公司的这台电脑的Nginx被人配置的有点乱,所以装了个Apache来搞个网页文件服务器。这里记录下搭建步骤以及访问权限的坑。
我是T型人小付,一位坚持终身学习的互联网从业者。喜欢我的博客欢迎在csdn上关注我,如果有问题欢迎在底下的评论区交流,谢谢。
安装Apache
Apache在centos的repo中叫做httpd,直接用yum安装即可
yum -y install httpd
之后查看一下版本
[root@testmachine test]# httpd -version
Server version: Apache/2.4.6 (CentOS)
Server built: Aug 8 2019 11:41:18
配置文件详解
配置文件在路径/etc/httpd目录
[root@testmachine httpd]# pwd
/etc/httpd
[root@testmachine httpd]# ll
total 0
drwxr-xr-x 2 root root 37 Mar 4 21:47 conf
drwxr-xr-x 2 root root 117 Mar 4 21:47 conf.d
drwxr-xr-x 2 root root 165 Mar 4 21:47 conf.modules.d
lrwxrwxrwx 1 root root 19 Mar 4 21:47 logs -> ../../var/log/httpd
lrwxrwxrwx 1 root root 29 Mar 4 21:47 modules -> ../../usr/lib64/httpd/modules
lrwxrwxrwx 1 root root 10 Mar 4 21:47 run -> /run/httpd
[root@testmachine httpd]#
其中主要的配置文件是conf/httpd.conf,而一些自定义的配置,例如针对不同url提供的不同目录的服务,就可以在conf.d中来配置,下面会讲。
其中httpd.conf中有几个比较关键的配置项在这里说一下
-
ServerRoot “/etc/httpd”
这里指定Apache的配置文件的位置,也就是上面提到的文件夹,不要修改 -
Listen 80
这里指定服务监听的IP和端口,不指定IP则默认是所有网卡。如果是多个站点,则这里要分多行分别指定多个端口 -
User apache/Group apache
启动httpd服务的用户和组,如果没有被自动创建需要手动useradd apache创建
默认情况下在httpd.conf中指定的站点是主站点,如果有多的站点需要用<VirtualHost>去单独指定。在有<VirtualHost>的情况下主站点的配置作废。下面这几个配置共同建立一套站点
-
DocumentRoot “/var/www/html”
这里配置该站点的主目录 -
下面这几个配置站点的访问权限
<Directory "/var/www/html">
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
单站点实际操作
第一个站点监听8888端口,将上面配置中改为Listen 8888。
然后新建conf.d/vhosts.conf文件,如下
<VirtualHost *:8888>
ServerName www.test.local
ServerAlias test.local
DocumentRoot /var/www/html/test/
</VirtualHost>
<Directory "/var/www/html/test/">
#Allowoverride authconfig
Require all granted
</Directory>
当用户访问8888端口的时候,返回目录/var/www/html/test/下的内容。如果该目录下没有index.html文件就变成了文件服务器。
注意,这里的DocumentRoot只能是/var/www/html/或者其子目录。如果想将/var/www/html/目录外的目录做为根目录,例如/data/,可能会有权限问题,需要在/var/www/html/中用软链接来解决
ls -s /data data
修改完配置可以用apachectl configtest命令去检查配置是否正确
没有问题就重启httpd服务
systemctl restart httpd
这时访问8888端口就会看到类似下面的页面

多站点实际操作
如果还想在9999端口返回/var/www/html/test2/下的内容。
首先增加一个监听的端口
Listen 8888
Listen 9999
然后在刚才的vhosts.conf中增加一个VirtualHost,如下
<VirtualHost *:8888>
ServerName www.test.local
ServerAlias test.local
DocumentRoot /var/www/html/test/
</VirtualHost>
<Directory "/var/www/html/test/">
#Allowoverride authconfig
Require all granted
</Directory>
<VirtualHost *:9999>
ServerName www.test.local
ServerAlias test.local
DocumentRoot /var/www/html/test2/
</VirtualHost>
<Directory "/var/www/html/test2/">
#Allowoverride authconfig
Require all granted
</Directory>
修改完配置记得重启httpd服务
systemctl restart httpd
再访问9999端口也成功了

报错详解
在访问站点时候出现500错误码,去/etc/httpd/logs/error_log中查看错误日志
[root@youzu logs]# tail -n 30 error_log
[Sun Mar 08 03:19:01 2020] [notice] Digest: generating secret for digest authentication ...
[Sun Mar 08 03:19:01 2020] [notice] Digest: done
[Sun Mar 08 03:19:01 2020] [notice] Apache/2.2.15 (Unix) DAV/2 configured -- resuming normal operations
[Sun Mar 08 18:36:02 2020] [error] [client 192.168.185.9] File does not exist: /var/www/html/favicon.ico, referer: http://10.18.97.106:8181/
[Mon Mar 09 00:16:00 2020] [notice] caught SIGTERM, shutting down
[Mon Mar 09 00:16:00 2020] [notice] suEXEC mechanism enabled (wrapper: /usr/sbin/suexec)
[Mon Mar 09 00:16:00 2020] [notice] Digest: generating secret for digest authentication ...
[Mon Mar 09 00:16:00 2020] [notice] Digest: done
[Mon Mar 09 00:16:00 2020] [notice] Apache/2.2.15 (Unix) DAV/2 configured -- resuming normal operations
[Mon Mar 09 00:16:47 2020] [notice] caught SIGTERM, shutting down
[Mon Mar 09 00:16:48 2020] [notice] suEXEC mechanism enabled (wrapper: /usr/sbin/suexec)
[Mon Mar 09 00:16:48 2020] [notice] Digest: generating secret for digest authentication ...
[Mon Mar 09 00:16:48 2020] [notice] Digest: done
[Mon Mar 09 00:16:48 2020] [notice] Apache/2.2.15 (Unix) DAV/2 configured -- resuming normal operations
[Mon Mar 09 00:17:21 2020] [crit] [client 192.168.185.7] configuration error: couldn't perform authentication. AuthType not set!: /
[Mon Mar 09 00:17:21 2020] [crit] [client 192.168.185.7] configuration error: couldn't perform authentication. AuthType not set!: /favicon.ico, referer: http://10.18.97.106:8182/
[Mon Mar 09 00:18:42 2020] [crit] [client 192.168.185.7] configuration error: couldn't perform authentication. AuthType not set!: /
[Mon Mar 09 00:18:42 2020] [crit] [client 192.168.185.7] configuration error: couldn't perform authentication. AuthType not set!: /favicon.ico, referer: http://10.18.97.106:8182/
[Mon Mar 09 00:18:43 2020] [crit] [client 192.168.185.7] configuration error: couldn't perform authentication. AuthType not set!: /
[Mon Mar 09 00:18:43 2020] [crit] [client 192.168.185.7] configuration error: couldn't perform authentication. AuthType not set!: /favicon.ico, referer: http://10.18.97.106:8182/
出现下面的错误
configuration error: couldn't perform authentication. AuthType not set!:
查看vhosts.conf的配置,我在目录鉴权部分写的是
<Directory "/var/www/html/itdocs/">
#Allowoverride authconfig
Require all granted
</Directory>
google了一下发现这个语法只在httpd版本2.4以后才支持,而我安装的版本如下
[root@testmachine conf.d]# httpd -v
Server version: Apache/2.2.15 (Unix)
Server built: Jun 19 2018 15:45:13
将配置改为如下,成功解决问题
<Directory "/var/www/html/itdocs/">
#Allowoverride authconfig
#Require all granted
Order allow,deny
Allow from all
</Directory>
4779



被折叠的 条评论
为什么被折叠?



