Centos8 使用编译安装nginx

一、Nginx是什么?

Nginx是一款轻量级Web服务器、反向代理服务器以及电子邮件代理服务器,并且具有高并发连接处理能力和低内存消耗的特点。前面已经学习使用yum进行安装配置了,现在尝试进行源码编译方式安装。

二、编译安装nginx

系统:CentOS 8.4.2105

nginx版本:nginx-1.21.6

2.1 安装准备

# 创建目录
mkdir /www

#
 下载
1.通过wget下载到目录下
wget http://nginx.org/download/nginx-1.21.6.tar.gz
2.先在浏览器下载然后上传到指定目录

#
 安装依赖,可能还需要其它依赖
yum -y install gcc gcc-c++ autoconf automake make
yum -y install zlib zlib-devel openssl openssl-devel pcre pcre-devel

#
 修改文件夹的权限为root用户,为了有执行权限
chown -R root:root nginx-1.21.6


#
 一般建议使用适当权限用户来运行程序。
groupadd -f nginx                # 创建组
useradd -g nginx nginx           # 创建组以及设置用户
usermod -s /sbin/nologin nginx   # 修改用户默认shell /sbin/nologin

2.3 配置安装模块及选项

./configure --user=nginx --group=nginx --prefix=/www/nginx --with-http_stub_status_module --with-http_ssl_module --with-stream --with-http_gzip_static_module --with-http_sub_module
--user:指定运行nginx服务的用户
--group:指定运行nginx服务的组
--prefix:指定安装nginx的主目录
--with:启用模块

跑完之后出现安装目录概要,下面的对应的文件路径

Configuration summary
  + using system PCRE2 library
  + using system OpenSSL library
  + using system zlib library

  nginx path prefix: "/www/nginx"
  nginx binary file: "/www/nginx/sbin/nginx"
  nginx modules path: "/www/nginx/modules"
  nginx configuration prefix: "/www/nginx/conf"
  nginx configuration file: "/www/nginx/conf/nginx.conf"
  nginx pid file: "/www/nginx/logs/nginx.pid"
  nginx error log file: "/www/nginx/logs/error.log"
  nginx http access log file: "/www/nginx/logs/access.log"
  nginx http client request body temporary files: "client_body_temp"
  nginx http proxy temporary files: "proxy_temp"
  nginx http fastcgi temporary files: "fastcgi_temp"
  nginx http uwsgi temporary files: "uwsgi_temp"
  nginx http scgi temporary files: "scgi_temp"

2.4 编译及安装

make && make install
# 编译安装时间可能要稍微久一点,等待完成即可。

三、基础配置

3.1 文件目录

cd /www/nginx
ls
conf :配置文件目录
html:网页文件目录
logs:日志和pid进程文件
sbin:二进制启动文件
cd sbin     # 进入目录
./nginx -V  #查看编译参数
./nginx -V  #查看nginx版本

3.2 进程的启动及暂停

cd /nginx/sbin/
./nginx 回车进程启动
netstat -nltp 查看使用的端口
ps -aux | grep nginx 查看进程
kill 进程号,上面netstat -nltp前面的号码就是进程号

3.3 firewall防火墙开放端口

firewall-cmd --zone=public --add-port=80/tcp --permanent # 开放tcp80端口
firewall-cmd --reload                                    # 刷新防火墙状态

3.4 临时关闭selinux

1、临时关闭:输入命令setenforce 0,重启系统后还会开启。
SELINUX可能会导致nginx进程无法启动的情况,及时检查。

2、永久关闭:输入命令vi /etc/selinux/config,将SELINUX=enforcing改为SELINUX=disabled,然后保存退出。

3.5 使用systemctl管理进程

vim /usr/lib/systemd/system/nginx.service文件,相关目录替换成自己的目录。

文件示例:

/usr/lib/systemd/system

[Unit]
Description=nginx-The High-performance HTTP Server
After=network.target
Wants=network.target

[Service]
PIDFile=/www/nginx/logs/nginx.pid
WorkingDirectory=/www/nginx
ExecStart=/www/nginx/sbin/nginx
Restart=on-abnormal
RestartSec=5s
KillMode=mixed

StandardOutput=null
StandardError=syslog

[Install]
WantedBy=multi-user.target

重启服务

systemctl daemon-reload # 刷新缓存
systemctl start nginx   # 启动进程
systemctl enable nginx  # 开机启动进程

四、添加模块

一开始我们按照业务需求进行模块的安装,但是后续随着业务发展肯定是需要一些新功能或者第三方模块,因此需要添加nginx的模块配置。

4.1 停止服务

systemctl stop nginx

4.2 备份文件

将二进制启动文件和html文件夹进行备份

cp /www/nginx/sbin/nginx /www/nginx.bak
cp -R /www/nginx/html/ /www/html_bak/

4.3 下载模块或者添加标准模块

这边以启用标准模块为测试。

# 以可选模块 http_flv_module进行测试

4.4 重新配置

将之前配置的文件复制过来

cd /www/nginx-1.21.6/  # 回到源码的文件夹
./configure --user=nginx --group=nginx --prefix=/www/nginx --with-http_stub_status_module --with-http_ssl_module --with-stream --with-http_gzip_static_module --with-http_sub_module

在后面添加 --with-http_flv_module,--with表示启用模块的意思

4.5 重新编译

进行重新编译,注意,不要make install

make

覆盖原有二进制启动文件

# 新生成的文件在编译目录下的objs下面nginx文件,覆盖到原有sbin目录下。
cp /nginx-1.21.6/objs/nginx /www/nginx/sbin/nginx

#
 查询编译参数,可以看到http_flv_module已经编译成功
/www/nginx/sbin/nginx -V

六、升级版本

升级版本与上面的重新编译安装方法类似。

  • 停止旧版本
  • 备份启动文件和配置文件以及网页文件等,以防万一。
  • 下载安装新版本进行编译安装,目录和原来一样,配置也是一样即可。
  • 编译make,不能make install否则会覆盖所有文件。
  • 替换文件。
  • 启动进程,检查版本即可。

总结:编译安装比较复杂,可能出现很多不可控因素,非熟悉的同学还是谨慎,升级或者重编译记得备份业务目录和配置文件。

配置步骤:

  • 下载安装包
  • 配置选项和启用模块
  • make&make install安装
  • 服务管理

本文由 mdnice 多平台发布

  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

不喜欢热闹的孩子

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值