Skip to content

Apache Cheetsheet

Apache 中文文档 - Apache HTTP 服务器 2.4 文档

隐藏Apache版本信息

参考链接:阿里云ECS服务器隐藏Apache版本信息

sh
# 1.打开apache配置文件
$ vim /etc/httpd/conf/httpd.conf

# 2.将下面两行代码添加到 Apache 配置文件底部:
ServerSignature Off
ServerTokens Prod

# ServerSignature Off 目的是让Apache网站服务器在所有错误页面上隐藏Apache版本信息。
# ServerTokens Prod 目的是在HTTP响应头中将服务器标记压缩到最小,否则Apache服务器将仍然在HTTP回应头部包含详细的服务器标记,这会泄漏Apache的版本号。

# 3.重启apache服务
$ systemctl restart httpd.service

Apache更改默认网站目录

参考链接:

  1. 创建想要的目录,如 /home/kevin/www/html 目录
shell
$ cd /home/kevin/www/
$ mkdir html
  1. 修改apache配置文件
shell
$ vim /etc/httpd/conf/httpd.conf 

# 把其中的 /var/www/html 改成 /home/kevin/www/html
# 把其中的 /var/www 改成 /home/kevin/www/
  1. 重启apache服务
shell
$ systemctl restart httpd.service
  1. 访问公网IP测试效果

若出现 403 Forbidden 错误,为文件夹赋予读取权限:

shell
$ chmod -R 755 /home/kevin

# 这里只改`/home/kevin/www/`不行,必须修改`/home/kevin`

Apache禁止显示目录索引

参考链接:Apache设置禁止访问网站目录

  1. 打开apache配置文件
sh
$ vim /etc/httpd/conf/httpd.conf
  1. 找到 Options Indexes FollowSymLinks ,去掉 Indexes
ini
...

<Directory "/home/kevin/www/html">

    #
    # 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.2/mod/core.html#options
    # for more information.
    #
    Options Indexes FollowSymLinks

    #
    # 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.
    #
    Order allow,deny
    Allow from all

</Directory>
  1. 重启apache服务
shell
$ systemctl restart httpd.service

Apache安装SSL证书并配置https

参考链接:

  1. 获取证书文件并上传到服务器
sh
$ mv -i /home/kevin/cert/etc/httpd

# 在Apache安装目录中新建cert目录,并将证书、证书链文件和密钥文件拷贝到cert目录中。
# 由于root不能登陆ftp,这里先将证书上传到kevin文件夹,再通过root账户转移文件。
  1. 安装SSL模块
sh
yum install mod_ssl openssl openssl-devel httpd-devel;
  1. 打开httpd.conf
sh
$ vim /etc/httpd/conf/httpd.conf
  1. 配置httpd.conf
ini
# 删除行首的配置语句注释符号“#”,加载mod_ssl.so模块启用SSL服务。(Apache默认是不启用该模块的)
# 如果找不到该配置,请重新编译mod_ssl模块。
LoadModule ssl_module modules/mod_ssl.so  

# 加载模块配置文件
Include conf.modules.d/*.conf
  1. 打开打开ssl.conf
sh
$ vim /etc/httpd/conf.d/ssl.conf
  1. 配置ssl.conf
ini
# 注意:需注释掉原来就存在的相应代码
<VirtualHost *:443>     
    ServerName xukaiwen.com               
    DocumentRoot "/home/kevin/www/html"        
    SSLEngine on
    # 添加SSL协议支持协议,去掉不安全的协议:
    SSLProtocol all -SSLv2 -SSLv3   
    # 修改加密套件:
    SSLCipherSuite HIGH:!RC4:!MD5:!aNULL:!eNULL:!NULL:!DH:!EDH:!EXP:+MEDIUM 
    SSLHonorCipherOrder on
    SSLCertificateFile /etc/httpd/cert/1877027_www.xukaiwen.com_public.crt    
    SSLCertificateKeyFile /etc/httpd/cert/1877027_www.xukaiwen.com.key
    SSLCertificateChainFile /etc/httpd/cert/1877027_www.xukaiwen.com_chain.crt
</VirtualHost>
  1. 重启apache服务
shell
$ systemctl restart httpd.service
  1. 配置http重定向
ini
# 在httpd.conf文件中的<VirtualHost *:80> </VirtualHost>中间添加以下重定向代码:
<VirtualHost *:80>
	RewriteEngine on
	RewriteCond %{SERVER_PORT} !^443$
	RewriteRule ^/?(.*)$ https://%{SERVER_NAME}/$1 [L,R]
</VirtualHost>