Centos 7.0 安装lnmp环境

【MySql 5.6】:

[php]
wget http://dev.mysql.com/get/mysql-community-release-el7-5.noarch.rpm
rpm -ivh mysql-community-release-el7-5.noarch.rpm
yum install mysql-community-server
成功安装之后重启mysql服务
service mysqld restart
初次安装mysql是root账户是没有密码的
设置密码的方法
mysql -uroot
mysql> set password for ‘root’@‘localhost’ = password(‘mypasswd’);
mysql> exit
[/php]

【nginx】

[php]
yum install gcc-c++
yum install pcre pcre-devel
yum install zlib zlib-devel
yum install openssl openssl–devel
wget http://nginx.org/download/nginx-1.7.4.tar.gz
tar -zxvf nginx-1.7.4.tar.gz
cd nginx-1.7.4
./configure \
–prefix=/usr/local/nginx \
–with-http_realip_module \
–with-http_sub_module \
–with-http_gzip_static_module \
–with-http_stub_status_module \
–with-pcre
make
make install
[/php]

【php】:

[php]
rpm -Uvh https://mirror.webtatic.com/yum/el7/epel-release.rpm
rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm

yum install php55w.x86_64 php55w-cli.x86_64 php55w-common.x86_64 php55w-gd.x86_64 php55w-ldap.x86_64 php55w-mbstring.x86_64 php55w-mcrypt.x86_64 php55w-mysql.x86_64 php55w-pdo.x86_64

yum install php56w.x86_64 php56w-cli.x86_64 php56w-common.x86_64 php56w-gd.x86_64 php56w-ldap.x86_64 php56w-mbstring.x86_64 php56w-mcrypt.x86_64 php56w-mysql.x86_64 php56w-pdo.x86_64

yum install php70w.x86_64 php70w-cli.x86_64 php70w-common.x86_64 php70w-gd.x86_64 php70w-ldap.x86_64 php70w-mbstring.x86_64 php70w-mcrypt.x86_64 php70w-mysql.x86_64 php70w-pdo.x86_64

yum install php55w-fpm
yum install php56w-fpm
yum install php70w-fpm
[/php]

【php-fpm配置】

[php]
groupadd www-data
useradd -g www-data www-data
编辑/etc/php-fpm.d/www.conf
listen.owner = www-data
listen.group = www-data www-data
listen.mode = 0666
[/php]

【配置nginx脚本】

[php]
[Unit]
Description=nginx – high performance web server
Documentation=http://nginx.org/en/docs/
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
PIDFile=/run/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true

[Install]
WantedBy=multi-user.target

放到/usr/lib/systemd/system/nginx.service

执行
systemctl daemon-reload

添加自启动
systemctl enable nginx.service
systemctl enable mysqld.service
systemctl enable php-fpm.service
[/php]

【禁用IPv6】

[php]
编辑文件
vi /etc/default/grub
在第5行加入:ipv6.disable=1
修改后如下
GRUB_TIMEOUT=5
GRUB_DEFAULT=saved
GRUB_DISABLE_SUBMENU=true
GRUB_TERMINAL_OUTPUT="console"
GRUB_CMDLINE_LINUX="ipv6.disable=1 rd.lvm.lv=centos/root rd.lvm.lv=centos/swap rhgb quiet"
GRUB_DISABLE_RECOVERY="true"

执行命令
grub2-mkconfig -o /boot/grub2/grub.cfg

验证
可以再次运行ifconfig
[/php]

【nginx基本 带thinkphp rewirte】

[php]
#worker_processes 1;
user www-data;
worker_processes 8;

error_log /usr/local/nginx/logs/nginx_error.log crit;
worker_rlimit_nofile 65535;

pid /run/nginx.pid;

events {
use epoll;
worker_connections 65535;
}

http {
include mime.types;
default_type application/octet-stream;

server_names_hash_bucket_size 128;
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;
client_max_body_size 8m;

sendfile on;
tcp_nopush on;
keepalive_timeout 60;
tcp_nodelay on;

fastcgi_intercept_errors on;
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_buffer_size 64k;
fastcgi_buffers 4 64k;
fastcgi_busy_buffers_size 128k;
fastcgi_temp_file_write_size 128k;

gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.0;
gzip_comp_level 6;
gzip_types text/plain application/x-javascript text/css application/xml;
gzip_vary on;
gzip_disable "MSIE [1-6].(?!.*SV1)";

server_tokens off;
#limit_zone limit $binary_remote_addr 10m;

log_format access ‘$remote_addr – $remote_user [$time_local] "$http_host $request" ‘
‘$status $body_bytes_sent "$http_referer" ‘
‘"$http_user_agent" $http_x_forwarded_for’;

server {
listen 80;
server_name localhost;

location ~ \.php$ {
try_files $uri =404;
fastcgi_pass localhost:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}

location / {
root html;
index index.html index.htmi index.php;
if (!-e $request_filename)
{
rewrite ^/(.*)$ /index.php?s=$1 last;
rewrite ^/$ /index.php last;
break;
}
}

location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)\??([\=a-z0-9]+)?$
{
access_log off;
expires 30d;
}

location ~ .*\.(js|css)\??([\=a-z0-9]+)?$
{
access_log off;
expires 30d;
}

location ~ /\.ht
{
deny all;
}
}
}
[/php]

