CentOS 7 编译安装 Nginx 1.15.5

CentOS 7 编译安装 Nginx 1.15.5

关于Nginx

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

本次实验环境
– 系统:CentOS Linux release 7.5.1804 (Core)
– 软件:Nginx 1.15.5
– 依赖:Pcre8.42、Zlib-1.2.11、Openssl-1.1.1
– 用户:root

1、创建用户及用户组

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

2、创建相关目录

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

3、安装基本环境

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

4、创建临时软件包目录

[root@7Core ~]# mkdir package && cd package

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

[root@7Core package]# wget https://ftp.pcre.org/pub/pcre/pcre-8.42.tar.gz
[root@7Core package]# tar -zxvf pcre-8.42.tar.gz
[root@7Core package]#cd pcre-8.42/ && ./configure && make && make install && cd ..

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

[root@7Core package]# wget http://www.zlib.net/fossils/zlib-1.2.11.tar.gz
[root@7Core package]# tar -zxvf zlib-1.2.11.tar.gz
[root@7Core package]# cd zlib-1.2.11 && ./configure && make && make install && cd ..

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

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

8、安装nginx-sticky-module

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

9、安装Nginx-1.15.5

//下载源码包
[root@7Core package]# wget http://nginx.org/download/nginx-1.15.5.tar.gz
//解压源码包
[root@7Core package]# tar -zxvf nginx-1.15.5.tar.gz
//进入工作目录
[root@7Core package]# cd nginx-1.15.5
//完整复制以下38行命令后回车执行配置
[root@7Core 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

[root@7Core nginx-1.15.5]# make && make install

11、将Nginx加入systemctl管理服务

[root@7Core 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权限

[root@7Core nginx-1.15.5]# chmod 754 /usr/lib/systemd/system/nginx.service

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

[root@7Core nginx-1.15.5]# systemctl daemon-reload

14、启动Nginx服务器

[root@7Core nginx-1.15.5]# systemctl start nginx
[root@7Core nginx-1.15.5]# systemctl enable nginx

15、查看Nginx服务启动状态

[root@7Core nginx-1.15.5]# systemctl status nginx

16、CentOS7 添加开放TCP 80端口 (这段可以不执行,配置到service文件中)

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

17、查看Nginx版本

[root@7Core nginx-1.15.5]# nginx -v
//成功显示如:nginx version: nginx/1.15.5

18、测试
http://192.168.160.128:80
//将显示Nginx的欢迎信息

———-完成

参考资料 https://blog.csdn.net/blog_7core_cn/article/details/83279088

来源:hwxie

声明:本站部分文章及图片转载于互联网,内容版权归原作者所有,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!

上一篇 2020年3月1日
下一篇 2020年3月1日

相关推荐