CENTOS7.9部署NGINX-1.20.2
Contents
一、centos7.9配置
#更改主机名
hostnamectl set-hostname nice-noe
init 6
#配置阿里云
type wget
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
yum clean all
yum makecache
#查看防火墙状态
systemctl status firewalld.service
#如是打开就关闭
systemctl stop firewalld
systemctl disable firewalld
#查看selinux状态
getenforce
#如何打开就关闭
vim /etc/selinux/config
改:7 SELINUX=enforcing 。
为:7 SELINUX=disabled
#前面的 7,表示文档中第 7 行。方便查找
#重启
init 6
二、nginx1.20.2配置
nginx-1.20.2 程序下载地址:https://nginx.org/download/nginx-1.20.2.tar.gz
2.1.下载安装包
wget https://nginx.org/download/nginx-1.20.2.tar.gz
2.2.下载epel源
yum -y install epel-release
yum clean all
yum makecache
2.3.安装各类依赖
yum -y install gcc gcc-c++ autoconf automake zlib zlib-devel openssl openssl-devel pcre pcre-devel
2.4.隐藏nginx标识和版本号
#解压
tar -zxf nginx-1.20.2.tar.gz -C /usr/local/src/
#修改nginx标识和版本号
cd /usr/local/src/nginx-1.20.2/
vim src/core/nginx.h
改:
13 #define NGINX_VERSION "1.20.2"
14 #define NGINX_VER "nginx/" NGINX_VERSION
为:
#define NGINX_VERSION "8.8.2" #此行修改的是你想要的版本号。
#define NGINX_VER "NICE-ONE/" NGINX_VERSION #此行修改的是你想修改的软件名称。
#修改 HTTP 头信息中的 connection 字段,防止回显 nginx 标识**
vim src/http/ngx_http_header_filter_module.c
改:49 static char ngx_http_server_string[] = "Server: nginx" CRLF;
为:49 static char ngx_http_server_string[] = "Server:NICE-ONE" CRLF;
#修改 ngx_http_special_response.c 文件定义了 Nginx 报 404 错误时,不回显标识和版本号。
vim src/http/ngx_http_special_response.c
改:
35 static u_char ngx_http_error_tail[] =
36 "<hr><center>nginx</center>" CRLF
37 "</body>" CRLF
38 "</html>" CRLF
为:
35 static u_char ngx_http_error_tail[] =
36 "<hr><center>XWS</center>" CRLF
37 "</body>" CRLF
38 "</html>" CRLF
2.5.编译安装
#创建用户
useradd -s /sbin/nologin -M -u 2000 nginx
id nginx
#nginx配置
./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_realip_module --with-http_gzip_static_module --with-http_dav_module --with-http_stub_status_module --with-http_addition_module --with-http_sub_module --with-http_flv_module --with-http_mp4_module --with-pcre --with-http_v2_module
#安装
make -j 2 && make install
2.6.初次启动nginx
#启动nginx
/usr/local/nginx/sbin/nginx
#查看是否被开启
netstat -antup | grep 80
2.7.把nginx加入到环境变量
vim /etc/profile.d/nginx.sh
source /etc/profile.d/nginx.sh
echo $PATH
2.8.测试服务是否生效
nginx -s stop
netstat -antup | grep 80
ngxinx
netstat -antup | grep 80
2.9.使用chkconfig管理,并实现开机启动
vim /etc/init.d/nginx
#!/bin/bash
# chkconfig: - 99 2
# description: Nginx Service Control Script
PROG="/usr/local/nginx/sbin/nginx"
PIDF="/usr/local/nginx/logs/nginx.pid"
case "$1" in
start)
$PROG
;;
stop)
kill -3 $(cat $PIDF)
;;
restart)
$0 stop &> /dev/null
if [ $? -ne 0 ] ; then continue ; fi
$0 start
;;
reload)
kill -1 $(cat $PIDF)
;;
*)
echo "Userage: $0 { start | stop | restart | reload }"
exit 1
esac
exit 0
2.10.使用chkconfig进行管理,配置服务开机自动启动
chmod +x /etc/init.d/nginx
chkconfig --add nginx
chkconfig nginx on
chkconfig
2.11.测试脚本情况
#停止nginx,看还有没有80
service nginx stop
netstat -antup | grep 80
#启动nginx,看有没有80
service nginx start
netstat -antup | grep 80
2.12.查看版本号修改情况
/usr/local/nginx/sbin/nginx -v
2.13也可以隐藏版本号
#隐藏版本号
vim /usr/local/nginx/conf/nginx.conf
19 default_type application/octet-stream; #在这行下面,插入以下内容
server_tokens off;
/usr/local/nginx/sbin/nginx -s reload
#使用另外一台设备测试
curl -I xxx.xxx.xxx.xxx
2.14.测试nginx是否能访问
#需要在阿里云先放行


2.15.修改配置文件,使支持php
#备份nginx.conf
cp /usr/local/nginx/conf/nginx.conf{,.bak}
修改用户为 nginx:
改:2 #user nobody;
为:3 user nginx;
#启用 PHP 支持
第 66 行始 修改为:(删掉“#”)
66 location ~ \.php$ {
67 root html;
68 fastcgi_pass 127.0.0.1:9000;
69 fastcgi_index index.php;
70 fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html/$fastcgi_script_name;
71 include fastcgi_params;
72 }