Nginx是一款是由俄罗斯程序设计师Igor Sysoev开发的高性能Web服务器,在面对高并发连接且处理静态资源较多的场景下,推荐使用Nginx替代Apache。
Linux编译安装Nginx
安装Nginx环境
操作系统:CentOS 7.7
Pcre:8.43
Zlib:1.2.11
OpenSSL:1.0.2u
Nginx:1.16.1
Nginx安装
第一步 安装依赖包
[root@wanghualang ~]# yum -y install gcc gcc-c++ autoconf automake
第二步 下载、解压源码包
[root@wanghualang ~]# cd /usr/local/src/ [root@wanghualang src]# wget --no-check-certificate http://zlib.net/zlib-1.2.11.tar.gz [root@wanghualang src]# wget --no-check-certificate https://ftp.pcre.org/pub/pcre/pcre-8.43.tar.gz [root@wanghualang src]# wget --no-check-certificate https://www.openssl.org/source/openssl-1.0.2u.tar.gz [root@wanghualang src]# wget --no-check-certificate http://nginx.org/download/nginx-1.16.1.tar.gz [root@wanghualang src]# tar xzf zlib-1.2.11.tar.gz [root@wanghualang src]# tar xzf pcre-8.43.tar.gz [root@wanghualang src]# tar xzf openssl-1.0.2u.tar.gz [root@wanghualang src]# tar xzf nginx-1.16.1.tar.gz
第三步 新建用户组、用户
[root@wanghualang ~]# groupadd nginx [root@wanghualang ~]# useradd nginx -M -g nginx -s /sbin/nologin
第四步 编译安装Nginx
[root@wanghualang ~]# cd /usr/local/src/nginx-1.16.1 [root@wanghualang nginx-1.16.1]# ./configure / --user=nginx / --group=nginx / --prefix=/usr/local/nginx / --with-pcre=/usr/local/src/pcre-8.43 / --with-openssl=/usr/local/src/openssl-1.0.2u / --with-zlib=/usr/local/src/zlib-1.2.11 / --with-http_gzip_static_module / --with-http_dav_module / --with-http_stub_status_module / --with-http_addition_module / --with-http_sub_module / --with-http_flv_module / --with-http_mp4_module / --with-http_ssl_module / --with-http_v2_module [root@wanghualang nginx-1.16.1]# make [root@wanghualang nginx-1.16.1]# make install
第五步 配置启动服务脚本、开机启动
[root@wanghualang ~]# vim /etc/init.d/nginx
然后输入下面的脚本
#!/bin/bash
#chkconfig: 2345 55 25
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
NAME=nginx
NGINX_BIN=/usr/local/nginx/sbin/$NAME
CONFIGFILE=/usr/local/nginx/conf/$NAME.conf
PIDFILE=/usr/local/nginx/logs/$NAME.pid
if [ -s /bin/ss ]; then
StatBin=/bin/ss
else
StatBin=/bin/netstat
fi
case "$1" in
start)
echo -n "Starting $NAME... "
if $StatBin -tnpl | grep -q nginx;then
echo "$NAME (pid `pidof $NAME`) already running."
exit 1
fi
$NGINX_BIN -c $CONFIGFILE
if [ "$?" != 0 ] ; then
echo " failed"
exit 1
else
echo " done"
fi
;;
stop)
echo -n "Stoping $NAME... "
if ! $StatBin -tnpl | grep -q nginx; then
echo "$NAME is not running."
exit 1
fi
$NGINX_BIN -s stop
if [ "$?" != 0 ] ; then
echo " failed. Use force-quit"
exit 1
else
echo " done"
fi
;;
status)
if $StatBin -tnpl | grep -q nginx; then
PID=`pidof nginx`
echo "$NAME (pid $PID) is running..."
else
echo "$NAME is stopped."
exit 0
fi
;;
force-quit|kill)
echo -n "Terminating $NAME... "
if ! $StatBin -tnpl | grep -q nginx; then
echo "$NAME is is stopped."
exit 1
fi
kill `pidof $NAME`
if [ "$?" != 0 ] ; then
echo " failed"
exit 1
else
echo " done"
fi
;;
restart)
$0 stop
sleep 1
$0 start
;;
reload)
echo -n "Reload service $NAME... "
if $StatBin -tnpl | grep -q nginx; then
$NGINX_BIN -s reload
echo " done"
else
echo "$NAME is not running, can't reload."
exit 1
fi
;;
configtest)
echo -n "Test $NAME configure files... "
$NGINX_BIN -t
;;
*)
echo "Usage: $0 {start|stop|restart|reload|status|configtest|force-quit|kill}"
exit 1
;;
esac
[root@wanghualang ~]# chmod +x /etc/init.d/nginx [root@wanghualang ~]# chkconfig --add nginx [root@wanghualang ~]# chkconfig nginx on
第六步 测试启动
[root@wanghualang ~]# service nginx start [root@wanghualang ~]# curl -s -I http://127.0.0.1 | grep HTTP
官方QQ群号码:922069959(空)、1093596563(空)