【VSFTP】
[php]
yum install vsftpd pam* db4* -y
useradd ftpuser -s /sbin/nologin
vi /etc/vsftpd/vsftpd.conf
[/php]

[php]
anonymous_enable=YES –> anonymous_enable=NO //不允许匿名用户访问,默认是允许。
#chroot_list_enable=YES –> chroot_list_enable=YES //不允许FTP用户离开自己主目录,默认是被注释掉的。
#chroot_list_file=/etc/vsftpd/chroot_list –> chroot_list_file=/etc/vsftpd/chroot_list
vi /etc/vsftpd/chroot_list 直接wq

local_enable=YES //允许本地用户访问,默认就是YES,不用改
write_enable=YES //允许写入,默认是YES,不用改
local_umask=022 //上传后文件的权限掩码,不用改
dirmessage_enable=YES //开启目录标语,默认是YES,开不开无所谓,我是默认就行
xferlog_enable=YES //开启日志,默认是YES,不用改
connect_from_port_20=YES //设定连接端口20,不用改
xferlog_std_format=YES //设定vsftpd的服务日志保存路径,不用改
(这步后面要有几个操作才能运行,也就是touch这个文件(见第五步),因为它本身不存在,而且还要给文件写入的权限)
#idle_session_timeout=600 –> idle_session_timeout=600 //会话超时,客户端连接到ftp但未操作,默认被注释掉,可根据个人情况修改
#async_abor_enable=YES –> async_abor_enable=YES //支持异步传输功能,默认是注释掉的,去掉注释
#ascii_upload_enable=YES –> ascii_upload_enable=YES //支持ASCII模式的下载功能,默认是注释掉的,去掉注释
#ascii_download_enable=YES –> ascii_download_enable=YES //支持ASCII模式的上传功能,默认是注释掉的,去掉注释
#ftpd_banner=Welcome to blah FTP service //FTP的登录欢迎语,本身是被注释掉的,去不去都行
#chroot_local_user=YES –> chroot_local_user=YES //禁止本地用户登出自己的FTP主目录,本身被注释掉,去掉注释
pam_service_name=vsftpd //设定pam服务下vsftpdd的验证配置文件名,不用改
userlist_enable=YES //拒绝登录用户名单,不用改
TCP_wrappers=YES //限制主机对VSFTP服务器的访问,不用改(通过/etc/hosts.deny和/etc/hosts.allow这两个文件来配置)

//添加
pam_service_name=vsftpd
userlist_file=/etc/vsftpd/vsftpd/user_list
tcp_wrappers=YES
guest_enable=YES
guest_username=ftpuser
virtual_use_local_privs=YES
user_config_dir=/etc/vsftpd/vconf
[/php]

[php]
touch /var/log/vsftpd.log //日志文件
chown vsftpdadmin.vsftpdadmin /var/log/vsftpd.log //属于vsftpdadmin这个宿主
mkdir /etc/vsftpd/vconf/
touch /etc/vsftpd/vconf/vir_user
vi /etc/vsftpd/vconf/vir_user
virtualuser //用户名
12345678 //密码
注意:第一行用户名,第二行是上一行用户名的密码,其他人的以此类推

db_load -T -t hash -f /etc/vsftpd/vconf/vir_user /etc/vsftpd/vconf/vir_user.db
chmod 600 /etc/vsftpd/vconf/vir_user.db
chmod 600 /etc/vsftpd/vconf/vir_user

清空/etc/pam.d/vsftpd
echo "auth required pam_userdb.so db=/etc/vsftpd/vconf/vir_user" >> /etc/pam.d/vsftpd
echo "account required pam_userdb.so db=/etc/vsftpd/vconf/vir_user" >> /etc/pam.d/vsftpd
[/php]

[php]
touch /etc/vsftpd/vconf/virtualuser
vim /etc/vsftpd/vconf/virtualuser

输入:
local_root=/home/virtualuser //虚拟用户的个人目录路径
anonymous_enable=NO
write_enable=YES
local_umask=022
anon_upload_enable=NO
anon_mkdir_write_enable=NO
idle_session_timeout=600
data_connection_timeout=120
max_clients=10
max_per_ip=5
local_max_rate=1048576 //本地用户的最大传输速度,单位是Byts/s,我设定的是10M
[/php]

