一、安装nginx

>>参考文章<<

安装教程,看代码&注释

# .sh

# 如果centos服务器是最低安装,则先安装weget
yum install -y wget
#下载nginx,截至写稿最新是1.18.0
cd /home
wget http://nginx.org/download/nginx-1.18.0.tar.gz
tar -zxvf nginx-1.18.0.tar.gz
cd nginx-1.18.0
#安装依赖
yum install -y pcre pcre-devel openssl openssl-devel gcc gcc gcc-c++ ncurses-devel perl
#可选安装步骤
#vim auto/cc/gcc
#在179行这样注释掉 CFLAGS="$CFLAGS -g" 使用:set nu显示行号 #支持ssl
./configure --prefix=/usr/local/nginx --user=www --group=www --with-http_stub_status_module --with-http_ssl_module
#编译和安装
make
make install #安装成功
#安装目录
cd /usr/local/nginx

二、配置教程

安全地配置nginx,主要是以低权限运行nginx,即使出现漏洞,也无法被反弹shell

2.1 创建用户和组

groupadd nologin
useradd nginx-nologin -M -s /sbin/nologin -g nologin
#-M 不指定家目录
#-s 指定shell ,nologin代表该用户无法用于登录,但是可以指定以此用户执行命令,详情在后面
#-g 指定组

2.2 获取https证书

这是可选的,如果你还没有域名,则需先购买域名,国内购买域名还需要备案。

如果你有域名,那么推荐使用https,证书可以免费申请,无需备案。

以阿里云为例,你使用域名申请后,推荐使用文件验证,因为dns验证的话需要等dns传播。验证通过一分钟左右就会审核通过,接着下载证书文件,要下载nginx的。下载后解压重命名为cert.pemcert.key,传到服务器/usr/local/nginx/conf/目录下。

后续配置中取消掉有关ssl的配置。

2.2 配置nginx.conf

vi /usr/local/nginx/conf/nginx.conf

嫌麻烦的可以直接删除粘贴我下面的配置

#运行用户和组
user nginx-nologin nologin;
worker_processes 1; #去掉注释
error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info; #去掉注释
pid logs/nginx.pid; events {
worker_connections 1024;
} http {
include mime.types;
default_type application/octet-stream; log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"'; access_log logs/access.log main; sendfile on;
#tcp_nopush on; #keepalive_timeout 0;
keepalive_timeout 65; #gzip on; server {
#使用8800,非root用户无法使用1024以下端口,后续使用防火墙转发流量
listen 8800;
server_name localhost; #charset koi8-r; ###强制http转https
#return 301 https://blog.yunmuq.xyz:4433; #access_log logs/host.access.log main; location / {
root html;
index index.html index.htm;
} error_page 404 /404.html; # redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
} # proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#} # deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
} # another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias; # location / {
# root html;
# index index.html index.htm;
# }
#} ###流量控制,没有https也可以用在http
#开辟10m内存存储相关信息,访问频率限制为一秒3次,访问频率超过会返回500状态码
#limit_req_zone $binary_remote_addr zone=mylimit:10m rate=3r/s; # HTTPS server
#
#server {
# listen 4433 ssl;
# server_name localhost; #流量控制
#继承上面的配置,并且新增一个缓冲区能缓存2次请求
#limit_req zone=mylimit burst=2; # ssl_certificate cert.pem;
# ssl_certificate_key cert.key; # ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m; # ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on; # location / {
# root html;
# index index.html index.htm;
# }
# error_page 404 /404.html; ### 497错误即400状态码中的,错误地使用http协议请求https端口
# error_page 497 /400.html; # redirect server error pages to the static page /50x.html
#
# error_page 500 502 503 504 /50x.html;
# location = /50x.html {
# root html;
# } #} }

2.3 配置防火墙

如果是最小安装,没有防火墙的话,还需要安装防火墙

可以运行命令检测是否安装

iptables
firewalld #centos7默认

如果两个都报错找不到命令的话需要安装,推荐firewalld

yum install -y firewalld
systemctl enable firewalld #开机启动
systemctl start firewalld #启动

接下来是防火墙配置

#开放端口
firewall-cmd --zone=public --add-port=80/tcp --permanent
firewall-cmd --zone=public --add-port=443/tcp --permanent
#流量转发
firewall-cmd --add-forward-port=port=80:proto=tcp:toport=8800 --permanent
firewall-cmd --add-forward-port=port=443:proto=tcp:toport=4433--permanent
#配置立即生效
firewall-cmd --reload

2.4 配置文件权限

这是最后一步,推荐把这些命令存为sh文件,并设置权限755,因为改动文件时可能需要重复执行。

chown -R nginx-nologin:nologin /usr/local/nginx
chmod -R 755 /usr/local/nginx

三、启动nginx

一切准备就绪,现在可以启动,推荐把启动命令保存为sh文件,并设置权限755

这里演示了nologin用户如何执行命令

首次启动:

#su即使用另一个用户执行命令,-s指定了shell,-c是命令内容
#nginx -c是检查配置是否正确
su -s /bin/bash -c "/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf" nginx-nologin
#-s reload用于运行时重载配置文件无需停止,也可以用于启动
su -s /bin/bash -c "/usr/local/nginx/sbin/nginx -s reload" nginx-nologin

如果要停止,可以使用

