碰到什么记录什么?!
访问目录让其返回 Forbidden
禁用 Apache 的目录索引功能 , 具体方法如下 :
编辑 /etc/apache2/apche2.conf
在网站目录的配置项下 : < Directory / var /www/> Options FollowSymLinks Indexes AllowOverride ALL Require all granted DirectoryIndex index.php index.html index.htm </Directory> 修改为 : <Directory /var/www/> Options FollowSymLinks AllowOverride ALL Require all granted DirectoryIndex index.php index.html index.htm </Directory>
也就是去掉 Indexes 这个 Options;
这样用户在访问某一个目录的时候,如果这里目录下面没有 DirectoryIndex 这个配置项中定义的文件,例如 index.html index.php;
Apache 就是直接返回 403 Forbidden;
但是这样的配置防止攻击者在不知道目标网站目录结构的情况,或者说这个配置项并不是是一个为了安全考虑的配置项;
当攻击者知道目标目录的结构的时候 , 那么这个配置项事实上是并没有什么用处的;
参考:
禁止 跨站 设置
禁止跨站设置,在 <VirtualHost *:80> </VirtualHost>中添加下面这句,意味着仅允许访问这两个目录(防菜刀中跨站):
php_admin_value open_basedir "/var/xxxxxxxxxxxxx/:/tmp/"
禁止在指定目录执行脚本
禁止在上传目录(/wp-content/uploads/)执行脚本,在 /etc/apache2/apache2.conf 或者sites-enabled下*.conf中添加:
<Directory /var/xxxxxxxxxx/wp-content/uploads> # 禁止解析 php 文件 php_admin_flag engine off # 禁止用户下载 php 文件 <filesmatch ".+\.ph(p[345]?|t|tml)$"> Order deny,allow Deny from all </filesmatch> </Directory>
访问带 php 这类后缀的文件即给出 403 禁止,即使不存在的页面也会报403,加以混淆;
一台服务器多个站点添加多个证书
如果需要设置多个 SSL 站点,在Apache 2.2以上版本中是开启SSL模块后是直接支持SNI的,添加NameVirtualHost *:443和SSLStrictSNIVHostCheck off两句后,就可以像http虚拟站点一样设置多个 https 虚拟站点;
但需要注意在sites-enabled下多个配置文件中,要有主次分,就是说默认站点需要一个SSL,其他站点使用另外一个 SSL证书 ;
多个https虚拟站点可以分别指向多个不同的证书文件,其中第一个默认https站点是在后续https站点配置找不到的时候自动使用的默认配置; <VirtualHost _default_:443> <VirtualHost *:443>
参考:博客/https多网站1个IP多个SSL证书的Apache设置办法
访问服务器IP地址,让其返回指定页面
一台服务器配置了多个站点,访问服务器IP地址,跳转到了一个站点,这和配置不当有关;
想要其访问ip返回指定页面,只需要在配置文件中再加一个虚拟站点配置;
<VirtualHost *:80> ServerName 127.0.0.1 ServerAlias xxx.xxx.xxx.xxx DocumentRoot /var/www </VirtualHost>
在/var/www/下写个index.html,内容就是想返回的内容,重启Apache2即可;
原理:Apache解释为当一个请求到达的时候,服务器会首先检查它是否使用了一个能和NameVirtualHost相匹配的IP地址。如果能够匹配,它就会查找每个与这个IP地址相对应的<VirtualHost>配置段,并尝试找出一个ServerName或ServerAlias配置项与请求的主机名相同的。如果找到了,它就会使用这个服务器的配置。否则,将使用符合这个IP地址的第一个列出的虚拟主机。
Apache2配置文件要有条理
之前一通配置,只追求能运行了,没追求配置代码的统一性,添加新站点后感觉一通乱糟糟;
1)/etc/apache2/apache2.conf里面不需要过多配置,因为他都将配置文件分配给了conf-enabled和sites-enabled目录里的*.conf文件:
# Include generic snippets of statements IncludeOptional conf-enabled/*.conf # Include the virtual host configurations: IncludeOptional sites-enabled/*.conf
2)由于sites-enabled文件里的内容是sites-available软连接过去的,这里在sites-available修改好配置文件后,需要进行软连接;
sudo ln -s /etc/apache2/sites-available/xxx.conf /etc/apache2/sites-enabled/xxx.conf
3)一个站点分配一个虚拟站点配置文件、一个SSL证书配置文件,便于管理;
虚拟站点配置:
<VirtualHost *:80>
ServerAdmin webmaster@ localhost
ServerName www.bodkin.ren
DocumentRoot /xxxxxxxxxx
php_admin_value open_basedir "/xxxxxxxxx/:/tmp/"
<Directory /xxxxxxxxxx>
Options FollowSymLinks
AllowOverride All
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
LogLevel warn
CustomLog ${APACHE_LOG_DIR}/access.log combined
<Directory /var/xxxxxxxxxx/wp-content/uploads>
php_admin_flag engine off
<filesmatch ".+\.ph(p[345]?|t|tml)$">
Order deny,allow
Deny from all
</filesmatch>
</Directory>
</VirtualHost>
SSL证书配置:
<IfModule mod_ssl.c> <VirtualHost *:443> ServerName www.bodkin.ren ServerAdmin webmaster@localhost DocumentRoot /xxxxxxxxxxxxxxxxx SSLEngine on SSLCertificateFile /xxxxxxxxxxxx.crt SSLCertificateKeyFile /xxxxxxxxxxxxxxx.key SSLCertificateChainFile /xxxxxxxxxxxxxxxx.crt <FilesMatch "\.(cgi|shtml|phtml|php)$"> SSLOptions +StdEnvVars </FilesMatch> <Directory /usr/lib/cgi-bin> SSLOptions +StdEnvVars </Directory> </VirtualHost> </IfModule>
证书采用的是 腾讯云 的,参考:
4)关于域名和一些服务器状态的配置在网站根目录下.htaccess中配置;
RewriteEngine On RewriteCond %{THE_REQUEST} !^GET\ /.*?static/(css|js|img) RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ index.php [L,QSA] RewriteCond %{HTTPS} !=on RewriteRule ^(.*) {SERVER_NAME}$1 [L,R]
也很简单,最后两条强制性https,用于输入也会跳转到https;
更方便的证书?
使用Let’s Encrypt 来启用HTTPS;
Github:
启用方法傻瓜式;
当三个月快到时,会给你留的邮箱发通告,这时候只需要重新申请一下即可;
./certbot-auto renew