Discuz! Board

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 6902|回复: 0
打印 上一主题 下一主题

centos7上搭建http服务器以及设置目录访问

[复制链接]

334

主题

337

帖子

1683

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
1683
跳转到指定楼层
楼主
发表于 2020-8-21 17:54:08 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
https://www.cnblogs.com/snake553/p/8856729.html


步骤:

1. 安装httpd服务

sudo yum install httpd



Apache 的所有配置文件都位于  /etc/httpd/conf  和  /etc/httpd/conf.d  。网站的数据默认位于  /var/www,但如果你愿意,你可以改变它。



2. 配置

Apache 主要的配置文件是 /etc/httpd/conf/httpd.conf 。 它包含许多在基本安装中不需要更改的配置。 实际上,只需对此文件进行一些更改即可启动并运行一个简单的网站。

2.1 监听端口

第一个要修改的是 Listen 配置项,它定义了 Apache 要监听页面请求的 IP 地址和端口。 现在,你只需要使这个网站可以从本地访问,所以使用 localhost 地址。 完成后,该行应该看起来像这样:

Listen 127.0.0.1:80

通过将此配置项设置为 localhost 的 IP 地址,Apache 将只侦听来自本地主机的连接。 如果您希望 Web 服务器侦听来自远程主机的连接,则可以使用主机的外部 IP 地址。



2.2 网站页面HTML文件位置

DocumentRoot 配置项指定组成网站页面的 HTML 文件的位置。 该配置项不需要更改,因为它已经指向标准位置。 该行应该看起来像这样:

DocumentRoot"/var/www/html"



Apache 安装包会创建 /var/www 目录。 如果您想更改存储网站文件的位置,则使用此配置项来完成此操作。 例如,您可能想要为 www 目录使用不同的名称,以更明确地识别网站。 这可以是这样的:

DocumentRoot"/var/mywebsite/html"



这些是创建一个简单网站需要唯一修改的 Apache 配置项。




2.3 防火墙端口设置:打开端口 80


(1)查询TCP/UDP的80端口占用情况:

sudo firewall-cmd --query-port=80/tcp

sudo firewall-cmd --query-port=80/udp

如果返回结果为“no”,则表示该端口尚未开放,需要作以下设置才可以;否则,跳过步骤2.3。

(2)永久开放TCP/UDP的80端口



sudo firewall-cmd --permanent --zone=public --add-port=80/tcp
sudo firewall-cmd --permanent --zone=public --add-port=80/udp



(3)重启防火墙




sudo firewall-cmd --reload





3.创建index.html文件

index.html 文件是你使用域名访问网站而不是访问特定网页时的默认文件。在 /var/www/html 中,创建一个名字为 index.html 的文件,在其中添加字符串 Hello World 。你不需要添加任何的 HTML 标志去完成这项工作。web 服务器的唯一任务是提供文本数据流,服务器不知道数据是什么,也不知道如何呈现它。它只是将数据流传输给请求主机。

保存文件后,将所有权设置为 apache.apache 。

chown apache.apache index.html



4. 启动 Apache


复制代码
$ sudo systemctl start httpd

$ 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 一 2018-04-16 11:01:59 CST; 5h 50min ago
     Docs: man:httpd(8)
           man:apachectl(8)
  Process: 41464 ExecStop=/bin/kill -WINCH ${MAINPID} (code=exited, status=0/SUCCESS)
Main PID: 41473 (httpd)
   Status: "Total requests: 6; Current requests/sec: 0; Current traffic:   0 B/sec"
   CGroup: /system.slice/httpd.service
           ├─41473 /usr/sbin/httpd -DFOREGROUND
           ├─41474 /usr/sbin/httpd -DFOREGROUND
           ├─41475 /usr/sbin/httpd -DFOREGROUND
           ├─41476 /usr/sbin/httpd -DFOREGROUND
           ├─41477 /usr/sbin/httpd -DFOREGROUND
           ├─41478 /usr/sbin/httpd -DFOREGROUND
           └─43670 /usr/sbin/httpd -DFOREGROUND

4月 16 11:01:58 Sun systemd[1]: Starting The Apache HTTP Server...
4月 16 11:01:59 Sun httpd[41473]: AH00557: httpd: apr_sockaddr_info_get() failed for Sun
4月 16 11:01:59 Sun httpd[41473]: AH00558: httpd: Could not reliably determine the s...age
4月 16 11:01:59 Sun systemd[1]: Started The Apache HTTP Server.
Hint: Some lines were ellipsized, use -l to show in full.

