参考文献: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. maven的两种打包插件 ,防止 将无用文件打入META_INF,找不到主类的问题

    第三种 打依赖包 将依赖其他jar的包都打进去 <plugin> <artifactId>maven-assembly-plugin</artifactId> &l ...

  2. CSS3@media媒体查询

    CSS3@media媒体查询 定义 media媒体查询, 当文档宽度变化时, 就可以根据文档宽度的变化来运用样式,不同的宽度应用不同的样式 使用 @media 查询,你可以针对不同的媒体类型定义不同的 ...

  3. qt注册表关联文件格式

    注册表相关知识:http://www.360doc.com/content/12/0812/19/3688062_229808059.shtml(超详细超实用) 将某种类型的格式关联到某个可执行程序, ...

  4. windows下使用electron+sqlite3

    1.前置条件 1.1:安装 python2.7.python 若是有问题,如果之前安装过多个版本,则必须 npm config set python "/path/python.exe&qu ...

  5. P2774 方格取数问题(网络流)

    P2774 方格取数问题 emm........仔细一看,这不是最大权闭合子图的题吗! 取一个点$(x,y)$,限制条件是同时取$(x,y+1),(x,y-1),(x+1,y),(x-1,y)$,只不 ...

  6. SSM学习(一)Mybatis

    Mybatis Notes Mybatis First 创建Maven项目 配置依赖 <dependencies> <dependency> <groupId>ju ...

  7. Laravel用post提交表单常见的两个错误

    最近在自学Laravel,测试用post方法提交表单时碰到两个错误: 1.获取传入的值时,报错如下图所示: 在stackFlow找答案时各种解释都有,但认真读一下报错信息,意思大概是:获取Http传入 ...

  8. VS2010下安装boost库

    在我们的C++项目中安装boost库,下面以VS2010版本作为例子,其它版本的设置也差不多. 一.编译生成boost库 1.下载最新的boost(本人下载的是boost_1_56_0).boost官 ...

  9. WinForm 绑定到嵌套对象上的属性

    WinFrom 绑定到嵌套对象上的属性 关键字: Windows Forms, DataBindings, Nested Class, 嵌套类 在 WinForm 中很早就已经支持数据绑定, 使用数据 ...

  10. 论文笔记:Fast Online Object Tracking and Segmentation: A Unifying Approach

    Fast Online Object Tracking and Segmentation: A Unifying Approach CVPR-2019 2019-03-11 23:45:12 Pape ...