参考文献:http://www.jb51.net/article/137596.htm,原文摘抄如下,并根据具体需要作了相应的修改。

步骤:

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:

通过将此配置项设置为 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=/tcp

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

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

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

sudo firewall-cmd --permanent --zone=public --add-port=/tcp
sudo firewall-cmd --permanent --zone=public --add-port=/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 一 -- :: CST; 5h 50min ago
Docs: man:httpd()
man:apachectl()
Process: ExecStop=/bin/kill -WINCH ${MAINPID} (code=exited, status=/SUCCESS)
Main PID: (httpd)
Status: "Total requests: 6; Current requests/sec: 0; Current traffic: 0 B/sec"
CGroup: /system.slice/httpd.service
├─ /usr/sbin/httpd -DFOREGROUND
├─ /usr/sbin/httpd -DFOREGROUND
├─ /usr/sbin/httpd -DFOREGROUND
├─ /usr/sbin/httpd -DFOREGROUND
├─ /usr/sbin/httpd -DFOREGROUND
├─ /usr/sbin/httpd -DFOREGROUND
└─ /usr/sbin/httpd -DFOREGROUND 4月 :: Sun systemd[]: Starting The Apache HTTP Server...
4月 :: Sun httpd[]: AH00557: httpd: apr_sockaddr_info_get() failed for Sun
4月 :: Sun httpd[]: AH00558: httpd: Could not reliably determine the s...age
4月 :: Sun systemd[]: 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 /.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 /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月 :: localhost.localdomain dbus[]: [system] Successfully activated service 'org.fedoraproject.Setroubleshootd'
4月 :: localhost.localdomain setroubleshoot[]: failed to retrieve rpm info for /mnt/Disk2T/L
4月 :: localhost.localdomain setroubleshoot[]: SELinux is preventing /usr/sbin/httpd from read access on the directory
/mnt/Disk2T/
L. For complete SELinux messages. run se
4月 :: localhost.localdomain python[]: 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 ***** 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月 :: localhost.localdomain fprintd[]: 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 设置SELinux 成为enforcing模式
setenforce

(2)永久关闭SELINUX

vi /etc/selinux/config

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

全文完。

centos7上搭建http服务器以及设置目录访问的更多相关文章

  1. Centos7上搭建ftp服务器

    ftp服务器搭建 1.安装好centos系统,配好yum仓库 其中vsftpd源在这下载 http://rpmfind.net/linux/rpm2html/search.php?query=vsft ...

  2. centos7上搭建ftp服务器(亲测可用)

    1.安装vsftpd 首先要查看你是否安装vsftp [root@localhost /]# rpm -q vsftpd vsftpd-3.0.2-10.el7.x86_64 (显示以上相关信息也就安 ...

  3. CentOS7上搭建WEB服务器

    mysql 安装 直接yum install mysql-server是不可以的 1 wget http://repo.mysql.com/mysql-community-release-el7-5. ...

  4. CentOS Linux上搭建PPPoE服务器及拨号设置

    CentOS Linux上搭建PPPoE服务器及拨号设置 搭建PPPoE,成功了的话,就觉得超级简单,在CentOS Linux更是5步左右就能搞定. 1.安装pppoe,安装完成后,会有pppoe- ...

  5. Ubuntu上搭建GPU服务器

    1.安装显卡驱动 2.安装CUDA 3.安装cuDNN 下载: 根据显卡类型以及操作系统,选定CUDA版本和语言设置,下载对应的显卡驱动. 驱动下载地址 安装 $ sudo ./NVIDIA-Linu ...

  6. windows 上搭建 sftp 服务器 -freesshd全过程( 在linux上部署逐浪CMS的必读教程)

    文章标题: windows 上搭建 sftp 服务器 - freesshd全过程 关键字 : freesshd 文章分类: 教程 创建时间: 2020年3月23日 缘由 动手 第一步:添加用户 第二步 ...

  7. CentOs上搭建git服务器

    CentOs上搭建git服务器 首先安装setuptools wget http://pypi.python.org/packages/source/s/setuptools/setuptools-0 ...

  8. 在路由器上搭建SVN服务器

    在路由器上搭建SVN服务器 SVN托管服务大家都不陌生了,我最早开始用的是谷歌提供的SVN,因为在上面托管的项目都是开源的,所以当有些项目不方便在网上公开的时候,就需要自己搭建SVN服务器了.wind ...

  9. Ubuntu上搭建Git服务器

    下面我们就看看,如何在Ubuntu上搭建Git服务器.我们使用VMware虚拟机安装两台Ubantu系统,分别命名为gitServer和gitClient_01. 1.安装OpenSSH并配置SSH无 ...

随机推荐

  1. IP通信基础学习第四周(上)

    IP地址现在由因特网名字与号码指派公司ICANN进行分配,它是标志一个主机(或路由器)和一条链路的接口,其编址方法有:分类的IP地址.子网的划分.构成超网. 分类两级IP地址可以记为:IP::={&l ...

  2. C博客作业04--数组

    1. 本章学习总结 1.1 思维导图 1.2 本章学习体会及代码量学习体会 1.2.1 学习体会 这几周学习了数组,一维数组,二维数组,字符数组,前一个题集还没做完,后一个题集就又发布了,当场去世,可 ...

  3. GHSpro多数据库连接

    GHSpro多数据库连接 文章 1 数据连接 XXX.Application.Web -> XmlConfig -> database.config <connectionStrin ...

  4. Spark大型电商项目实战-及其改良(2) RDD优化效果不稳定的真正原因

    首先看没有map join的第2任务: 时间线如下 接着是对应id的算子计算时间表 Stage Id Description Submitted Duration Tasks: Succeeded/T ...

  5. IDEA中Git的更新、提交、还原方法

    第一步:在提交项目之前必须先对项目进行更新,此项特别重要,如果不进行更新,别人有项目提交到服务器上,那么你的项目将会提交不上去,使用git解决冲突会比较麻烦,即使你解决了冲突,但是有时候不注意会冲掉别 ...

  6. 【题解】Luogu P1471 方差

    原题传送门 简单进行推导之后,就能发现很妙的结论 用线段树维护区间和,区间平方和就珂以算出结果 #include <bits/stdc++.h> #define db double #de ...

  7. 隐藏软键盘(解决自定义Dialog中无法隐藏的问题)

    /** * Dialog中隐藏软键盘不管用 * @param activity */ public static void HideSoftKeyBoard(Activity activity){ t ...

  8. 王之泰201771010131《面向对象程序设计(java)》第十四周学习总结

    第一部分:理论知识学习部分 第12章 Swing用户界面组件 12.1.Swing和MVC设计模式 a 设计模式初识b 模型—视图—控制器模式c Swing组件的模型—视图—控制器分析 12.2布局管 ...

  9. Bigger-Mai 养成计划,前端基础学习之HTML

    HTML 超文本标记语言(Hyper Text Markup Language) 1.一套规则,浏览器认识的规则. 2.开发者: 学习Html规则 开发后台程序: - 写Html文件(充当模板的作用) ...

  10. bug日记之---------js中调用另一个js中的有ajax的方法, 返回值为undefind

    今天做一个OCR授权的需求, 需要开发一个OCR弹框, 让用户选择是否授权给第三方识别公司(旷世科技)保存和识别用户个人信息, 照片等. 其中用到了在一个js的方法中调用另外一个js的方法, 其中有一 ...