业务需求:单独部署Apache服务来做图片服务器

1.环境:

1.1腾讯云主机,初始化安装环境,最小化安装centos7.4 x86_64位
系统版本:

[root@VM_82_178_centos conf]# cat /etc/redhat-release 
CentOS Linux release 7.4.1708 (Core) 

1.2httpd服务安装方式:yum 安装
yum install httpd -y
httpd源码包下载地址:
http://archive.apache.org/dist/httpd/

httpd服务版本:

[root@VM_82_178_centos conf]# httpd -v
Server version: Apache/2.4.6 (CentOS)
Server built:   Jun 27 2018 13:48:59

提示:只需要安装apache服务足够提供图片服务器应用了

1.3开启Rewrite功能模块:
说明:在centos7的系统上,yum安装httpd服务,默认安装的apache服务的版本是2.4.6,它默认已经安装好了支持Rewrite功能模块

[root@VM_82_178_centos ~]# find / -name mod_rewrite.so
/usr/lib64/httpd/modules/mod_rewrite.so

补充:在centos6的系统上,yum安装httpd服务,默认安装的apache服务的版本是2.2.15

并且默认已经在配置文件/etc/httpd/conf.modules.d/00-base.conf 中加载了这个模块LoadModule rewrite_module modules/mod_rewrite.so
[root@VM_82_178_centos conf.modules.d]# grep mod_rewrite.so /etc/httpd/conf.modules.d/00-base.conf 
LoadModule rewrite_module modules/mod_rewrite.so

并且Apache/2.4.6 默认支持的模块为101个,而apache2.2.x以下版本默认才支持61个模块

2、apache不同版本开启rewrite功能介绍:

在apache1.x版本和apache2.2.x版本以及apache2.4.x开启rewrite功能的方式是不一样的

2.1、 Apache 1.x

Apache 1.x 的用户请检查 conf/httpd.conf 中是否存在如下两段代码:

 LoadModule Rewrite_module libexec/mod_Rewrite.so
  AddModule mod_Rewrite.c

Apache 1.x版本配置Apache配置文件httpd.conf实现rewrite
找到



没有的自己添加,然后之间写入你的rewrite规则,例如:


RewriteEngine On
RewriteRule ^q/[0-9]+/(.*)$  /uploads/picture/$1 [L]


    DocumentRoot "/data/www/images"
    ServerName  img.yb898.cn
    ServerAlias img.yb898.cn
    ErrorLog "logs/img.yb898.cn-error_log"
    CustomLog "logs/img.yb898.cn-access_log" common
        
                #Options Indexes FollowSymlinks
                Options  FollowSymlinks
                ##Allowoverride All
                Allowoverride None
               Order Allow,Deny
                Allow from all
          
              RewriteEngine On
             RewriteRule ^q/[0-9]+/(.*)$  /uploads/picture/$1 [L]
          
        

注意:如果网站使用通过虚拟主机来定义,请务必加到虚拟主机配置,即 中去,如果加在虚拟主机配置外部将可能无法使用

2.2、 Apache 2.2.15

Apache 2.2.15版本的用户请检查 conf/httpd.conf 中是否存在如下一段代码:

LoadModule Rewrite_module modules/mod_rewrite.so

注:如果前面有#,将其去掉。并且保证你的apache文件里有mod_rewrite.so文件(1.X版的要有mod_rewrite.c)。


    DocumentRoot "/data/www/images"
    ServerName  img.yb898.cn
    ServerAlias img.yb898.cn
    ErrorLog "logs/img.yb898.cn-error_log"
    CustomLog "logs/img.yb898.cn-access_log" common
        
                #Options Indexes FollowSymlinks
                Options  FollowSymlinks
                ##Allowoverride All
                Allowoverride None
               Order Allow,Deny
                Allow from all
        

2.3 apache2.4.6版本:

(已经测试验证是成功的)



    DocumentRoot "/data/www/images"
    ServerName  img.yb898.cn
    ServerAlias img.yb898.cn
    ErrorLog "logs/img.yb898.cn-error_log"
    CustomLog "logs/img.yb898.cn-access_log" common
        
                #Options Indexes FollowSymlinks
                Options  FollowSymlinks
                Require all granted
                RewriteEngine on
                RewriteRule ^q/[0-9]+/(.*)$  /uploads/picture/$1 [L]
                RewriteRule ^[0-9]+.([0-9]+).([0-9]+).([0-9]+).(.*)$     /uploads/picture/$1/$2/$3/$4 [L]
        

浏览器请求链接:

http://img.yb898.cn/q/12345/2016/07/28/1520924032.png
http://img.yb898.cn/12345.2016.07.28/1520924032.png
http://img.yb898.cn/uploads/picture/2016/07/28/1520924032.png

