apache 搭建图片预览服务

本贴最后更新于 323 天前,其中的信息可能已经时移俗易

apache 搭建图片预览服务

apache 安装

yum install httpd -y

systemctl start httpd
systemctl enable httpd

备注:端口默认是 80

httpd 设置

打开 apache 的主配置文件

vim /etc/httpd/conf/httpd.conf

找到第 42 行 Listen,这里可以设施服务的端口

#
# Listen: Allows you to bind Apache to specific IP addresses and/or
# ports, instead of the default. See also the <VirtualHost>
# directive.
#
# Change this to Listen on specific IP addresses as shown below to 
# prevent Apache from glomming onto all bound IP addresses.
#
#Listen 12.34.56.78:80
Listen 9000

#
# Dynamic Shared Object (DSO) Support
#

找到第 119 行至 131 行,将 DocumentRoot、Directory 设置成自己的文件或图片仓库目录,如果我的目录地址是/home/file

#
# 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 "/home/file"

#
# Relax access to content within /var/www.
#
<Directory "/home/file">
    AllowOverride None
    # Allow open access:
    Require all granted
</Directory>

# Further relax access to the default document root:
<Directory "/home/file">
    #
    # Possible values for the Options directive are "None", "All",
    # or any combination of:
    #   Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews
    #

这样我们就可以直接浏览我们仓库的文件目录或者图片了

如果没有具体的访问文件,就会默认跳转到根目录,其它所有的文件信息都能看到,这样文件就会暴露,我们可以设置目录禁止访问:

    #
    Options Indexes FollowSymLinks
    #

将 Options Indexes FollowSymLinks 修改为 Options FollowSymLinks,其实就是将 Indexes 去掉,Indexes 表示若当前目录没有 index.html 就会显示目录结构。

当目录中存在中文的时候,默认的 httpd 配置会显示乱码,我们可以修改 httpd 的编码

# Further relax access to the default document root:
<Directory "/home/file">
    #
    # Possible values for the Options directive are "None", "All",
    # or any combination of:
    #   Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews
    #
    # Note that "MultiViews" must be named *explicitly* --- "Options All"
    # doesn't give it to you.
    #
    # The Options directive is both complicated and important.  Please see
    # http://httpd.apache.org/docs/2.4/mod/core.html#options
    # for more information.
    #
    Options FollowSymLinks
    IndexOptions Charset=GB2312
    #
    # AllowOverride controls what directives may be placed in .htaccess files.
    # It can be "All", "None", or any combination of the keywords:
    #   Options FileInfo AuthConfig Limit
    #
    AllowOverride None

    #
    # Controls who can get stuff from this server.
    #
    Require all granted
</Directory>

在 Options FollowSymLinks 下加 IndexOptions Charset=GB2312 这一行配置就可以了,重启 httpd 服务,乱码问题解决。

回帖
请输入回帖内容...