CentOS 7.5 编译安装 Nginx 1.15.5

原文地址(推荐):https://blog.7core.cn/thread-18.htm

关于Nginx

  • Nginx是一个高性能、轻量级的HTTP和反向代理服务及电子邮件IMAP/POP3/SMTP代理服务
  • 因它的稳定性、丰富的功能集、示例配置文件和低系统资源消耗而闻名。
  • 特点:内存占用少,并发能力强
  • 事实上Nginx的并发能力确实在同类型的网页服务器中表现较好,
  • *使用Nginx的大型网站有:百度、京东、新浪、网易、腾讯、淘宝等。

本次实验环境
- 系统:Centos7.5(1804)
- 软件:Nginx 1.15.5
- 依赖:Pcre8.42、Zlib-1.2.11、Openssl-1.1.1
- 用户:root

1、创建用户及用户组

//创建系统用户组Nginx
[[email protected] ~]#groupadd -r nginx
//创建系统用户nginx并加入nginx系统用户组
// -r: 添加系统用户
// -g: 指定要创建的用户所属组
// -s: 新帐户的登录shell  //其中/sbin/nologin指用户不能用来登录系统
// -d: 新帐户的主目录
// -M: 不要创建用户的主目录 //将要被创建的系统用户nginx不会在/home目录下创建nginx家目录 
[[email protected] ~]# useradd -r -g nginx -s /sbin/nologin -d /usr/local/nginx -M nginx

2、创建相关目录

//创建缓存目录在/var/tmp/nginx/
[[email protected] ~]# mkdir -pv /var/tmp/nginx/{client_body,proxy,fastcgi,uwsgi,scgi}
//赋予Nginx用户权限
[[email protected] ~]# chown -R nginx:nginx /var/tmp/nginx/
//创建日志目录在/usr/local/nginx/
[[email protected] ~]# mkdir -pv /usr/local/nginx/logs
//赋予Nginx用户权限
[[email protected] ~]# chown -R nginx:nginx /usr/local/nginx/

3、安装基本环境

[[email protected] ~]# yum -y install gcc gcc-c++ autoconf automake make wget vim
[[email protected] ~]# yum -y install openssl openssl-devel libxml2-devel libxslt-devel perl-devel perl-ExtUtils-Embed libtool zlib zlib-devel pcre pcre-devel patch

4、创建临时软件包目录

[email protected] ~]# mkdir package && cd package

5、安装PCRE-8.42(Nginx的Rewrite功能)

[[email protected] package]# wget https://ftp.pcre.org/pub/pcre/pcre-8.42.tar.gz
[[email protected] package]# tar -zxvf pcre-8.42.tar.gz
[[email protected] package]#cd pcre-8.42/ && ./configure && make && make install && cd ..

6、安装Zlib-1.2.11(Nginx的Gzip压缩功能)

[[email protected] package]# wget http://www.zlib.net/fossils/zlib-1.2.11.tar.gz
[[email protected] package]# tar -zxvf zlib-1.2.11.tar.gz
[[email protected] package]# cd zlib-1.2.11 && ./configure && make && make install && cd ..

7、安装OpenSSL-1.1.1(nginx第三方模块—nginx-sticky-module的使用)

[[email protected] package]# wget https://www.openssl.org/source/openssl-1.1.1-pre8.tar.gz 
[[email protected] package]# tar -zxvf openssl-1.1.1-pre8.tar.gz
[[email protected] package]# cd openssl-1.1.1-pre8 && ./config && make && make install && cd ..

8、安装nginx-sticky-module

[[email protected] package]# wget https://bitbucket.org/nginx-goodies/nginx-sticky-module-ng/get/master.tar.gz
[[email protected] package]# tar -zxvf master.tar.gz
[[email protected] package]# mv nginx-goodies-nginx-sticky-module-ng-08a395c66e42/ nginx-sticky-module/

9、安装Nginx-1.15.5