复制代码



5. 访问web服务器

在Chrome或firefox浏览器中,输入本机的url地址: http://localhost ,即可访问到本机。

试试看,屏幕上显示的内容是不是和文件 /var/www/html/index.html 中的内容一致呢?



6. 开启目录结构

具体操作参考这篇博客《centos7 下httpd服务器开启目录》。

6.1 修改配置文件 welcome.conf

将配置文件 /etc/httpd/conf.d/welcome.conf 的-号改为+号:

原文:    Options -Indexes

修改后: Options +Indexes

备注:Indexes 其实就是Apache中的索引服务,想了解它的信息可以参考这篇博客:《基于Apache服务器的文件列表,即文件的http下载模式》。



6.2 重启http服务

在终端执行命令  systemctl restart httpd ,重启服务就可以看到目录服务器下的目录了



6.3 了解配置文件welcome.conf

如果6.2一步之后得到了形如这样的目录结构,则可以跳过这一小节。

否则,还是花一分钟来了解下这个配置文件吧!

这里是它的全文:


复制代码
[User@Host ~]$ cat  /etc/httpd/conf.d/welcome.conf
#
# This configuration file enables the default "Welcome" page if there
# is no default index page present for the root URL.  To disable the
# Welcome page, comment out all the lines below.
#
# NOTE: if this file is removed, it will be restored on upgrades.
#
<LocationMatch "^/+$">
    Options +Indexes
    ErrorDocument 403 /.noindex.html
</LocationMatch>

<Directory /usr/share/httpd/noindex>
    AllowOverride None
    Require all granted
</Directory>

Alias /.noindex.html /usr/share/httpd/noindex/index.html
Alias /noindex/css/bootstrap.min.css /usr/share/httpd/noindex/css/bootstrap.min.css
Alias /noindex/css/open-sans.css /usr/share/httpd/noindex/css/open-sans.css
Alias /images/apache_pb.gif /usr/share/httpd/noindex/images/apache_pb.gif
Alias /images/poweredby.png /usr/share/httpd/noindex/images/poweredby.png

复制代码



解释一下,"default index page“ 指的是位于http文件服务器下载目录的文档 index.html。

在本例中,这个文件的全称是 /var/www/html/index.html 。

在这里,为了使welcome.conf文档中的目录结构设置生效,我们必须删除index.html!



删除index.html之后,再浏览器中打开本机的网址 http://localhost ,看看结果是不是变成目录结构了呢?



6.4  更改http服务器的默认目录

在配置文件 /etc/httpd/conf/httpd.conf 中一共有三个地方需要修改,这里以目标目录  /pub/meetings/test  为例。

(1)修改参数 “DocumentRoot”:

关于这个参数的一部分原文长这样:

[User@Host ~]$ cat /etc/httpd/conf/httpd.conf | grep "DocumentRoot"
# DocumentRoot: The directory out of which you will serve your
DocumentRoot "/var/www/html"
    # access content that does not live under the DocumentRoot.

可以看到,它默认的目录位于  /var/www/html 。

接下来,我们注释掉原文,把它改成我们需要的  /pub/meetings/test   目录。


复制代码
[User@Host ~]$ sudo vi /etc/httpd/conf/httpd.conf
...
# DocumentRoot: The directory out of which you will serve your
# DocumentRoot "/var/www/html"
DocumentRoot "/pub/meetings/test"
    # access content that does not live under the DocumentRoot.
...

复制代码

(2)修改目录参数


复制代码
[User@Host ~]$ sudo vi /etc/httpd/conf/httpd.conf
...
#
# Relax access to content within /var/www.
#
#<Directory "/var/www">
<Directory "/pub/meetings">
    AllowOverride None
    # Allow open access:
    Require all granted
</Directory>
...

复制代码

(3)再次修改目录参数


复制代码

[User@Host ~]$ sudo vi /etc/httpd/conf/httpd.conf
...
# Further relax access to the default document root:
#<Directory "/var/www/html">
<Directory "/pub/meetings/test">
    #
    # Possible values for the Options directive are "None", "All",
    # or any combination of:
    #   Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews
    #
...


复制代码



(4)同样地,再次重启http服务,确保我们的更改立即生效:

sudo systemctl restart httpd



