Tenken blog

Linux源码编译安装Nginx

安装说明

系统环境:Centos 6.X
软件包:nginx-1.8.1(目前最新稳定版)
安装路径:/usr/local/nginx

准备工作

1、安装编译工具及库文件

1
yum install -y gcc gcc-c++ make automake autoconf

2、下载Nginx源码以及编译安装需要的第三方库,openssl-1.0.2k.tar.gz(支持ssl模块)、pcre-8.38.tar.bz2(支持rewrite)、zlib-1.2.8.tar.gz(支持gzip模块)、nginx-1.8.1.tar.gz
我的github上面已经打包好:https://github.com/Tenke007/LNMP

安装openssl软件

1
2
3
4
5
6
7
8
9
10
#进入安装目录
cd /usr/local/
#解压
tar -xvf openssl-1.0.2k.tar.gz
#进入源码目录
cd openssl-1.0.2k
#配置
./config --prefix=/usr/local/openssl/
#编译安装
make && make install

安装pcre软件

1
2
3
4
5
6
7
8
9
cd /usr/local/
#解压
tar -xvf pcre-8.38.tar.bz2
#进入源码目录
cd pcre-8.38
#配置
./configure --prefix=/usr/local/pcre/
#编译安装
make && make install

安装zlib软件

1
2
3
4
5
6
7
8
9
cd /usr/local/
#解压
tar -xvf zlib-1.2.8.tar.gz
#进入源码目录
cd zlib-1.2.8
#配置
./configure --prefix=/usr/local/zlib/
#编译安装
make && make install

安装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
#添加nginx用户和用户组
groupadd nginx
useradd -g nginx nginx -s /bin/false
#创建缓存目录(如果配置中没有指定temp,则可以省略这步)
mkdir -pv /var/tmp/nginx/{client,proxy,fastcgi}
#解压
tar -xvf nginx-1.8.1.tar.gz
#配置(使用openssl、pcre、zlib的源码路径)
cd nginx-1.8.1
./configure \
--user=nginx \
--group=nginx \
--prefix=/usr/local/nginx \
--with-http_ssl_module \
--with-openssl=/usr/local/openssl-1.0.2k \
--with-pcre=/usr/local/pcre-8.38 \
--with-zlib=/usr/local/zlib-1.2.8 \
--with-http_stub_status_module \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--pid-path=/var/run/nginx/nginx.pid \
--lock-path=/var/lock/nginx.lock \
--http-client-body-temp-path=/var/tmp/nginx/client \
--http-proxy-temp-path=/var/tmp/nginx/proxy \
--http-fastcgi-temp-path=/var/tmp/nginx/fastcgi \
--with-http_gzip_static_module \
--with-threads
#编译安装
make && make install
#测试一下
/usr/local/nginx/sbin/nginx -V

需要注意两点:
1、使用openssl、pcre、zlib的源码路径
2、nginx-1.8.1只能使用openssl-1.0的版本,openssl-1.1的版本编译都会报错,试过了好几个版本都不行,暂时不知道原因。

添加自动启动脚本

vi /etc/init.d/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
#!/bin/bash
# nginx Startup script for the Nginx HTTP Server
# it is v.0.0.2 version.
# chkconfig: - 85 15
# description: Nginx is a high-performance web and proxy server.
# It has a lot of features, but it's not for everyone.
# processname: nginx
# pidfile: /var/run/nginx/nginx.pid
# config: /usr/local/nginx/conf/nginx.conf
nginxd=/usr/local/nginx/sbin/nginx
nginx_config=/usr/local/nginx/conf/nginx.conf
nginx_pid=/var/run/nginx/nginx.pid
RETVAL=0
prog="nginx"
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ ${NETWORKING} = "no" ] && exit 0
[ -x $nginxd ] || exit 0
# Start nginx daemons functions.
start() {
if [ -e $nginx_pid ];then
echo "nginx already running...."
exit 1
fi
echo -n $"Starting $prog: "
daemon $nginxd -c ${nginx_config}
RETVAL=$?
echo
[ $RETVAL = 0 ] && touch /var/lock/subsys/nginx
return $RETVAL
}
# Stop nginx daemons functions.
stop() {
echo -n $"Stopping $prog: "
killproc $nginxd
RETVAL=$?
echo
[ $RETVAL = 0 ] && rm -f /var/lock/subsys/nginx /var/run/nginx/nginx.pid
}
# reload nginx service functions.
reload() {
echo -n $"Reloading $prog: "
#kill -HUP `cat ${nginx_pid}`
killproc $nginxd -HUP
RETVAL=$?
echo
}
# See how we were called.
case "$1" in
start)
start
;;
stop)
stop
;;
reload)
reload
;;
restart)
stop
start
;;
status)
status $prog
RETVAL=$?
;;
*)
echo $"Usage: $prog {start|stop|restart|reload|status|help}"
exit 1
esac
exit $RETVAL

添加开机启动命令

1
2
3
4
5
6
7
8
9
10
11
12
chkconfig --add nginx
chkconfig nginx on

#查看是否已经生效
chkconfig --list

#启动nginx
service nginx start
#重启
service nginx restart
#关闭
service nginx stop

如果嫌麻烦可以用yum安装第三方依赖库

1
yum install -y pcre pcre-devel zlib zlib-devel openssl openssl-devel

nginx的配置改为:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
./configure \
--user=nginx \
--group=nginx \
--prefix=/usr/local/nginx \
--with-http_ssl_module \
--with-http_stub_status_module \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--pid-path=/var/run/nginx/nginx.pid \
--lock-path=/var/lock/nginx.lock \
--http-client-body-temp-path=/var/tmp/nginx/client \
--http-proxy-temp-path=/var/tmp/nginx/proxy \
--http-fastcgi-temp-path=/var/tmp/nginx/fastcgi \
--with-http_gzip_static_module \
--with-threads

测试nginx是否成功

1、直接IP访问80端口,看到welcome nginx这几个字就代表安装成功了
2、命令测试:ps -ef|grep nginx

下一篇安装PHP的时候会说到nginx配置PHP环境

请我吃辣条吧~~