liunx7下centos6.8编译安装nginx1.12.1
正式开始前,编译环境gcc g++ 开发库之类的需要提前装好。
如果是ububtu平台初始安装编译安装则使用如下指令:
apt-get install build-essential
apt-get installlibtool
如果是centos则如下:
当没有make时:
安装make:
yum -y install gcc automake autoconf libtool make
安装c++编译环境
yum -y install gcc-c++
下面正式开始
---------------------------------------------------------------------------
一般我们都需要先装pcre, zlib,openssl,前者为了重写rewrite,后者为了gzip压缩,在后者是为了使用ssl
1,选择源码包所存放的目录,这里为/usr/local/src
2,所有依赖的安装目录为/usr/local/related
安装pcre:
cd/usr/local/src
wget https://sourceforge.net/projects/pcre/files/pcre/8.41/pcre-8.41.tar.gz/
tar-zxvfpcre-8.41.tar.gz
cd pcre-8.41
./configure --prefix=/usr/local/related/pcre
make
makeinstall
如果安装的是bz2 ,提示tar (child): bzip2:无法 exec: 没有那个文件或目录 tar (child): Error is not recoverable: exiting now
缺少bzip2包
yum install -y bzip2
然后
tar -jxf xxx.tar.bz2即可
安装zlib
cd /usr/local/src
wget http://zlib.net/zlib-1.2.11.tar.gz --这个玩意更新很快,旧版地址会被官方更新,所以只能打开官网找最新的下载
tar -zxvf zlib-1.2.11.tar.gz
cd zlib-1.2.11
./configure --prefix=/usr/local/related/zlib
make
make install
安装openssl
cd /usr/local/src
wget https://www.openssl.org/source/openssl-1.0.1t.tar.gz
tar-zxvf openssl-1.0.1t.tar.gz
config --prefix=/usr/local/related/openssl
make
make install
安装它的时候或者安装mysql的依赖包bison 的时候都需要perl的支持所以:
cd /usr/local/src
wget http://www.cpan.org/src/5.0/perl-5.26.0.tar.gz
tar -zxf perl-5.26.0.tar.gz
./configure.gnu --prefix=/usr/local/related/pel
make && make install
最后安装nginx
cd /usr/local/src
wget http://nginx.org/download/nginx-1.12.1.tar.gz
tar -zxvf nginx-1.12.1.tar.gz
cd nginx-1.12.1
./configure--sbin-path=/usr/local/nginx/nginx\ --设置nginx的运行目录
--conf-path=/usr/local/nginx/nginx.conf\ --设置nginx的配置目录
--pid-path=/usr/local/nginx/nginx.pid\ --设置nginx的pid
--with-http_v2_module\ --开启http2支持
--with-http_ssl_module\ --开启ssl模块
--with-pcre=/usr/local/src/pcre-8.41\ --pcre的源码包所在路径 --可用新版本
--with-zlib=/usr/local/src/zlib-1.2.11\ --zlib的源码包所在路径 --可用新版本
--with-openssl=/usr/local/src/openssl-1.0.1t \ --openssl的源码包所在路径 --可用新版本
--user=nginx \
--group=nginx
./configure --sbin-path=/usr/local/nginx/nginx --conf-path=/usr/local/nginx/nginx.conf --pid-path=/usr/local/nginx/nginx.pid --with-http_ssl_module --with-http_v2_module --with-pcre=/usr/local/src/pcre-8.41 --with-zlib=/usr/local/src/zlib-1.2.11 --with-openssl=/usr/local/src/openssl-1.0.1t --user=nginx --group=nginx
make
make install
至此,大功告成
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
如果编译pcre报如下错误:
libtool:compile:unrecognizedoption`-DHAVE_CONFIG_H'
libtool: compile: Try `libtool --help'formoreinformation.
make[1]:***[pcrecpp.lo]Error1
make[1]:Leavingdirectory`/usr/local/src/pcre-8.34'
make:***[all]Error2
那么需要g++,
yum -y install g++
安装完毕后重新编译pcre即可:
make clean
./configure --prefix=/usr/local/related/pcre
make && make install
__________________________________________________________________________________________________________________________
nginx的编译选项:
make是用来编译的,它从Makefile中读取指令,然后编译。
make install是用来安装的,它也从Makefile中读取指令,安装到指定的位置。
configure命令是用来检测你的安装平台的目标特征的。它定义了系统的各个方面,包括nginx的被允许使用的连接处理的方法,比如它会检测你是不是有CC或GCC,并不是需要CC或GCC,它是个shell脚本,执行结束时,它会创建一个Makefile文件。nginx的configure命令支持以下参数:
--prefix=
定义一个目录,存放服务器上的文件 ,也就是nginx的安装目录。默认使用path
/usr/local/nginx。
--sbin-path=
设置nginx的可执行文件的路径,默认为path
.prefix
/sbin/nginx--conf-path=
设置在nginx.conf配置文件的路径。nginx允许使用不同的配置文件启动,通过命令行中的-c选项。默认为path
.prefix
/conf/nginx.conf--pid-path=
path 设置nginx.pid文件,将存储的主进程的进程号。安装完成后,可以随时改变的文件名 , 在nginx.conf配置文件中使用 PID指令。默认情况下,文件名 为
.prefix
/logs/nginx.pid--error-log-path=
设置主错误,警告,和诊断文件的名称。安装完成后,可以随时改变的文件名 ,在nginx.conf配置文件中 使用 的error_log指令。默认情况下,文件名 为path
.prefix
/logs/error.log--http-log-path=
设置主请求的HTTP服务器的日志文件的名称。安装完成后,可以随时改变的文件名 ,在nginx.conf配置文件中 使用 的access_log指令。默认情况下,文件名 为path
.prefix
/logs/access.log--user=
设置nginx工作进程的用户。安装完成后,可以随时更改的名称在nginx.conf配置文件中 使用的 user指令。默认的用户名是nobody。name
--group=
设置nginx工作进程的用户组。安装完成后,可以随时更改的名称在nginx.conf配置文件中 使用的 user指令。默认的为非特权用户。name
--with-select_module
--without-select_module 启用或禁用构建一个模块来允许服务器使用select()方法。该模块将自动建立,如果平台不支持的kqueue,epoll,rtsig或/dev/poll。
--with-poll_module
--without-poll_module
启用或禁用构建一个模块来允许服务器使用poll()方法。该模块将自动建立,如果平台不支持的kqueue,epoll,rtsig或/dev/poll。--without-http_gzip_module
— 不编译压缩的HTTP服务器的响应模块。编译并运行此模块需要zlib库。--without-http_rewrite_module
不编译重写模块。编译并运行此模块需要PCRE库支持。--without-http_proxy_module
— 不编译http_proxy模块。--with-http_ssl_module
— 使用https协议模块。默认情况下,该模块没有被构建。建立并运行此模块的OpenSSL库是必需的。--with-pcre=
— 设置PCRE库的源码路径。PCRE库的源码(版本4.4 - 8.30)需要从PCRE网站下载并解压。其余的工作是Nginx的./ configure和make来完成。正则表达式使用在location指令和 ngx_http_rewrite_module 模块中。path
--with-pcre-jit
—编译PCRE包含“just-in-time compilation”(1.1.12中, pcre_jit指令)。--with-zlib=
—设置的zlib库的源码路径。要下载从 zlib(版本1.1.3 - 1.2.5)的并解压。其余的工作是Nginx的./ configure和make完成。ngx_http_gzip_module模块需要使用zlib 。path
--with-cc-opt=
— 设置额外的参数将被添加到CFLAGS变量。例如,当你在FreeBSD上使用PCRE库时需要使用:parameters
--with-cc-opt="-I /usr/local/include。
.如需要需要增加select()支持的文件数量
:--with-cc-opt="-D FD_SETSIZE=2048".
--with-ld-opt=
—设置附加的参数,将用于在链接期间。例如,当在FreeBSD下使用该系统的PCRE库,应指定:parameters
--with-ld-opt="-L /usr/local/lib".
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
最后设置nginx的开机自启动:
在/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: /etc/nginx/nginx.conf
# config: /etc/sysconfig/nginx
#pidfile: /usr/local/nginx/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/nginx"
prog=$(basename $nginx)
NGINX_CONF_FILE="/usr/local/nginx/nginx.conf"
[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx
lockfile=/usr/local/nginx/lock/nginx
make_dirs() {
# make required directories
user=`$nginx -V 2>&1 | grep "configure arguments:.*--user=" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -`
if [ -n "$user" ]; then
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
fi
}
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
}
#!/bin/sh
#
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
注意修改#pidfile,nginx,NGINX_CONF_FILE这三个所指定的路径为自己安装nginx时指定的,还要注意lockfile所指定的lock目录是否存在
写好后添加可执行权限
chmod a+x /etc/init.d/nginx
然后使用chkconfig --add nginx 来将nginx添加到系统管理中
添加完毕可使用chkconfig --list 来查看是否add成功
最后使用chkconfig nginx on 来设置开机启动
最后配置nginx.conf即可,请参阅下一篇博文