一、环境准备
1、系统和内内核版本
1
2
3
4
|
[root@linux-node1 ~]
# cat /etc/redhat-release
CentOS release 6.8 (Final)
[root@linux-node1 ~]
# uname -r
2.6.32-642.el6.x86_64
|
2、主机名称和IP地址
1
2
3
4
|
[root@linux-node1 ~]
# hostname
linux-node1.example.com
[root@linux-node1 ~]
# hostname -I
192.168.56.11
|
3、关闭防火墙和SELINUX
1
2
3
4
|
[root@linux-node1 ~]
# getenforce
Disabled
[root@linux-node1 ~]
# /etc/init.d/iptables status
iptables: Firewall is not running.
|
4、使用阿里yum源
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
[root@linux-node1 ~]
# mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup
[root@linux-node1 ~]
# wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo
[root@linux-node1 ~]
# wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo
[root@linux-node1 ~]
# yum repolist
Loaded plugins: fastestmirror, security
Loading mirror speeds from cached hostfile
* base: mirrors.aliyun.com
* epel: mirrors.aliyun.com
* extras: mirrors.aliyun.com
* updates: mirrors.aliyun.com
repo
id
repo name status
base CentOS-6 - Base - mirrors.aliyun.com 6,696
epel Extra Packages
for
Enterprise Linux 6 - x86_64 12,355
extras CentOS-6 - Extras - mirrors.aliyun.com 64
updates CentOS-6 - Updates - mirrors.aliyun.com 959
repolist: 20,074
|
二、nginx安装
1、必要软件准备
(1)安装 pcre,是为了支持rewrite功能,如果你已经装了,请跳过这一步
1
|
[root@linux-node1 ~]
# yum install -y pcre pcre-devel
|
(2)安装openssl,是为了ssl的支持,如果不需要 ssl 支持,请跳过这一步
1
|
[root@linux-node1 ~]
# yum install -y openssl openssl-devel
|
2、安装nginx
(1)创建软件包存放目录
1
|
[root@linux-node1 ~]
# mkdir -p /server/tools
|
(2)下载nginx源码包
1
2
3
4
5
|
[root@linux-node1 ~]
# cd /server/tools
[root@linux-node1 tools]
# wget -q http://nginx.org/download/nginx-1.8.1.tar.gz
[root@linux-node1 tools]
# ll
total 816
-rw-r--r-- 1 root root 833473 Jan 27 2016 nginx-1.8.1.
tar
.gz
|
(3)解压nginx源码包
1
2
3
4
5
|
[root@linux-node1 tools]
# tar xf nginx-1.8.1.tar.gz
[root@linux-node1 tools]
# ll
total 820
drwxr-xr-x 8 1001 1001 4096 Jan 26 2016 nginx-1.8.1
-rw-r--r-- 1 root root 833473 Jan 27 2016 nginx-1.8.1.
tar
.gz
|
(4)创建nginx用户
1
2
3
|
[root@linux-node1 nginx-1.8.1]
# useradd -rs /sbin/nologin nginx
[root@linux-node1 nginx-1.8.1]
# id nginx
uid=498(nginx) gid=498(nginx)
groups
=498(nginx)
|
(5)编译安装
1
2
3
4
5
6
7
8
|
[root@linux-node1 tools]
# cd nginx-1.8.1
[root@linux-node1 tools]
# ./configure --prefix=/usr/local/nginx-1.8.1 \
--user=nginx \
--group=nginx \
--with-http_ssl_module \
--with-http_spdy_module \
--with-http_stub_status_module \
--with-pcre
|
参数解释:
--user=nginx :指定程序运行时的用户
--group=nginx :指定程序运行时的用户组
--prefix=/usr/local/nginx-1.8.1 :安装路径
– with-http_stub_status_module :支持 nginx 状态查询,可以用来监控nginx
– with-http_ssl_module :支持https
– with-http_spdy_module :支持google的 spdy, 想了解请百度 spdy, 这个必须有 ssl 的支持
– with-pcre :为了支持 rewrite 重写功能,必须制定 pcre
提示:出现如下的内容,表明nginx configure完成
……………省略内容……………
Configuration summary
+ using system PCRE library
+ using system OpenSSL library
+ md5: using OpenSSL library
+ sha1: using OpenSSL library
+ using system zlib library
nginx path prefix: "/usr/local/nginx-1.8.1"
nginx binary file: "/usr/local/nginx-1.8.1/sbin/nginx"
nginx configuration prefix: "/usr/local/nginx-1.8.1/conf"
nginx configuration file: "/usr/local/nginx-1.8.1/conf/nginx.conf"
nginx pid file: "/usr/local/nginx-1.8.1/logs/nginx.pid"
nginx error log file: "/usr/local/nginx-1.8.1/logs/error.log"
nginx http access log file: "/usr/local/nginx-1.8.1/logs/access.log"
nginx http client request body temporary files: "client_body_temp"
nginx http proxy temporary files: "proxy_temp"
nginx http fastcgi temporary files: "fastcgi_temp"
nginx http uwsgi temporary files: "uwsgi_temp"
nginx http scgi temporary files: "scgi_temp"
1
|
[root@linux-node1 nginx-1.8.1]
# make && make install
|
……………省略内容……………
make[1]: Leaving directory `/server/tools/nginx-1.8.1'
提示:出现上面的内容,表示nginx安装完成
(6)去除nginx目录版本号
1
2
3
4
|
[root@linux-node1 ~]
# ln -s /usr/local/nginx-1.8.1/ /usr/local/nginx
[root@linux-node1 ~]
# ll -d /usr/local/nginx*
lrwxrwxrwx 1 root root 23 Mar 13 20:01
/usr/local/nginx
->
/usr/local/nginx-1
.8.1/
drwxr-xr-x 6 root root 4096 Mar 13 19:56
/usr/local/nginx-1
.8.1
|
3、启动、关闭和重新加载配置文件
(1)查看nginx命令帮助
1
2
3
4
5
6
7
8
9
10
11
12
13
|
[root@linux-node1 ~]
# /usr/local/nginx/sbin/nginx -h
nginx version: nginx
/1
.8.1
Usage: nginx [-?hvVtq] [-s signal] [-c filename] [-p prefix] [-g directives]
Options:
-?,-h : this help
-
v
: show version and
exit
-V : show version and configure options
then
exit
-t :
test
configuration and
exit
-q : suppress non-error messages during configuration testing
-s signal : send signal to a master process: stop, quit, reopen, reload
-p prefix :
set
prefix path (default:
/usr/local/nginx-1
.8.1/)
-c filename :
set
configuration
file
(default: conf
/nginx
.conf)
-g directives :
set
global directives out of configuration
file
|
参数翻译:
参数 |
含义 |
-?,-h | 帮助 |
-v | 查看nginx版本 |
-V |
查看nginx版本以及编译安装参数 |
-t |
检查nginx配置文件语法 |
-q | 在配置测试期间禁止非错误消息 |
-s signal | 指定nginx服务停止、退出、重启和重新加载 |
-p prefix | 指定nginx配置文件nginx.conf目录 |
-c filename |
指定nginx配置文件 |
-g directives | 设置配置文件中全局指令 |
(2)启动nginx
1
2
3
4
5
6
7
8
9
|
[root@linux-node1 ~]
# /usr/local/nginx/sbin/nginx
[root@linux-node1 ~]
# netstat -nlutp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID
/Program
name
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 4098
/nginx
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1428
/sshd
tcp 0 0 :::22 :::* LISTEN 1428
/sshd
[root@linux-node1 ~]
# curl -s http://localhost | grep nginx.com
<a href=
"http://nginx.com/"
>nginx.com<
/a
>.<
/p
>
|
(3)关闭nginx
1
2
3
4
5
6
7
|
[root@linux-node1 ~]
# /usr/local/nginx/sbin/nginx -s stop
[root@linux-node1 ~]
# netstat -nlutp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID
/Program
name
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1428
/sshd
tcp 0 0 :::22 :::* LISTEN 1428
/sshd
[root@linux-node1 ~]
# curl -s http://localhost | grep nginx.com
|
(4)加载配置文件
1
2
|
[root@linux-node1 ~]
# /usr/local/nginx/sbin/nginx -s reload
/usr/local/nginx-1
.5.1
/sbin/nginx
|
(5)nginx启动脚本
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
|
[root@linux-node1 ~]
# cat /etc/init.d/nginx
#!/bin/sh
#
# nginx - this script starts and stops the nginx daemon
#
# chkconfig: - 85 15
# description: Nginx is an HTTP(S) server, HTTP(S) reverse \
# proxy and IMAP/POP3 proxy server
# processname: nginx
# config: /usr/local/nginx/conf/nginx.conf
# config: /usr/local/nginx/conf/nginx.conf
# pidfile: /usr/local/nginx/logs/nginx.pid
# Source function library.
.
/etc/rc
.d
/init
.d
/functions
# Source networking configuration.
.
/etc/sysconfig/network
# Check that networking is up.
[
"$NETWORKING"
=
"no"
] &&
exit
0
nginx=
"/usr/local/nginx//sbin/nginx"
prog=$(
basename
$nginx)
NGINX_CONF_FILE=
"/usr/local/nginx/conf/nginx.conf"
[ -f
/etc/sysconfig/nginx
] && .
/etc/sysconfig/nginx
lockfile=
/var/lock/subsys/nginx
make_dirs() {
# make required directories
user=`$nginx -V 2>&1 |
grep
"configure arguments:"
|
sed
's/[^*]*--user=\([^ ]*\).*/\1/g'
-`
if
[ -z
"`grep $user /etc/passwd`"
];
then
useradd
-M -s
/bin/nologin
$user
fi
options=`$nginx -V 2>&1 |
grep
'configure arguments:'
`
for
opt
in
$options;
do
if
[ `
echo
$opt |
grep
'.*-temp-path'
` ];
then
value=`
echo
$opt |
cut
-d
"="
-f 2`
if
[ ! -d
"$value"
];
then
# echo "creating" $value
mkdir
-p $value &&
chown
-R $user $value
fi
fi
done
}
start() {
[ -x $nginx ] ||
exit
5
[ -f $NGINX_CONF_FILE ] ||
exit
6
make_dirs
echo
-n $
"Starting $prog: "
daemon $nginx -c $NGINX_CONF_FILE
retval=$?
echo
[ $retval -
eq
0 ] &&
touch
$lockfile
return
$retval
}
stop() {
echo
-n $
"Stopping $prog: "
killproc $prog -QUIT
retval=$?
echo
[ $retval -
eq
0 ] &&
rm
-f $lockfile
return
$retval
}
restart() {
#configtest || return $?
stop
sleep
1
start
}
reload() {
#configtest || return $?
echo
-n $
"Reloading $prog: "
killproc $nginx -HUP
RETVAL=$?
echo
}
force_reload() {
restart
}
configtest() {
$nginx -t -c $NGINX_CONF_FILE
}
rh_status() {
status $prog
}
rh_status_q() {
rh_status >
/dev/null
2>&1
}
case
"$1"
in
start)
rh_status_q &&
exit
0
$1
;;
stop)
rh_status_q ||
exit
0
$1
;;
restart|configtest)
$1
;;
reload)
rh_status_q ||
exit
7
$1
;;
force-reload)
force_reload
;;
status)
rh_status
;;
condrestart|try-restart)
rh_status_q ||
exit
0
;;
*)
echo
$
"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
exit
2
esac
[root@linux-node1 ~]
# chmod +x /etc/init.d/nginx
[root@linux-node1 ~]
# /etc/init.d/nginx
Usage:
/etc/init
.d
/nginx
{start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}
|
到这来nginx就算是安装完成啦O(∩_∩)O哈哈~
本文转自 炫维 51CTO博客,原文链接:http://blog.51cto.com/xuanwei/1905865