su -s /bin/bash -c "/usr/local/nginx/sbin/nginx -s quit" nginx-nologin

安装nginx并安全地配置和启动的更多相关文章

  1. CentOS安装Nginx,并配置nodejs反向代理

    安装介绍 安装位置:/usr/local/nginx nginx安装包下载地址:http://nginx.org/download/nginx-1.7.11.tar.gz 安装依赖软件 安装nginx ...

  2. 已安装nginx支持https配置 the "ssl" parameter requires ngx_http_ssl_module

    原文链接:https://blog.seosiwei.com/detail/28 nginx已安装,ssl模块未安装的解决方法: 如果需要在linux中编译自己的nginx服务器,请参照:https: ...

  3. mac下安装nginx及相关配置

    1. 安装 Homebrew   首先 homebrew是什么?它是Mac中的一款软件包管理工具,通过brew可以很方便的在Mac中安装软件或者是卸载软件.不了解的同学看以看官网(https://br ...

  4. Nginx使用(配置开机启动)

    环境: 系统:CentOS 6.5 Final 安装目录:/usr/local/nginx Nginx开机自启: ①编写shell实现控制 vi /etc/init.d/nginx 添加内容: #!/ ...

  5. windows下安装nginx和基本配置

    1.下载并安装nginx 到nginx官网上下载相应的安装包,http://nginx.org/en/download.html: 下载之后进行解压,将解压后的文件放到自己心仪的目录下,如下图所示: ...

  6. Docker 安装 Nginx 负载均衡配置

    Docker 安装 # 1)安装依赖包 yum install -y yum-utils device-mapper-persistent-data lvm2 # 2)添加Docker软件包源(否则d ...

  7. Docker安装Nginx(含:Windows启动、重启、停止)

    Docker安装Nginx #docker pull nginx:latest (第一次启动Docker-Nginx) #docker run --detach \ --publish 80:80 \ ...

  8. Linux学习系列之一:在centos 7.5上安装nginx 以及简单配置

    说到Linux我们都知道那是相当相当得重要得啊,在计算机这个行业,开发运维都是离不开它得.我作为一个准毕业生,智商可能不太够,只能自己笨鸟先飞,自己操作起来咯.俗话说的好,好记性不如难笔头嘛.而且ng ...

  9. CentOS7 编译安装Nginx+php并配置php-fpm模块

    1.编译安装PHP7.2.0 去官网下载安装包:http://php.net/downloads.php ,完成之后,上传至服务器,并释放压缩包 .tar.gz cd php- 因为我们需要编译安装, ...

随机推荐

  1. QQ群web前端分析三——pageSpeed

    使用pageSpeed插件,试试页面分析,看看有没有什么问题.等会上图 第一个问题,大部分人使用默认图片,但是这个图片的url确不一样,导致重复请求了若干次,这个...., 第二个问题,图片没有指定默 ...

  2. Docker - 解决容器内获取的时间和主机的时间不一样的问题

    问题背景 分别在容器和主机下执行 date 命令 可以看到,时间是完全不一样的 解决方案 在运行容器时,挂载 /etc/localtime  目录 docker run -d -v /etc/loca ...

  3. 无需开发,IT事件接入钉钉的方法详解

    1.市场在拥抱钉钉 虎嗅8月30日发表了一篇文章<为什么有很多企业沉迷钉钉无法自拔>,有兴趣的可以去看看,下附文章链接. 文章不短,其中有一部分阐述了:钉钉抓住以人为核心的"智能 ...

  4. Mysql之存储过程与存储函数

    1 存储过程 1.1 什么是存储过程 存储过程是一组为了完成某项特定功能的sql语句集,其实质上就是一段存储在数据库中的代码,他可以由声明式的sql语句(如CREATE,UPDATE,SELECT等语 ...

  5. 解密Cookie,这一篇就够了

    一.Cookie介绍 因为HTTP协议是无状态的,每次请求都是独立的,服务器端无法判断两次请求是否来自同一个用户,进而也就无法判断用户的登录状态,也不知道用户上一次做了什么.所以Cookie就是用来绕 ...

  6. oracle 11g 配置口令复杂度

    oracle 11g 配置口令复杂度 使用ORACLE自带的utlpwdmg.sql脚本来实现 找到本地的utlpwdmg.sql脚本 find / -name utlpwdmg.sql 查看 /ho ...

  7. CTF-lottery[git文件泄露利用+PHP弱类型]

    知识点:PHP弱类型 .git文件泄露 玩攻防世界 遇到一个题lottery  进去看看  分析玩法 我们发现 进入登陆用户都是初始值 金钱是20  彩票号码必须输入7位 然后看你输入的彩票号码有多少 ...

  8. SD卡被格式化了还能恢复吗

    SD卡轻便小巧,它的主要功能是拓展便携式设备.包括:数据相机.手机及其他的多媒体播放器等的存储空间,缓解设备本身的存储压力. 很多用户反应,SD卡使用了一定的时间后,会出现SD卡受损的提示,再次打开的 ...

  9. 利用css3和js实现旋转木马图片小demo

    先看效果图: 上源码 html代码 <!DOCTYPE html> <html lang="en"> <head> <meta chars ...

  10. 从递归到memo到动态规划

    //memo,记忆化搜索 class Solution {    int[][] memo;    public boolean wordBreak(String s, List<String& ...