第一周环境准备任务
任务目标:准备学习环境,学习web服务器的搭建过程,并做响应的加固学习。
推荐环境:linux+nginx+php-fpm+mysql(为后续搭建nginx+lua的waf做准备)。
要求:将整个环境的搭建过程进行详细记录,收集网络上的加固文档,学习加固技术,从而思考不加固可能存在的安全问题,对于加固的过程以及对于安全的思考都需要作详细的记录。
环境搭建
centos7系统
最小安装系统网络配置问题
https://www.cnblogs.com/iskylite/p/7801113.html
安装Nginx(ubuntu)
sudo apt-get install nginx
启动Nginx服务:
service nginx start
浏览器输入网址看看是否正常:http://192.168.1.208(修改成你设置的静态IP)
网页显示”Welcome to nginx!”说明已经安装成功并运行。
安装mysql(ubuntu)
sudo apt-get install mysql-server mysql-client
Ubuntu18.04安装Mysql没有提示设置密码
https://blog.csdn.net/weixin_36115529/article/details/84635212
Nginx
官方文档
http://nginx.org/en/
简单点说,Nginx就是实现http协议的服务器
FastCGI:快速通用网关接口-->连接PHP
Nginx cache:磁盘,也可以借助memcached实现内存级别的缓存

当一个请求过来之后,首先是发送到网卡,根据tcp/ip协议簇经内核空间处理,发送到用户空间进程响应,判断响应的文件(index.html),用户空间发起系统调用,内核空间对磁盘进行读取,放到内核buffer,再将其复制到用户进程(封装),用户进程再返回给请求用户。Nginx支持在内核封装,也就是sendfile
Nginx在centos7下的安装

一般是由Nginx用户来进行work进程维护的,下面来添加用户

Nginx参数
详见:https://www.jianshu.com/p/436af4ae2b1c
Nginx安装
# ./configure \
--prefix=/usr/local/nginx \
--sbin-path=/usr/sbin/nginx \
--conf-path=/etc/nginx/nginx.conf \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--pid-path=/var/run/nginx.pid \
--lock-path=/var/lock/nginx.lock \
--user=nginx \
--group=nginx \
--with-http_ssl_module \
--with-http_flv_module \
--with-http_stub_status_module \
--with-http_gzip_static_module \
--http-client-body-temp-path=/var/tmp/nginx/client/ \
--http-proxy-temp-path=/var/tmp/nginx/proxy/ \
--http-fastcgi-temp-path=/var/tmp/nginx/fcgi/ \
--http-uwsgi-temp-path=/var/tmp/nginx/uwsgi \
--http-scgi-temp-path=/var/tmp/nginx/scgi \
--with-pcre \
--with-file-aio \
--with-http_image_filter_module
make && make install

报错解决

安装完成之后再次进行安装仍会报错,如图

再次安装,报错如图

最后

安装成功

Nginx命令

-t 测试Nginx配置是否正确

-s 向主进程发送信号
-c 启动时指定主配文件
不加任何参数直接运行Nginx

停止Nginx运行

指定主配文件运行

该主配文件上图已指定

Nginx脚本运行

脚本内容如下
#!/bin/sh
#
# nginx - this script starts and stops the nginx daemon
#
# chkconfig: - 85 15
# description: Nginx is an HTTP(S) server, HTTP(S) reverse \
# proxy and IMAP/POP3 proxy server
# processname: nginx
# config: /etc/nginx/nginx.conf
# config: /etc/sysconfig/nginx
# pidfile: /var/run/nginx.pid
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0
nginx="/usr/sbin/nginx"
prog=$(basename $nginx)
NGINX_CONF_FILE="/etc/nginx/nginx.conf"
[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx
lockfile=/var/lock/subsys/nginx
make_dirs() {
# make required directories
user=`nginx -V 2>&1 | grep "configure arguments:" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -`
options=`$nginx -V 2>&1 | grep 'configure arguments:'`
for opt in $options; do
if [ `echo $opt | grep '.*-temp-path'` ]; then
value=`echo $opt | cut -d "=" -f 2`
if [ ! -d "$value" ]; then
# echo "creating" $value
mkdir -p $value && chown -R $user $value
fi
fi
done
}
start() {
[ -x $nginx ] || exit 5
[ -f $NGINX_CONF_FILE ] || exit 6
make_dirs
echo -n $"Starting $prog: "
daemon $nginx -c $NGINX_CONF_FILE
retval=$?
echo
[ $retval -eq 0 ] && touch $lockfile
return $retval
}
stop() {
echo -n $"Stopping $prog: "
killproc $prog -QUIT
retval=$?
echo
[ $retval -eq 0 ] && rm -f $lockfile
return $retval
}
restart() {
configtest || return $?
stop
sleep 1
start
}
reload() {
configtest || return $?
echo -n $"Reloading $prog: "
killproc $nginx -HUP
RETVAL=$?
echo
}
force_reload() {
restart
}
configtest() {
$nginx -t -c $NGINX_CONF_FILE
}
rh_status() {
status $prog
}
rh_status_q() {
rh_status >/dev/null 2>&1
}
case "$1" in
start)
rh_status_q && exit 0
$1
;;
stop)
rh_status_q || exit 0
$1
;;
restart|configtest)
$1
;;
reload)
rh_status_q || exit 7
$1
;;
force-reload)
force_reload
;;
status)
rh_status
;;
condrestart|try-restart)
rh_status_q || exit 0
;;
*)
echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
exit 2
esac

