LNMP之编译安装Nginx1.8.0 stable(稳定版)
1、yum安装nginx所欲要的生存环境,也就是库文件
yum -y install make gcc gcc-c++ glibc glibc-devel automake autoconf libtool make
2、给nginx添加系统用户
useradd -s /sbin/nologin -M -r nginx
3、解压安装nginx1.8.0
tar xf nginx-1.8.0.tar.gz cd nginx-1.8.0 ./configure --prefix=/usr/local/nginx1.8 \ --sbin-path=/usr/local/nginx1.8/nginx \ --conf-path=/usr/local/nginx1.8/conf/nginx.conf \ --pid-path=/usr/local/nginx1.8/pid/nginx.pid \ --with-http_ssl_module \ --user=nginx \ --group=nginx \ --with-pcre=../pcre-8.37 \ --with-zlib=../zlib-1.2.8 \ --with-http_ssl_module \ --with-openssl=../openssl-1.0.1p make make install echo $?
4、nginx服务器启动程序参数的详细说明
/usr/local/nginx-1.8.0/sbin/nginx -t
nginx相关参数
-?,-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.0/)
-c filename : set configuration file (default: conf/nginx.conf)
-g directives : set global directives out of configuration file
5、安装过程出现的问题
./configure: error: the HTTP gzip module requires the zlib library. You can either disable the module by using --without-http_gzip_module option, or install the zlib library into the system, or build the zlib library statically from the source with nginx by using --with-zlib=<path> option. 解决方法: tar xf zlib-1.2.8.tar.gz cd zlib-1.2.8 ./configure --prefix=/usr/local/zlib1.2.8 make && make install cd nginx-1.8.0 ./configure --prefix=/usr/local/nginx-1.8.0 --user=nginx --group=nginx --with-http_ssl_module --with-openssl=/usr/local/openssl/ --with-zlib=/usr/local/zlib1.2.8/
3、配置nginx自启动文件
3.1添加nginx的系统配置文件
vim /etc/sysconfig/nginx # Configuration file for the nginx service. NGINX=/usr/local/nginx-1.8.0/sbin/nginx CONFFILE=/usr/local/nginx-1.8.0/conf/nginx.conf LOCKFILE=/var/lock/subsys/nginx
3.2配置nginx服务器开机自启动文件
vim /etc/rc.d/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-1.8.0/conf/nginx.conf
# config: /etc/sysconfig/nginx
# pidfile: /var/run/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-1.8.0/sbin/nginx"
prog=$(basename $nginx)
NGINX_CONF_FILE="/usr/local/nginx-1.8.0/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' -`
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 >/dev/null 2>&1 || return $?
stop
sleep 1
start
}
reload() {
configtest >/dev/null 2>&1 || 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
3.3设置、检查系统中的nginx服务
#增加nginx服务。chkconfig确保每个运行级有一项启动(S)或者杀死(K)入口。如有缺少,则会从缺省的init脚本自动建立
chkconfig --add nginx
#设置nginx在开启时候的运行级别的状态,默认级别为2、3、4、5
chkconfig nginx on
#检查nginx的启动级别
chkconfig --list | grep nginx nginx 0:off1:off2:on3:on4:on5:on6:of
3.4测试nginx的启动服务:
3.4.1、测试nginx的默认配置语法:
[root@fnw ~]# service nginx configtest nginx: the configuration file /usr/local/nginx-1.8.0/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx-1.8.0/conf/nginx.conf test is successful
3.4.2、启动nginx服务
[root@fnw ~]# service nginx start Starting nginx: [ OK ]
3.4.3、重启nginx服务
[root@fnw ~]# service nginx restart Stopping nginx: [ OK ] Starting nginx: [ OK ]
3.4.4、使用reload平滑重启nginx
[root@fnw ~]# service nginx reload Reloading nginx: [ OK ]
3.4.5、查看nginx的状态
[root@fnw ~]# service nginx status nginx (pid 46887 46866) is running...
3.4.6、关闭nginx并查看nginx的状态
[root@fnw ~]# service nginx stop Stopping nginx: [ OK ] [root@fnw ~]# service nginx status nginx is stopped [root@fnw ~]#
以上是LNMP环境下nginx的安装部署过程,如有疑惑请联系QQ:877306754