经过这样一番设置,在浏览器(http://localhost)看到的,应该就是  /pub/meetings/test  的目录结构了。



(5)如果第(4)步不能正常访问到目标目录,那么,通常是由于Apache用户关于该文件夹的权限太低(apache的用户:apache 运行apache的组:apache,默认权限为750)[有关内容可以从这里了解]。

我们需要给它赋予作以下设置

# 755或者777均可 (二选一:最好是777,因为它具有写权限)
# 755: rwxr-xr-x
sudo chmod -R 755 /pub/meetings/
# 777: rwxrwxrwx
sudo chmod -R 777 /pub/meetings/

再重启http服务,就可以搞定了。



(6)其它一些细节设置

默认的设置有一些地方需要修改:不支持中文、显示优化等等。

具体执行的操作是将默认的设置参数按照如下方式修改,增加4个参数:

sudo vi /etc/httpd/conf.d/autoindex.conf
...
#IndexOptions FancyIndexing HTMLTable VersionSort
IndexOptions FancyIndexing HTMLTable VersionSort FoldersFirst Charset=UTF-8 NameWidth=* XHTML

...

其中,FoldersFirst 保证显示结果中的文件夹名称居于前面,UTF-8字符集有效地解决了中文显示的问题,“NameWidth=*”的作用不详。

关于更详细的配置过程,可以参考这篇博客《基于Apache服务器的文件列表,即文件的http下载模式》。



(7) 加载 NTFS 格式的分区时遇到的问题

按以上方法对 NTFS 格式的分区所在的目录进行设置,并不能浏览每一个分区内的内容,只能看到分区的根目录下的内容。

根据系统报告可以进行修复:


复制代码
[User@Host ~]$ journalctl -xe
...
4月 18 16:33:23 localhost.localdomain dbus[733]: [system] Successfully activated service 'org.fedoraproject.Setroubleshootd'
4月 18 16:33:24 localhost.localdomain setroubleshoot[32222]: failed to retrieve rpm info for /mnt/Disk2T/L
4月 18 16:33:24 localhost.localdomain setroubleshoot[32222]: SELinux is preventing /usr/sbin/httpd from read access on the directory
                                                             /mnt/Disk2T/L. For complete SELinux messages. run se
4月 18 16:33:24 localhost.localdomain python[32222]: SELinux is preventing /usr/sbin/httpd from read access on the directory /mnt/Disk2T/L.
                                                      
                                                      *****  Plugin catchall_boolean (89.3 confidence) suggests   ******************
                                                      
                                                      If you want to allow httpd to use fusefs
                                                      Then you must tell SELinux about this by enabling the 'httpd_use_fusefs' boolean.
                                                      You can read 'None' man page for more details.
                                                      Do
                                                      setsebool -P httpd_use_fusefs 1
                                                      
                                                      *****  Plugin catchall (11.6 confidence) suggests   **************************
                                                      
                                                      If you believe that httpd should be allowed read access on the L directory by default.
                                                      Then you should report this as a bug.
                                                      You can generate a local policy module to allow this access.
                                                      Do
                                                      allow this access for now by executing:
                                                      # ausearch -c 'httpd' --raw | audit2allow -M my-httpd
                                                      # semodule -i my-httpd.pp
                                                      
4月 18 16:33:32 localhost.localdomain fprintd[32183]: No devices in use, exit
[User@Host ~]$ sudo setsebool -P httpd_use_fusefs 1
[User@Host ~]$ sudo systemctl restart httpd

复制代码





7. 创建局域网内的机器互访

这部分的内容不属于Web服务器搭建的范畴,有关的内容可以参考路由器DMZ端口映射的配置。

主要原理就是,将提供Web服务的主机的80端口映射出去,使上一级局域网中的用户也能访问到它。

设置完成之后,在浏览器中采用主机加端口的方式来访问,比如:

首页:http://172.28.21.201:11080/index.html

或者,目录结构: http://172.28.21.201:11080/





8. 重启后无法访问http服务器

首先查询http服务的状态: sudo systemctl status httpd 。

如果http服务的状态正常,显示它“active”,则问题不出在httpd服务上。

最有可能出问题的是SELINUX的设置,因为重启之后,之前的设置都不生效了。

关闭Centos 7 的方法很简单:

(0)查询SELINUX的状态

getenforce



(1)临时关闭SELINUX

#设置SELinux 成为permissive模式
##setenforce 1 设置SELinux 成为enforcing模式
setenforce 0



(2)永久关闭SELINUX

vi /etc/selinux/config

将 SELINUX=enforcing 改为 SELINUX=disabled ,设置后需要重启才能生效。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|飞克网 ( 皖ICP备16023507号 )

GMT+8, 2024-4-24 00:38 , Processed in 0.213388 second(s), 20 queries .

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表