nginx.conf:主配文件
mime.types:
fastcgi:连接php
nginx主配文件结构:所有语句都以分号结束
-
全局部分
- events
最终的连接数是全局部分的worker_processes*worker_connections。
下面来访问一下
nginx成功启动无法访问
解决:https://www.cnblogs.com/chenleideblog/p/10499807.html
限制IP地址访问
ip访问限制:
location / {
allow 192.168.10.0/24;
allow 192.168.10.10;
deny 192.168.10.0.24;
deny 192.168.10.1;
deny all;
}
用户名的访问控制:
location / {
auth_basic "closed site";
auth_basic_user_file conf/htpasswd;
}


效果

输入tom以及密码进行访问
location高级用法
Location语法规则: location [=|~|~*|^~] /uri/ { … }
= 开头表示精确匹配,只匹配当前目录
^~ 开头表示uri以某个常规字符串开头,理解为匹配 url路径即可。nginx不对url做编码,因此请求为/static/20%/aa,可以被规则^~ /static/ /aa匹配到。
~ 开头表示区分大小写的正则匹配
~* 开头表示不区分大小写的正则匹配
/ 通用匹配,任何请求都会匹配到。
多个location配置的情况下匹配顺序为
首先匹配 =,其次匹配^~, 其次是按文件中顺序的正则匹配,最后是交给 / 通用匹配。当有匹配成功时候,停止匹配,按当前匹配规则处理请求。
例子,有如下匹配规则:
location = / {
#规则A
}
location = /login {
#规则B
}
location ^~ /static/ {
#规则C
}
location ~ \.(gif|jpg|png|js|css|)$ {
#规则D
}
location ~* \.png$ {
#规则E
}
location !~ \.xhtml$ {
#规则F
}
location !~* \.xhtml$ {
#规则G
}
location / {
#规则H
}
那么产生的效果如下:
访问根目录/, 比如http://localhost/ 将匹配规则A
访问 http://localhost/login 将匹配规则B,http://localhost/register 则匹配规则H
访问 http://localhost/static/a.html 将匹配规则C
访问 http://localhost/a.gif, http://localhost/b.jpg 将匹配规则D和规则E,但是规则D顺序优先,规则E不起作用,而 http://localhost/static/c.png 则优先匹配到 规则C
访问 http://localhost/a.PNG 则匹配规则E,而不会匹配规则D,因为规则E不区分大小写。
访问 http://localhost/a.xhtml 不会匹配规则F和规则G,http://localhost/a.XHTML不会匹配规则G,因为不区分大小写。规则F,规则G属于排除法,符合匹配规则但是不会匹配到
访问 http://localhost/category/id/1111 则最终匹配到规则H,因为以上规则都不匹配,这个时候应该是nginx转发请求给后端应用服务器,比如FastCGI(php),tomcat(jsp),nginx作为方向代理服务器存在。
所以实际使用中,通常至少有三个匹配规则定义,如下:
#直接匹配网站根,通过域名访问网站首页比较频繁,使用这个会加速处理
#这里是直接转发给后端应用服务器了,也可以是一个静态首页
# 第一个必选规则
location = / {
proxy_pass http://tomcat:8080/index
}
# 第二个必选规则是处理静态文件请求,这是nginx作为http服务器的强项
# 有两种配置模式,目录匹配或后缀匹配,任选其一或搭配使用
location ^~ /static/ {
root /webroot/static/;
}
location ~* \.(gif|jpg|jpeg|png|css|js|ico)$ {
root /webroot/res/;
}
#第三个规则就是通用规则,用来转发动态请求到后端应用服务器
#非静态文件请求就默认是动态请求,自己根据实际把握
#毕竟目前的一些框架的流行,带.php,.jsp后缀的情况很少了
location / {
proxy_pass http://tomcat:8080/
}
https
主配文件已经给出范本

我们只需稍加修改生成证书

网友评论