十一週二次課php
11.28限制某個目錄禁止解析PHPcss
11.29限制user_agenthtml
11.30/11.31PHP相關配置mysql
11.28限制某個目錄禁止解析PHPlinux
• 核心配置文件內容web
<Directory /data/wwwroot/www.123.com/upload>sql
php_admin_flag engine offshell
</Directory>數據庫
• curl測試時直接返回了php源代碼,並未解析apache
1.打開虛擬主機主機配置文件
[root@tianqi-01 ~]# vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf
//把代碼放到配置文件中
<VirtualHost *:80>
DocumentRoot "/data/wwwroot/abc.com"
ServerName abc.com
ServerAlias www.abc.com www.123.com
ErrorLog "logs/abc.com-error_log"
CustomLog "logs/abc.com-access_log" common
</VirtualHost>
<VirtualHost *:80>
DocumentRoot "/data/wwwroot/111.com"
ServerName 111.com
ServerAlias www.example.com 2111.com.cn
# <Directory /data/wwwroot/111.com>
#<FilesMatch 123.php>
#AllowOverride AuthConfig
#AuthName "111.com user auth"
#AuthType Basic
#AuthUserFile /data/.htpasswd
#require valid-user
#</FilesMatch>
<Directory /data/wwwroot/111.com/upload> ##把upload目錄下全部的php禁止解析
php_admin_flag engine off //禁止解析php
<FilesMatch (.*)\.php(.*)> //如今這裏全部訪問php都會是403
Order allow,deny //若是不作這個deny,就會直接訪問到源代碼,這樣就不太友好
Deny from all
</FilesMatch>
</Directory>
<Directory /data/wwwroot/111.com>
<FilesMatch "admin.php(.*)">
Order deny,allow
Deny from all
Allow from 127.0.0.1
</FilesMatch>
</Directory>
<Directory /data/wwwroot/111.com>
SetEnvIfNoCase Referer "http://111.com" local_ref
SetEnvIfNoCase Referer "http://ask.apelearn.com" local_ref
SetEnvIfNoCase Referer "^$" local_ref
<FilesMatch "\.(txt|doc|mp3|zip|rar|jpg|gif|png)">
Order Allow,Deny
Allow from env=local_ref
</FilesMatch>
</Directory>
SetEnvIf Request_URI ".*\.gif$" img
SetEnvIf Request_URI ".*\.jpg$" img
SetEnvIf Request_URI ".*\.png$" img
SetEnvIf Request_URI ".*\.bmp$" img
SetEnvIf Request_URI ".*\.swf$" img
SetEnvIf Request_URI ".*\.js$" img
SetEnvIf Request_URI ".*\.css$" img
CustomLog "|/usr/local/apache2.4/bin/rotatelogs -l logs/111.com-access_%Y%m%d.log 86400" combined env=!img
</VirtualHost>
2.檢查配置文件是否存在語法錯誤,並從新加載配置文件
[root@tianqi-01 ~]# /usr/local/apache2.4/bin/apachectl -t
Syntax OK
[root@tianqi-01 ~]# /usr/local/apache2.4/bin/apachectl graceful
[root@tianqi-01 ~]#
3.建立upload目錄,新建php文件
[root@tianqi-01 111.com]# mkdir upload
[root@tianqi-01 111.com]# ls
123.php admin eagle1.jpg index.php upload
[root@tianqi-01 111.com]# cp 123.php upload/
[root@tianqi-01 111.com]#
4.curl訪問
[root@tianqi-01 111.com]# curl -x127.0.0.1:80 '111.com/upload/123.php' -I
HTTP/1.1 403 Forbidden
Date: Mon, 05 Mar 2018 13:28:50 GMT
Server: Apache/2.4.29 (Unix) PHP/7.1.6
Content-Type: text/html; charset=iso-8859-1
[root@tianqi-01 111.com]#
5.這時再去虛擬主機配置文件中註銷掉FilesMatch
[root@tianqi-01 111.com]# vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf
<Directory /data/wwwroot/111.com/upload>
php_admin_flag engine off //把upload目錄下的全部php禁止解析
#<FilesMatch (.*)\.php(.*)>
#Order allow,deny
#Deny from all
#</FilesMatch>
</Directory>
而後保存退出
6.檢查配置文件是否存在語法錯誤,並從新加載配置文件
[root@tianqi-01 111.com]# /usr/local/apache2.4/bin/apachectl -t
Syntax OK
[root@tianqi-01 111.com]# /usr/local/apache2.4/bin/apachectl graceful
[root@tianqi-01 111.com]#
7.這時候訪問php,會發現沒法解析php
[root@tianqi-01 111.com]# curl -x127.0.0.1:80 'http://111.com/upload/123.php'
<?php
echo "123.php";
[root@tianqi-01 111.com]#
8.在瀏覽器中訪問http://111.com/upload/123.php,會提示直接下載,這是由於沒法解析php
9.這時候再打開虛擬主機配置文件,取消FilesMatch註釋
[root@tianqi-01 111.com]# !vim
vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf
<Directory /data/wwwroot/111.com/upload>
php_admin_flag engine off
<FilesMatch (.*)\.php(.*)>
Order allow,deny
Deny from all
</FilesMatch>
</Directory>
10.檢查配置文件是否存在語法錯誤並從新加載配置文件
[root@tianqi-01 111.com]# /usr/local/apache2.4/bin/apachectl -t
Syntax OK
[root@tianqi-01 111.com]# /usr/local/apache2.4/bin/apachectl graceful
[root@tianqi-01 111.com]#
11.這時再去瀏覽器訪問php,會直接顯示Forbidden
12.即便去訪問一個不存在的php文件,也會顯示顯示Forbidden
11.29限制user_agent
• user_agent能夠理解爲瀏覽器標識
• 核心配置文件內容
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_USER_AGENT} .*curl.* [NC,OR]
RewriteCond %{HTTP_USER_AGENT} .*baidu.com.* [NC]
RewriteRule .* - [F]
</IfModule>
• curl -A "123123" 指定user_agent
有時候,網站會受到一種叫 cc 攻擊,CC攻擊就是黑客,經過軟件,肉雞同時去訪問一個站點,超過服務器的併發,就會致使站點宕機;經過肉雞,軟件去訪問站點,就是普通的訪問,沒有什麼特殊的,只是讓站點超過併發致使嚴重超負荷而宕機,因此沒辦法去進行控制;所謂CC攻擊都會有一個規律的特徵,就是user_agent是一致的,好比同一個IP、同一個標識、同一個地址;遇到這種規律的user_agent頻繁訪問的狀況咱們就能夠斷定他就是CC攻擊,咱們就能夠經過限制他的user_agent 減輕服務器壓力,只須要讓他從正常訪問的200,限制爲403,就能減輕服務器的壓力,由於403僅僅是一個請求,只會使用到不多的帶寬,畢竟他沒有牽扯到php 和mysql
cc攻擊
肉雞 (受黑客遠程控制的電腦)
1.打開虛擬主機配置文件
[root@tianqi-01 111.com]# vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf
<VirtualHost *:80>
DocumentRoot "/data/wwwroot/abc.com"
ServerName abc.com
ServerAlias www.abc.com www.123.com
ErrorLog "logs/abc.com-error_log"
CustomLog "logs/abc.com-access_log" common
</VirtualHost>
<VirtualHost *:80>
DocumentRoot "/data/wwwroot/111.com"
ServerName 111.com
ServerAlias www.example.com 2111.com.cn
# <Directory /data/wwwroot/111.com>
#<FilesMatch 123.php>
#AllowOverride AuthConfig
#AuthName "111.com user auth"
#AuthType Basic
#AuthUserFile /data/.htpasswd
#require valid-user
#</FilesMatch>
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_USER_AGENT} .*curl.* [NC,OR]
RewriteCond %{HTTP_USER_AGENT} .*baidu.com.* [NC]
RewriteRule .* - [F]
</IfModule>
2.而後檢查配置文件是否存在語法錯誤,並從新加載配置文件
[root@tianqi-01 111.com]# /usr/local/apache2.4/bin/apachectl -t
Syntax OK
[root@tianqi-01 111.com]# /usr/local/apache2.4/bin/apachectl graceful
[root@tianqi-01 111.com]#
3.去訪問的時候,會顯示403,這是由於限制了user_agent
[root@tianqi-01 111.com]# curl -x127.0.0.1:80 '111.com/upload/123.php' -I
HTTP/1.1 403 Forbidden
Date: Tue, 06 Mar 2018 01:04:51 GMT
Server: Apache/2.4.29 (Unix) PHP/7.1.6
Content-Type: text/html; charset=iso-8859-1
[root@tianqi-01 111.com]# curl -x127.0.0.1:80 '111.com/123.php' -I
HTTP/1.1 403 Forbidden
Date: Tue, 06 Mar 2018 01:05:02 GMT
Server: Apache/2.4.29 (Unix) PHP/7.1.6
Content-Type: text/html; charset=iso-8859-1
[root@tianqi-01 111.com]#
4.查看訪問日誌
[root@tianqi-01 111.com]# tail /usr/local/apache2.4/logs/111.com-access_20180306.log
127.0.0.1 - - [06/Mar/2018:08:40:44 +0800] "HEAD HTTP://111.com/upload/123.php HTTP/1.1" 403 - "-" "curl/7.29.0"
127.0.0.1 - - [06/Mar/2018:08:42:52 +0800] "HEAD HTTP://111.com/upload/123.php HTTP/1.1" 403 - "-" "curl/7.29.0"
127.0.0.1 - - [06/Mar/2018:08:43:10 +0800] "HEAD HTTP://111.com/upload/123.php HTTP/1.1" 403 - "-" "tianqi tianqi"
127.0.0.1 - - [06/Mar/2018:08:59:56 +0800] "HEAD HTTP://111.com/upload/123.php HTTP/1.1" 403 - "-" "tianqi tianqi"
127.0.0.1 - - [06/Mar/2018:09:00:12 +0800] "GET HTTP://111.com/upload/123.php HTTP/1.1" 403 223 "-" "tianqi tianqi"
127.0.0.1 - - [06/Mar/2018:09:00:33 +0800] "HEAD HTTP://111.com/123.php HTTP/1.1" 200 - "-" "tianqi tianqi"
127.0.0.1 - - [06/Mar/2018:09:00:39 +0800] "GET HTTP://111.com/123.php HTTP/1.1" 200 7 "-" "tianqi tianqi"
127.0.0.1 - - [06/Mar/2018:09:04:51 +0800] "HEAD HTTP://111.com/upload/123.php HTTP/1.1" 403 - "-" "curl/7.29.0"
127.0.0.1 - - [06/Mar/2018:09:05:02 +0800] "HEAD HTTP://111.com/123.php HTTP/1.1" 403 - "-" "curl/7.29.0"
[root@tianqi-01 111.com]#
5.測試是不是由於user_agent纔會被信任的
6.首先自定義user_agent
//模擬user_agent,去訪問會看到狀態嗎爲200,能夠正常訪問
[root@tianqi-01 111.com]# curl -A "tianqi tianqi" -x127.0.0.1:80 '111.com/123.php' -I
HTTP/1.1 200 OK
Date: Tue, 06 Mar 2018 01:09:02 GMT
Server: Apache/2.4.29 (Unix) PHP/7.1.6
X-Powered-By: PHP/7.1.6
Content-Type: text/html; charset=UTF-8
[root@tianqi-01 111.com]# curl -A "tianqi tianqi" -x127.0.0.1:80 '111.com/123.php'
123.php[root@tianqi-01 111.com]#
7.查看訪問日誌,會看到user_agent是tianqi tianqi
123.php[root@tianqi-01 111.com]# !tail
tail /usr/local/apache2.4/logs/111.com-access_20180306.log
127.0.0.1 - - [06/Mar/2018:08:42:52 +0800] "HEAD HTTP://111.com/upload/123.php HTTP/1.1" 403 - "-" "curl/7.29.0"
127.0.0.1 - - [06/Mar/2018:08:43:10 +0800] "HEAD HTTP://111.com/upload/123.php HTTP/1.1" 403 - "-" "tianqi tianqi"
127.0.0.1 - - [06/Mar/2018:08:59:56 +0800] "HEAD HTTP://111.com/upload/123.php HTTP/1.1" 403 - "-" "tianqi tianqi"
127.0.0.1 - - [06/Mar/2018:09:00:12 +0800] "GET HTTP://111.com/upload/123.php HTTP/1.1" 403 223 "-" "tianqi tianqi"
127.0.0.1 - - [06/Mar/2018:09:00:33 +0800] "HEAD HTTP://111.com/123.php HTTP/1.1" 200 - "-" "tianqi tianqi"
127.0.0.1 - - [06/Mar/2018:09:00:39 +0800] "GET HTTP://111.com/123.php HTTP/1.1" 200 7 "-" "tianqi tianqi"
127.0.0.1 - - [06/Mar/2018:09:04:51 +0800] "HEAD HTTP://111.com/upload/123.php HTTP/1.1" 403 - "-" "curl/7.29.0"
127.0.0.1 - - [06/Mar/2018:09:05:02 +0800] "HEAD HTTP://111.com/123.php HTTP/1.1" 403 - "-" "curl/7.29.0"
127.0.0.1 - - [06/Mar/2018:09:09:02 +0800] "HEAD HTTP://111.com/123.php HTTP/1.1" 200 - "-" "tianqi tianqi"
127.0.0.1 - - [06/Mar/2018:09:09:13 +0800] "GET HTTP://111.com/123.php HTTP/1.1" 200 7 "-" "tianqi tianqi"
[root@tianqi-01 111.com]#
11.30/11.31PHP相關配置
[root@tianqi-01 111.com]# ls
123.php admin eagle1.jpg index.php upload
[root@tianqi-01 111.com]# vim index.php
<?php
phpinfo();
在瀏覽器中打開111.com/index.php
//配置文件並無加載,下載去複製一個
[root@tianqi-01 111.com]# cd /usr/local/src/php-7.1.6
[root@tianqi-01 php-7.1.6]# cp php.ini-development /usr/local/php7/etc/php.ini
[root@tianqi-01 php-7.1.6]# /usr/local/apache2.4/bin/apachectl graceful
[root@tianqi-01 php-7.1.6]#
此時再去瀏覽器中打開111.com/index.php,會看到配置文件已經有了,因此phpinfo是最準的。
1.打開php文件,禁掉一些危險的函數(默認裏面是空的)
[root@tianqi-01 php-7.1.6]# vim /usr/local/php7/etc/php.ini
//搜索disable_functions,在disable_functions=後面禁掉一些危險的函數
disable_functions=eval,assert,popen,passthru,escapeshellarg,escapeshellcmd,passthru,exec,system,chroot,scandir,chgrp,chown,escapeshellcmd,escapeshellarg,shell_exec,proc_get_status,ini_alter,ini_restore,dl,pfsockopen,openlog,syslog,readlink,symlink,leak,popepassthru,stream_socket_server,popen,proc_open,proc_close,phpinfo
2.查看是否存在語法錯誤,並從新加載配置文件
[root@tianqi-01 php-7.1.6]# /usr/local/apache2.4/bin/apachectl -t
Syntax OK
[root@tianqi-01 php-7.1.6]# /usr/local/apache2.4/bin/apachectl graceful
[root@tianqi-01 php-7.1.6]#
3.再去瀏覽器訪問111.com/123.php,會看到phpinfo被禁掉了
4.還須要使用phpinfo,因此在此編輯配置文件,在disable_functions後面去掉phpinfo,檢查語法錯誤並從新加載配置文件
[root@tianqi-01 php-7.1.6]# vim /usr/local/php7/etc/php.ini
disable_functions=eval,assert,popen,passthru,escapeshellarg,escapeshellcmd,passthru,exec,system,chroot,scandir,chgrp,chown,escapeshellcmd,escapeshellarg,shell_exec,proc_get_status,ini_alter,ini_restore,dl,pfsockopen,openlog,syslog,readlink,symlink,leak,popepassthru,stream_socket_server,popen,proc_open,proc_close
[root@tianqi-01 php-7.1.6]# /usr/local/apache2.4/bin/apachectl -t
Syntax OK
[root@tianqi-01 php-7.1.6]# /usr/local/apache2.4/bin/apachectl graceful
[root@tianqi-01 php-7.1.6]#
5.編輯php配置文件
[root@tianqi-01 php-7.1.6]# vim /usr/local/php7/etc/php.ini
//搜索timezone
在date.timezone定義時區
//須要刪除分號;
date.timezone = Asia/Shanghai
//搜索display
//須要刪除分號;
將display_errors = On改成display_errors = Off
將disable_functions=後面加上phpinfo,繼續作實驗
6.檢查配置文件是否存在語法錯誤,並從新加載配置文件
[root@tianqi-01 php-7.1.6]# /usr/local/apache2.4/bin/apachectl -t
Syntax OK
[root@tianqi-01 php-7.1.6]# /usr/local/apache2.4/bin/apachectl graceful
[root@tianqi-01 php-7.1.6]#
7.這時候再去用瀏覽器訪問111.com/index.php,會看到的是白頁,這就是由於display_errors=Off
8.用curl試一下
[root@tianqi-01 php-7.1.6]# curl -x127.0.0.1:80 http://111.com/index.php
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>403 Forbidden</title>
</head><body>
<h1>Forbidden</h1>
<p>You don't have permission to access /index.php
on this server.<br />
</p>
</body></html>
[root@tianqi-01 php-7.1.6]#
如今看到的狀態碼仍是403,由於此時限制了user_agent。
[root@tianqi-01 php-7.1.6]# curl -A "a" -x127.0.0.1:80 http://111.com/index.php
[root@tianqi-01 php-7.1.6]# curl -A "a" -x127.0.0.1:80 http://111.com/index.php -I
HTTP/1.1 200 OK
Date: Tue, 06 Mar 2018 02:33:32 GMT
Server: Apache/2.4.29 (Unix) PHP/7.1.6
X-Powered-By: PHP/7.1.6
Content-Type: text/html; charset=UTF-8
[root@tianqi-01 php-7.1.6]#
沒有任何的輸出,就是白頁這就有問題了,由於不顯示任何內容,一切都是未知的。
9.打開配置文件
[root@tianqi-01 php-7.1.6]# !vim
vim /usr/local/php7/etc/php.ini
搜索log_errors,log_errors = On,將錯誤日誌文件打開
搜索/error_log,將錯誤日誌文件定義到tmp目錄下
將;error_log = php_errors.log改成error_log=/tmp/php_errors.log,去掉分號;。
搜索error_reporting,定義日誌級別
; error_reporting
; Default Value: E_ALL & ~E_NOTICE & ~E_STRICT & ~E_DEPRECATED
; Development Value: E_ALL
; Production Value: E_ALL & ~E_DEPRECATED & ~E_STRICT
按n鍵查找下一個
error_reporting = E_ALL
保存退出
10.檢查配置配置文件是否存在語法錯誤,並從新加載配置配置文件
[root@tianqi-01 php-7.1.6]# /usr/local/apache2.4/bin/apachectl -t
Syntax OK
[root@tianqi-01 php-7.1.6]# /usr/local/apache2.4/bin/apachectl graceful
[root@tianqi-01 php-7.1.6]#
11.這時再去訪問一遍,會看到tmp目錄下生成了php_errors.log
[root@tianqi-01 php-7.1.6]# curl -A "a" -x127.0.0.1:80 http://111.com/index.php
[root@tianqi-01 php-7.1.6]# ls /tmp/
pear
php_errors.log
systemd-private-10939be3906d4885a694c9ccaa969513-vgauthd.service-up8a1e
systemd-private-10939be3906d4885a694c9ccaa969513-vmtoolsd.service-qrMrpr
systemd-private-42380971d6c34175aa4f900510bf7f60-vgauthd.service-Wj57Dr
systemd-private-42380971d6c34175aa4f900510bf7f60-vmtoolsd.service-EC3gse
systemd-private-491ebf486ca940d885ed9348c17675f5-vgauthd.service-3sBthy
systemd-private-491ebf486ca940d885ed9348c17675f5-vmtoolsd.service-sJ80kS
systemd-private-70996ce584314349997855a51a9da3d4-vgauthd.service-fehOdP
systemd-private-70996ce584314349997855a51a9da3d4-vmtoolsd.service-WZC9Q4
systemd-private-74b18b5fecc548f1ac450ce1fea7233b-vgauthd.service-DBvBHm
systemd-private-74b18b5fecc548f1ac450ce1fea7233b-vmtoolsd.service-AjXoy6
systemd-private-8531739ab5bb4905a63c0a8665d16b3b-vgauthd.service-vh8gyS
systemd-private-8531739ab5bb4905a63c0a8665d16b3b-vmtoolsd.service-koi7xB
systemd-private-9644814bb86e455d8e911deb7225488b-vgauthd.service-KLJ6Yc
systemd-private-9644814bb86e455d8e911deb7225488b-vmtoolsd.service-g2bpAg
systemd-private-aa279ed589d542bcbc4f2903687ce469-vgauthd.service-xDYbZY
systemd-private-aa279ed589d542bcbc4f2903687ce469-vmtoolsd.service-tTo26l
[root@tianqi-01 php-7.1.6]#
12.查看php_errors.log,會看到屬主屬組是daemon
[root@tianqi-01 php-7.1.6]# ll /tmp/php_errors.log
-rw-r--r-- 1 daemon daemon 145 Mar 6 11:07 /tmp/php_errors.log
[root@tianqi-01 php-7.1.6]#
13.deamon其實是httpd的屬主,php_errors.log日誌文件是以httpd這個進程的身份去生成的
[root@tianqi-01 php-7.1.6]# ps aux |grep httpd
root 1113 0.0 1.3 258888 13628 ? Ss 08:39 0:00 /usr/local/apache2.4/bin/httpd -k graceful
daemon 1886 0.0 1.2 545716 12428 ? Sl 11:07 0:00 /usr/local/apache2.4/bin/httpd -k graceful
daemon 1887 0.0 1.0 545716 10404 ? Sl 11:07 0:00 /usr/local/apache2.4/bin/httpd -k graceful
daemon 1888 0.0 1.4 613364 14868 ? Sl 11:07 0:00 /usr/local/apache2.4/bin/httpd -k graceful
root 1977 0.0 0.0 112660 984 pts/0 S+ 11:11 0:00 grep --color=auto httpd
[root@tianqi-01 php-7.1.6]#
[root@tianqi-01 111.com]# grep error_log /usr/local/php7/etc/php.ini
; server-specific log, STDERR, or a location specified by the error_log
; Set maximum length of log_errors. In error_log information about the source is
error_log = /tmp/php_errors.log
;error_log = syslog
; OPcache error_log file name. Empty string assumes "stderr".
;opcache.error_log=
[root@tianqi-01 111.com]#
14.查看文件日誌
[root@tianqi-01 111.com]# cat /tmp/php_errors.log
[06-Mar-2018 11:33:18 Asia/Shanghai] PHP Warning: phpinfo() has been disabled for security reasons in /data/wwwroot/111.com/index.php on line 2
[root@tianqi-01 111.com]#
15.下面模擬一個錯誤
[root@tianqi-01 111.com]# vim /data/wwwroot/111.com/2.php
<?php
echo 123;
dgebszfhasdf
16.訪問2.php,會出現一些錯誤
[root@tianqi-01 111.com]# curl -A "a" -x127.0.0.1:80 http://111.com/2.php
[root@tianqi-01 111.com]# curl -A "a" -x127.0.0.1:80 http://111.com/2.php -I
HTTP/1.0 500 Internal Server Error
Date: Tue, 06 Mar 2018 06:08:02 GMT
Server: Apache/2.4.29 (Unix) PHP/7.1.6
X-Powered-By: PHP/7.1.6
Connection: close
Content-Type: text/html; charset=UTF-8
[root@tianqi-01 111.com]#
一樣是一個白頁,只不過這個白頁的狀態碼是500
17.看一下錯誤日誌
[root@tianqi-01 111.com]# cat /tmp/php_errors.log
[06-Mar-2018 11:33:18 Asia/Shanghai] PHP Warning: phpinfo() has been disabled for security reasons in /data/wwwroot/111.com/index.php on line 2
[06-Mar-2018 13:56:54 Asia/Shanghai] PHP Parse error: syntax error, unexpected end of file in /data/wwwroot/111.com/2.php on line 4
[06-Mar-2018 14:08:02 Asia/Shanghai] PHP Parse error: syntax error, unexpected end of file in /data/wwwroot/111.com/2.php on line 4
[root@tianqi-01 111.com]#
這裏是syntax error,比warning的級別更高
1.打開php配置文件
[root@tianqi-01 php-7.1.6]# vim /usr/local/php7/etc/php.ini
//搜索open_basedir,並刪除分號;
open_basedir=/data/wwwroot/1111.com:/tmp //若是限制錯了,限制成1111.com:
而後保存退出
2.查看配置文件是否存在語法錯誤,並從新加載配置文件
[root@tianqi-01 php-7.1.6]# /usr/local/apache2.4/bin/apachectl -t
Syntax OK
[root@tianqi-01 php-7.1.6]# /usr/local/apache2.4/bin/apachectl graceful
[root@tianqi-01 php-7.1.6]#
3.用curl訪問2.php
[root@tianqi-01 php-7.1.6]# curl -A "a" -x127.0.0.1:80 http://111.com/2.php -I
HTTP/1.0 500 Internal Server Error
Date: Wed, 07 Mar 2018 11:05:42 GMT
Server: Apache/2.4.29 (Unix) PHP/7.1.6
X-Powered-By: PHP/7.1.6
Connection: close
Content-Type: text/html; charset=UTF-8
[root@tianqi-01 php-7.1.6]#
4.如今改正2.php,去掉第3行的亂碼
[root@tianqi-01 php-7.1.6]# vim /data/wwwroot/111.com/2.php
<?php
echo 123;
保存退出
5.再次用curl訪問2.php,狀態碼仍是500
[root@tianqi-01 php-7.1.6]# curl -A "a" -x127.0.0.1:80 http://111.com/2.php -I
HTTP/1.0 500 Internal Server Error
Date: Wed, 07 Mar 2018 11:08:06 GMT
Server: Apache/2.4.29 (Unix) PHP/7.1.6
X-Powered-By: PHP/7.1.6
Connection: close
Content-Type: text/html; charset=UTF-8
[root@tianqi-01 php-7.1.6]#
6.查看錯誤輸出
[root@tianqi-01 php-7.1.6]# cat /tmp/php_errors.log
[06-Mar-2018 11:33:18 Asia/Shanghai] PHP Warning: phpinfo() has been disabled for security reasons in /data/wwwroot/111.com/index.php on line 2
[06-Mar-2018 13:56:54 Asia/Shanghai] PHP Parse error: syntax error, unexpected end of file in /data/wwwroot/111.com/2.php on line 4
[06-Mar-2018 14:08:02 Asia/Shanghai] PHP Parse error: syntax error, unexpected end of file in /data/wwwroot/111.com/2.php on line 4
[07-Mar-2018 19:03:19 Asia/Shanghai] PHP Warning: Unknown: open_basedir restriction in effect. File(/data/wwwroot/111.com/index.php) is not within the allowed path(s): (/data/wwwroot/1111.com:/tmp) in Unknown on line 0
[07-Mar-2018 19:03:19 Asia/Shanghai] PHP Warning: Unknown: failed to open stream: Operation not permitted in Unknown on line 0
[07-Mar-2018 19:03:19 Asia/Shanghai] PHP Fatal error: Unknown: Failed opening required '/data/wwwroot/111.com/index.php' (include_path='.:/usr/local/php7/lib/php') in Unknown on line 0
[07-Mar-2018 19:04:20 Asia/Shanghai] PHP Warning: Unknown: open_basedir restriction in effect. File(/data/wwwroot/111.com/index.php) is not within the allowed path(s): (/data/wwwroot/1111.com:/tmp) in Unknown on line 0
[07-Mar-2018 19:04:20 Asia/Shanghai] PHP Warning: Unknown: failed to open stream: Operation not permitted in Unknown on line 0
[07-Mar-2018 19:04:20 Asia/Shanghai] PHP Fatal error: Unknown: Failed opening required '/data/wwwroot/111.com/index.php' (include_path='.:/usr/local/php7/lib/php') in Unknown on line 0
[07-Mar-2018 19:04:24 Asia/Shanghai] PHP Warning: Unknown: open_basedir restriction in effect. File(/data/wwwroot/111.com/index.php) is not within the allowed path(s): (/data/wwwroot/1111.com:/tmp) in Unknown on line 0
[07-Mar-2018 19:04:24 Asia/Shanghai] PHP Warning: Unknown: failed to open stream: Operation not permitted in Unknown on line 0
[07-Mar-2018 19:04:24 Asia/Shanghai] PHP Fatal error: Unknown: Failed opening required '/data/wwwroot/111.com/index.php' (include_path='.:/usr/local/php7/lib/php') in Unknown on line 0
[07-Mar-2018 19:04:42 Asia/Shanghai] PHP Warning: Unknown: open_basedir restriction in effect. File(/data/wwwroot/111.com/2.php) is not within the allowed path(s): (/data/wwwroot/1111.com:/tmp) in Unknown on line 0
[07-Mar-2018 19:04:42 Asia/Shanghai] PHP Warning: Unknown: failed to open stream: Operation not permitted in Unknown on line 0
[07-Mar-2018 19:04:42 Asia/Shanghai] PHP Fatal error: Unknown: Failed opening required '/data/wwwroot/111.com/2.php' (include_path='.:/usr/local/php7/lib/php') in Unknown on line 0
[07-Mar-2018 19:05:42 Asia/Shanghai] PHP Warning: Unknown: open_basedir restriction in effect. File(/data/wwwroot/111.com/2.php) is not within the allowed path(s): (/data/wwwroot/1111.com:/tmp) in Unknown on line 0
[07-Mar-2018 19:05:42 Asia/Shanghai] PHP Warning: Unknown: failed to open stream: Operation not permitted in Unknown on line 0
[07-Mar-2018 19:05:42 Asia/Shanghai] PHP Fatal error: Unknown: Failed opening required '/data/wwwroot/111.com/2.php' (include_path='.:/usr/local/php7/lib/php') in Unknown on line 0
[07-Mar-2018 19:08:06 Asia/Shanghai] PHP Warning: Unknown: open_basedir restriction in effect. File(/data/wwwroot/111.com/2.php) is not within the allowed path(s): (/data/wwwroot/1111.com:/tmp) in Unknown on line 0
[07-Mar-2018 19:08:06 Asia/Shanghai] PHP Warning: Unknown: failed to open stream: Operation not permitted in Unknown on line 0
[07-Mar-2018 19:08:06 Asia/Shanghai] PHP Fatal error: Unknown: Failed opening required '/data/wwwroot/111.com/2.php' (include_path='.:/usr/local/php7/lib/php') in Unknown on line 0
[root@tianqi-01 php-7.1.6]#
7.從新編輯配置文件,將1111.com改成111.com
[root@tianqi-01 php-7.1.6]# vim /usr/local/php7/etc/php.ini
open_basedir = /data/wwwroot/1111.com:/tmp改成
open_basedir = /data/wwwroot/111.com:/tmp
保存並退出
8.查看配置文件是否存在語法錯誤,並從新加載配置文件
[root@tianqi-01 php-7.1.6]# /usr/local/apache2.4/bin/apachectl -t
Syntax OK
[root@tianqi-01 php-7.1.6]# /usr/local/apache2.4/bin/apachectl graceful
[root@tianqi-01 php-7.1.6]#
9.再次用curl訪問2.php,狀態碼會顯示200
[root@tianqi-01 php-7.1.6]# curl -A "a" -x127.0.0.1:80 http://111.com/2.php -I
HTTP/1.1 200 OK
Date: Wed, 07 Mar 2018 11:15:15 GMT
Server: Apache/2.4.29 (Unix) PHP/7.1.6
X-Powered-By: PHP/7.1.6
Content-Type: text/html; charset=UTF-8
[root@tianqi-01 php-7.1.6]#
10.如果服務器上跑了n多個站點,該如何作限制呢?
11.應該針對站點、這些網站作open_basedir,可是php.ini是作不到的,由於php.ini是針對全部站點的
12.取消第一步中對open_basedir的設置
[root@tianqi-01 php-7.1.6]# vim /usr/local/php7/etc/php.ini
將open_basedir = /data/wwwroot/111.com:/tmp改成
open_basedir =
而後保存退出
13.但咱們能夠在虛擬主機配置文件中設置,在apache虛擬主機文件中去設置
[root@tianqi-01 php-7.1.6]# vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf
<VirtualHost *:80>
DocumentRoot "/data/wwwroot/abc.com"
ServerName abc.com
ServerAlias www.abc.com www.123.com
php_admin_value open_basedir "/data/wwwroot/abc.com:/tmp" //這裏限定在abc.com目錄下
ErrorLog "logs/abc.com-error_log"
CustomLog "logs/abc.com-access_log" common
</VirtualHost>
<VirtualHost *:80>
DocumentRoot "/data/wwwroot/111.com"
ServerName 111.com
ServerAlias www.example.com 2111.com.cn
# <Directory /data/wwwroot/111.com>
#<FilesMatch 123.php>
#AllowOverride AuthConfig
#AuthName "111.com user auth"
#AuthType Basic
#AuthUserFile /data/.htpasswd
#require valid-user
#</FilesMatch>
php_admin_value open_basedir "/data/wwwroot/111.com:/tmp" //這裏限定在111.com目錄下
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_USER_AGENT} .*curl.* [NC,OR]
RewriteCond %{HTTP_USER_AGENT} .*baidu.com.* [NC]
RewriteRule .* - [F]
</IfModule>
14.查看配置文件是否存在語法錯誤,並從新加載配置文件
[root@tianqi-01 php-7.1.6]# /usr/local/apache2.4/bin/apachectl -t
Syntax OK
[root@tianqi-01 php-7.1.6]# /usr/local/apache2.4/bin/apachectl graceful
[root@tianqi-01 php-7.1.6]#
15.用curl繼續訪問2.php
[root@tianqi-01 php-7.1.6]# curl -A "a" -x127.0.0.1:80 http://111.com/2.php
123[root@tianqi-01 php-7.1.6]#
//這裏就可使用不一樣的虛擬主機去限制不一樣的open_basedir
友情連接:阿銘Linux