RHEL/CentOS 7 系统上源码编译安装nginx,并隐藏替换nginx名称

来源:互联网 发布:欧洲卡车模拟2mac联机 编辑:程序博客网 时间:2024/05/16 15:51

        Nginx是一个 Web服务器,也可以用作反向代理,负载平衡器和HTTP缓存。该软件由 Igor Sysoev 创建,并于2004年首次公开发布。

Nginx 是免费的开源软件,根据类似 BSD许可证的条款发布。大部分 Web服务器通常使用Nginx作为负载均衡器。
整体采用模块化设计是Nginx的一个重大特点,甚至http服务器核心功能也是一个模块。旧版本的Nginx的模块是静态的,添加和删除模块都要对Nginx进行重新编译,1.9.11以及更新的版本已经支持动态模块加载。

        Nginx安装方式有2种:安装包安装和源码编译安装。

Linux下采用安装包的方式安装Nginx可以参考其官方文档 http://nginx.org/en/linux_packages.html 。

源码编译安装可以更灵活的配置Nginx并且安装第三方的插件,因此本文只介绍源码编译安装这种方式,其官方文档 https://www.nginx.com/resources/admin-guide/installing-nginx-open-source/ 。

        RHEL/CentOS 6 或以下版本操作系统下Nginx的编译安装请参考《RHEL/CentOS服务器上源码编译安装nginx》。

1. 准备工作

安装过程我们会隐藏并替换Nginx的名称,我们暂且用“httpdserver”这个名字来替换“nginx”。

安装编译工具、依赖包

yum -y install gcc gcc-c++yum -y install zlib zlib-devel openssl openssl-devel pcre pcre-develyum -y install unzip zip

以上安装的是一些主要的依赖包,具体可根据自己情况或者报错信息提示安装或修改。

下载Nginx第三方模块

下载substitutions

wget -c https://github.com/yaoweibin/ngx_http_substitutions_filter_module/archive/master.zip -O ngx_http_substitutions_filter_module-master.zipunzip ngx_http_substitutions_filter_module-master.zip

substitutions主要用于nginx反向代理时内容替换。

其他第三方模块请参考 https://www.nginx.com/resources/wiki/modules/ 。

新建用户名和用户组

sudo groupadd -r httpdserversudo useradd -s /sbin/nologin -g httpdserver -r httpdserver
新建的用户组和用户主要是在编译配置的时候指定nginx运行的用户和用户组。

2. Nginx编译安装

下载源码并解压

wget -c http://nginx.org/download/nginx-1.12.2.tar.gztar -zxvf nginx-1.12.2.tar.gzcd nginx-1.12.2

Nginx当前最新版为1.12.2。

隐藏并替换Nginx名称

(1) 编辑修改nginx.h源代码

vi src/core/nginx.h
将其中的“nginx”改为“httpdserver
#define NGINX_VERSION      "1.12.2"#define NGINX_VER          "httpdserver/" NGINX_VERSION
(2)编辑修改ngx_http_header_filter_module.c源代码
vi src/http/ngx_http_header_filter_module.c
将其中的“nginx”改为“httpdserver
static u_char ngx_http_server_string[] = "Server: httpdserver" CRLF;
其作用是修改http的ResponseHeader信息。
(3)编辑修改http/ngx_http_special_response.c源代码

vi src/http/ngx_http_special_response.c
将其中的“nginx”改为“httpdserver
static u_char ngx_http_error_tail[] ="<hr><center>httpdserver</center>" CRLF"</body>" CRLF"</html>" CRLF
其作用是修改错误页的底部Footer。

配置

./configure \--with-http_ssl_module \--add-module=/opt/software/ngx_http_substitutions_filter_module-master \--prefix=/usr/local/httpdserver \--conf-path=/usr/local/httpdserver/conf/httpdserver.conf

其中“/opt/software/”为substitutions的下载目录。

若要进一步的替换“nginx”为“httpdserver”,可以在安装后conf/httpdserver.conf配置文件中配置,也可使用以下配置命令提前改变。

./configure \--prefix=/usr/local/httpdserver \--sbin-path=/usr/local/sbin/httpdserver \--conf-path=/usr/local/httpdserver/conf/httpdserver.conf \--pid-path=/var/run/httpdserver.pid \--error-log-path=/var/log/httpdserver/error.log \--http-log-path=/var/log/httpdserver/access.log \--user=httpdserver \--group=httpdserver \--with-http_ssl_module \--add-module=/opt/software/ngx_http_substitutions_filter_module-master
configure详细参数请参考 http://nginx.org/en/docs/configure.html ,也可以通过以下命令查看。
./configure --help

编译并安装

makemake install

3. 设置Nginx为开机启动服务

RHEL/CentOS 7以上是用Systemd进行系统初始化的,Systemd是Linux系统中最新的初始化系统(init),它主要的设计目标是克服sysvinit固有的缺点,提高系统的启动速度。
Systemd服务文件以.service结尾。

在/lib/systemd/system/目录下创建httpdserver.service文件

vi /lib/systemd/system/httpdserver.service
文件内容如下:

[Unit] Description=httpdserver After=network.target  [Service] Type=forking ExecStart=/usr/local/httpdserver/sbin/nginx ExecReload=/usr/local/httpdserver/sbin/nginx -s reload ExecStop=/usr/local/httpdserver/sbin/nginx -s quitPrivateTmp=true  [Install] WantedBy=multi-user.target
[Unit]:服务的说明
Description:描述服务
After:描述服务类别
[Service]:服务运行参数的设置
Type=forking是后台运行的形式
ExecStart为服务的具体运行命令
ExecReload为重启命令
ExecStop为停止命令
PrivateTmp=True表示给服务分配独立的临时空间
注意:[Service]的启动、重启、停止命令全部要求使用绝对路径
[Install]运行级别下服务安装的相关设置,可设置为多用户,即系统运行级别为3

服务相关的命令:

systemctl enable httpdserver.service#设置开机自启动systemctl disable httpdserver.service#停止开机自启动systemctl start httpdserver.service#启动httpdserver服务systemctl stop httpdserver.service#停止httpdserver服务systemctl status httpdserver.service#查看服务当前状态systemctl restart httpdserver.service#重新启动httpdserver服务systemctl list-units --type=service#查看所有已启动的服务

4. 添加防火墙规则

RHEL/CentOS 7系统已经用firewalld服务替代了iptables服务,并使用了新的防火墙管理命令firewall-cmd与图形化工具firewall-config。

Firewall的特点是拥有运行时配置与永久配置选项且能够支持动态更新以及”zone”的区域功能概念,并使用图形化工具firewall-config和文本管理工具firewall-cmd。

firewall-cmd --permanent --add-port=80/tcpfirewall-cmd --permanent --add-port=443/tcpfirewall-cmd --reload
添加打开本机80端口和443端口。

5. 配置Nginx

配置Nginx参考其官方文档:

http://nginx.org/en/docs/

https://www.nginx.com/resources/admin-guide/

动态模块加载参考 https://www.nginx.com/products/nginx/modules/ 。


RHEL/CentOS 6 或以下版本操作系统下Nginx的编译安装请参考《RHEL/CentOS服务器上源码编译安装nginx》。