[php]
chmod a-w 目录地址
禁止chroot根目录的写权限vsftp 2.3.5强制安全策略
[/php]

CentOS防SYN攻击

netstat -anp |awk ‘{print $6}’|sort|uniq -c |sort -rn
172 ESTABLISHED

  59 CONNECTED

  589 SYN_RECV

  15 STREAM

SYN居然这么高,继续追查是那些ip发出的SYN:

[root@tweb ~]# netstat -an | grep SYN | awk ‘{print $5}’ | awk -F: ‘{print $1}’ | sort | uniq -c | sort -nr | more
首先说一下SYN的攻击原理:

  在TCP/IP协议中,TCP协议提供可靠的连接服务,采用三次握手建立一个连接。

  第一次握手:建立连接时,客户端发送syn包(syn=j)到服务器,并进入SYN_SEND状态,等待服务器确认;

  第二次握手:服务器收到syn包,必须确认客户的SYN(ack=j+1),同时自己也发送一个SYN包(syn=k),即SYN+ACK包,此时服务器进入SYN_RECV状态;

  第三次握手:客户端收到服务器的SYN+ACK包,向服务器发送确认包ACK(ack=k+1),此包发送完毕,客户端和服务器进入ESTABLISHED状态,完成三次握手。 完成三次握手,客户端与服务器开始传送数据.

如果用户与服务器发起连接请求只进行到第二次握手而不再响应服务器,服务器就会不停地等待用户的确认,如果过多这样的连接就会把服务器端的连接队列占满就会导致正常的用户无法建立连接。所以我们直接从SYN的连接上进行如下改动:

  查看linux默认的syn配置:

 [root@web ~]# sysctl -a | grep _syn
net.ipv4.tcp_max_syn_backlog = 1024

  net.ipv4.tcp_syncookies = 1

  net.ipv4.tcp_synack_retries = 5

  net.ipv4.tcp_syn_retries = 5

  tcp_max_syn_backlog 是SYN队列的长度,加大SYN队列长度可以容纳更多等待连接的网络连接数。tcp_syncookies是一个开关,是否打开SYN Cookie 功能,该功能可以防止部分SYN攻击。tcp_synack_retries和tcp_syn_retries定义SYN 的重试连接次数,将默认的参数减小来控制SYN连接次数的尽量少。

  以下是我修改后的参数,可以根据自己服务器的实际情况进行修改:

复制代码
[root@web ~]# more /etc/rc.d/rc.local
  #!/bin/sh
  # This script will be executed *after* all the other init scripts.
  # You can put your own initialization stuff in here if you don’t
  # want to do the full Sys V style init stuff.
  touch /var/lock/subsys/local
  ulimit -HSn 65535
  /usr/local/apache2/bin/apachectl start
  #####
  sysctl -w net.ipv4.tcp_max_syn_backlog=2048
  sysctl -w net.ipv4.tcp_syncookies=1
  sysctl -w net.ipv4.tcp_synack_retries=3
  sysctl -w net.ipv4.tcp_syn_retries=3
复制代码
为了不重启服务器而使配置立即生效,可以执行

#sysctl -w net.ipv4.tcp_max_syn_backlog=2048
  #sysctl -w net.ipv4.tcp_syncookies=1
  #sysctl -w net.ipv4.tcp_synack_retries=3
  #sysctl -w net.ipv4.tcp_syn_retries=3
也有的人喜欢用访问控制列表来防止SYN的攻击,在一定程度上减缓了syn的攻击:

  Syn 洪水攻击

#iptables -A INPUT -p tcp –syn -m limit –limit 1/s -j ACCEPT
  –limit 1/s 限制syn并发数每秒1次
防端口扫描

 # iptables -A FORWARD -p tcp –tcp-flags SYN,ACK,FIN,RST RST -m limit –limit 1/s -j ACCEPT
死亡之ping

# iptables -A FORWARD -p icmp –icmp-type echo-request -m limit –limit 1/s -j ACCEPT
#>iptables-save >/etc/sysconfig/iptables
进行查看,#iptables -L

ACCEPT tcp — anywhere anywhere tcp flags:FIN,SYN,RST,ACK/SYN limit: avg 1/sec burst 5

ACCEPT tcp — anywhere anywhere tcp flags:FIN,SYN,RST,ACK/RST limit: avg 1/sec burst 5

ACCEPT icmp — anywhere anywhere icmp echo-request limit: avg 1/sec burst 5

再次进行查看syn连接:

[root@web ~]# netstat -an | grep SYN | awk ‘{print $5}’ | awk -F: ‘{print $1}’ | sort | uniq -c | sort -nr | more
  20 10.92.10.220
  1 125.43.36.199
明显SYN连接数已经下来了