文件存放路径:

[root@VM_82_178_centos images]# ll /data/www/images/uploads/picture/2016/07/28/1520924032.png 
-rw-r--r-- 1 root root 89032 Mar 13 14:53 /data/www/images/uploads/picture/2016/07/28/1520924032.png

提示:
一个常见的问题:配置完rewrite后有时会显示Forbidden You don't have permission to access /data/www/images on this server.这样的错误。
加上Options FollowSymLinks就行了,注意不能加Indexes,这个表示显示目录(根目录没有index.html的情况下就显示文件夹的目录结构)。

3.让apache支持.htaccess

3.1对于apache1.x的版本:


RewriteEngine On
RewriteRule ^q/[0-9]+/(.*)$  /uploads/picture/$1 [L]

里的代码删除掉

找到#AccessFileName .htaccess把前面的#注释去掉

找到:


    DocumentRoot "/data/www/images"
    ServerName  img.yb898.cn
    ServerAlias img.yb898.cn
    ErrorLog "logs/img.yb898.cn-error_log"
    CustomLog "logs/img.yb898.cn-access_log" common
        
                #Options Indexes FollowSymlinks
                Options  FollowSymlinks
                 #Allow Override All
                Allowoverride None
               Order Allow,Deny
                Allow from all
        

把里边的AllowOverride None 改为 Allow Override All 这样.htaccess文件才能启用。

3.2、对于apache2.x的版本

找到#AccessFileName .htaccess把前面的#注释去掉
找到:


    DocumentRoot "/data/www/images"
    ServerName  img.yb898.cn
    ServerAlias img.yb898.cn
    ErrorLog "logs/img.yb898.cn-error_log"
    CustomLog "logs/img.yb898.cn-access_log" common
        
                #Options Indexes FollowSymlinks
                Options  FollowSymlinks
                Allowoverride All
                Require all granted
        

把参数 Allowoverride All添加上,这样.htaccess文件才能启用。

3.3、针对apache2.4.6举例:

RewriteEngine on
RewriteRule ^q/[0-9]+/(.*)$  /uploads/picture/$1 [L]

直接把上面的代码写入到.htaccess文件,并且把.htaccess文件放到站点或者图片的根目录下,重启apache就行了。

[root@VM_82_178_centos images]# cat  /data/www/images/.htaccess
RewriteEngine on
RewriteRule ^q/[0-9]+/(.*)$  /uploads/picture/$1 [L]
RewriteRule ^[0-9]+.([0-9]+).([0-9]+).([0-9]+).(.*)$     /uploads/picture/$1/$2/$3/$4 [L]

为了安全起见,在httpd.conf文件添加如下参数禁止.htaccess文件通过浏览器请求被下载


    Require all denied
    #Require all granted

一般的apache以上配置就行了,但我的还不行,所以研究了一下apache的配置文件httpd.conf发现了这样一个参数:

AccessFileName ht.access

 
意思是:AccessFileName定义每个目录下的访问控制文件的文件名,缺省为.htaccess (大多数人直接就是.htaccess,所以好多网上教程都没写这步,而我的是ht.access),可以通过更改这个文件,来改变不同目录的访问控制限制。

将ht.access改为 .htaccess 重启apache就行了。

AccessFileName .htaccess

题外:.htaccess位置问题:htaccess文件(或者"分布式配置文件")提供了针对每个目录改变配置的方法,即在一个特定的目录中放置一个包含指令的文件,其中的指令作用于此目录及其所有子目录。(每一个文件夹下都可以有个.htaccess文件)

同时rewrite规则写入到.htaccess 配置文件的一个好处就是:在每次新添加规则到.htaccess 文件中时,apache服务不需要重启,就直接可以生效

httpd.conf去掉注释参数如下:亲测可用
[root@VM_82_178_centos ~]# grep -vE '^#|^$' /etc/httpd/conf/httpd.conf