//下载源码包
[[email protected] package]# wget http://nginx.org/download/nginx-1.15.5.tar.gz
//解压源码包
[[email protected] package]# tar -zxvf nginx-1.15.5.tar.gz
//进入工作目录
[[email protected] package]# cd nginx-1.15.5
//完整复制以下38行命令后回车执行配置
[[email protected] nginx-1.15.5]# ./configure \
--prefix=/usr/local/nginx \
--sbin-path=/usr/sbin/nginx \
--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/lock/nginx.lock \
--user=nginx \
--group=nginx \
--with-http_ssl_module \
--with-http_v2_module \
--with-http_dav_module \
--with-http_flv_module \
--with-http_realip_module \
--with-http_addition_module \
--with-http_xslt_module \
--with-http_stub_status_module \
--with-http_sub_module \
--with-http_random_index_module \
--with-http_degradation_module \
--with-http_secure_link_module \
--with-http_gzip_static_module \
--with-http_perl_module \
--add-module=../nginx-sticky-module \
--with-pcre=../pcre-8.42 \
--with-zlib=../zlib-1.2.11 \
--with-openssl=../openssl-1.1.1-pre8 \
--with-file-aio \
--with-mail \
--with-mail_ssl_module \
--http-client-body-temp-path=/var/tmp/nginx/client_body \
--http-proxy-temp-path=/var/tmp/nginx/proxy \
--http-fastcgi-temp-path=/var/tmp/nginx/fastcgi \
--http-uwsgi-temp-path=/var/tmp/nginx/uwsgi \
--http-scgi-temp-path=/var/tmp/nginx/scgi \
--with-stream \
--with-ld-opt="-Wl,-E"
//编译选项说明   
--prefix=PATH   //指定nginx的安装目录默认 /usr/local/nginx
--sbin-path=PATH   //设置nginx可执行文件的名称。默认/sbin/nginx
--conf-path=PATH   //设置nginx.conf配置文件的名称。默认/conf/nginx.conf
--pid-path=PATH   //设置存储主进程ID文件nginx.pid的名称。默认/logs/nginx.pid
--error-log-path=PATH   //设置错误,警告和诊断文件的名称。默认/logs/error.log
--http-log-path=PATH   //置HTTP服务器的请求日志文件的名称。默认/logs/access.log
--lock-path=PATH   //安装文件锁定,防止安装文件被利用及误操作
--user=nginx   //指定程序运行时的非特权用户。可以随时在nginx.conf配置文件更改。默认为nobody
--group=nginx   //指定程序运行时的非特权用户所在组名称。默认设置为非root用户的名称
--with-http_realip_module   //启用ngx_http_realip_module支持(允许从请求标头更改客户端的IP地址值,默认关闭)
--with-http_ssl_module   //启用ngx_http_ssl_module支持(使支持https请求,需已安装openssl)
--with-http_stub_status_module   //启用ngx_http_stub_status_module支持(获取nginx自上次启动以来的工作状态)
--with-http_gzip_static_module   //启用ngx_http_gzip_module支持(与without-http_gzip_module功能一致)
--http-client-body-temp-path=PATH   //定义http客户端请求临时文件路径
--http-proxy-temp-path=PATH   //定义http代理临时文件路径
--http-fastcgi-temp-path=PATH   //定义http fastcgi临时文件路径
--http-uwsgi-temp-path=PATH   //定义http scgi临时文件路径
--with-pcre   //设置pcre库的源码路径,如果已通过yum方式安装,使用–with-pcre自动找到库文件。
              //使用--with-pcre=PATH时,需要从PCRE网站下载pcre库的源码(版本8.42)并解压
              //剩下的就交给Nginx的./configure和make来完成。
              //perl正则表达式使用在location指令和 ngx_http_rewrite_module模块中。
--with-zlib=PATH   //指定 zlib-1.2.11的源码目录。默认启用网络传输压缩模块ngx_http_gzip_module时需要使用zlib
--with-http_ssl_module   //使用https协议模块。默认情况下,该模块没有被构建。前提是openssl已安装
--add-module=PATH   //添加第三方外部模块,如nginx-sticky-module-ng或缓存模块。每次添加新的模块都要重新编译

10、配置完成后编译并安装Nginx

[[email protected] nginx-1.15.5]# make && make install

11、将Nginx加入systemctl管理服务

[[email protected] nginx-1.15.5]# vim /usr/lib/systemd/system/nginx.service
#按i进入编辑模式写入以下内容(不包含本行)
[Unit]
Description=nginx - high performance web server
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf
ExecReload=/usr/sbin/nginx -s reload
ExecStop=/usr/sbin/nginx -s stop
[Install]
WantedBy=multi-user.target

12、给予文件754权限

[[email protected] nginx-1.15.5]# chmod 754 /usr/lib/systemd/system/nginx.service

13、修改或新增文件需要执行以下语句才能生效

[[email protected] nginx-1.15.5]# systemctl daemon-reload

14、启动Nginx服务器

[[email protected] nginx-1.15.5]# systemctl start nginx
[[email protected] nginx-1.15.5]# systemctl enable nginx

15、查看Nginx服务启动状态

[[email protected] nginx-1.15.5]# systemctl status nginx

16、CentOS7 添加开放TCP 80端口

//开放80端口、如果需要https则开放443端口
[[email protected] nginx-1.15.5]# firewall-cmd --zone=public --add-port=80/tcp --permanent
//重载防火墙配置
[[email protected] nginx-1.15.5]# firewall-cmd --reload

17、查看Nginx版本

[[email protected] nginx-1.15.5]# nginx -v
nginx version: nginx/1.15.5

返回结果如下图,即安装成功
CentOS 7.5 编译安装 Nginx 1.15.5