PHP中的配置相當重要,包含php.ini的配置,還有系統權限的配置,一下是我總結的一些配置php
./configure \ --with-libdir=lib64 \ --prefix=/usr/ \ --exec-prefix=/usr \ --bindir=/usr/bin \ --sbindir=/usr/sbin \ --sysconfdir=/etc \ --datadir=/usr/share \ --includedir=/usr/include \ --libexecdir=/usr/libexec \ --localstatedir=/var \ --sharedstatedir=/usr/com \ --mandir=/usr/share/man \ --infodir=/usr/share/info \ --cache-file=../config.cache \ --with-config-file-path=/etc \ --with-config-file-scan-dir=/etc/php.d \ --with-mysql=/usr/local/mysql \ --with-mysqli=/usr/local/mysql/bin/mysql_config \ --with-iconv-dir \ --with-freetype-dir=/usr/local/lib \ --with-jpeg-dir=/usr/local/lib \ --with-png-dir=/usr/local/lib \ --with-zlib \ --with-libxml-dir=/usr \ --enable-xml \ --disable-rpath \ --enable-bcmath \ --enable-shmop \ --enable-sysvsem \ --enable-inline-optimization \ --with-curl \ --enable-mbregex \ --enable-fpm \ --enable-mbstring \ --with-mcrypt=/usr/local/lib \ --with-gd \ --enable-gd-native-ttf \ --with-openssl \ --with-mhash \ --enable-pcntl \ --enable-sockets \ --with-xmlrpc \ --enable-zip \ --enable-soap \ --enable-opcache \ --with-pdo-mysql \ --enable-embed=shared \ --enable-debug \ --enable-dtrace
上面是一些比較經常使用的配置選項html
[root@localhost]# php -m [PHP Modules] bcmath Core ctype curl date dom ereg fetch_url fileinfo filter gd hash iconv json libxml mbstring mcrypt memcached mhash mongo mysql mysqli mysqlnd openssl pcntl pcre PDO pdo_mysql pdo_sqlite Phar posix Reflection session shmop SimpleXML soap sockets SPL sqlite3 standard sysvsem tokenizer trace vld xhprof xml xmlreader xmlrpc xmlwriter Zend OPcache zip zlib [Zend Modules] Zend OPcache
咱們能夠經過php -m 來查看,mysql
一、有些不須要的模塊,在咱們編譯的時候就不要啓用了nginx
二、有些默認啓用的模塊,在咱們編譯的時候要禁用web
能夠經過expose_php
來關閉sql
[root@localhost ~]# curl -I 192.168.1.30 HTTP/1.1 200 OK Server: nginx Date: Wed, 15 Jul 2015 19:16:10 GMT Content-Type: text/html; charset=utf-8 Connection: keep-alive Vary: Accept-Encoding X-Powered-By: PHP/5.5.23 [root@localhost ~]# vim /etc/php.ini expose_php = Off
服務器的版本信息也更改,在編譯nginx以前能夠將nginx修改成apapche
shell
display_errors = Off display_startup_errors = On log_errors = On error_reporting = 0 error_log = /var/log/php_errors.log
另外還要記錄apache或者Nginx的訪問日誌,這個要在web server中配置
apache
web程序最不安全的就在這個地方了,用戶能夠上傳文件,好比圖片、腳本,而後再經過其餘方式,對網站作一些破壞性的工做,因此上傳的時候,要嚴格檢查文件的格式json
file_uploads = On upload_tmp_dir =/data/www/tmp upload_max_filesize = 2M max_file_uploads = 20
若是這個特性被啓動,將會禁用file_get_contents()、include、require中獲取諸如FTP或網頁內容這些遠程數據vim
allow_url_fopen=Off
post_max_size=2m
最大執行時間、用於處理請求數據的最大時間、以及最大可用內存數、防止hash構造
max_execution_time = 30
max_input_time = 30
memory_limit = 40M
max_input_vars = 1000
[root@localhost ~]# vim /etc/php.ini disable_functions =exec,passthru,shell_exec,system,proc_open,popen,curl_exec,curl_multi_exec,parse_ini_file,show_source open_basedir=/var/www/html/ safe_mode_exec_dir = /usr/local/bin/ #確認以Apache或www這種非root用戶運行Apache。/var/www/html目錄下的owner也應是非root用戶: [root@localhost ~]# chown -R apache:apache /var/www/html/ #DocumentRoot下的文件應禁止運行或建立。設置該目錄下的文件權限爲0444(只讀): [root@localhost ~]# chmod -R 0444 /var/www/html/ #設置該目錄下的全部文件夾權限爲0445 [root@localhost ~]# find /var/www/html/ -type d -print0 | xargs -0 -I {} chmod 0445 {} #配置文件加上寫保護 [root@localhost ~]# chattr +i /etc/php.ini /etc/php.d/* /etc/my.ini /etc/httpd/conf/httpd.conf
攻擊者會使用wget之類的工具從你的Web服務器下載文件。使用iptables來阻擋Apache用戶的傳出鏈接。ipt_owner模塊會爲本地數據包的生成者分配不一樣角色。它只對OUTPUT chain有效。下面指令容許vivek用戶經過80端口進行外部訪問
/sbin/iptables -A OUTPUT -o eth0 -m owner --uid-owner vivek -p tcp --dport 80 -m state --state NEW,ESTABLISHED -j ACCEPT /sbin/iptables --new-chain apache_user /sbin/iptables --append OUTPUT -m state --state ESTABLISHED,RELATED -j ACCEPT /sbin/iptables --append OUTPUT -m owner --uid-owner apache -j apache_user # allow apache user to connec to our smtp server /sbin/iptables --append apache_user -p tcp --syn -d 192.168.1.100 --dport 25 -j RETURN # Allow apache user to connec to api server for spam validation /sbin/iptables --append apache_user -p tcp --syn -d 66.135.58.62 --dport 80 -j RETURN /sbin/iptables --append apache_user -p tcp --syn -d 66.135.58.61 --dport 80 -j RETURN /sbin/iptables --append apache_user -p tcp --syn -d 72.233.69.89 --dport 80 -j RETURN /sbin/iptables --append apache_user -p tcp --syn -d 72.233.69.88 --dport 80 -j RETURN ######################### ## Add more rules here ## ######################### # No editing below # Drop everything for apache outgoing connection /sbin/iptables --append apache_user -j REJECT
apache/nginx log
[root@localhost ~]# tail -f /var/log/httpd/error_log [root@localhost ~]# grep 'login.php' /var/log/httpd/error_log [root@localhost ~]# egrep -i "denied|error|warn" /var/log/httpd/error_log
php log
[root@localhost ~]# tail -f /var/log/httpd/php_scripts_error.log [root@localhost ~]# grep "...etc/passwd" /var/log/httpd/php_scripts_error.log
php backdoors
[root@localhost ~]# grep -iR 'c99' /var/www/html/ [root@localhost ~]# grep -iR 'r57' /var/www/html/ [root@localhost ~]# find /var/www/html/ -name \*.php -type f -print0 | xargs -0 grep c99 [root@localhost ~]# grep -RPn "(passthru|shell_exec|system|base64_decode|fopen|fclose|eval)" /var/www/html/
ModSecurity是一個開源的入侵檢測和防範的Web應用引擎。安裝mod_security能夠保護Apache和PHP應用免受XSS和其餘攻擊
能夠限制在必定時間內的訪問頻率
英文原文
http://www.cyberciti.biz/tips/php-security-best-practices-tutorial.html