ServerRoot "/etc/httpd"
Include conf.modules.d/*.conf
User apache
Group apache
ServerAdmin root@localhost
ServerName 127.0.0.1:80

    AllowOverride none
    #Require all denied
    Require all granted

DocumentRoot "/var/www/html"

    Options Indexes FollowSymLinks
    AllowOverride None
    Require all granted


    DirectoryIndex index.html index.html.var

AccessFileName .htaccess

    Require all denied
    #Require all granted

ErrorLog "logs/error_log"
LogLevel warn

    LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
    LogFormat "%h %l %u %t \"%r\" %>s %b" common
    
      # You need to enable mod_logio.c to use %I and %O
      LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %I %O" combinedio
    
    CustomLog "logs/access_log" combined


    ScriptAlias /cgi-bin/ "/var/www/cgi-bin/"


    AllowOverride None
    Options None
    Require all granted


    TypesConfig /etc/mime.types
    AddType application/x-compress .Z
    AddType application/x-gzip .gz .tgz
    AddType application/x-httpd-php .asp .py .pl .bop .foo .133t .jsp .com
    AddType text/html .shtml
    AddOutputFilter INCLUDES .shtml

AddDefaultCharset UTF-8

    #
    MIMEMagicFile conf/magic

EnableSendfile on
IncludeOptional conf.d/*.conf
Servername 0.0.0.0:80
Listen 80

ServerAdmin [email protected]
DocumentRoot /var/www/html
ServerName 119.29.97.131
ServerAlias 119.29.97.131


    DocumentRoot "/data/www/images"
    ServerName  img.yb898.cn
    ServerAlias img.yb898.cn
    ErrorLog "logs/img.yb898.cn-error_log"
    CustomLog "logs/img.yb898.cn-access_log" common
        
                #Options Indexes FollowSymlinks
                Options  FollowSymlinks
                Allowoverride All
                Require all granted
        

[root@VM_82_178_centos ~]# 

4.httpd.conf开启IncludeOptional参数

配置文件/etc/httpd/conf/httpd.conf中开启IncludeOptional参数,使每个vhost虚拟主机单独 写到一个配置文件中


[root@VM_82_178_centos vhost]# grep IncludeOptional /etc/httpd/conf/httpd.conf
IncludeOptional conf.d/*.conf
IncludeOptional conf/vhost/*.conf
[root@VM_82_178_centos vhost]# grep -C 10 IncludeOptional /etc/httpd/conf/httpd.conf

    MIMEMagicFile conf/magic

#EnableMMAP and EnableSendfile: On systems that support it, 
EnableSendfile on

IncludeOptional conf.d/*.conf
IncludeOptional conf/vhost/*.conf
Servername 0.0.0.0:80

Listen 80

ServerAdmin [email protected]
DocumentRoot /var/www/html
ServerName 119.29.97.131
ServerAlias 119.29.97.131
[root@VM_82_178_centos vhost]# cat /etc/httpd/conf/vhost/img.yb898.cn.conf

    DocumentRoot "/data/www/images"
    ServerName  img.yb898.cn
    ServerAlias img.yb898.cn
    ErrorLog "logs/img.yb898.cn-error_log"
    CustomLog "logs/img.yb898.cn-access_log" common
        
                #Options Indexes FollowSymlinks
                Options  FollowSymlinks
                Allowoverride All
                Require all granted
        

centos7行httpd服务的相关的 启动命令如下:


[root@VM_82_178_centos vhost]# systemctl status httpd.service
[root@VM_82_178_centos vhost]# systemctl reload httpd.service
[root@VM_82_178_centos vhost]# systemctl start httpd.service
[root@VM_82_178_centos vhost]# systemctl stop httpd.service
[root@VM_82_178_centos vhost]# httpd -t
Syntax OK
[root@VM_82_178_centos vhost]# apachectl  configtest
Syntax OK
[root@VM_82_178_centos vhost]# apachectl  start
[root@VM_82_178_centos vhost]# apachectl  stop
[root@VM_82_178_centos vhost]# apachectl  restart

查看apache服务状态:

[root@VM_82_178_centos vhost]# systemctl status httpd.service
● httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled)
   Active: active (running) since Wed 2018-08-29 16:36:20 CST; 6s ago
     Docs: man:httpd(8)
           man:apachectl(8)
 Main PID: 11789 (httpd)
   Status: "Processing requests..."
   CGroup: /system.slice/httpd.service
           ├─11789 /usr/sbin/httpd -DFOREGROUND
           ├─11790 /usr/sbin/httpd -DFOREGROUND
           ├─11791 /usr/sbin/httpd -DFOREGROUND
           ├─11792 /usr/sbin/httpd -DFOREGROUND
           ├─11793 /usr/sbin/httpd -DFOREGROUND
           └─11794 /usr/sbin/httpd -DFOREGROUND

Aug 29 16:36:20 VM_82_178_centos systemd[1]: Starting The Apache HTTP Server...
Aug 29 16:36:20 VM_82_178_centos systemd[1]: Started The Apache HTTP Server.

测试apache服务:
http://img.yb898.cn/q/12345/2016/07/28/1520924032.png
http://img.yb898.cn/12345.2016.07.28/1520924032.png
http://img.yb898.cn/uploads/picture/2016/07/28/1520924032.png

参考文档:
https://www.cnblogs.com/CheeseZH/p/5169352.html
https://www.cnblogs.com/mrcln/p/5635515.html
https://www.cnblogs.com/miketwais/p/mod_rewrite.html