Nginx是一款是由俄罗斯程序设计师Igor Sysoev开发的高性能Web服务器,在面对高并发连接且处理静态资源较多的场景下,推荐使用Nginx替代Apache。
实验环境
操作系统:CentOS 7.9
Pcre:8.44
Zlib:1.2.11
OpenSSL:1.1.1i
Nginx:1.18
安装篇
第一步 安装依赖包
- # yum -y install gcc gcc-c++ autoconf automake
第二步 下载、解压源码包
- # cd /usr/local/src/
- # wget --no-check-certificate http://zlib.net/zlib-1.2.11.tar.gz
- # wget --no-check-certificate https://ftp.pcre.org/pub/pcre/pcre-8.44.tar.gz
- # wget --no-check-certificate https://www.openssl.org/source/openssl-1.1.1i.tar.gz
- # wget --no-check-certificate http://nginx.org/download/nginx-1.18.0.tar.gz
- # tar xzf zlib-1.2.11.tar.gz
- # tar xzf pcre-8.44.tar.gz
- # tar xzf openssl-1.1.1i.tar.gz
- # tar xzf nginx-1.18.0.tar.gz
第三步 新建用户组、用户
- # groupadd nginx
- # useradd nginx -M -g nginx -s /sbin/nologin
第四步 编译安装Nginx
- # cd /usr/local/src/nginx-1.18.0
- # ./configure \
- --user=nginx \
- --group=nginx \
- --prefix=/usr/local/nginx \
- --with-pcre=/usr/local/src/pcre-8.44 \
- --with-openssl=/usr/local/src/openssl-1.1.1i \
- --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
- # make
- # make install
第五步 配置启动服务脚本、开机启动
- # vim /etc/init.d/nginx
- #! /bin/sh
- # 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
- ulimit -n 8192
- case "$1" in
- start)
- echo -n "Starting $NAME... "
- if [ -f $PIDFILE ]; then
- mPID=$(cat $PIDFILE)
- isStart=$(ps ax | awk '{ print $1 }' | grep -e "^${mPID}$")
- if [ "$isStart" != '' ]; then
- echo "$NAME (pid $(pidof $NAME)) already running."
- exit 1
- fi
- fi
- $NGINX_BIN -c $CONFIGFILE
- if [ "$?" != 0 ]; then
- echo " failed"
- exit 1
- else
- echo " done"
- fi
- ;;
- stop)
- echo -n "Stoping $NAME... "
- if [ -f $PIDFILE ]; then
- mPID=$(cat $PIDFILE)
- isStart=$(ps ax | awk '{ print $1 }' | grep -e "^${mPID}$")
- if [ "$isStart" = '' ]; then
- echo "$NAME is not running."
- exit 1
- fi
- else
- 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 [ -f $PIDFILE ]; then
- mPID=$(cat $PIDFILE)
- isStart=$(ps ax | awk '{ print $1 }' | grep -e "^${mPID}$")
- if [ "$isStart" != '' ]; then
- echo "$NAME (pid $(pidof $NAME)) already running."
- exit 1
- else
- echo "$NAME is stopped"
- exit 0
- fi
- else
- echo "$NAME is stopped"
- exit 0
- fi
- ;;
- restart)
- $0 stop
- sleep 1
- $0 start
- ;;
- reload)
- echo -n "Reload service $NAME... "
- if [ -f $PIDFILE ]; then
- mPID=$(cat $PIDFILE)
- isStart=$(ps ax | awk '{ print $1 }' | grep -e "^${mPID}$")
- if [ "$isStart" != '' ]; then
- $NGINX_BIN -s reload
- echo " done"
- else
- echo "$NAME is not running, can't reload."
- exit 1
- fi
- 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}"
- exit 1
- ;;
- esac
- # chmod +x /etc/init.d/nginx
- # chkconfig --add nginx
- # chkconfig nginx on
第六步 测试启动
- # service nginx start
- # curl -s -I http://127.0.0.1 | grep HTTP
2019年12月19日 11:36 0楼
装完没有nginx.conf文件样
2019年12月19日 11:46 1层
回复 菜鸟 每一步都操作到位的话,主配置文件就在:/usr/local/nginx/conf/nginx.conf
2019年12月19日 12:31 2层
回复 王花郎 可以教教把NGINX和PHP关联起来吗。我配置了一些NGINX,不太起作用。