nginx的安装与部署

1.安装nginx

在server1中

lftp 172.25.254.250
> cd /pub/docs/lamp
> get nginx-1.20.1.tar.gz
> exit

tar zxf nginx-1.20.1.tar.gz          ##解压1.20版本的nginx

[root@server1 ~]# ls
nginx-1.20.1  nginx-1.20.1.tar.gz    ##查看解压是否成功

2.源码编译

./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module --with-threads --with-file-aio        ##因为缺少依赖,所以编译一定会失败,只要根据提示下载所需要的依赖
yum install -y gcc	
yum install -y pcre-devel
yum install -y openssl-devel
./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module --with-threads --with-file-aio        ##再次编译即可

最后显示如下图所示

 然后执行命令

make install

完成后显示如下图所示

cd /root/nginx-1.20.1/auto/cc
vim gcc
# debug
#CFLAGS="$CFLAGS -g"         #将这行注释 

cd   src/core/
vim nginx.h
#define NGINX_VER          "nginx/"        ##将后面的NGINX_VERSION删掉,可以隐藏版本号

操作完成后重新编译

./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module --with-threads --with-file-aio        

make

 会发现nginx小了很多

再次查看nginx版本信息会发现隐藏了版本号,提高了安全性

curl -I localhost
HTTP/1.1 502 Bad Gateway
Server: nginx/
Date: Thu, 16 Sep 2021 03:10:22 GMT
Content-Type: text/html
Content-Length: 494
Connection: keep-alive
ETag: "6142b392-1ee"

创建一个不能个系统交互的用户nginx

useradd -d /usr/local/nginx -M -s /sbin/nologin nginx

更改nginx的配置文件nginx .conf

#user  nobody;
worker_processes  auto;                ##自动设置

events {
    worker_connections  65535;        ##worker能接受的最大文件连接数


http {
        upstream westos{
        server 172.25.50.2:80;
        server 172.25.50.3:80;
        }
    include       mime.types;
    default_type  application/octet-stream;


nginx -t                              ##检测语法是否正确,正确会显示successful
nginx -s reload

设置nginx开机自启

##在真机172.25.254.50中
cd /mnt/westospub/docs/lamp
scp nginx.service server1:

##在172.25.50.1(server1)中
mv nginx.service /usr/lib/systemd/system

nginx.service原文

[Unit]
Description=The NGINX HTTP and reverse proxy server
After=syslog.target network.target remote-fs.target nss-lookup.target

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

[Install]
WantedBy=multi-user.target

完成以上操作后

systemctl daemon-reload                    ##刷新服务器列表
systemctl enable --now nginx.service       ##设置nginx服务为开机自启

如果reload不成功

则需要查看并关闭工作端的worker

ps ax | grep nginx
kill -9 #worker_id#

  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值