美文网首页Linux服务器
Centos7上搭建http服务器以及设置目录访问

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

作者: DBA成长之路 | 来源:发表于2019-08-06 20:51 被阅读0次

    步骤:

    1. 安装httpd服务

    [root@localhost ~]# mount /dev/cdrom /mnt
    mount: /dev/sr0 is write-protected, mounting read-only
    [root@localhost ~]# cd /etc/yum.repos.d/
    [root@localhost yum.repos.d]# ls
    CentOS-Base.repo  CentOS-Debuginfo.repo  CentOS-Media.repo    CentOS-Vault.repo
    CentOS-CR.repo    CentOS-fasttrack.repo  CentOS-Sources.repo
    [root@localhost yum.repos.d]# mkdir bak
    [root@localhost yum.repos.d]# mv *.repo bak/
    [root@localhost yum.repos.d]# ls
    bak
    [root@localhost yum.repos.d]# vi CentOS-Base.repo
    [CentOS]
    name=centos 7.6 dvd
    baseurl=file:///mnt/
    enabled=1
    gpgcheck=0
    [root@localhost yum.repos.d]# yum repolist
    Loaded plugins: fastestmirror, langpacks
    Loading mirror speeds from cached hostfile
    CentOS                                                             | 3.6 kB  00:00:00     
    (1/2): CentOS/group_gz                                             | 166 kB  00:00:00     
    (2/2): CentOS/primary_db                                           | 3.1 MB  00:00:00     
    repo id                               repo name                                     status
    CentOS                                centos 7.6 dvd                                4,021
    repolist: 4,021
    [root@localhost yum.repos.d]# yum install httpd -y
    Loaded plugins: fastestmirror, langpacks
    Loading mirror speeds from cached hostfile
    Resolving Dependencies
    --> Running transaction check
    ---> Package httpd.x86_64 0:2.4.6-88.el7.centos will be installed
    --> Processing Dependency: httpd-tools = 2.4.6-88.el7.centos for package: httpd-2.4.6-88.el7.centos.x86_64
    --> Running transaction check
    ---> Package httpd-tools.x86_64 0:2.4.6-88.el7.centos will be installed
    --> Finished Dependency Resolution
    
    Dependencies Resolved
    
    ==========================================================================================
     Package              Arch            Version                       Repository       Size
    ==========================================================================================
    Installing:
     httpd                x86_64          2.4.6-88.el7.centos           CentOS          2.7 M
    Installing for dependencies:
     httpd-tools          x86_64          2.4.6-88.el7.centos           CentOS           90 k
    
    Transaction Summary
    ==========================================================================================
    Install  1 Package (+1 Dependent package)
    
    Total download size: 2.8 M
    Installed size: 9.5 M
    Downloading packages:
    ------------------------------------------------------------------------------------------
    Total                                                      26 MB/s | 2.8 MB  00:00:00     
    Running transaction check
    Running transaction test
    Transaction test succeeded
    Running transaction
      Installing : httpd-tools-2.4.6-88.el7.centos.x86_64                                 1/2 
      Installing : httpd-2.4.6-88.el7.centos.x86_64                                       2/2 
      Verifying  : httpd-tools-2.4.6-88.el7.centos.x86_64                                 1/2 
      Verifying  : httpd-2.4.6-88.el7.centos.x86_64                                       2/2 
    
    Installed:
      httpd.x86_64 0:2.4.6-88.el7.centos                                                      
    
    Dependency Installed:
      httpd-tools.x86_64 0:2.4.6-88.el7.centos                                                
    
    Complete!
    
    

    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端口占用情况:

    [root@localhost ~]# firewall-cmd --query-port=80/tcp 
    no
    [root@localhost ~]# firewall-cmd --query-port=80/udp
    no
    

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

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

    [root@localhost ~]# firewall-cmd --permanent --zone=public --add-port=80/tcp 
    success
    [root@localhost ~]# firewall-cmd --permanent --zone=public --add-port=80/udp
    success
    

    (3)重启防火墙

    [root@localhost ~]# firewall-cmd --reload
    success
    

    3.创建index.html文件

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

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

    [root@localhost html]# echo "this is my first webpage" > /var/www/html/index.html
    [root@localhost html]# chown apache.apache /var/www/html/index.html
    

    4. 启动 Apache

    [root@localhost ~]# systemctl start httpd
    [root@localhost ~]# 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 Tue 2019-08-06 20:23:19 CST; 31s ago
         Docs: man:httpd(8)
               man:apachectl(8)
     Main PID: 10233 (httpd)
       Status: "Total requests: 0; Current requests/sec: 0; Current traffic:   0 B/sec"
        Tasks: 6
       CGroup: /system.slice/httpd.service
               ├─10233 /usr/sbin/httpd -DFOREGROUND
               ├─10236 /usr/sbin/httpd -DFOREGROUND
               ├─10237 /usr/sbin/httpd -DFOREGROUND
               ├─10238 /usr/sbin/httpd -DFOREGROUND
               ├─10240 /usr/sbin/httpd -DFOREGROUND
               └─10242 /usr/sbin/httpd -DFOREGROUND
    
    Aug 06 20:23:19 localhost systemd[1]: Starting The Apache HTTP Server...
    Aug 06 20:23:19 localhost httpd[10233]: AH00558: httpd: Could not reliably determine ...ge
    Aug 06 20:23:19 localhost 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. 开启目录结构

    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一步之后得到了形如这样的目录结构,则可以跳过这一小节。

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

    这里是它的全文:

    [root@localhost ~]# 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”:

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

    [root@localhost ~]# 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 目录(119行)。

    [root@localhost ~]#  vi /etc/httpd/conf/httpd.conf
    ...
    # DocumentRoot: The directory out of which you will serve your
    # documents. By default, all requests are taken from this directory, but
    # symbolic links and aliases may be used to point to other locations.
    #
    #DocumentRoot "/var/www/html"
    DocumentRoot "/pub/meetings/test"
    ...
    

    (2)修改目录参数

     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)再次修改目录参数

    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服务,确保我们的更改立即生效:

    [root@localhost ~]# mkdir -p /pub/meetings/test
    [root@localhost ~]# chown -R apache:apache /pub/meetings/test
    [root@localhost ~]# systemctl restart httpd
    

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

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

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

    755或者777均可 (二选一:最好是777,因为它具有写权限)

    755: rwxr-xr-x

    chmod -R 755 /pub/meetings/

    777: rwxrwxrwx

    chmod -R 777 /pub/meetings/

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

    (6)其它一些细节设置

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

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

    [root@localhost ~]# 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
    [root@localhost ~]# setsebool -P httpd_use_fusefs 1 
    [root@localhost ~]# systemctl restart httpd
    
    

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

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

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

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

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

    或者,目录结构: http://192.168.1.50: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 ,设置后需要重启才能生效。

    全文完。

    相关文章

      网友评论

        本文标题:Centos7上搭建http服务器以及设置目录访问

        本文链接:https://www.haomeiwen.com/subject/kbqgdctx.html