Nginx 源码安装

在 CentOS 系统下,虽然可以通过 Yum 工具一键下载 Nginx ,但是当我们需求安装第三方模块、开启某些隐藏功能的时候,就需要我们自己手动下载源码,并编译安装 Nginx 来定制自己的 Nginx 。本文主要演示如何在 CentOS 系统下源码编译安装 Nginx,并安装第三方模块。

准备工作

1. 安装编译工具、依赖包

当前系统为 CentOS7 64 位,首先安装缺少的依赖包:

yum update

yum -y install gcc gcc-c++ make libtool zlib zlib-devel openssl openssl-devel pcre pcre-devel

yum -y install unzip patch # 安装第三方模块会使用到

这些软件也可以通过下载源码来编译安装,只是要注意编译时默认安装的目录,确保在安装 Nginx 时正确指定相关依赖的安装目录。

2. 新建匿名用户和用户组

新建的用户组和用户主要是在编译配置的时候指定 Nginx 运行的用户和用户组:

groupadd -r nginx

useradd -s /sbin/nologin -g nginx -r nginx

Nginx 编译安装

1. 下载源码包

# 下载最新稳定版本
wget http://nginx.org/download/nginx-1.12.2.tar.gz 

# 下载负载均衡健康检查的第三方模块
wget https://github.com/yaoweibin/nginx_upstream_check_module/archive/master.zip

# 重命名
mv master.zip nginx_upstream_check_module.zip

2. 解压

tar -zxvf nginx-1.12.2.tar.gz

unzip nginx_upstream_check_module.zip

3. 配置编译参数

集成 nginx_upstream_check_module 第三方模块至 Nginx 中:

cd nginx-1.12.2
patch -p1 < /usr/local/src/nginx_http_upstream_check_module/check_1.12.1+.patch  # 安装对应版本的补丁

配置 Nginx 编译参数:

./configure \
--prefix=/usr/local/nginx \
--sbin-path=/usr/sbin/nginx \
--modules-path=/usr/lib64/nginx/modules \
--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/run/nginx.lock \
--http-client-body-temp-path=/var/cache/nginx/client_temp \
--http-proxy-temp-path=/var/cache/nginx/proxy_temp \
--http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp \
--http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp \
--http-scgi-temp-path=/var/cache/nginx/scgi_temp \
--user=nginx \
--group=nginx \
--with-compat \
--with-file-aio \
--with-threads \
--with-http_addition_module \
--with-http_auth_request_module \
--with-http_dav_module \
--with-http_flv_module \
--with-http_gunzip_module \
--with-http_gzip_static_module \
--with-http_mp4_module \
--with-http_random_index_module \
--with-http_realip_module \
--with-http_secure_link_module \
--with-http_slice_module \
--with-http_ssl_module \
--with-http_stub_status_module \
--with-http_sub_module \
--with-http_v2_module \
--with-mail \
--with-mail_ssl_module \
--with-stream \
--with-stream_realip_module \
--with-stream_ssl_module \
--with-stream_ssl_preread_module \
--add-module=../nginx_upstream_check_module 

# --with-http_gunzip_module 开启隐藏模块
# --add-module= 安装第三发模块

4. 编译并安装

make && make install

5. 检查是否安装成功

# 查看 Nginx 版本号
nginx -v   

# 查看 Nginx 配置信息是否正确
nginx -t 

设置开机启动系统服务

1. 建立服务文件

vim /lib/systemd/system/nginx.service

2. 配置信息

[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target
 
[Service]
Type=forking
PIDFile=/run/nginx.pid
ExecStartPost=/bin/sleep 0.1
ExecStartPre=/usr/sbin/nginx -t -c /etc/nginx/nginx.conf
ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
 
[Install]
WantedBy=multi-user.target

3. 修改文件权限

chmod 754 /lib/systemd/system/nginx.service

4. 设置为开机启动,同时防火墙开启80端口

# 启动 Nginx
systemctl start nginx   
# 设置开机启动
systemctl enable nginx  

# 防火墙开启80端口
firewall-cmd --zone=public --add-port=80/tcp --permanent 
# 重启防火墙
systemctl restart firewalld.service 

参考文章:

  1. nginx 服务器安装及配置文件详解
  2. CentOS7.0 下编译安装Nginx 1.10.0
  3. CentOS 7自定义开机启动系统服务
  4. nginx使用yum/编译安装,心跳检查模块
    原文作者:Jochen
    原文地址: https://segmentfault.com/a/1190000014081550
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