安装CentOS7 虚拟机安裝 CentOS7 选择虚拟机 Virtual Machine 最小安装CentOS7 以下是用 Hyper-V + CentOS-7-x86_64-Minimal-2003.iso
一個root密碼,一個管理員用戶(用于VSCode|PHPStorm 使用ssh|sftp直接编辑项目), (安装gitea加了个git用户,没有sudo权限)
其它全部 auto
修改主机名 1 2 3 $ hostnamectl set-hostname "php72.vm" $ cat /etc/hostname php72.vm
配置网络 查看网卡
修改网络配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 $ cd /etc/sysconfig/network-scripts/ $ ll total 232 -rw-r--r--. 1 root root 348 Feb 5 03:49 ifcfg-eth0 -rw-r--r--. 1 root root 254 Aug 19 2019 ifcfg-lo $ sudo vim /etc/sysconfig/network-scripts/ifcfg-eth0 TYPE=Ethernet PROXY_METHOD=none BROWSER_ONLY=no BOOTPROTO=static DEFROUTE=yes IPV4_FAILURE_FATAL=no IPV6INIT=yes IPV6_AUTOCONF=yes IPV6_DEFROUTE=yes IPV6_FAILURE_FATAL=no IPV6_ADDR_GEN_MODE=stable-privacy NAME=eth0 UUID=0296b603-2823-40a5-9f39-7743e87b527a DEVICE=eth0 ONBOOT=yes IPADDR=192.168.3.105 PREFIX=24 GATEWAY=192.168.3.1 DNS1=192.168.3.1 $ ifup eth0
换Xshell连ssh截图和复制方便
修改时区 1 2 3 4 5 6 7 8 9 $ sudo rm -rf /etc/localtime $ sudo ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime $ sudo vim /etc/sysconfig/clock > ZONE="Asia/Shanghai" > UTC=false > ARC=false
安装 ntp 校验时间 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 $ sudo yum install ntp ntpdate $ sudo ntpdate cn.pool.ntp.org $ sudo hwclock --systohc $ date $ hwclock $ date -R $ cat /etc/ntp.conf $ sudo systemctl enable ntpd $ sudo systemctl start ntpd $ sudo vim /etc/rc.d/rc.local > /usr/sbin/ntpdate cn.pool.ntp.org > /dev/null 2>&1; /sbin/hwclock -w $ sudo crontab -e > 0 */1 * * * ntpdate cn.pool.ntp.org > /dev/null 2>&1; /sbin/hwclock -w $ sudo crontab -l > 0 */1 * * * ntpdate cn.pool.ntp.org > /dev/null 2>&1; /sbin/hwclock -w $ date -R > Sat, 06 Feb 2021 12:44:09 +0800
安装 strace
在Linux系统中,strace命令是一个集诊断、调试、统计与一体的工具,可用来追踪调试程序,能够与其他命令搭配使用
1 2 $ sudo yum install strace
查看selinux状态
1 2 3 4 5 6 7 8 9 10 $ sestatus SELinux status: enabled (开启) SELinuxfs mount: /sys/fs/selinux SELinux root directory: /etc/selinux Loaded policy name: targeted Current mode: enforcing (强制执行) Mode from config file: enforcing Policy MLS status: enabled Policy deny_unknown status: allowed Max kernel policy version: 31
查看firewalld状态
1 2 3 4 5 6 7 8 9 10 $ systemctl status firewalld firewalld.service - firewalld - dynamic firewall daemon Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled; vendor preset: enabled) Active: active (running)(在运行) since Fri 2021-02-05 03:31:43 EST; 57min ago Docs: man:firewalld(1) Main PID: 719 (firewalld) CGroup: /system.slice/firewalld.service └─719 /usr/bin/python2 -Es /usr/sbin/firewalld --nofork --nopid
查看firewalld开放端口(–list-ports)和 服务(–list-services)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 $ sudo firewall-cmd --list-all public (active) target: default icmp-block-inversion: no interfaces: eth0 sources: services: dhcpv6-client ssh ports: protocols: masquerade: no forward-ports: source-ports: icmp-blocks: rich rules:
更新系统
安装Apache(Httpd)
开机启动httpd
1 sudo systemctl enable httpd
现在启动httpd
1 2 sudo systemctl start httpd
查看netstat命令属于哪个包
1 yum whatprovides netstat
安装net-tools
1 sudo yum install net-tools -y
查看端口监听 (80/tcp 被 httpd 监听,说明httpd已经启动) netstat 参数 -t (tcp) 仅显示tcp相关选项 -u (udp)仅显示udp相关选项 -n 拒绝显示别名,能显示数字的全部转化为数字 -l 仅列出在Listen(监听)的服务状态 -p 显示建立相关链接的程序名
1 2 $ sudo netstat -tnlp | grep 80 tcp6 0 0 :::80 :::* LISTEN 21835/httpd
查看firewalld是否支持http服务
1 2 3 4 5 $ sudo firewall-cmd --get-services | sed 's/ /\n/g' | grep http http https wbem-http wbem-https
firewalld开放http服务(80端口),也可以用–add-port=80/tcp
1 2 3 $ sudo firewall-cmd --zone=public --add-service=http --permanent $ sudo firewall-cmd --zone=public --add-port=80/tcp --permanent
重新加载firewalld配置使设定生效
1 sudo firewall-cmd --reload
修改/var/www/html所属用户
1 2 3 4 5 6 $ sudo chown -R jason:jason /var/www/html $ ll /var/www/ total 0 drwxr-xr-x. 2 root root 6 Nov 16 11:19 cgi-bin drwxr-xr-x. 2 jason jason 24 Feb 5 07:25 html
安装VIM
新建并编辑测试网页
1 vim /var/www/html/index.html
内容
1 2 3 4 5 6 7 8 9 <html > <head > <meta chatset ="utf-8" /> <title > test title</title > </head > <body > <h1 > test body </h1 > <body > </html >
测试静态页,或者浏览器打开IP网址
安装 MySQL5.6 安装MySQL5.6 1 2 $ yum remove mariadb mariadb-server
安装MySQL5.6要用到的YUM源网址https://dev.mysql.com/downloads/repo/yum/
1 2 3 4 5 6 7 8 9 10 11 $ wget https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm $ yum localinstall mysql80-community-release-el7-3.noarch.rpm $ yum repolist all | grep mysql $ yum --disablerepo=mysql80-community --enablerepo=mysql56-community install mysql-server $ systemctl start mysqld $ systemctl enable mysqld
使用yum-config-manager 关闭mysql80-community 开启 mysql56-community 也可以直接编辑/etc/yum.repos.d/ 下的配置文件 (非必要) 怕以后忘了更新成mysql8
1 2 3 4 5 6 yum whatprovides yum-config-mananger yum install yum-utils yum repolist all | grep mysql yum-config-manager --disable mysql80-community yum-config-manager --enable mysql56-community yum repolist all | grep -E 'mysql.*enabled'
初始化 首次安装设定root密码,删除匿名用户,删除test数据库,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 $ sudo mysql_secure_installation Enter current password for root (enter for none): Set root password? [Y/n] y New password: Re-enter new password: Remove anonymous users ? [Y/n] y Disallow root login remotely? [Y/n] y Remove test database and access to it? [Y/n] y Reload privilege tables now? [Y/n] y
连接 MySQL ,添加用户 方便测试用%,正式使用localhost
1 2 3 4 5 6 7 8 9 10 11 # 登录mysql $ mysql -u root -p # 新建用户 MySQL> CREATE USER '用户名'@'%' IDENTIFIED BY '密码'; # 赋予权限,简单方便用 ALL MySQL> GRANT ALL PRIVILEGES ON *.* TO '用户名'@'%'; # 使生效 MySQL> FLUSH PRIVILEGES; # 退出 MySQL> quit;
安装PHP 7.2 查看YUM安装PHP版本
CentOS 7 YUM 默认安装php是5.4,所以安装YUM源用来安装PHP7.2
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 $ sudo yum install epel-release $ sudo rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm $ sudo yum search php | grep php72 $ sudo yum install php72w-cli \ php72w-common \ php72w-devel \ php72w-fpm \ php72w-gd \ php72w-mbstring \ php72w-mysqlnd \ php72w-pdo \ php72w-xml \ mod_php72w $ php -v PHP 7.2.34 (cli) (built: Oct 1 2020 13:37:37) ( NTS ) $ php-fpm -v PHP 7.2.34 (fpm-fcgi) (built: Oct 1 2020 13:40:44)
新建并编辑php测试文件
1 2 rm -f /var/www/html/index.htmlvim /var/www/html/index.php
内容
测试 http://ip/index.php 或者:
1 2 3 4 5 6 $ curl -I http://localhost/index.php HTTP/1.1 200 OK Date: Fri, 05 Feb 2021 14:11:07 GMT Server: Apache/2.4.6 (CentOS) PHP/7.2.34 X-Powered-By: PHP/7.2.34 Content-Type: text/html; charset=UTF-8
CentOS7下PHP7.2安装mcrypt
php-fpm https://cwiki.apache.org/confluence/display/HTTPD/PHP-FPM
1 2 3 4 5 6 $ sudo systemctl enable php-fpm $ sudo systemctl start php-fpm $ sudo systemctl status php-fpm
查看端口监听
1 2 3 $ sudo netstat -nltp | grep 9000 tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN 23148/php-fpm: mast
所有.php使用php-fpm代理
1 2 3 4 5 $ sudo vim /etc/httpd/conf.d/php-fpm.conf > <FilesMatch \.php$> > SetHandler "proxy:fcgi://127.0.0.1:9000" > </FilesMatch>
重启apache测试
1 2 3 4 5 6 7 $ apachectl configtest $ sudo systemctl restart httpd $ curl http://localhost/index.php | grep php-fpm
使用 .user.ini 1 2 3 4 5 6 7 $ var /etc/php.ini user_ini.filename = ".user.ini" user_ini.cache_ttl = 300 $ systemctl restart httpd $ vim .user.ini > upload_max_filesize="20M"
创建VirtualHost,使用自定域名/主机名,开启rewrite,.htaccess 修改/etc/hosts加入自定域名
1 2 3 $ sudo vim /etc/hosts 127.0.0.1 php72.vm localhost localhost.localdomain localhost4 localhost4.localdomain4 ::1 php72.vm localhost localhost.localdomain localhost6 localhost6.localdomain
测试域名
1 2 3 $ ping php72.vm PING php72.vm (127.0.0.1) 56(84) bytes of data. 64 bytes from php72.vm (127.0.0.1): icmp_seq=1 ttl=64 time=0.046 ms
新建VirtualHost
1 2 3 4 5 6 7 $ sudo vim /etc/httpd/conf.d/vhosts.conf <VirtualHost *:80> DocumentRoot "/var/www/html" ServerName localhost ServerAlias php72.vm </VirtualHost>
修改httpd.conf开启.htaccess
1 2 3 4 5 6 7 8 9 $ sudo vim /etc/httpd/conf/httpd.conf <Directory "/var/www/html" > Options FollowSymLinks AllowOverride All Require all granted </Directory>
重启httpd
1 sudo systemctl restart httpd
测试http://php72.vm/index.php 或者:
1 curl http://php72.vm | grep php-fpm
测试 PUT METHOD
1 2 $ curl -X PUT -I http://localhost HTTP/1.1 200 OK
测试 DELETE METHOD
1 2 $ curl -X DELETE -I http://localhost HTTP/1.1 200 OK
安装Git
1 2 3 4 5 $ sudo yum install git -y $ git --version > git version 1.8.3.1
安装Composer
1 2 3 4 5 6 $ curl https://install.phpcomposer.com/installer -o ~/composer-setup.php $ php ~/composer-setup.php $ sudo mv ~/composer.phar /usr/bin/composer $ sudo chmod +x /usr/bin/composer $ composer --version > Composer version 2.0.9 2021-01-27 16:09:27
安装 ModSecurity 安装 ModSecurity https://www.modsecurity.org/
1 2 3 4 5 $ yum install mod_security $ sudo systemctl restart httpd
安装 CRS (CoreRuleSet) 规则集 https://coreruleset.org/installation/
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 $ su $ sudo yum install wget $ cd /etc/httpd $ wget https://github.com/coreruleset/coreruleset/archive/v3.3.0.tar.gz $ tar -zxvf v3.3.0.tar.gz $ ln -s coreruleset-3.3.0 /etc/httpd/crs $ cp crs/crs-setup.conf.example crs/crs-setup.conf $ sudo vim /etc/httpd/conf.d/mod_security.conf > IncludeOptional crs/crs-setup.conf > IncludeOptional crs/rules/*.conf $ apachectl configtest $ systemctl restart httpd $ curl -I http://localhost?exec =/bin/bash > HTTP/1.1 403 Forbidden curl -X PUT -I http://localhost > HTTP/1.1 403 Forbidden
开放PUT,DELETE 方法 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 $ cd /etc/httpd/crs/rules $ cp REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf $ cp RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf $ vim REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf SecRule &TX:allowed_methods "@eq 0" \ "id:9011601,\ phase:1,\ pass,\ nolog,\ ver:'OWASP_CRS/3.3.0',\ setvar:'tx.allowed_methods=GET PUT DELETE HEAD POST OPTIONS'" $ apachectl configtest $ systemctl restart httpd $ curl -X PUT -I http://localhost > HTTP/1.1 200 OK
添加白名单 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 $ tail -f /var/log/httpd/error_log | grep ModSecurity $ echo "SecRuleRemoveById 941160" >> /etc/httpd/crs/rules/white-list.conf $ echo "SecRuleRemoveById 949110" >> /etc/httpd/crs/rules/white-list.conf $ echo "SecRuleRemoveById 980130" >> /etc/httpd/crs/rules/white-list.conf $ cat /etc/httpd/crs/rules/white-list.conf > SecRuleRemoveById 941160 > SecRuleRemoveById 949110 > SecRuleRemoveById 980130 $ systemctl restart httpd $ echo "" >/var/log/httpd/error_log $ tail -f /var/log/httpd/error_log $ sort white-list.conf | uniq > tmp.conf ;mv -f tmp.conf white-list.conf $ cat white-list.conf SecRuleRemoveById 11862 SecRuleRemoveById 932100 SecRuleRemoveById 932110 SecRuleRemoveById 932130 SecRuleRemoveById 941100 SecRuleRemoveById 941110 SecRuleRemoveById 941160 SecRuleRemoveById 941310 SecRuleRemoveById 942100 SecRuleRemoveById 949110 SecRuleRemoveById 980130 $ vim /etc/httpd/conf.d/mod_security.conf SecRuleEngine DetectionOnly $ systemctl restart httpd
mod_evasive DDos 防火墙 for apache 一定要有DDOS防火墙 不然就等于光着身子被乱箭穿身, 总能被刺中一次! 最好是有硬件防火墙! ===
参考网址https://www.digitalocean.com/community/tutorials/how-to-protect-against-dos-and-ddos-with-mod_evasive-for-apache-on-centos-7
epel for CentOS7 网址https://centos.pkgs.org/7/epel-x86_64/epel-release-7-13.noarch.rpm.html
1 2 3 4 5 6 7 8 $ yum repolist all | grep epel $ wget https://download-ib01.fedoraproject.org/pub/epel/7/x86_64/Packages/e/epel-release-7-13.noarch.rpm $ yum localinstall epel-release-7-13.noarch.rpm $ yum install yum-plugin-protectbase mod_evasive
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 $ cat /etc/httpd/conf.d/mod_evasive.conf | grep -Ev '^$|#' LoadModule evasive20_module modules/mod_evasive24.so <IfModule mod_evasive24.c> DOSHashTableSize 3097 DOSPageCount 3 DOSSiteCount 50 DOSPageInterval 1 DOSSiteInterval 1 DOSBlockingPeriod 180 </IfModule>
1 2 3 DOSWhitelist 111.111.111.111 DOSWhitelist 222.222.222.222
1 2 $ systemctl restart httpd
其它 SELinux SELinux 对指定目录开放写权限,或者web根目录全开 1 2 $ sudo chcon -R -t httpd_sys_rw_content_t ./uploads
查看SElinux策略内httpd相关规则的布尔值 原则是先按默认,在需要时打开相关设定
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 $ getsebool -a | grep httpd httpd_anon_write --> off 匿名写关闭 httpd_builtin_scripting --> on httpd_can_check_spam --> off httpd_can_connect_ftp --> off PHP连接ftp如果需要才打开 httpd_can_connect_ldap --> off httpd_can_connect_mythtv --> off httpd_can_connect_zabbix --> off httpd_can_network_connect --> off 需要时打开,网络资源的访问如redis,读写网络资源 httpd_can_network_connect_cobbler --> off httpd_can_network_connect_db --> off 连接外网数据库,如果需要才打开 httpd_can_network_memcache --> off 连接外网memcache httpd_can_network_relay --> off httpd_can_sendmail --> off mail发信,要打开 httpd_dbus_avahi --> off httpd_dbus_sssd --> off httpd_dontaudit_search_dirs --> off httpd_enable_cgi --> on PHP-FPM 是fast-cgi 要打开 httpd_graceful_shutdown --> on ...下边其它都是off
Sendmail mail: command not found 1 2 3 4 5 6 7 $ sudo yum install sendmail mailx jwhois $ sudo systemctl enable sendmail $ sudo systemctl start sendmail $ sudo setsebool -P httpd_can_sendmail 1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 $ getsebool -a | grep -Ev off | grep httpd httpd_builtin_scripting --> on httpd_can_sendmail --> on httpd_enable_cgi --> on httpd_graceful_shutdown --> on $ firewall-cmd --list-all public (active) target: default icmp-block-inversion: no interfaces: eth0 sources: services: dhcpv6-client http https ssh ports: 3000/tcp (3000是因为我安装了gitea不包含在测试环境) protocols: masquerade: no forward-ports: source-ports: icmp-blocks: rich rules:
至此, CentOS7
Apache 2.4
PHP-FPM 7.2
MySQL 5.6
ModSecurity
CRS
mod_evasive
Firewalld
SELinux
测试环境正常.
持续更新…