Nginx源码编译部署

1.下载源码包

1
wget http://nginx.org/download/nginx-1.10.3.tar.gz

2.安装依赖库

1
[admin@haifly-bj-static1 ~]$ sudo yum install gcc-c++ pcre pcre-devel zlib zlib-devel openssl openssl-devel -y

3.解压源码包

1
2
[admin@haifly-bj-static1 ~]$ tar xf nginx-1.10.3.tar.gz
[admin@haifly-bj-static1 downloads]$ cd nginx-1.10.3

4.配置对外隐藏nginx版本

1
2
3
4
5
[admin@haifly-bj-static1 ~]$ vim src/core/nginx.h
13行把
#define NGINX_VERSION "1.10.3"
改成
#define NGINX_VERSION "0.0.1"
1
2
3
4
5
[admin@haifly-bj-static1 ~]$ vim src/http/ngx_http_header_filter_module.c
49行把
static char ngx_http_server_string[] = "Server: nginx" CRLF;
改成
static char ngx_http_server_string[] = "Server: hart-edu" CRLF;
1
2
3
4
5
[admin@haifly-bj-static1 ~]$ vim src/http/ngx_http_special_response.c
36行把
"<hr><center>nginx</center>" CRLF
改成
"<hr><center>hart-edu</center>" CRLFLF

5.编译安装

1
2
[admin@haifly-bj-static1 nginx-1.10.3]$ ./configure --prefix=/work/admin/nginx --with-http_stub_status_module --with-http_ssl_module --with-file-aio --with-http_realip_module
[admin@haifly-bj-static1 nginx-1.10.3]$ make && make install

6.启动、停止、重启

启动

1
[admin@haifly-bj-static1 admin]$ ./nginx/sbin/nginx

停止

1
[admin@haifly-bj-static1 admin]$ ./nginx/sbin/nginx -s stop

重启

1
[admin@haifly-bj-static1 admin]$ ./nginx/sbin/nginx -s reload

7.配置为系统服务,开机启动

Centos 6开机启动

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
[admin@haifly-bj-static1 ~]$ cat /etc/init.d/nginx

#! /bin/bash

# chkconfig: - 85 15

PATH=/work/admin/nginx
DESC="nginx daemon"
NAME=nginx
DAEMON=$PATH/sbin/$NAME
CONFIGFILE=$PATH/conf/$NAME.conf
PIDFILE=$PATH/logs/$NAME.pid
SCRIPTNAME=/etc/init.d/$NAME
set -e
[ -x "$DAEMON" ] || exit 0
do_start() {
$DAEMON -c $CONFIGFILE || echo -n "nginx already running"
}
do_stop() {
$DAEMON -s stop || echo -n "nginx not running"
}
do_reload() {
$DAEMON -s reload || echo -n "nginx can't reload"
}
case "$1" in
start)
echo -n "Starting $DESC: $NAME"
do_start
echo "."
;;
stop)
echo -n "Stopping $DESC: $NAME"
do_stop
echo "."
;;
reload|graceful)
echo -n "Reloading $DESC configuration..."
do_reload
echo "."
;;
restart)
echo -n "Restarting $DESC: $NAME"
do_stop
do_start
echo "."
;;
*)
echo "Usage: $SCRIPTNAME {start|stop|reload|restart}" >&2
exit 3
;;
esac
exit 0

添加执行权限

1
[admin@haifly-bj-static1 ~]$ sudo chmod +x /etc/init.d/nginx

添加为开机启动

1
2
3
[admin@haifly-bj-static1 ~]$ sudo chkconfig --add nginx
[admin@haifly-bj-static1 ~]$ sudo chkconfig nginx on
[admin@haifly-bj-static1 ~]$ chkconfig --list

Centos 7开机启动

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
[admin@haifly-bj-static1 ~]$ cat /lib/systemd/system/nginx.service

[Unit]
Description=nginx service
After=network.target

[Service]
User=admin
Type=forking
ExecStart=/work/admin/nginx/sbin/nginx
ExecReload=/work/admin/nginx/sbin/nginx -s reload
ExecStop=/work/admin/nginx/sbin/nginx -s quit
PrivateTmp=true

[Install]
WantedBy=multi-user.target

添加为开启启动

1
2
[admin@haifly-bj-static1 ~]$ sudo systemctl enable nginx
[admin@haifly-bj-static1 ~]$ systemctl is-enabled nginx
-------------本文结束感谢您的阅读-------------